Create an Ethereum Wallet in iOS — Part 2

Calvin Chang
Coinmonks
Published in
4 min readFeb 26, 2022

--

It’s been delayed for a while about this Part 2, I have been busy in the last past 6 months, so let’s go straight to it!

For the short recap, Part 1 includes the following topics:

Accounts

Load existing account

1. Entering a hex-string of the account private key

2. Entering a 12-word mnemonic phrase

Generate, delete and export the account

In Part 2, we will talk about the following topics:

Balances

Displays ETH, DAI, and USDC balance

You can check part 1 for more information about Account operations, this article will focus on displaying the balance in Ethereum

Then, let’s start diving into the topics!

Display ETH balances

As we know, ETH is the main token in Ethereum and other ERC-20 tokens like DAI and USDC. Both of them exist on Ethereum, you can think that they are a branch of ETH.

For more information about the ERC-20 token, you can check this article

In short, ERC-20 tokens are similar, in some respects, to bitcoin, Litecoin, and any other cryptocurrency; ERC-20 tokens are blockchain-based assets that have value and can be sent and received. The primary difference is that instead of running on their blockchain, ERC-20 tokens are issued on the Ethereum network.

We know that the whole blockchain is like a huge and immutable ledger. If we want to query the balance, first of all, we need to know which page we want to check. We don’t have a page number or bookmark, but we have a wallet address. According to the address, we can search on the ledger and find the ETH balance.

It’s easy to do it with the Web3 Library, it looks like:

func getETHBalance(wallet: Web3Wallet) throws -> String? {  guard let walletAddress = EthereumAddress(wallet.address) else {
throw Web3ServiceError.noAddress
}
web3.addKeystoreManager(try fetchKeyStoreManager(wallet: wallet)) let balance = try web3.eth.getBalance(address: walletAddress) return Web3.Utils.formatToEthereumUnits(
balance,
toUnits: .eth,
decimals: 3
)
}
  • First, the input is the wallet which we would like to query the balance.
  • Generate the Ethereum address
  • Fetch the Keystore which contain the wallet address and add it to the web3 service
  • Web3 service is bonded to the provider, so we can use it to query the balance
  • Format the balance to the ETH unit

Display DAI and USDC balance

As I mentioned before, the DAI, USDC, or other ERC-20 tokens are issued on the Ethereum blockchain and follow the ERC-20 technical standard and can be executed by the smart contract.

About the smart contract, you can check this article for more information.

Maybe it’s not a super nice example, but you can think like this. The huge ledger named Ethereum has the section called USDC and DAI.

The smart contract is the guide to guide you to do some action.

First of all, we first need to find the section to find the balance.

func getERC20Balance(
wallet: Web3Wallet,
token: ERC20Token
) throws -> String? {
guard let walletAddress = EthereumAddress(wallet.address),
let erc20ContractAddress = EthereumAddress(token.address, ignoreChecksum: true) else {
throw Web3ServiceError.noAddress
}
guard let contract = web3.contract(Web3.Utils.erc20ABI, at: erc20ContractAddress, abiVersion: 2) else {
throw Web3ServiceError.noContract
}
web3.addKeystoreManager(try fetchKeyStoreManager(wallet: wallet)) var options = TransactionOptions.defaultOptions options.from = walletAddress options.gasPrice = .automatic options.gasLimit = .automatic let tx = contract.read(
TxMethod.balanceOf.rawValue,
parameters: [walletAddress] as [AnyObject],
extraData: Data(),
transactionOptions: options
)
let tokenBalance = try tx?.call() guard let balanceBigUInt = tokenBalance?[“0”] as? BigUInt else {
throw Web3ServiceError.noBalance
}
return Web3.Utils.formatToEthereumUnits(
balanceBigUInt,
toUnits: .eth,
decimals: 3
)
}
  • The inputs, which is the wallet which we would like to query the balance and which token we want to query
  • Generate the Ethereum address for the wallet
  • Generate the Ethereum address for the token, we will use it to find the smart contract
  • Fetch the smart contract
  • Fetch the Keystore which contain the wallet address and add it to the web3 service
  • Read the contract to get the balance from the wallet
  • Format it to eth unit

Let’s it, if you would like to display other ERC-20 tokens, you can also use a similar approach.

This article may still have some incorrect place, welcome to correct or discuss with me!

If you think this article is helpful, don’t forget to give me some claps.

Happy coding, enjoy the block-chain technology 🙂

If you would like to reach out, feel free to contact me through
Twitter or Linkedin

Cheers 🍻

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--

Calvin Chang
Coinmonks

The developer, the alcohol lover, the forever learner and the crazy explorer.