org.fisco.bcos.web3j.abi.Utils Java Examples

The following examples show how to use org.fisco.bcos.web3j.abi.Utils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TransService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
public Object sendQueryTransaction(String encodeStr, String contractAddress, String funcName, String contractAbi, int groupId, String userAddress) {

        Web3j web3j = web3ApiService.getWeb3j(groupId);
        String callOutput ;
        try {
           callOutput = web3j.call(Transaction.createEthCallTransaction(userAddress, contractAddress, encodeStr), DefaultBlockParameterName.LATEST)
                    .send().getValue().getOutput();
        } catch (IOException e) {
            throw new FrontException(TRANSACTION_FAILED);
        }

        AbiDefinition abiDefinition = getFunctionAbiDefinition(funcName, contractAbi);
        if (Objects.isNull(abiDefinition)) {
            throw new FrontException(IN_FUNCTION_ERROR);
        }
        List<String> funOutputTypes = AbiUtil.getFuncOutputType(abiDefinition);
        List<TypeReference<?>> finalOutputs = AbiUtil.outputFormat(funOutputTypes);

        List<Type> typeList = FunctionReturnDecoder.decode(callOutput, Utils.convert(finalOutputs));
        Object response;
        if (typeList.size() > 0) {
            response = AbiUtil.callResultParse(funOutputTypes, typeList);
        } else {
            response = typeList;
        }
        return response;
    }
 
Example #2
Source File: BlockInfoServiceTest.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
@Test
public void testInput() throws IOException {
    Block block = ethClient.getBlock(BigInteger.valueOf(8677));
    List<TransactionResult> transactionResults = block.getTransactions();
    log.info("transactionResults.size:{}", transactionResults.size());
    for (TransactionResult result : transactionResults) {
        BcosTransactionReceipt ethGetTransactionReceipt = web3j.getTransactionReceipt((String) result.get()).send();
        Optional<TransactionReceipt> opt = ethGetTransactionReceipt.getTransactionReceipt();
        if (opt.isPresent()) {
            log.info("TransactionReceipt hash: {}", opt.get().getTransactionHash());
            Optional<Transaction> optt =
                    web3j.getTransactionByHash(opt.get().getTransactionHash()).send().getTransaction();
            if (optt.isPresent()) {
                log.info("transaction hash : {}", optt.get().getHash());
                log.info("transaction info : {}", optt.get().getValue());
                List<Type> lt = new ArrayList<>();
                lt.add(Uint64.DEFAULT);
                List<TypeReference<?>> references = new ArrayList<>();
                TypeReference<Uint64> typeReference = new TypeReference<Uint64>() {
                };
                references.add(typeReference);
                List<TypeReference<Type>> ll = Utils.convert(references);
                List<Type> inputList = FunctionReturnDecoder.decode(optt.get().getInput(), ll);
                log.info("input : {}", inputList.size());
                log.info("input : {}", JacksonUtils.toJson(inputList));
            }

        }
    }

}
 
Example #3
Source File: FiscoBcos2.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<SendResult> sendRawTransaction(String topicName, String transactionHex) {
    return web3j.sendRawTransaction(transactionHex).sendAsync().thenApplyAsync(ethSendTransaction -> {
        SendResult sendResult = new SendResult();
        sendResult.setTopic(topicName);

        Optional<TransactionReceipt> receiptOptional = getTransactionReceiptRequest(ethSendTransaction.getTransactionHash());
        if (receiptOptional.isPresent()) {
            List<TypeReference<?>> referencesList = Collections.singletonList(new TypeReference<Uint256>() {
            });
            List<Type> returnList = FunctionReturnDecoder.decode(
                    String.valueOf(receiptOptional.get().getOutput()),
                    Utils.convert(referencesList));

            int sequence = ((BigInteger) returnList.get(0).getValue()).intValue();
            if (sequence == 0) {
                log.error("this FISCO-BCOS account has no permission to publish event");
                sendResult.setStatus(SendResult.SendResultStatus.NO_PERMISSION);
            } else {
                sendResult.setStatus(SendResult.SendResultStatus.SUCCESS);
                sendResult.setEventId(DataTypeUtils.encodeEventId(topicName,
                        receiptOptional.get().getBlockNumber().intValue(),
                        sequence));
            }
        } else {
            sendResult.setStatus(SendResult.SendResultStatus.ERROR);
        }

        return sendResult;
    });
}
 
Example #4
Source File: CallContract.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public List<Object> decode(String data, String retType) throws Exception {
    List<Object> result = new ArrayList<>();
    if (!retType.equals("") && data != null) {
        String types[] = retType.split(",");
        List<TypeReference<?>> references = getTypeReferenceList(types);

        List<Type> returns = FunctionReturnDecoder.decode(data, Utils.convert(references));

        result = types2Objects(returns, types);
    }
    return result;
}
 
Example #5
Source File: CallContract.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public List<Type> decode(String data, TypeReference<?>... typeReferences) {
    if (data.isEmpty() || data.equals("0x")) return null;
    List<TypeReference<?>> typeReferencesList = Arrays.<TypeReference<?>>asList(typeReferences);
    return FunctionReturnDecoder.decode(data, Utils.convert(typeReferencesList));
}
 
Example #6
Source File: Event.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public Event(String name, List<TypeReference<?>> parameters) {
    this.name = name;
    this.parameters = Utils.convert(parameters);
}
 
Example #7
Source File: Function.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
public Function(
        String name, List<Type> inputParameters, List<TypeReference<?>> outputParameters) {
    this.name = name;
    this.inputParameters = inputParameters;
    this.outputParameters = Utils.convert(outputParameters);
}
 
Example #8
Source File: CallContractTest.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
private static void testSyncCallContract(CallContract callContract, String address) {
    CallResult contractResult;

    contractResult =
            callContract.call(
                    address,
                    "getStringOld",
                    new Utf8String("hello world"),
                    new Int256(10086),
                    new Bool(true));

    List<TypeReference<?>> referencesList =
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {});
    List<Type> returnList1 =
            FunctionReturnDecoder.decode(
                    contractResult.getOutput(), Utils.convert(referencesList));
    System.out.println("call getStringOld: " + (String) returnList1.get(0).getValue());

    TransactionReceipt receipt;
    receipt =
            callContract.sendTransaction(
                    gasPrice,
                    gasLimit,
                    address,
                    "setAndget",
                    new Utf8String("hello world"),
                    new Int256(10086));
    referencesList =
            Arrays.<TypeReference<?>>asList(
                    new TypeReference<Utf8String>() {}, new TypeReference<Int256>() {});
    List<Type> returnList2 =
            FunctionReturnDecoder.decode(receipt.getOutput(), Utils.convert(referencesList));
    System.out.println(
            "call setAndget: "
                    + (String) returnList2.get(0).getValue()
                    + ", "
                    + (BigInteger) returnList2.get(1).getValue());

    receipt =
            callContract.sendTransaction(
                    address, "setAndget", new Utf8String("hello world"), new Int256(10086));
    referencesList =
            Arrays.<TypeReference<?>>asList(
                    new TypeReference<Utf8String>() {}, new TypeReference<Int256>() {});
    List<Type> returnList3 =
            FunctionReturnDecoder.decode(receipt.getOutput(), Utils.convert(referencesList));
    System.out.println(
            "default call setAndget: "
                    + (String) returnList3.get(0).getValue()
                    + ", "
                    + (BigInteger) returnList3.get(1).getValue());

    contractResult =
            callContract.call(
                    address,
                    "getArray",
                    new StaticArray2(
                            typeMap(
                                    Arrays.asList(
                                            BigInteger.valueOf(-1), BigInteger.valueOf(2)),
                                    Int16.class)),
                    new DynamicArray(
                            typeMap(
                                    Arrays.asList(BigInteger.valueOf(2), BigInteger.valueOf(2)),
                                    Uint16.class)));
    List<Type> returnList4 =
            callContract.decode(
                    contractResult.getOutput(),
                    new TypeReference<StaticArray2<Int16>>() {},
                    new TypeReference<DynamicArray<Int16>>() {});
    System.out.println(
            "call getArray: "
                    + callContract.convertList((List<Type>) returnList4.get(0).getValue())
                    + ", "
                    + callContract.convertList((List<Type>) returnList4.get(1).getValue()));

    List<List<BigInteger>> dyadicArray = new ArrayList<List<BigInteger>>();
    dyadicArray.add(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(2)));
    dyadicArray.add(Arrays.asList(BigInteger.valueOf(-1), BigInteger.valueOf(992)));
    byte[] bytes = new byte[] {'a', 'b'};
    contractResult =
            callContract.call(
                    address,
                    "newTest",
                    new StaticArray2(typeMap(dyadicArray, StaticArray2.class, Int256.class)),
                    new DynamicBytes(bytes));
    List<Type> returnList5 =
            callContract.decode(
                    contractResult.getOutput(),
                    new TypeReference<StaticArray2<StaticArray2<Int256>>>() {},
                    new TypeReference<DynamicBytes>() {});
    System.out.println(
            "call newTest: "
                    + callContract.convertListList(
                            (List<StaticArray<Int256>>) returnList5.get(0).getValue())
                    + ", "
                    + new String((byte[]) returnList5.get(1).getValue())
                    + ", "
                    + dyadicArray);
}