org.web3j.abi.datatypes.Function.getOutputParameters()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(15.0k)|赞(0)|评价(0)|浏览(183)

本文整理了Java中org.web3j.abi.datatypes.Function.getOutputParameters()方法的一些代码示例,展示了Function.getOutputParameters()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Function.getOutputParameters()方法的具体详情如下:
包路径:org.web3j.abi.datatypes.Function
类名称:Function
方法名:getOutputParameters

Function.getOutputParameters介绍

暂无

代码示例

代码示例来源:origin: web3j/web3j

private void confirmAllowance(String owner, String spender, String contractAddress,
                  BigInteger expected) throws Exception {
  Function function = allowance(owner, spender);
  String responseValue = callSmartContractFunction(function, contractAddress);
  List<Type> response = FunctionReturnDecoder.decode(
      responseValue, function.getOutputParameters());
  assertThat(response.size(), is(function.getOutputParameters().size()));
  assertThat(response.get(0), equalTo(new Uint256(expected)));
}

代码示例来源:origin: web3j/web3j

@Test
public void testEmptyResultFunctionDecode() {
  Function function = new Function(
      "test",
      Collections.emptyList(),
      Collections.singletonList(new TypeReference<Uint>() { }));
  assertThat(FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
      is(Collections.emptyList()));
}

代码示例来源:origin: web3j/web3j

@Test
public void testVoidResultFunctionDecode() {
  Function function = new Function(
      "test",
      Collections.emptyList(),
      Collections.emptyList());
  assertThat(FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
      is(Collections.emptyList()));
}

代码示例来源:origin: web3j/web3j

@Test
public void testSimpleFunctionDecode() {
  Function function = new Function(
      "test",
      Collections.<Type>emptyList(),
      Collections.singletonList(new TypeReference<Uint>(){})
  );
  assertThat(FunctionReturnDecoder.decode(
      "0x0000000000000000000000000000000000000000000000000000000000000037",
      function.getOutputParameters()),
      equalTo(Collections.singletonList(new Uint(BigInteger.valueOf(55)))));
}

代码示例来源:origin: web3j/web3j

@Test
public void testFunctionEmptyStringResultDecode() {
  Function function = new Function("test",
      Collections.emptyList(),
      Collections.singletonList(new TypeReference<Utf8String>() {
      }));
  List<Type> utf8Strings = FunctionReturnDecoder.decode(
      "0x0000000000000000000000000000000000000000000000000000000000000020"
          + "0000000000000000000000000000000000000000000000000000000000000000",
      function.getOutputParameters());
  assertThat(utf8Strings.get(0).getValue(), is(""));
}

代码示例来源:origin: web3j/web3j

private void confirmBalance(
    String address, String contractAddress, BigInteger expected) throws Exception {
  Function function = balanceOf(address);
  String responseValue = callSmartContractFunction(function, contractAddress);
  List<Type> response = FunctionReturnDecoder.decode(
      responseValue, function.getOutputParameters());
  assertThat(response.size(), is(1));
  assertThat(response.get(0), equalTo(new Uint256(expected)));
}

代码示例来源:origin: web3j/web3j

@Test
public void testSimpleFunctionStringResultDecode() {
  Function function = new Function("simple",
      Arrays.asList(),
      Collections.singletonList(new TypeReference<Utf8String>() {
      }));
  List<Type> utf8Strings = FunctionReturnDecoder.decode(
      "0x0000000000000000000000000000000000000000000000000000000000000020"
          + "000000000000000000000000000000000000000000000000000000000000000d"
          + "6f6e65206d6f72652074696d6500000000000000000000000000000000000000",
      function.getOutputParameters());
  assertThat(utf8Strings.get(0).getValue(), is("one more time"));
}

代码示例来源:origin: web3j/web3j

@Test
public void testMultipleResultFunctionDecode() {
  Function function = new Function(
      "test",
      Collections.<Type>emptyList(),
      Arrays.asList(new TypeReference<Uint>() { }, new TypeReference<Uint>() { })
  );
  assertThat(FunctionReturnDecoder.decode(
      "0x0000000000000000000000000000000000000000000000000000000000000037"
      + "0000000000000000000000000000000000000000000000000000000000000007",
      function.getOutputParameters()),
      equalTo(Arrays.asList(new Uint(BigInteger.valueOf(55)),
          new Uint(BigInteger.valueOf(7)))));
}

代码示例来源:origin: web3j/web3j

private BigInteger getTotalSupply(String contractAddress) throws Exception {
  Function function = totalSupply();
  String responseValue = callSmartContractFunction(function, contractAddress);
  List<Type> response = FunctionReturnDecoder.decode(
      responseValue, function.getOutputParameters());
  assertThat(response.size(), is(1));
  return (BigInteger) response.get(0).getValue();
}

代码示例来源:origin: web3j/web3j

@Test
public void testDecodeMultipleStringValues() {
  Function function = new Function("function",
      Collections.<Type>emptyList(),
      Arrays.asList(
          new TypeReference<Utf8String>() { }, new TypeReference<Utf8String>() { },
          new TypeReference<Utf8String>() { }, new TypeReference<Utf8String>() { }));
  assertThat(FunctionReturnDecoder.decode(
      "0x0000000000000000000000000000000000000000000000000000000000000080"
          + "00000000000000000000000000000000000000000000000000000000000000c0"
          + "0000000000000000000000000000000000000000000000000000000000000100"
          + "0000000000000000000000000000000000000000000000000000000000000140"
          + "0000000000000000000000000000000000000000000000000000000000000004"
          + "6465663100000000000000000000000000000000000000000000000000000000"
          + "0000000000000000000000000000000000000000000000000000000000000004"
          + "6768693100000000000000000000000000000000000000000000000000000000"
          + "0000000000000000000000000000000000000000000000000000000000000004"
          + "6a6b6c3100000000000000000000000000000000000000000000000000000000"
          + "0000000000000000000000000000000000000000000000000000000000000004"
          + "6d6e6f3200000000000000000000000000000000000000000000000000000000",
      function.getOutputParameters()),
      equalTo(Arrays.asList(
          new Utf8String("def1"), new Utf8String("ghi1"),
          new Utf8String("jkl1"), new Utf8String("mno2"))));
}

代码示例来源:origin: web3j/web3j

/**
 * Execute constant function call - i.e. a call that does not change state of the contract
 *
 * @param function to call
 * @return {@link List} of values returned by function call
 */
private List<Type> executeCall(
    Function function) throws IOException {
  String encodedFunction = FunctionEncoder.encode(function);
  org.web3j.protocol.core.methods.response.EthCall ethCall = web3j.ethCall(
      Transaction.createEthCallTransaction(
          transactionManager.getFromAddress(), contractAddress, encodedFunction),
      defaultBlockParameter)
      .send();
  String value = ethCall.getValue();
  return FunctionReturnDecoder.decode(value, function.getOutputParameters());
}

代码示例来源:origin: web3j/web3j

@Test
public void testContractCreation() throws Exception {
  boolean accountUnlocked = unlockAccount();
  assertTrue(accountUnlocked);
  String transactionHash = sendTransaction();
  assertFalse(transactionHash.isEmpty());
  TransactionReceipt transactionReceipt =
      waitForTransactionReceipt(transactionHash);
  assertThat(transactionReceipt.getTransactionHash(), is(transactionHash));
  assertFalse("Contract execution ran out of gas",
      transactionReceipt.getGasUsed().equals(GAS_LIMIT));
  String contractAddress = transactionReceipt.getContractAddress();
  assertNotNull(contractAddress);
  Function function = createFibonacciFunction();
  String responseValue = callSmartContractFunction(function, contractAddress);
  assertFalse(responseValue.isEmpty());
  List<Type> uint = FunctionReturnDecoder.decode(
      responseValue, function.getOutputParameters());
  assertThat(uint.size(), is(1));
  assertThat(uint.get(0).getValue(), equalTo(BigInteger.valueOf(13)));
}

代码示例来源:origin: web3j/web3j

@Test
public void testGreeterContract() throws Exception {
  boolean accountUnlocked = unlockAccount();
  assertTrue(accountUnlocked);
  // create our smart contract
  String createTransactionHash = sendCreateContractTransaction();
  assertFalse(createTransactionHash.isEmpty());
  TransactionReceipt createTransactionReceipt =
      waitForTransactionReceipt(createTransactionHash);
  assertThat(createTransactionReceipt.getTransactionHash(), is(createTransactionHash));
  assertFalse("Contract execution ran out of gas",
      createTransactionReceipt.getGasUsed().equals(GAS_LIMIT));
  String contractAddress = createTransactionReceipt.getContractAddress();
  assertNotNull(contractAddress);
  // call our getter
  Function getFunction = createGreetFunction();
  String responseValue = callSmartContractFunction(getFunction, contractAddress);
  assertFalse(responseValue.isEmpty());
  List<Type> response = FunctionReturnDecoder.decode(
      responseValue, getFunction.getOutputParameters());
  assertThat(response.size(), is(1));
  assertThat(response.get(0).getValue(), is(VALUE));
}

代码示例来源:origin: TrustWallet/trust-wallet-android-source

private BigDecimal getBalance(Wallet wallet, TokenInfo tokenInfo) throws Exception {
  org.web3j.abi.datatypes.Function function = balanceOf(wallet.address);
  String responseValue = callSmartContractFunction(function, tokenInfo.address, wallet);
  List<Type> response = FunctionReturnDecoder.decode(
      responseValue, function.getOutputParameters());
  if (response.size() == 1) {
    return new BigDecimal(((Uint256) response.get(0)).getValue());
  } else {
    return null;
  }
}

代码示例来源:origin: ethjava/web3j-sample

/**
 * 查询代币符号
 *
 * @param web3j
 * @param contractAddress
 * @return
 */
public static String getTokenSymbol(Web3j web3j, String contractAddress) {
  String methodName = "symbol";
  String symbol = null;
  String fromAddr = emptyAddress;
  List<Type> inputParameters = new ArrayList<>();
  List<TypeReference<?>> outputParameters = new ArrayList<>();
  TypeReference<Utf8String> typeReference = new TypeReference<Utf8String>() {
  };
  outputParameters.add(typeReference);
  Function function = new Function(methodName, inputParameters, outputParameters);
  String data = FunctionEncoder.encode(function);
  Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
  EthCall ethCall;
  try {
    ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
    List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
    symbol = results.get(0).getValue().toString();
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
  }
  return symbol;
}

代码示例来源:origin: ethjava/web3j-sample

/**
 * 查询代币名称
 *
 * @param web3j
 * @param contractAddress
 * @return
 */
public static String getTokenName(Web3j web3j, String contractAddress) {
  String methodName = "name";
  String name = null;
  String fromAddr = emptyAddress;
  List<Type> inputParameters = new ArrayList<>();
  List<TypeReference<?>> outputParameters = new ArrayList<>();
  TypeReference<Utf8String> typeReference = new TypeReference<Utf8String>() {
  };
  outputParameters.add(typeReference);
  Function function = new Function(methodName, inputParameters, outputParameters);
  String data = FunctionEncoder.encode(function);
  Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
  EthCall ethCall;
  try {
    ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
    List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
    name = results.get(0).getValue().toString();
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
  }
  return name;
}

代码示例来源:origin: ethjava/web3j-sample

/**
 * 查询代币发行总量
 *
 * @param web3j
 * @param contractAddress
 * @return
 */
public static BigInteger getTokenTotalSupply(Web3j web3j, String contractAddress) {
  String methodName = "totalSupply";
  String fromAddr = emptyAddress;
  BigInteger totalSupply = BigInteger.ZERO;
  List<Type> inputParameters = new ArrayList<>();
  List<TypeReference<?>> outputParameters = new ArrayList<>();
  TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
  };
  outputParameters.add(typeReference);
  Function function = new Function(methodName, inputParameters, outputParameters);
  String data = FunctionEncoder.encode(function);
  Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);
  EthCall ethCall;
  try {
    ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).sendAsync().get();
    List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
    totalSupply = (BigInteger) results.get(0).getValue();
  } catch (InterruptedException | ExecutionException e) {
    e.printStackTrace();
  }
  return totalSupply;
}

代码示例来源:origin: ethjava/web3j-sample

/**
 * 查询代币余额
 */
public static BigInteger getTokenBalance(Web3j web3j, String fromAddress, String contractAddress) {
  String methodName = "balanceOf";
  List<Type> inputParameters = new ArrayList<>();
  List<TypeReference<?>> outputParameters = new ArrayList<>();
  Address address = new Address(fromAddress);
  inputParameters.add(address);
  TypeReference<Uint256> typeReference = new TypeReference<Uint256>() {
  };
  outputParameters.add(typeReference);
  Function function = new Function(methodName, inputParameters, outputParameters);
  String data = FunctionEncoder.encode(function);
  Transaction transaction = Transaction.createEthCallTransaction(fromAddress, contractAddress, data);
  EthCall ethCall;
  BigInteger balanceValue = BigInteger.ZERO;
  try {
    ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();
    List<Type> results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());
    balanceValue = (BigInteger) results.get(0).getValue();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return balanceValue;
}

代码示例来源:origin: AppStoreFoundation/asf-sdk

@Override
public String getContractAddressById(String fromAddress, int chainId, String contractId) {
 List<Type> arguments = new ArrayList<>();
 List<TypeReference<?>> returnValues = new ArrayList<>();
 returnValues.add(new TypeReference<Address>() {
 });
 arguments.add(stringToBytes32(contractId));
 Function getContractAddressById =
   new Function("getContractAddressById", arguments, returnValues);
 String encodedFunction = FunctionEncoder.encode(getContractAddressById);
 Transaction ethCallTransaction = createEthCallTransaction(fromAddress,
   proxyContractAddressProvider.getProxyContractAddress(chainId), encodedFunction);
 try {
  EthCall rawResponse = web3jProvider.get(chainId)
    .ethCall(ethCallTransaction, DefaultBlockParameterName.LATEST)
    .send();
  if (!rawResponse.hasError()) {
   List<Type> response = FunctionReturnDecoder.decode(rawResponse.getValue(),
     getContractAddressById.getOutputParameters());
   return ((Address) response.get(0)).getValue();
  } else {
   throw new RuntimeException(mapErrorToMessage(rawResponse.getError()));
  }
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: org.jbpm.contrib/ethereum-workitem

public static Object queryExistingContract(Credentials credentials,
                      Web3j web3j,
                      String contractAddress,
                      String contractMethodName,
                      List<Type> contractMethodInputTypes,
                      List<TypeReference<?>> contractMethodOutputTypes
) throws Exception {
  Function function = getFunction(contractMethodName,
                  contractMethodInputTypes,
                  contractMethodOutputTypes);
  Transaction transaction = Transaction.createEthCallTransaction(credentials.getAddress(),
                                  contractAddress,
                                  getEncodedFunction(function));
  EthCall response = web3j.ethCall(
      transaction,
      DefaultBlockParameterName.LATEST).sendAsync().get();
  List<Type> responseTypeList = FunctionReturnDecoder.decode(
      response.getValue(),
      function.getOutputParameters());
  if (responseTypeList != null && responseTypeList.size() > 0) {
    return responseTypeList.get(0).getValue();
  } else {
    return null;
  }
}

相关文章

微信公众号

最新文章

更多