Java Code Examples for org.web3j.abi.TypeReference

The following examples show how to use org.web3j.abi.TypeReference. 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: web3j   Author: web3j   File: EventTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreation() {

    List<TypeReference<?>> parameters =
            Arrays.<TypeReference<?>>asList(
                    new TypeReference<Address>() {}, new TypeReference<Uint256>() {});
    Event event = new Event("testName", parameters);

    assertEquals(event.getName(), "testName");

    Iterator<TypeReference<?>> expectedParameter = parameters.iterator();
    for (TypeReference<?> actualParameter : event.getParameters()) {
        assertEquals(expectedParameter.next(), actualParameter);
    }

    assertEquals(0, event.getIndexedParameters().size());

    for (TypeReference<?> nonIndexedParameter : event.getNonIndexedParameters()) {
        assertEquals(false, nonIndexedParameter.isIndexed());
    }
}
 
Example #2
Source Project: etherscan-explorer   Author: bing-chou   File: ENS.java    License: GNU General Public License v3.0 6 votes vote down vote up
public Observable<TransferEventResponse> transferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("Transfer", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, TransferEventResponse>() {
        @Override
        public TransferEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            TransferEventResponse typedResponse = new TransferEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.owner = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #3
Source Project: web3j   Author: web3j   File: ShipIt.java    License: Apache License 2.0 6 votes vote down vote up
public RemoteCall<Tuple8<String, String, BigInteger, BigInteger, BigInteger, BigInteger, String, byte[]>> shipments(String param0) {
    final Function function = new Function(FUNC_SHIPMENTS, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(param0)), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}, new TypeReference<Address>() {}, new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}, new TypeReference<Uint8>() {}, new TypeReference<Uint256>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Bytes32>() {}));
    return new RemoteCall<Tuple8<String, String, BigInteger, BigInteger, BigInteger, BigInteger, String, byte[]>>(
            new Callable<Tuple8<String, String, BigInteger, BigInteger, BigInteger, BigInteger, String, byte[]>>() {
                @Override
                public Tuple8<String, String, BigInteger, BigInteger, BigInteger, BigInteger, String, byte[]> call() throws Exception {
                    List<Type> results = executeCallMultipleValueReturn(function);
                    return new Tuple8<String, String, BigInteger, BigInteger, BigInteger, BigInteger, String, byte[]>(
                            (String) results.get(0).getValue(), 
                            (String) results.get(1).getValue(), 
                            (BigInteger) results.get(2).getValue(), 
                            (BigInteger) results.get(3).getValue(), 
                            (BigInteger) results.get(4).getValue(), 
                            (BigInteger) results.get(5).getValue(), 
                            (String) results.get(6).getValue(), 
                            (byte[]) results.get(7).getValue());
                }
            });
}
 
Example #4
Source Project: etherscan-explorer   Author: bing-chou   File: Arrays.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<List> returnArray() {
    final Function function = new Function("returnArray", 
            java.util.Arrays.<Type>asList(), 
            java.util.Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Address>>() {}));
    return new RemoteCall<List>(
            new Callable<List>() {
                @Override
                @SuppressWarnings("unchecked")
                public List call() throws Exception {
                    List<Type> result = (List<Type>) executeCallSingleValueReturn(function, List.class);
                    return convertToNative(result);
                }
            });
}
 
Example #5
Source Project: trust-wallet-android-source   Author: trustwallet   File: TokenRepository.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static byte[] createTokenTransferData(String to, BigInteger tokenAmount) {
    List<Type> params = Arrays.<Type>asList(new Address(to), new Uint256(tokenAmount));

    List<TypeReference<?>> returnTypes = Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {
    });

    Function function = new Function("transfer", params, returnTypes);
    String encodedFunction = FunctionEncoder.encode(function);
    return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction));
}
 
Example #6
Source Project: besu   Author: hyperledger   File: DefaultOnChainPrivacyGroupManagementContract.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<byte[]> getVersion() {
  final org.web3j.abi.datatypes.Function function =
      new org.web3j.abi.datatypes.Function(
          FUNC_GETVERSION,
          Arrays.<Type>asList(),
          Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}));
  return executeRemoteCallSingleValueReturn(function, byte[].class);
}
 
Example #7
Source Project: web3j   Author: web3j   File: SolidityFunctionWrapper.java    License: Apache License 2.0 5 votes vote down vote up
private static void buildVariableLengthReturnFunctionConstructor(
        MethodSpec.Builder methodBuilder,
        String functionName,
        String inputParameters,
        List<TypeName> outputParameterTypes,
        boolean useUpperCase)
        throws ClassNotFoundException {

    List<Object> objects = new ArrayList<>();
    objects.add(Function.class);
    objects.add(Function.class);
    objects.add(funcNameToConst(functionName, useUpperCase));

    objects.add(Arrays.class);
    objects.add(Type.class);
    objects.add(inputParameters);

    objects.add(Arrays.class);
    objects.add(TypeReference.class);
    for (TypeName outputParameterType : outputParameterTypes) {
        objects.add(TypeReference.class);
        objects.add(outputParameterType);
    }

    String asListParams =
            Collection.join(outputParameterTypes, ", ", typeName -> "new $T<$T>() {}");

    methodBuilder.addStatement(
            "final $T function = new $T($N, \n$T.<$T>asList($L), \n$T"
                    + ".<$T<?>>asList("
                    + asListParams
                    + "))",
            objects.toArray());
}
 
Example #8
Source Project: web3j   Author: web3j   File: SimpleStorage.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> set(BigInteger x) {
    final Function function = new Function(
            FUNC_SET, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(x)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #9
Source Project: web3j   Author: web3j   File: Ballot.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> delegate(String to) {
    final Function function = new Function(
            FUNC_DELEGATE, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(to)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #10
Source Project: web3j   Author: web3j   File: ERC721.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> transferFrom(String _from, String _to, BigInteger _tokenId, BigInteger weiValue) {
    final Function function = new Function(
            FUNC_TRANSFERFROM, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_from), 
            new org.web3j.abi.datatypes.Address(_to), 
            new org.web3j.abi.datatypes.generated.Uint256(_tokenId)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function, weiValue);
}
 
Example #11
Source Project: web3j-quorum   Author: web3j   File: HumanStandardToken.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<BigInteger> allowance(String _owner, String _spender) {
    final Function function = new Function(FUNC_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 #12
Source Project: besu   Author: hyperledger   File: OnChainPrivacyGroupManagementProxy.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<String> implementation() {
  final Function function =
      new Function(
          FUNC_IMPLEMENTATION,
          Arrays.<Type>asList(),
          Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
  return executeRemoteCallSingleValueReturn(function, String.class);
}
 
Example #13
Source Project: besu   Author: hyperledger   File: OnChainPrivacyGroupManagementProxy.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> removeParticipant(
    byte[] enclaveKey, byte[] account) {
  final Function function =
      new Function(
          FUNC_REMOVEPARTICIPANT,
          Arrays.<Type>asList(
              new org.web3j.abi.datatypes.generated.Bytes32(enclaveKey),
              new org.web3j.abi.datatypes.generated.Bytes32(account)),
          Collections.<TypeReference<?>>emptyList());
  return executeRemoteCallTransaction(function);
}
 
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 List<ContentChangedEventResponse> getContentChangedEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("ContentChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<ContentChangedEventResponse> responses = new ArrayList<ContentChangedEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        ContentChangedEventResponse typedResponse = new ContentChangedEventResponse();
        typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
        typedResponse.hash = (byte[]) eventValues.getNonIndexedValues().get(0).getValue();
        responses.add(typedResponse);
    }
    return responses;
}
 
Example #15
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 #16
Source Project: web3j-quorum   Author: web3j   File: Greeter.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> kill() {
    final Function function = new Function(
            FUNC_KILL,
            Arrays.<Type>asList(),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #17
Source Project: besu   Author: hyperledger   File: SimpleStorage.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> set(final 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 #18
Source Project: web3j   Author: web3j   File: Arrays.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<List> returnArray() {
    final Function function = new Function(FUNC_RETURNARRAY, 
            java.util.Arrays.<Type>asList(), 
            java.util.Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Address>>() {}));
    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 #19
Source Project: eventeum   Author: ConsenSys   File: EventEmitter.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> emitEventBytes16(byte[] bytes16Value) {
    final Function function = new Function(
            FUNC_EMITEVENTBYTES16, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes16(bytes16Value)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #20
Source Project: jbpm-work-items   Author: kiegroup   File: EthereumUtils.java    License: Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example #21
Source Project: web3j   Author: web3j   File: Ballot.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> vote(BigInteger proposal) {
    final Function function = new Function(
            FUNC_VOTE, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(proposal)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #22
Source Project: web3j   Author: web3j   File: ERC20.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> approve(String _spender, BigInteger _value) {
    final Function function = new Function(
            FUNC_APPROVE, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_spender), 
            new org.web3j.abi.datatypes.generated.Uint256(_value)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #23
Source Project: etherscan-explorer   Author: bing-chou   File: PublicResolver.java    License: GNU General Public License v3.0 5 votes vote down vote up
public List<NameChangedEventResponse> getNameChangedEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("NameChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<NameChangedEventResponse> responses = new ArrayList<NameChangedEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        NameChangedEventResponse typedResponse = new NameChangedEventResponse();
        typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
        typedResponse.name = (String) eventValues.getNonIndexedValues().get(0).getValue();
        responses.add(typedResponse);
    }
    return responses;
}
 
Example #24
Source Project: web3j   Author: web3j   File: ERC20.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<BigInteger> allowance(String _owner, String _spender) {
    final Function function = new Function(FUNC_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 #25
Source Project: web3j   Author: web3j   File: SolidityFunctionWrapper.java    License: Apache License 2.0 5 votes vote down vote up
private static CodeBlock buildVariableLengthEventInitializer(
        String eventName, List<NamedTypeName> parameterTypes) {

    List<Object> objects = new ArrayList<>();
    objects.add(Event.class);
    objects.add(eventName);

    objects.add(Arrays.class);
    objects.add(TypeReference.class);
    for (NamedTypeName parameterType : parameterTypes) {
        objects.add(TypeReference.class);
        objects.add(parameterType.getTypeName());
    }

    String asListParams =
            parameterTypes.stream()
                    .map(
                            type -> {
                                if (type.isIndexed()) {
                                    return "new $T<$T>(true) {}";
                                } else {
                                    return "new $T<$T>() {}";
                                }
                            })
                    .collect(Collectors.joining(", "));

    return CodeBlock.builder()
            .addStatement(
                    "new $T($S, \n" + "$T.<$T<?>>asList(" + asListParams + "))",
                    objects.toArray())
            .build();
}
 
Example #26
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<TransactionReceipt> approveAndCall(String _spender, BigInteger _value, byte[] _extraData) {
    final Function function = new Function(
            "approveAndCall", 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_spender), 
            new org.web3j.abi.datatypes.generated.Uint256(_value), 
            new org.web3j.abi.datatypes.DynamicBytes(_extraData)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #27
Source Project: web3j-quorum   Author: web3j   File: HumanStandardToken.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> transfer(String _to, BigInteger _value) {
    final Function function = new Function(
            FUNC_TRANSFER,
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_to),
                    new org.web3j.abi.datatypes.generated.Uint256(_value)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #28
Source Project: client-sdk-java   Author: PlatONnetwork   File: SolidityFunctionWrapper.java    License: Apache License 2.0 5 votes vote down vote up
private static void buildVariableLengthReturnFunctionConstructor(
        MethodSpec.Builder methodBuilder, String functionName, String inputParameters,
        List<TypeName> outputParameterTypes) throws ClassNotFoundException {

    List<Object> objects = new ArrayList<>();
    objects.add(Function.class);
    objects.add(Function.class);
    objects.add(funcNameToConst(functionName));

    objects.add(Arrays.class);
    objects.add(Type.class);
    objects.add(inputParameters);

    objects.add(Arrays.class);
    objects.add(TypeReference.class);
    for (TypeName outputParameterType : outputParameterTypes) {
        objects.add(TypeReference.class);
        objects.add(outputParameterType);
    }

    String asListParams = Collection.join(
            outputParameterTypes,
            ", ",
            typeName -> "new $T<$T>() {}");

    methodBuilder.addStatement("final $T function = new $T($N, \n$T.<$T>asList($L), \n$T"
            + ".<$T<?>>asList("
            + asListParams + "))", objects.toArray());
}
 
Example #29
Source Project: web3j   Author: web3j   File: PublicResolver.java    License: Apache License 2.0 5 votes vote down vote up
public RemoteCall<String> text(byte[] node, String key) {
    final Function function = new Function(FUNC_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 #30
Source Project: etherscan-explorer   Author: bing-chou   File: Greeter.java    License: GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> kill() {
    final Function function = new Function(
            "kill", 
            Arrays.<Type>asList(), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}