Create a multichain wallet that displays realtime balances, transactions, and NFTs using Sim APIs and Express.js
The final UI we'll build together in this guide
Create Your Project Structure
wallet-ui
directory.
Next, initialize a new Node.js project with npm:package.json
file with default values and configure it to use ES modules.
Afterward, install the required packages:.env
file.Configure Environment Variables
.env
file in your project root:.env
file in your code editor and add your Sim API key:.env
file to version control. If you are using Git, add .env
to your .gitignore
file.Add Starter Code
views
will hold our EJS templates, and public
will serve static assets like CSS.
Now, create the core files:server.js
with this basic Express server code:views/wallet.ejs
:public/styles.css
:Verify Project Structure
ls
in your terminal. Your project directory wallet-ui/
should now contain:node server.js
in the terminal to start the server.
Visit http://localhost:3001
to see the blank wallet.
Our scaffolded wallet UI without any data.
.env
file contains the correct SIM_API_KEY
and that it is loaded correctly.
Also, verify the walletAddress
in the URL is a valid EVM wallet address.
Check your terminal for any error messages from server.js
.server.js
to fetch these balances. Add this function before your app.get('/')
route handler:
fetch
.
It includes your SIM_API_KEY
in the headers and sends a GET request to the /v1/evm/balances/{address}
endpoint.
The Balances API gives you access to various URL query parameters that you can include to modify the response.
We have included metadata=url,logo
to include a token’s URL and logo.
Next, modify your app.get('/')
route handler in server.js
to call getWalletBalances
and pass the fetched tokens to your template:
getWalletBalances
if a walletAddress
is provided.balances
to the wallet.ejs
template.views/wallet.ejs
file you created earlier is already set up to display these tokens.
Restart your server with node server.js
and refresh your browser, providing a walletAddress
in the URL.
For example: http://localhost:3001/?walletAddress=0xd8da6bf26964af9d7eed9e03e53415d37aa96045
You should now see the wallet populated with token balances, logos, prices for each token, and how much of that token the wallet holds.
Wallet displaying token balances (in wei) with logos and prices.
1.23
ETH instead of 1230000000000000000
wei, we need to adjust the amount using the token’s decimals
property, which is also returned from the Balances API.
We can add a new property, balanceFormatted
, to each token object.
Modify your getWalletBalances
function in server.js
as follows. The main change is mapping over data.balances
to add the balanceFormatted
property:
getWalletBalances
will include a balanceFormatted
string, which our EJS template (views/wallet.ejs
) already uses: <%= token.balanceFormatted || token.amount %>
.
Restart the server and refresh the browser. You will now see formatted balances.
Wallet displaying properly formatted token balances with logos.
$0.00
.
Let’s calculate the total USD value of the wallet and properly show it.
The Balances API provides a value_usd
field with each token.
This field represents the total U.S. dollar value of the wallet’s entire holding for that specific token.
Let’s modify the app.get('/')
route handler to iterate through the fetched tokens and sum their individual value_usd
to calculate the totalWalletUSDValue
.
reduce
method to iterate over the tokens
array.
For each token
, we access its value_usd
property, parse it as a float, and add it to the running sum
.
The calculated totalWalletUSDValue
is then formatted to two decimal places and passed to the template.
The views/wallet.ejs
template already has <p class="total-balance-amount"><%= totalWalletUSDValue %></p>
, so it will display the calculated total correctly.
Restart your server and refresh the browser page with a wallet address.
You should now see the total wallet value at the top of the UI accurately reflecting the sum of all token balance USD values.
Wallet showing the correctly calculated total USD value.
Balances API
endpoint to display realtime token balances and total portfolio value.
In the next guide, Add Account Activity, we will enhance this wallet by adding a transaction history feature in the UI using the Activity API.