Java Code Examples for org.web3j.abi.datatypes.Function

The following examples show how to use org.web3j.abi.datatypes.Function. These examples are extracted from open source projects. 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 Project: eventeum   Author: ConsenSys   File: EventEmitter.java    License: Apache License 2.0 6 votes vote down vote up
public Flowable<DummyEventAdditionalTypesEventResponse> dummyEventAdditionalTypesEventFlowable(EthFilter filter) {
    return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<Log, DummyEventAdditionalTypesEventResponse>() {
        @Override
        public DummyEventAdditionalTypesEventResponse apply(Log log) {
            Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(DUMMYEVENTADDITIONALTYPES_EVENT, log);
            DummyEventAdditionalTypesEventResponse typedResponse = new DummyEventAdditionalTypesEventResponse();
            typedResponse.log = log;
            typedResponse.uint16Value = (BigInteger) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.int64Value = (BigInteger) eventValues.getIndexedValues().get(1).getValue();
            typedResponse.addressArray = (List<String>) eventValues.getNonIndexedValues().get(0).getValue();
            typedResponse.byteValue = (byte[]) eventValues.getNonIndexedValues().get(1).getValue();
            typedResponse.boolValue = (Boolean) eventValues.getNonIndexedValues().get(2).getValue();
            return typedResponse;
        }
    });
}
 
Example #2
Source Project: web3j   Author: web3j   File: Ballot.java    License: Apache License 2.0 6 votes vote down vote up
public RemoteCall<Tuple4<BigInteger, Boolean, String, BigInteger>> voters(String param0) {
    final Function function = new Function(FUNC_VOTERS,
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(param0)),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<Bool>() {}, new TypeReference<Address>() {}, new TypeReference<Uint256>() {}));
    return new RemoteCall<Tuple4<BigInteger, Boolean, String, BigInteger>>(
            new Callable<Tuple4<BigInteger, Boolean, String, BigInteger>>() {
                @Override
                public Tuple4<BigInteger, Boolean, String, BigInteger> call() throws Exception {
                    List<Type> results = executeCallMultipleValueReturn(function);
                    return new Tuple4<BigInteger, Boolean, String, BigInteger>(
                            (BigInteger) results.get(0).getValue(),
                            (Boolean) results.get(1).getValue(),
                            (String) results.get(2).getValue(),
                            (BigInteger) results.get(3).getValue());
                }
            });
}
 
Example #3
Source Project: web3j   Author: web3j   File: Arrays.java    License: Apache License 2.0 6 votes vote down vote up
public RemoteFunctionCall<List> multiDynamic(List<List<BigInteger>> input) {
    final Function function = new Function(FUNC_MULTIDYNAMIC, 
            java.util.Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.StaticArray2>(
                    org.web3j.abi.datatypes.generated.StaticArray2.class,
                    org.web3j.abi.Utils.typeMap(input, org.web3j.abi.datatypes.generated.StaticArray2.class,
            org.web3j.abi.datatypes.generated.Uint256.class))), 
            java.util.Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Uint256>>() {}));
    return new RemoteFunctionCall<List>(function,
            new Callable<List>() {
                @Override
                @SuppressWarnings("unchecked")
                public List call() throws Exception {
                    List<Type> result = (List<Type>) executeCallSingleValueReturn(function, List.class);
                    return convertToNative(result);
                }
            });
}
 
Example #4
Source Project: web3j   Author: web3j   File: PublicResolver.java    License: Apache License 2.0 6 votes vote down vote up
public RemoteCall<Tuple2<BigInteger, byte[]>> ABI(byte[] node, BigInteger contentTypes) {
    final Function function = new Function(FUNC_ABI, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node), 
            new org.web3j.abi.datatypes.generated.Uint256(contentTypes)), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<DynamicBytes>() {}));
    return new RemoteCall<Tuple2<BigInteger, byte[]>>(
            new Callable<Tuple2<BigInteger, byte[]>>() {
                @Override
                public Tuple2<BigInteger, byte[]> call() throws Exception {
                    List<Type> results = executeCallMultipleValueReturn(function);
                    return new Tuple2<BigInteger, byte[]>(
                            (BigInteger) results.get(0).getValue(), 
                            (byte[]) results.get(1).getValue());
                }
            });
}
 
Example #5
Source Project: alpha-wallet-android   Author: AlphaWallet   File: TokenRepository.java    License: MIT License 6 votes vote down vote up
@Override
public Single<ContractLocator> getTokenResponse(String address, int chainId, String method)
{
    return Single.fromCallable(() -> {
        ContractLocator contractLocator = new ContractLocator(INVALID_CONTRACT, chainId);
        Function function = new Function(method,
                                                                 Arrays.<Type>asList(),
                                                                 Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));

        Wallet temp = new Wallet(null);
        String responseValue = callCustomNetSmartContractFunction(function, address, temp, chainId);
        if (responseValue == null) return contractLocator;

        List<Type> response = FunctionReturnDecoder.decode(
                responseValue, function.getOutputParameters());
        if (response.size() == 1)
        {
            return new ContractLocator((String) response.get(0).getValue(), chainId);
        }
        else
        {
            return contractLocator;
        }
    });
}
 
Example #6
Source Project: client-sdk-java   Author: PlatONnetwork   File: FunctionReturnDecoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #7
Source Project: web3j   Author: web3j   File: Arrays.java    License: Apache License 2.0 6 votes vote down vote up
public RemoteFunctionCall<List> multiFixed(List<List<BigInteger>> input) {
    final Function function = new Function(FUNC_MULTIFIXED, 
            java.util.Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.StaticArray6<org.web3j.abi.datatypes.generated.StaticArray2>(
                    org.web3j.abi.datatypes.generated.StaticArray2.class,
                    org.web3j.abi.Utils.typeMap(input, org.web3j.abi.datatypes.generated.StaticArray2.class,
            org.web3j.abi.datatypes.generated.Uint256.class))), 
            java.util.Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Uint256>>() {}));
    return new RemoteFunctionCall<List>(function,
            new Callable<List>() {
                @Override
                @SuppressWarnings("unchecked")
                public List call() throws Exception {
                    List<Type> result = (List<Type>) executeCallSingleValueReturn(function, List.class);
                    return convertToNative(result);
                }
            });
}
 
Example #8
Source Project: client-sdk-java   Author: PlatONnetwork   File: Contract.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
protected <T extends Type, R> R executeCallSingleValueReturn(
        Function function, Class<R> returnType) throws IOException {
    T result = executeCallSingleValueReturn(function);
    if (result == null) {
        throw new ContractCallException("Empty value (0x) returned from contract");
    }

    Object value = result.getValue();
    if (returnType.isAssignableFrom(value.getClass())) {
        return (R) value;
    } else if (result.getClass().equals(Address.class) && returnType.equals(String.class)) {
        return (R) result.toString();  // cast isn't necessary
    } else {
        throw new ContractCallException(
                "Unable to convert response: " + value
                        + " to expected type: " + returnType.getSimpleName());
    }
}
 
Example #9
Source Project: alpha-wallet-android   Author: AlphaWallet   File: TokenRepository.java    License: MIT License 6 votes vote down vote up
public static byte[] createDropCurrency(MagicLinkData order, int v, byte[] r, byte[] s, String recipient)
{
    Function function = new Function(
            "dropCurrency",
            Arrays.asList(new org.web3j.abi.datatypes.generated.Uint32(order.nonce),
                          new org.web3j.abi.datatypes.generated.Uint32(order.amount),
                          new org.web3j.abi.datatypes.generated.Uint32(order.expiry),
                          new org.web3j.abi.datatypes.generated.Uint8(v),
                          new org.web3j.abi.datatypes.generated.Bytes32(r),
                          new org.web3j.abi.datatypes.generated.Bytes32(s),
                          new org.web3j.abi.datatypes.Address(recipient)),
            Collections.emptyList());

    String encodedFunction = FunctionEncoder.encode(function);
    return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction));
}
 
Example #10
Source Project: ethsigner   Author: PegaSysEng   File: SimpleStorage.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> set(BigInteger value) {
  final Function function =
      new Function(
          FUNC_SET,
          Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(value)),
          Collections.<TypeReference<?>>emptyList());
  return executeRemoteCallTransaction(function);
}
 
Example #11
Source Project: web3j   Author: web3j   File: FunctionReturnDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testVoidResultFunctionDecode() {
    Function function = new Function("test", Collections.emptyList(), Collections.emptyList());

    assertEquals(
            FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
            (Collections.emptyList()));
}
 
Example #12
Source Project: web3j   Author: web3j   File: ContractTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticGasProvider() throws IOException, TransactionException {
    StaticGasProvider gasProvider = new StaticGasProvider(BigInteger.TEN, BigInteger.ONE);
    TransactionManager txManager = mock(TransactionManager.class);

    when(txManager.executeTransaction(
                    any(BigInteger.class),
                    any(BigInteger.class),
                    anyString(),
                    anyString(),
                    any(BigInteger.class),
                    anyBoolean()))
            .thenReturn(new TransactionReceipt());

    contract = new TestContract(ADDRESS, web3j, txManager, gasProvider);

    Function func =
            new Function(
                    "test",
                    Collections.<Type>emptyList(),
                    Collections.<TypeReference<?>>emptyList());
    contract.executeTransaction(func);

    verify(txManager)
            .executeTransaction(
                    eq(BigInteger.TEN),
                    eq(BigInteger.ONE),
                    anyString(),
                    anyString(),
                    any(BigInteger.class),
                    anyBoolean());
}
 
Example #13
Source Project: ETHWallet   Author: snailflying   File: TokenRepository.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static org.web3j.abi.datatypes.Function balanceOf(String owner) {
    return new org.web3j.abi.datatypes.Function(
            "balanceOf",
            Collections.singletonList(new Address(owner)),
            Collections.singletonList(new TypeReference<Uint256>() {
            }));
}
 
Example #14
Source Project: etherscan-explorer   Author: bing-chou   File: PublicResolver.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<String> text(byte[] node, String key) {
    Function function = new Function("text",
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node),
            new org.web3j.abi.datatypes.Utf8String(key)),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    return executeRemoteCallSingleValueReturn(function, String.class);
}
 
Example #15
Source Project: besu   Author: hyperledger   File: OnChainPrivacyGroupManagementProxy.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<Boolean> canExecute() {
  final Function function =
      new Function(
          FUNC_CANEXECUTE,
          Arrays.<Type>asList(),
          Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));
  return executeRemoteCallSingleValueReturn(function, Boolean.class);
}
 
Example #16
Source Project: web3j   Author: web3j   File: SimpleAuction.java    License: Apache License 2.0 5 votes vote down vote up
public Flowable<HighestBidIncreasedEventResponse> highestBidIncreasedEventFlowable(EthFilter filter) {
    return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<Log, HighestBidIncreasedEventResponse>() {
        @Override
        public HighestBidIncreasedEventResponse apply(Log log) {
            Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(HIGHESTBIDINCREASED_EVENT, log);
            HighestBidIncreasedEventResponse typedResponse = new HighestBidIncreasedEventResponse();
            typedResponse.log = log;
            typedResponse.bidder = (String) eventValues.getNonIndexedValues().get(0).getValue();
            typedResponse.amount = (BigInteger) eventValues.getNonIndexedValues().get(1).getValue();
            return typedResponse;
        }
    });
}
 
Example #17
Source Project: etherscan-explorer   Author: bing-chou   File: ENS.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> setOwner(byte[] node, String owner) {
    Function function = new Function(
            "setOwner",
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node),
            new org.web3j.abi.datatypes.Address(owner)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #18
Source Project: besu   Author: hyperledger   File: EventEmitter.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<BigInteger> value() {
  final Function function =
      new Function(
          FUNC_VALUE,
          Arrays.<Type>asList(),
          Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
  return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
 
Example #19
Source Project: web3j   Author: web3j   File: ENS.java    License: Apache License 2.0 5 votes vote down vote up
public Flowable<NewResolverEventResponse> newResolverEventFlowable(EthFilter filter) {
    return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<Log, NewResolverEventResponse>() {
        @Override
        public NewResolverEventResponse apply(Log log) {
            Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(NEWRESOLVER_EVENT, log);
            NewResolverEventResponse typedResponse = new NewResolverEventResponse();
            typedResponse.log = log;
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.resolver = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #20
Source Project: besu   Author: hyperledger   File: EventEmitter.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<String> sender() {
  final Function function =
      new Function(
          FUNC_SENDER,
          Arrays.<Type>asList(),
          Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
  return executeRemoteCallSingleValueReturn(function, String.class);
}
 
Example #21
Source Project: web3j-quorum   Author: web3j   File: Greeter.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> newGreeting(String _greeting) {
    final Function function = new Function(
            FUNC_NEWGREETING,
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #22
Source Project: etherscan-explorer   Author: bing-chou   File: PublicResolver.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> setContent(byte[] node, byte[] hash) {
    Function function = new Function(
            "setContent",
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node),
            new org.web3j.abi.datatypes.generated.Bytes32(hash)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #23
Source Project: jbpm-work-items   Author: kiegroup   File: Greeter.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<String> greet() {
    Function function = new Function("greet",
                                     Arrays.<Type>asList(),
                                     Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
                                     }));
    return executeRemoteCallSingleValueReturn(function,
                                              String.class);
}
 
Example #24
Source Project: besu   Author: hyperledger   File: CrossContractReader.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> remoteDestroy(final String crossAddress) {
  final Function function =
      new Function(
          FUNC_REMOTEDESTROY,
          Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, crossAddress)),
          Collections.<TypeReference<?>>emptyList());
  return executeRemoteCallTransaction(function);
}
 
Example #25
Source Project: etherscan-explorer   Author: bing-chou   File: HumanStandardTokenIT.java    License: GNU General Public License v3.0 5 votes vote down vote up
public void sendTransferFromTransaction(
        Credentials credentials, String from, String to, BigInteger value,
        String contractAddress) throws Exception {

    Function function = transferFrom(from, to, value);
    String functionHash = execute(credentials, function, contractAddress);

    TransactionReceipt transferTransactionReceipt =
            waitForTransactionReceipt(functionHash);
    assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash));

    List<Log> logs = transferTransactionReceipt.getLogs();
    assertFalse(logs.isEmpty());
    Log log = logs.get(0);

    Event transferEvent = transferEvent();
    List<String> topics = log.getTopics();

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(transferEvent);
    assertThat(topics.get(0), is(encodedEventSignature));
    assertThat(new Address(topics.get(1)), is(new Address(from)));
    assertThat(new Address(topics.get(2)), is(new Address(to)));

    // verify qty transferred
    List<Type> results = FunctionReturnDecoder.decode(
            log.getData(), transferEvent.getNonIndexedParameters());
    assertThat(results, equalTo(Collections.singletonList(new Uint256(value))));
}
 
Example #26
Source Project: web3j   Author: web3j   File: EthCallIT.java    License: Apache License 2.0 5 votes vote down vote up
private EthCall ethCall(BigInteger value) throws java.io.IOException {
    final Function function =
            new Function(
                    Revert.FUNC_SET,
                    Collections.singletonList(new Uint256(value)),
                    Collections.emptyList());
    String encodedFunction = FunctionEncoder.encode(function);

    return web3j.ethCall(
                    Transaction.createEthCallTransaction(
                            ALICE.getAddress(), contract.getContractAddress(), encodedFunction),
                    DefaultBlockParameterName.LATEST)
            .send();
}
 
Example #27
Source Project: web3j   Author: web3j   File: FunctionReturnDecoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleFunctionDecode() {
    Function function =
            new Function(
                    "test",
                    Collections.<Type>emptyList(),
                    Collections.singletonList(new TypeReference<Uint>() {}));

    assertEquals(
            FunctionReturnDecoder.decode(
                    "0x0000000000000000000000000000000000000000000000000000000000000037",
                    function.getOutputParameters()),
            (Collections.singletonList(new Uint(BigInteger.valueOf(55)))));
}
 
Example #28
Source Project: etherscan-explorer   Author: bing-chou   File: HumanStandardToken.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<BigInteger> allowance(String _owner, String _spender) {
    final Function function = new Function("allowance", 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_owner), 
            new org.web3j.abi.datatypes.Address(_spender)), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
 
Example #29
Source Project: Android-Wallet-Token-ERC20   Author: EasyToken   File: TokenERC20.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<BigInteger> allowance(String param0, String param1) {
    Function function = new Function("allowance",
            Arrays.<Type>asList(new Address(param0),
            new Address(param1)),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
    return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
 
Example #30
Source Project: web3j   Author: web3j   File: ERC721.java    License: Apache License 2.0 5 votes vote down vote up
public Flowable<ApprovalForAllEventResponse> approvalForAllEventFlowable(EthFilter filter) {
    return web3j.ethLogFlowable(filter).map(new io.reactivex.functions.Function<Log, ApprovalForAllEventResponse>() {
        @Override
        public ApprovalForAllEventResponse apply(Log log) {
            Contract.EventValuesWithLog eventValues = extractEventParametersWithLog(APPROVALFORALL_EVENT, log);
            ApprovalForAllEventResponse typedResponse = new ApprovalForAllEventResponse();
            typedResponse.log = log;
            typedResponse._owner = (String) eventValues.getIndexedValues().get(0).getValue();
            typedResponse._operator = (String) eventValues.getIndexedValues().get(1).getValue();
            typedResponse._approved = (Boolean) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}