org.web3j.abi.datatypes.Function类的使用及代码示例

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

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

Function介绍

[英]Function type.
[中]函数类型。

代码示例

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

public RemoteCall<String> resolver(byte[] node) {
  final Function function = new Function(FUNC_RESOLVER, 
      Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)), 
      Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
  return executeRemoteCallSingleValueReturn(function, String.class);
}

代码示例来源: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

public static String encode(Function function) {
  List<Type> parameters = function.getInputParameters();
  String methodSignature = buildMethodSignature(function.getName(), parameters);
  String methodId = buildMethodId(methodSignature);
  StringBuilder result = new StringBuilder();
  result.append(methodId);
  return encodeParameters(parameters, result);
}

代码示例来源: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

private TransactionReceipt executeTransaction(
    Function function, BigInteger weiValue)
    throws IOException, TransactionException {
  return executeTransaction(FunctionEncoder.encode(function), weiValue, function.getName());
}

代码示例来源: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

public RemoteCall<String> owner(byte[] node) {
  final Function function = new Function(FUNC_OWNER, 
      Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)), 
      Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
  return executeRemoteCallSingleValueReturn(function, String.class);
}

代码示例来源: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

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: org.web3j/abi

public static String encode(Function function) {
  List<Type> parameters = function.getInputParameters();
  String methodSignature = buildMethodSignature(function.getName(), parameters);
  String methodId = buildMethodId(methodSignature);
  StringBuilder result = new StringBuilder();
  result.append(methodId);
  return encodeParameters(parameters, result);
}

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

public RemoteCall<BigInteger> ttl(byte[] node) {
  final Function function = new Function(FUNC_TTL, 
      Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)), 
      Arrays.<TypeReference<?>>asList(new TypeReference<Uint64>() {}));
  return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}

代码示例来源: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

/**
 * 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

public RemoteCall<Boolean> supportsInterface(byte[] interfaceID) {
  final Function function = new Function(FUNC_SUPPORTSINTERFACE, 
      Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes4(interfaceID)), 
      Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));
  return executeRemoteCallSingleValueReturn(function, Boolean.class);
}

代码示例来源: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

@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

public RemoteCall<byte[]> content(byte[] node) {
  final Function function = new Function(FUNC_CONTENT, 
      Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)), 
      Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}));
  return executeRemoteCallSingleValueReturn(function, byte[].class);
}

代码示例来源: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 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: web3j/web3j

public RemoteCall<String> addr(byte[] node) {
  final Function function = new Function(FUNC_ADDR, 
      Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node)), 
      Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
  return executeRemoteCallSingleValueReturn(function, String.class);
}

相关文章