org.web3j.abi.FunctionEncoder Java Examples

The following examples show how to use org.web3j.abi.FunctionEncoder. 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: HumanStandardTokenIT.java    From web3j with Apache License 2.0 6 votes vote down vote up
private String execute(Credentials credentials, Function function, String contractAddress)
        throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedFunction = FunctionEncoder.encode(function);

    RawTransaction rawTransaction =
            RawTransaction.createTransaction(
                    nonce, GAS_PRICE, GAS_LIMIT, contractAddress, encodedFunction);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse =
            web3j.ethSendRawTransaction(hexValue).sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example #3
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 #4
Source File: OnChainPrivacyGroupManagementProxy.java    From besu with Apache License 2.0 6 votes vote down vote up
public static RemoteCall<OnChainPrivacyGroupManagementProxy> deploy(
    Web3j web3j,
    Credentials credentials,
    ContractGasProvider contractGasProvider,
    String _implementation) {
  String encodedConstructor =
      FunctionEncoder.encodeConstructor(
          Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _implementation)));
  return deployRemoteCall(
      OnChainPrivacyGroupManagementProxy.class,
      web3j,
      credentials,
      contractGasProvider,
      BINARY,
      encodedConstructor);
}
 
Example #5
Source File: OnChainPrivacyGroupManagementProxy.java    From besu with Apache License 2.0 6 votes vote down vote up
public static RemoteCall<OnChainPrivacyGroupManagementProxy> deploy(
    Web3j web3j,
    TransactionManager transactionManager,
    ContractGasProvider contractGasProvider,
    String _implementation) {
  String encodedConstructor =
      FunctionEncoder.encodeConstructor(
          Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, _implementation)));
  return deployRemoteCall(
      OnChainPrivacyGroupManagementProxy.class,
      web3j,
      transactionManager,
      contractGasProvider,
      BINARY,
      encodedConstructor);
}
 
Example #6
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private String execute(
        Credentials credentials, Function function, String contractAddress) throws Exception {
    BigInteger nonce = getNonce(credentials.getAddress());

    String encodedFunction = FunctionEncoder.encode(function);

    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            contractAddress,
            encodedFunction);

    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);

    EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example #7
Source File: GreeterContractIT.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private String sendCreateContractTransaction() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());

    String encodedConstructor =
            FunctionEncoder.encodeConstructor(Collections.singletonList(new Utf8String(VALUE)));

    Transaction transaction = Transaction.createContractTransaction(
            ALICE.getAddress(),
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            BigInteger.ZERO,
            getGreeterSolidityBinary() + encodedConstructor);

    org.web3j.protocol.core.methods.response.EthSendTransaction
            transactionResponse = web3j.ethSendTransaction(transaction)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example #8
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private static MethodSpec buildDeployWithParams(
        MethodSpec.Builder methodBuilder, String className, String inputParams,
        String authName, boolean isPayable) {

    methodBuilder.addStatement("$T encodedConstructor = $T.encodeConstructor("
                    + "$T.<$T>asList($L)"
                    + ")",
            String.class, FunctionEncoder.class, Arrays.class, Type.class, inputParams);
    if (isPayable) {
        methodBuilder.addStatement(
                "return deployRemoteCall("
                        + "$L.class, $L, $L, $L, $L, $L, encodedConstructor, $L)",
                className, WEB3J, authName, GAS_PRICE, GAS_LIMIT, BINARY, INITIAL_VALUE);
    } else {
        methodBuilder.addStatement(
                "return deployRemoteCall($L.class, $L, $L, $L, $L, $L, encodedConstructor)",
                className, WEB3J, authName, GAS_PRICE, GAS_LIMIT, BINARY);
    }

    return methodBuilder.build();
}
 
Example #9
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private String callSmartContractFunction(
        Function function, String contractAddress, NetworkInfo network, Wallet wallet) throws Exception
{
    try
    {
        String encodedFunction = FunctionEncoder.encode(function);

        org.web3j.protocol.core.methods.request.Transaction transaction
                = createEthCallTransaction(wallet.address, contractAddress, encodedFunction);
        EthCall response = getService(network.chainId).ethCall(transaction, DefaultBlockParameterName.LATEST).send();

        return response.getValue();
    }
    catch (InterruptedIOException|UnknownHostException e)
    {
        //expected to happen when user switches wallets
        return "0x";
    }
}
 
Example #10
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
/**
 * Call smart contract function on custom network contract. This would be used for things like ENS lookup
 * Currently because it's tied to a mainnet contract address there's no circumstance it would work
 * outside of mainnet. Users may be confused if their namespace doesn't work, even if they're currently
 * using testnet.
 *
 * @param function
 * @param contractAddress
 * @param wallet
 * @return
 */
private String callCustomNetSmartContractFunction(
        Function function, String contractAddress, Wallet wallet, int chainId)  {
    String encodedFunction = FunctionEncoder.encode(function);

    try
    {
        org.web3j.protocol.core.methods.request.Transaction transaction
                = createEthCallTransaction(wallet.address, contractAddress, encodedFunction);
        EthCall response = getService(chainId).ethCall(transaction, DefaultBlockParameterName.LATEST).send();

        return response.getValue();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return null;
    }
}
 
Example #11
Source File: TokenRepository.java    From alpha-wallet-android with 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 #12
Source File: EnsResolver.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private String callSmartContractFunction(
        Function function, String contractAddress, int chainId) throws Exception
{
    try
    {
        String encodedFunction = FunctionEncoder.encode(function);

        org.web3j.protocol.core.methods.request.Transaction transaction
                = createEthCallTransaction(TokenscriptFunction.ZERO_ADDRESS, contractAddress, encodedFunction);
        EthCall response = TokenRepository.getWeb3jService(chainId).ethCall(transaction, DefaultBlockParameterName.LATEST).send();

        return response.getValue();
    }
    catch (InterruptedIOException | UnknownHostException e)
    {
        //expected to happen when user switches wallets
        return "0x";
    }
}
 
Example #13
Source File: HumanStandardToken.java    From web3j-quorum with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, 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, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
}
 
Example #14
Source File: BlindAuction.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<BlindAuction> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger _biddingTime, BigInteger _revealTime, String _beneficiary) {
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_biddingTime),
            new org.web3j.abi.datatypes.generated.Uint256(_revealTime), 
            new org.web3j.abi.datatypes.Address(_beneficiary)));
    return deployRemoteCall(BlindAuction.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
}
 
Example #15
Source File: Ballot.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<Ballot> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, List<byte[]> proposalNames) {
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.Bytes32>(
                    org.web3j.abi.datatypes.generated.Bytes32.class,
                    org.web3j.abi.Utils.typeMap(proposalNames, org.web3j.abi.datatypes.generated.Bytes32.class))));
    return deployRemoteCall(Ballot.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
}
 
Example #16
Source File: HumanStandardTokenIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
private String callSmartContractFunction(Function function, String contractAddress)
        throws Exception {
    String encodedFunction = FunctionEncoder.encode(function);

    org.web3j.protocol.core.methods.response.EthCall response =
            web3j.ethCall(
                            Transaction.createEthCallTransaction(
                                    ALICE.getAddress(), contractAddress, encodedFunction),
                            DefaultBlockParameterName.LATEST)
                    .sendAsync()
                    .get();

    return response.getValue();
}
 
Example #17
Source File: EthCallIT.java    From web3j with 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 #18
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, TransactionManager transactionManager, 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, transactionManager, contractGasProvider, BINARY, encodedConstructor);
}
 
Example #19
Source File: OnChainPrivacyTransactionBuilder.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static String getEncodedAddToGroupFunction(
        Base64String enclaveKey, List<byte[]> participants) {
    final Function function =
            new Function(
                    "addParticipants",
                    Arrays.asList(
                            new Bytes32(enclaveKey.raw()),
                            new DynamicArray<>(
                                    Bytes32.class, Utils.typeMap(participants, Bytes32.class))),
                    Collections.emptyList());
    return FunctionEncoder.encode(function);
}
 
Example #20
Source File: TokenERC20.java    From Android-Wallet-Token-ERC20 with Apache License 2.0 5 votes vote down vote up
public static RemoteCall<TokenERC20> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialSupply, String tokenName, String tokenSymbol, BigInteger tokendecimals) {
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new Uint256(initialSupply),
            new Utf8String(tokenName),
            new Utf8String(tokenSymbol),
            new Uint8(tokendecimals)));
    return deployRemoteCall(TokenERC20.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor);
}
 
Example #21
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 #22
Source File: TransactionHandler.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String callSmartContractFunction(
        Function function, String contractAddress) throws Exception {
    String encodedFunction = FunctionEncoder.encode(function);
    return makeEthCall(
            org.web3j.protocol.core.methods.request.Transaction
                    .createEthCallTransaction(null, contractAddress, encodedFunction));
}
 
Example #23
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 #24
Source File: Ballot.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<Ballot> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, List<byte[]> proposalNames) {
    String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.DynamicArray<org.web3j.abi.datatypes.generated.Bytes32>(
                    org.web3j.abi.datatypes.generated.Bytes32.class,
                    org.web3j.abi.Utils.typeMap(proposalNames, org.web3j.abi.datatypes.generated.Bytes32.class))));
    return deployRemoteCall(Ballot.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor);
}
 
Example #25
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
public static byte[] createTokenTransferData(String to, BigInteger tokenAmount) {
    List<Type> params = Arrays.asList(new Address(to), new Uint256(tokenAmount));
    List<TypeReference<?>> returnTypes = Collections.singletonList(new TypeReference<Bool>() {});
    Function function = new Function("transfer", params, returnTypes);
    String encodedFunction = FunctionEncoder.encode(function);
    return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction));
}
 
Example #26
Source File: CallSmartContractFunction.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public EthSendTransaction execute(final NodeRequests node) {
  final Function function =
      new Function(functionName, Collections.emptyList(), Collections.emptyList());
  final RawTransactionManager transactionManager =
      new RawTransactionManager(node.eth(), BENEFACTOR_ONE);
  try {
    return transactionManager.sendTransaction(
        GAS_PRICE, GAS_LIMIT, contractAddress, FunctionEncoder.encode(function), BigInteger.ZERO);
  } catch (IOException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #27
Source File: TokenRepository.java    From Upchain-wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
private String callSmartContractFunction(
        org.web3j.abi.datatypes.Function function, String contractAddress, String walletAddress) throws Exception {
    String encodedFunction = FunctionEncoder.encode(function);

    org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
            Transaction.createEthCallTransaction(walletAddress, contractAddress, encodedFunction),
            DefaultBlockParameterName.LATEST)
            .sendAsync().get();

    return response.getValue();
}
 
Example #28
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 #29
Source File: Contract.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #30
Source File: TokenRepository.java    From trust-wallet-android-source with GNU General Public License v3.0 5 votes vote down vote up
private String callSmartContractFunction(
        org.web3j.abi.datatypes.Function function, String contractAddress, Wallet wallet) throws Exception {
    String encodedFunction = FunctionEncoder.encode(function);

    org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
            Transaction.createEthCallTransaction(wallet.address, contractAddress, encodedFunction),
            DefaultBlockParameterName.LATEST)
            .sendAsync().get();

    return response.getValue();
}