org.web3j.abi.datatypes.Type Java Examples

The following examples show how to use org.web3j.abi.datatypes.Type. 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: SendingTokensActivity.java    From guarda-android-wallets with GNU General Public License v3.0 7 votes vote down vote up
private String getContractTransfer() {
    int tokenDecimals = tokensManager.getTokenByCode(tokenCode).getDecimal();
    BigInteger tokenValue = Converter.toDecimals(new BigDecimal(amountToSend), tokenDecimals).toBigInteger();
    List<Type> inputParams = new ArrayList<>();

    Type address = new Address(walletNumber);
    inputParams.add(address);

    Type value = new Uint(tokenValue);
    inputParams.add(value);

    Function function = new Function(
            "transfer",
            inputParams,
            Collections.<TypeReference<?>>emptyList());

    return FunctionEncoder.encode(function);
}
 
Example #2
Source File: OnChainPrivacyGroupManagementProxy.java    From besu with Apache License 2.0 6 votes vote down vote up
@Deprecated
public static RemoteCall<OnChainPrivacyGroupManagementProxy> deploy(
    Web3j web3j,
    TransactionManager transactionManager,
    BigInteger gasPrice,
    BigInteger gasLimit,
    String _implementation) {
  String encodedConstructor =
      FunctionEncoder.encodeConstructor(
          Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _implementation)));
  return deployRemoteCall(
      OnChainPrivacyGroupManagementProxy.class,
      web3j,
      transactionManager,
      gasPrice,
      gasLimit,
      BINARY,
      encodedConstructor);
}
 
Example #3
Source File: FunctionReturnDecoderTest.java    From web3j with 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());

    assertEquals(utf8Strings.get(0).getValue(), ("one more time"));
}
 
Example #4
Source File: Contract.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public static EventValues staticExtractEventParameters(
        Event event, Log log) {

    List<String> topics = log.getTopics();
    String encodedEventSignature = EventEncoder.encode(event);
    if (!topics.get(0).equals(encodedEventSignature)) {
        return null;
    }

    List<Type> indexedValues = new ArrayList<>();
    List<Type> nonIndexedValues = FunctionReturnDecoder.decode(
            log.getData(), event.getNonIndexedParameters());

    List<TypeReference<Type>> indexedParameters = event.getIndexedParameters();
    for (int i = 0; i < indexedParameters.size(); i++) {
        Type value = FunctionReturnDecoder.decodeIndexedValue(
                topics.get(i + 1), indexedParameters.get(i));
        indexedValues.add(value);
    }
    return new EventValues(indexedValues, nonIndexedValues);
}
 
Example #5
Source File: Utils.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
static <T extends Type> String getTypeName(TypeReference<T> typeReference) {
    try {
        java.lang.reflect.Type reflectedType = typeReference.getType();

        Class<?> type;
        if (reflectedType instanceof ParameterizedType) {
            type = (Class<?>) ((ParameterizedType) reflectedType).getRawType();
            return getParameterizedTypeName(typeReference, type);
        } else {
            type = Class.forName(reflectedType.getTypeName());
            return getSimpleTypeName(type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example #6
Source File: Arrays.java    From web3j with Apache License 2.0 6 votes vote down vote up
public RemoteFunctionCall<List> fixedReverse(List<BigInteger> input) {
    final Function function = new Function(FUNC_FIXEDREVERSE, 
            java.util.Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.StaticArray10<org.web3j.abi.datatypes.generated.Uint256>(
                    org.web3j.abi.datatypes.generated.Uint256.class,
                    org.web3j.abi.Utils.typeMap(input, org.web3j.abi.datatypes.generated.Uint256.class))), 
            java.util.Arrays.<TypeReference<?>>asList(new TypeReference<StaticArray10<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 #7
Source File: OnChainPrivacyGroupManagementInterface.java    From besu with Apache License 2.0 6 votes vote down vote up
public RemoteFunctionCall<List> getParticipants(byte[] enclaveKey) {
  final Function function =
      new Function(
          FUNC_GETPARTICIPANTS,
          Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(enclaveKey)),
          Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Bytes32>>() {}));
  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 File: DefaultFunctionReturnDecoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Type> Type decodeEventParameter(
        String rawInput, TypeReference<T> typeReference) {

    String input = Numeric.cleanHexPrefix(rawInput);

    try {
        Class<T> type = typeReference.getClassType();

        if (Bytes.class.isAssignableFrom(type)) {
            Class<Bytes> bytesClass = (Class<Bytes>) Class.forName(type.getName());
            return TypeDecoder.decodeBytes(input, bytesClass);
        } else if (Array.class.isAssignableFrom(type)
                || BytesType.class.isAssignableFrom(type)
                || Utf8String.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, Bytes32.class);
        } else {
            return TypeDecoder.decode(input, type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example #9
Source File: PublicResolver.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> setAddr(byte[] node, String addr) {
    final Function function = new Function(
            FUNC_SETADDR, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node), 
            new org.web3j.abi.datatypes.Address(addr)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #10
Source File: TransactionHandler.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private List callSmartContractFunctionArray(
        org.web3j.abi.datatypes.Function function, String contractAddress, String address) throws Exception
{
    String encodedFunction = FunctionEncoder.encode(function);
    String value = makeEthCall(
            org.web3j.protocol.core.methods.request.Transaction
                    .createEthCallTransaction(address, contractAddress, encodedFunction));

    List<Type> values = FunctionReturnDecoder.decode(value, function.getOutputParameters());
    if (values.isEmpty()) return null;

    Type T = values.get(0);
    Object o = T.getValue();
    return (List) o;
}
 
Example #11
Source File: FunctionReturnDecoderTest.java    From client-sdk-java with 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>(){})
    );

    assertThat(FunctionReturnDecoder.decode(
            "0x0000000000000000000000000000000000000000000000000000000000000037",
            function.getOutputParameters()),
            equalTo(Collections.singletonList(new Uint(BigInteger.valueOf(55)))));
}
 
Example #12
Source File: AbiTypesMapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private MethodSpec.Builder generateIntTypes(MethodSpec.Builder builder, String packageName) {
    for (int bitSize = 8; bitSize <= Type.MAX_BIT_LENGTH; bitSize += 8) {

        builder = addStatement(builder, packageName,
                Uint.TYPE_NAME + bitSize, Uint.class.getSimpleName() + bitSize);
        builder = addStatement(builder, packageName,
                Int.TYPE_NAME + bitSize, Int.class.getSimpleName() + bitSize);
    }
    return builder;
}
 
Example #13
Source File: HumanStandardToken.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> approveAndCall(String _spender, BigInteger _value, byte[] _extraData) {
    final Function function = new Function(
            FUNC_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 #14
Source File: HumanStandardToken.java    From web3j-quorum with Apache License 2.0 5 votes vote down vote up
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider, BigInteger _initialAmount, String _tokenName, BigInteger _decimalUnits, String _tokenSymbol) {
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_initialAmount),
            new org.web3j.abi.datatypes.Utf8String(_tokenName),
            new org.web3j.abi.datatypes.generated.Uint8(_decimalUnits),
            new org.web3j.abi.datatypes.Utf8String(_tokenSymbol)));
    return deployRemoteCall(HumanStandardToken.class, web3j, credentials, contractGasProvider, BINARY, encodedConstructor);
}
 
Example #15
Source File: PayWages.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<PayWages> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit,
		BigInteger initialWeiValue, String _personnel, String _financer) {
	String encodedConstructor = FunctionEncoder.encodeConstructor(
			Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_personnel), new org.web3j.abi.datatypes.Address(_financer)));
	return deployRemoteCall(PayWages.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue);
}
 
Example #16
Source File: ENS.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> setTTL(byte[] node, BigInteger ttl) {
    final Function function = new Function(
            FUNC_SETTTL, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node), 
            new org.web3j.abi.datatypes.generated.Uint64(ttl)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #17
Source File: DefaultFunctionReturnDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static <T extends Type> int getDataOffset(String input, int offset, Class<T> type) {
    if (DynamicBytes.class.isAssignableFrom(type)
            || Utf8String.class.isAssignableFrom(type)
            || DynamicArray.class.isAssignableFrom(type)) {
        return TypeDecoder.decodeUintAsInt(input, offset) << 1;
    } else {
        return offset;
    }
}
 
Example #18
Source File: PlatOnTypeEncoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static String encode(Type parameter) {
	if (parameter instanceof IntType) {
		return encodeInt(((IntType) parameter));
	}else if (parameter instanceof Utf8String) {
        return encodeString((Utf8String) parameter);
    }else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #19
Source File: SimpleAuction.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> withdraw() {
    final Function function = new Function(
            FUNC_WITHDRAW, 
            Arrays.<Type>asList(), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #20
Source File: ContractTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<Utf8String> callSingleValue() {
    Function function =
            new Function(
                    "call",
                    Collections.<Type>emptyList(),
                    Collections.<TypeReference<?>>singletonList(
                            new TypeReference<Utf8String>() {}));
    return executeRemoteCallSingleValueReturn(function);
}
 
Example #21
Source File: BlindAuction.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> reveal(List<BigInteger> _values, List<Boolean> _fake, List<byte[]> _secret) {
    final Function function = new Function(
            FUNC_REVEAL, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.Uint256>(
                    org.web3j.abi.datatypes.generated.Uint256.class,
                    org.web3j.abi.Utils.typeMap(_values, org.web3j.abi.datatypes.generated.Uint256.class)), 
            new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.Bool>(
                    org.web3j.abi.datatypes.Bool.class,
                    org.web3j.abi.Utils.typeMap(_fake, org.web3j.abi.datatypes.Bool.class)), 
            new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.Bytes32>(
                    org.web3j.abi.datatypes.generated.Bytes32.class,
                    org.web3j.abi.Utils.typeMap(_secret, org.web3j.abi.datatypes.generated.Bytes32.class))), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #22
Source File: Contract.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected static <S extends Type, T> 
        List<T> convertToNative(List<S> arr) {
    List<T> out = new ArrayList<T>();
    for (Iterator<S> it = arr.iterator(); it.hasNext(); ) {
        out.add((T)it.next().getValue());
    }
    return out;
}
 
Example #23
Source File: HumanStandardToken.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, Credentials credentials, GasProvider contractGasProvider,
		BigInteger _initialAmount, String _tokenName, BigInteger _decimalUnits, String _tokenSymbol) {
	String encodedConstructor = FunctionEncoder.encodeConstructor(
			Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_initialAmount), new org.web3j.abi.datatypes.Utf8String(_tokenName),
					new org.web3j.abi.datatypes.generated.Uint8(_decimalUnits), new org.web3j.abi.datatypes.Utf8String(_tokenSymbol)));
	return deployRemoteCall(HumanStandardToken.class, web3j, credentials, contractGasProvider, BINARY, encodedConstructor);
}
 
Example #24
Source File: TransactionHandler.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private <T> T getContractData(String address, org.web3j.abi.datatypes.Function function) throws Exception
{
    String responseValue = callSmartContractFunction(function, address);

    List<Type> response = FunctionReturnDecoder.decode(
            responseValue, function.getOutputParameters());
    if (response.size() == 1)
    {
        return (T) response.get(0).getValue();
    }
    else
    {
        return null;
    }
}
 
Example #25
Source File: Example.java    From tutorials with MIT License 5 votes vote down vote up
public RemoteCall<TransactionReceipt> ExampleFunction() {
    final Function function = new Function(
            "ExampleFunction", 
            Arrays.<Type>asList(), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #26
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@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>() {}));

    assertEquals(
            FunctionReturnDecoder.decode(
                    "0x0000000000000000000000000000000000000000000000000000000000000080"
                            + "00000000000000000000000000000000000000000000000000000000000000c0"
                            + "0000000000000000000000000000000000000000000000000000000000000100"
                            + "0000000000000000000000000000000000000000000000000000000000000140"
                            + "0000000000000000000000000000000000000000000000000000000000000004"
                            + "6465663100000000000000000000000000000000000000000000000000000000"
                            + "0000000000000000000000000000000000000000000000000000000000000004"
                            + "6768693100000000000000000000000000000000000000000000000000000000"
                            + "0000000000000000000000000000000000000000000000000000000000000004"
                            + "6a6b6c3100000000000000000000000000000000000000000000000000000000"
                            + "0000000000000000000000000000000000000000000000000000000000000004"
                            + "6d6e6f3200000000000000000000000000000000000000000000000000000000",
                    function.getOutputParameters()),
            (Arrays.asList(
                    new Utf8String("def1"), new Utf8String("ghi1"),
                    new Utf8String("jkl1"), new Utf8String("mno2"))));
}
 
Example #27
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private Contract deployContract(TransactionReceipt transactionReceipt)
        throws Exception {

    prepareTransaction(transactionReceipt);

    String encodedConstructor = FunctionEncoder.encodeConstructor(
            Arrays.<Type>asList(new Uint256(BigInteger.TEN)));

    return TestContract.deployRemoteCall(
            TestContract.class, web3j, SampleKeys.CREDENTIALS,
            ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT,
            "0xcafed00d", encodedConstructor, BigInteger.ZERO).send();
}
 
Example #28
Source File: CrossContractReader.java    From besu with Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> deployRemote(final String crossAddress) {
  final Function function =
      new Function(
          FUNC_DEPLOYREMOTE,
          Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, crossAddress)),
          Collections.<TypeReference<?>>emptyList());
  return executeRemoteCallTransaction(function);
}
 
Example #29
Source File: Greeter.java    From web3j with 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 #30
Source File: ERC20.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> transferFrom(String _from, String _to, BigInteger _value) {
    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(_value)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}