Ethereum Service

To use features of ethereum like sending an ERC20 token, you need to use a call method through the EthereumService class that inherits the CoinService interface. The EthereumService class is available to get from the CoinServiceFactory.getCoinService().

Get EthereumService

CoinServiceFactory includes a static method that is the getCoinService to get the specific inherited CoinService.

CoinNetworkInfo coinNetworkInfo =
                   new CoinNetworkInfo(
                           CoinType.ETH,
                           EthereumNetworkType.MAINNET,
                           RPC_ENDPOINT  //ex. https://mainnet.infura.io/v3/xxxxx
                        );

EthereumService etherService =
                        (EthereumService) CoinServiceFactory
                                .getCoinService(
                                        getApplicationContext(),
                                        coinNetworkInfo
                                );

If you want to use a new EthereumService, a CoinNetworkInfo that assigns CoinType.ETH is required. After creation, reassigning of CoinNetworkInfo is not allowed. If you want to change the NetworkType or 'JSON-RPC URL', you should create a new instance through the CoinServiceFactory.getCoinService().

Simple Payment APIs

The APIs below will be treated at Simple Payment UI separately.

  • createEthereumPaymentSheetActivityIntent – Creates an Intent that shows up in the Ethereum Payment Sheet.

  • createTokenPaymentSheetActivityIntent – Creates an Intent that shows up in the ERC20 token Payment Sheet.

  • createNftPaymentSheetActivityIntent – Creates an Intent that shows up in the unsafe ERC721 token Payment Sheet.

  • createSafeNftPaymentSheetActivityIntent – Creates an Intent that shows up in the safe ERC721 token Payment Sheet.

Base CoinService APIs

EthereumCoinService class inherits the CoinService. Base APIs are implemented through the EthereumCoinService.

The list below are the APIs in CoinService.

  • getBalance – Get the balance of the input account with CoinNetworkInfo in the CoinService.
etherService.getBalance(account).setCallback(
        new ListenableFutureTask.Callback<BigInteger>() {
            @Override
            public void onSuccess(BigInteger result) {
                //success
            }
            @Override
            public void onFailure(ExecutionException exception) {
                //failure
            }
            @Override
            public void onCancelled(InterruptedException exception) {
                //cancelled
            }
        });
  • isValidAddress – Returns whether the address is valid or not.
boolean result = etherService.isValidAddress(ethAddress);
  • getFeeInfo - Get the fee information of the coin.
etherService.getFeeInfo().setCallback(
        new ListenableFutureTask.Callback<EthereumFeeInfo>() {
            @Override
            public void onSuccess(EthereumFeeInfo result) {
                //success
            }
            @Override
            public void onFailure(ExecutionException exception) {
                //failure
            }
            @Override
            public void onCancelled(InterruptedException exception) {
                //cancelled
            }
        });
  • sendRawTransaction - Sends a raw transaction. You can create the RawTransaction instance by using RawTransaction.createEtherTransaction. This API can be blocked with the AvailabilityException by SDK’s Availability smart contract in case of critical issue or Ethereum’s own problem.
RawTransaction rawTx =
        RawTransaction.createEtherTransaction(
                nonce,
                gasPrice,
                gasLimit,
                toAddress,
                value
        );
byte[] encodedTransaction = TransactionEncoder.encode(rawTx);
try {
    etherService.sendRawTransaction(
            hardwareWalletManager.getConnectedHardwareWallet(),
            account,
            encodedTransaction
    )
            .setCallback(new ListenableFutureTask.Callback<TransactionResult>() {
                @Override
                public void onSuccess(TransactionResult result) {
                    //success
                }
                @Override
                public void onFailure(ExecutionException exception) {
                    //failure
                }
                @Override
                public void onCancelled(InterruptedException exception) {
                    //cancelled
                }
            });
} catch (AvailabilityException e) {
    //handle exception
}

Ethereum specific APIs

  • getTransactionDetail – Gets the detailed information for a single Ethereum transaction by txId. It will return as an EthereumTransaction that contains the transaction data like block number, hash, value, nonce, and etc.
EthereumService etherService =
        (EthereumService) CoinServiceFactory
                .getCoinService(
                        context,
                        coinNetworkInfo
                );
etherService.getTransactionDetail(txId)
        .setCallback(
                new ListenableFutureTask.Callback<EthereumTransaction>() {
                    @Override
                    public void onSuccess(EthereumTransaction result) {
                        //handle success
                    }
                    @Override
                    public void onFailure(ExecutionException exception) {
                        //handle failure
                    }
                    @Override
                    public void onCancelled(InterruptedException exception) {
                        //handle cancel
                    }
                });
  • sendTransaction – Sends an Ethereum transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Ethereum’s own problem.
try {
    etherService
            .sendTransaction(
                    connectedWallet,
                    account,
                    EthereumUtils.convertGweiToWei(new BigDecimal(gasPrice)),
                    new BigInteger(gasLimit),
                    toAddress,
                    EthereumUtils.convertEthToWei(new BigDecimal(ethAmount)),
                    null  // nonce
            ).setCallback(
            new ListenableFutureTask.Callback<TransactionResult>() {
                @Override
                public void onSuccess(TransactionResult result) {
                    //success
                }
                @Override
                public void onFailure(ExecutionException exception) {
                    //fail
                }
                @Override
                public void onCancelled(InterruptedException exception) {
                    //cancelled
                }
            });
} catch (AvailabilityException e) {
    //handle exception
}
  • sendTokenTransaction – Sends an Ethereum Token (ERC20) transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Ethereum’s own problem.
try {
    etherService.sendTokenTransaction(
            connectedWallet,
            account,
            toAddress,
            toContractAddress,
            new BigInteger(gasPrice),
            new BigInteger(gasLimit),
            new BigInteger(tokenAmount),
            nonce
    ).setCallback(
            new ListenableFutureTask.Callback<TransactionResult>() {
                @Override
                public void onSuccess(TransactionResult result) {
                    //success
                }
                @Override
                public void onFailure(ExecutionException exception) {
                    //failure
                }
                @Override
                public void onCancelled(InterruptedException exception) {
                    //cancelled
                }
            });
} catch (AvailabilityException e) {
    //handle exception
}
  • sendSmartContractTransaction – Sends an Ethereum Smart Contract transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Ethereum’s own problem.
try {
    etherService
            .sendSmartContractTransaction(
                    hardwareWallet,
                    account,
                    toContractAddress,
                    EthereumUtils.convertGweiToWei(new BigDecimal(gasPrice)),
                    new BigInteger(gasLimit),
                    encodedFunction,
                    value,
                    null  // nonce
            )
            .setCallback(
                    new ListenableFutureTask.Callback<TransactionResult>() {
                        @Override
                        public void onSuccess(TransactionResult result) {
                            //success
                        }
                        @Override
                        public void onFailure(ExecutionException exception) {
                            //failure
                        }
                        @Override
                        public void onCancelled(InterruptedException exception) {
                            //cancelled
                        }
                    });
} catch (AvailabilityException e) {
    //handle exception
}
  • sendSafeNftTransaction - Sends an Ethereum Non-Fungible Token (ERC721) safe transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by SDK’s availability of the smart contract in case of critical issue or Ethereum’s own problem.
try {
    etherService
            .sendSafeNftTransaction(
                    hardwareWallet,
                    account,
                    toAddress,
                    toContractAddress,
                    EthereumUtils.convertGweiToWei(new BigDecimal(gasPrice)),
                    new BigInteger(gasLimit),
                    BigInteger.valueOf(NftId),
                    data,
                    null  // nonce
            )
            .setCallback(
                    new ListenableFutureTask.Callback<TransactionResult>() {
                        @Override
                        public void onSuccess(TransactionResult result) {
                            //success
                        }
                        @Override
                        public void onFailure(ExecutionException exception) {
                            //failure
                        }
                        @Override
                        public void onCancelled(InterruptedException exception) {
                            //cancelled
                        }
                    });
} catch (AvailabilityException e) {
    //handle exception
}
  • sendNftTransaction – Sends an Ethereum Non-Fungible Token (ERC721) unsafe transaction with parameters. It requires a connected HardwareWallet instance that can sign with the user’s private key. This API can be blocked with the AvailabilityException by the SDK’s availability of the smart contract in case of critical issue or Ethereum’s own problem.
try {
    etherService
            .sendNftTransaction(
                    hardwareWallet,
                    account,
                    toAddress,
                    toContractAddress,
                    EthereumUtils.convertGweiToWei(new BigDecimal(gasPrice)),
                    new BigInteger(gasLimit),
                    BigInteger.valueOf(nftId),
                    null  // nonce
            )
            .setCallback(
                new ListenableFutureTask.Callback<TransactionResult>() {
                    @Override
                    public void onSuccess(TransactionResult result) {
                        //success
                    }
                    @Override
                    public void onFailure(ExecutionException exception) {
                        //failure
                    }
                    @Override
                    public void onCancelled(InterruptedException exception) {
                        //cancelled
                    }
                });
} catch (AvailabilityException e) {
    //handle exception
}
  • signPersonalMessage - Signs the message for verifying the user from the DApp (Decentralized application).
try {
    etherService.signPersonalMessage(
            hardwareWallet,
            account,
            "message".getBytes()
    ).setCallback(new ListenableFutureTask.Callback<byte[]>() {
        @Override
        public void onSuccess(byte[] result) {
            //success
        }

        @Override
        public void onFailure(ExecutionException exception) {
            //failure
        }

        @Override
        public void onCancelled(InterruptedException exception) {
            //cancelled
        }
    });
} catch (AvailabilityException e) {
    //handle exception
}
  • getTokenInfo – Retrieves the ERC20 token information.
etherService.getTokenInfo(tokenAddress).setCallback(
        new ListenableFutureTask.Callback<EthereumTokenInfo>() {
            @Override
            public void onSuccess(EthereumTokenInfo result) {
                //success
            }
            @Override
            public void onFailure(ExecutionException exception) {
                //failure
            }
            @Override
            public void onCancelled(InterruptedException exception) {
                //cancelled
            }
        });
  • estimateGasLimit – Gets the gas limit for executing a transaction.
etherService.estimateGasLimit(
        account,
        toAddress,
        value,
        data
).setCallback(new ListenableFutureTask.Callback<BigInteger>() {
    @Override
    public void onSuccess(final BigInteger result) {
        //success
    }
    @Override
    public void onFailure(ExecutionException exception) {
        //failure
    }
    @Override
    public void onCancelled(InterruptedException exception) {
        //cancelled
    }
});
  • addTokenAddress – Adds the token address to input in the EthereumAccount.
etherService.addTokenAddress(
        account,
        tokenAddress)
        .setCallback(new ListenableFutureTask.Callback<EthereumAccount>() {
            @Override
            public void onSuccess(final EthereumAccount account) {
                //success
            }
            @Override
            public void onFailure(ExecutionException exception) {
                //failure
            }
            @Override
            public void onCancelled(InterruptedException exception) {
                //cancelled
            }
        });
  • callSmartContractFunction – Calls the smart contract function on the Ethereum network. The transaction is not broadcasted to the network.
etherService
        .callSmartContractFunction(
                account,
                toContractAddress,
                encodedFunction
        )
        .setCallback(new ListenableFutureTask.Callback<String>() {
            @Override
            public void onSuccess(String result) {
                //success
            }
            @Override
            public void onFailure(ExecutionException exception) {
                //failure
            }
            @Override
            public void onCancelled(InterruptedException exception) {
                //cancelled
            }
        });
  • convertToChecksumAddress – Gets the Checksum address encoding as per (ref. EIP-55)
String checksumAddress = convertToChecksumAddress(ethAddress)