Java Code Examples for org.web3j.abi.FunctionEncoder#encode()

The following examples show how to use org.web3j.abi.FunctionEncoder#encode() . 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: 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 3
Source File: TransactionManager.java    From BitcoinWallet with MIT License 6 votes vote down vote up
public String signContractTransaction(String contractAddress,
                                      String to,
                                      BigInteger nonce,
                                      BigInteger gasPrice,
                                      BigInteger gasLimit,
                                      BigDecimal amount,
                                      BigDecimal decimal,
                                      WalletFile walletfile,
                                      String password) throws Exception {
    BigDecimal realValue = amount.multiply(decimal);
    Function function = new Function("transfer",
            Arrays.asList(new Address(to), new Uint256(realValue.toBigInteger())),
            Collections.emptyList());
    String data = FunctionEncoder.encode(function);
    RawTransaction rawTransaction = RawTransaction.createTransaction(
            nonce,
            gasPrice,
            gasLimit,
            contractAddress,
            data);
    return signData(rawTransaction, walletfile, password);
}
 
Example 4
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 5
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 6
Source File: GreeterContractIT.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 7
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 8
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 9
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 10
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 11
Source File: DeployContractIT.java    From etherscan-explorer with GNU General Public License v3.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 12
Source File: HumanStandardTokenIT.java    From etherscan-explorer with GNU General Public License v3.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 13
Source File: EthereumUtils.java    From jbpm-work-items with Apache License 2.0 5 votes vote down vote up
public static String getEncodedFunction(String queryName,
                                        List<Type> queryInputTypes,
                                        List<TypeReference<?>> queryOutputTypes) {

    return FunctionEncoder.encode(getFunction(queryName,
                                              queryInputTypes,
                                              queryOutputTypes));
}
 
Example 14
Source File: Contract.java    From web3j with Apache License 2.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);

    String value = call(contractAddress, encodedFunction, defaultBlockParameter);

    return FunctionReturnDecoder.decode(value, function.getOutputParameters());
}
 
Example 15
Source File: DeployContractIT.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 16
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 17
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
public static byte[] createTicketTransferData(String to, List<BigInteger> tokenIndices, Token token) {
    Function function = token.getTransferFunction(to, tokenIndices);

    String encodedFunction = FunctionEncoder.encode(function);
    return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction));
}
 
Example 18
Source File: EventFilterIT.java    From web3j with Apache License 2.0 4 votes vote down vote up
@Test
public void testEventFilter() throws Exception {
    unlockAccount();

    Function function = createFibonacciFunction();
    String encodedFunction = FunctionEncoder.encode(function);

    BigInteger gas = estimateGas(encodedFunction);
    String transactionHash = sendTransaction(ALICE, CONTRACT_ADDRESS, gas, encodedFunction);

    TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash);

    assertFalse(gas.equals(transactionReceipt.getGasUsed()));

    List<Log> logs = transactionReceipt.getLogs();
    assertFalse(logs.isEmpty());

    Log log = logs.get(0);

    List<String> topics = log.getTopics();
    assertEquals(topics.size(), (1));

    Event event =
            new Event(
                    "Notify",
                    Arrays.asList(
                            new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));

    // check function signature - we only have a single topic our event signature,
    // there are no indexed parameters in this example
    String encodedEventSignature = EventEncoder.encode(event);
    assertEquals(topics.get(0), (encodedEventSignature));

    // verify our two event parameters
    List<Type> results =
            FunctionReturnDecoder.decode(log.getData(), event.getNonIndexedParameters());
    assertEquals(
            results,
            (Arrays.asList(
                    new Uint256(BigInteger.valueOf(7)), new Uint256(BigInteger.valueOf(13)))));

    // finally check it shows up in the event filter
    List<EthLog.LogResult> filterLogs =
            createFilterForEvent(encodedEventSignature, CONTRACT_ADDRESS);
    assertFalse(filterLogs.isEmpty());
}
 
Example 19
Source File: TokenRepository.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
public static byte[] createTrade(Token token, BigInteger expiry, List<BigInteger> ticketIndices, int v, byte[] r, byte[] s)
{
    Function function = token.getTradeFunction(expiry, ticketIndices, v, r, s);
    String encodedFunction = FunctionEncoder.encode(function);
    return Numeric.hexStringToByteArray(Numeric.cleanHexPrefix(encodedFunction));
}
 
Example 20
Source File: RemoteFunctionCall.java    From web3j with Apache License 2.0 2 votes vote down vote up
/**
 * return an encoded function, so it can be manually signed and transmitted
 *
 * @return the function call, encoded.
 */
public String encodeFunctionCall() {
    return FunctionEncoder.encode(function);
}