Ethereum Service

For using features of ethereum like sending an ERC20 token, it should 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 mCoinNetworkInfo =
                       new CoinNetworkInfo(
                               CoinType.ETH,
                               EthereumNetworkType.MAINNET,
                               "{Node Url}" //ex. https://mainnet.infura.io/v3/xxxxx
                            );
    
    EthereumService etherService =
                            (EthereumService) CoinServiceFactory
                                    .getCoinService(
                                            getApplicationContext(),
                                            mCoinNetworkInfo
                                    );

For example, If you want to use a new EthereumService, a CoinNetworkInfo is required that assigns with CoinType.ETH. After creation, reassigning the 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 in the ERC20 token Payment Sheet.

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

  • createSafeNftPaymentSheetActivityIntent – Creates an Intent that can show 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.

        List<Account> accounts =
        mSblockchain.getAccountManager()
                .getAccounts(null, CoinType.ETH, EthereumNetworkType.ROPSTEN);    
        
        etherService.getBalance(accounts[0]).setCallback(
                new ListenableFutureTask.Callback<BigInteger>() {
                    @Override
                    public void onSuccess(BigInteger result) {
                        //success
                    }
                    @Override
                    public void onFailure(ExecutionException exception) {
                        //failure
                    }
                    @Override
                    public void onCancelled(@NotNull InterruptedException exception) {
                        //cancelled
                    }
                });
  • isValidAddress – Returns whether the address is valid or not.

        boolean result =
            CoinServiceFactory
                    .getCoinService(
                            mOwnerActivity.getApplicationContext(),
                            mCoinNetworkInfo
                    )
                    .isValidAddress("0x2bbb1386a853df31031db1f372921a436f8e72a8");
    
  • getFeeInfo - Get the fee information of the coin.

        etherService.getFeeInfo().setCallback(
                new ListenableFutureTask.Callback<FeeInfo>() {
                    @Override
                    public void onSuccess(FeeInfo 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 cause of critical issue or Ethereum’s own problem.

        BigInteger nonce = new BigInteger("6");
        BigInteger value = EthereumUtils.convertEthToWei(new BigDecimal("0.1"));
        BigInteger gasPrice = EthereumUtils.convertGweiToWei(new BigDecimal("10"));
        BigInteger gasLimit = new BigInteger("21000");
        RawTransaction rawTx =
                RawTransaction.createEtherTransaction(
                        nonce,
                        gasPrice,
                        gasLimit,
                        "0x136591d00349c210d2bea4eb861a0a811efc8b81",
                        value
                );
        byte[] encodedTransaction = TransactionEncoder.encode(rawTx);
        try {
            etherService.sendRawTransaction(
                    mHardwareWalletManager.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 detail data like block number, hash, value, nonce, and else.

        EthereumService etherService =
                (EthereumService) CoinServiceFactory
                        .getCoinService(
                                mOwnerActivity.getApplicationContext(),
                                mCoinNetworkInfo
                        );
        etherService.getTransactionDetail(
                "0xc7d2920aa7d7b6cd5943de7c7c9afb4596879bd4de3c11863bac1ede0b87f347")
                .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 the 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 cause of critical issue or Ethereum’s own problem.

        try {
            etherService
                    .sendTransaction(
                            mConnectedWallet,
                            ethAccount,
                            EthereumUtils.convertGweiToWei(new BigDecimal("10")),
                            new BigInteger("21000"),
                            "0x2a4f1841cb382b254c3f0e41f7b7f04890ec742a",
                            EthereumUtils.convertEthToWei(new BigDecimal("0.1")),
                            null
                    ).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 and 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 of the SDK’s Availability smart contract cause of critical issue or Ethereum’s own problem.

        try {
            etherService.sendTokenTransaction(
                    mConnectedWallet,
                    ethAccount,
                    "0x136591d00349c210D2beA4EB861a0A811efC8B81",
                    "0xFab46E002BbF0b4509813474841E0716E6730136",
                    new BigInteger("1000000000"),
                    new BigInteger("3000000"),
                    new BigInteger("12345678901234567890"),
                    null
            ).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 smart contract cause of critical issue or Ethereum’s own problem.

        try {
            etherService
                    .sendSmartContractTransaction(
                            hardwareWallet,
                            (EthereumAccount) account,
                            "0xFab46E002BbF0b4509813474841E0716E6730136",
                            EthereumUtils.convertGweiToWei(new BigDecimal("10")),
                            new BigInteger("500000"),
                            encodedFunction,
                            null,
                            null
                    )
                    .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 smart contract cause of critical issue or Ethereum’s own problem.

          try {
              etherService
                      .sendSafeNftTransaction(
                              hardwareWallet,
                              (EthereumAccount) account,
                              "0x948d5c45aed7649e831f44d05670babf872a0655",
                              "0xc8262fdec7fd685a9a4bda5e12765b5fe00a7e6c",
                              EthereumUtils.convertGweiToWei(new BigDecimal("10")),
                              new BigInteger("500000"),
                              BigInteger.valueOf(3),
                              null,
                              null
                      )
                      .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 smart contract cause of critical issue or Ethereum’s own problem.

          try {
              etherService
                      .sendNftTransaction(
                              hardwareWallet,
                              (EthereumAccount) account,
                              "0x3c433a1c5e1213b38f693fe5f8abd0e992a9a461",
                              "0xc8262fdec7fd685a9a4bda5e12765b5fe00a7e6c",
                              EthereumUtils.convertGweiToWei(new BigDecimal("10")),
                              new BigInteger("500000"),
                              BigInteger.valueOf(3),
                              null
                      )
                      .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(@NotNull ExecutionException exception) {
                      //failure
                  }
          
                  @Override
                  public void onCancelled(@NotNull InterruptedException exception) {
                      //cancelled
                  }
              });
          } catch (AvailabilityException e) {
              //handle exception
          }
    • getTokenInfo – Retrieves the ERC20 token information.

          // Ropsten FAU token
          String tokenAddress = "0xFab46E002BbF0b4509813474841E0716E6730136"; 
          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(
                  (EthereumAccount) accounts.get(0),
                  "0xFab46E002BbF0b4509813474841E0716E6730136",
                  BigInteger.ZERO,
                  "0xa9059cbb0000000000000000000000000dd75e755a64c3200525b2c67d09027debffca53000000000000000000000000000000000000000000000003af677facae24f000"
          ).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(
                  (EthereumAccount) accounts.get(0),
                  "0xFab46E002BbF0b4509813474841E0716E6730136")
                  .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,
                          "0xa1f7caca719cadb45ff553e1a77eef72ed69c063",
                          "0x18b0c3fd"
                  )
                  .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 : https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md )

          String checksumAddress = convertToChecksumAddress("0x7c688f41a4cD8DF0b20aFBF8Bdb5Ff3bB8fa5C98")