Display user inventories
Learn how to display a user's asset and game currencies in your application.
At the core of all Web3 games is the concept of true asset ownership. It is therefore critical for games and marketplaces to offer users a complete and up-to-date view of the assets they own. Immutable provides a variety of tools to help with this.
Game currencies
These are fungible tokens (ERC20) that provide a store of value and allow users to purchase other items. An example of this kind of token is $IMX.
To learn how to create these tokens, see our In-game currencies guide.
Use Checkout Wallet Widget (web-based applications)
The Checkout Wallet Widget is a drop-in solution for web-based games and marketplaces that simplifies the process of checking wallet balances on the Immutable zkEVM and Ethereum networks. Read the docs to learn how to integrate it.
Bring your own UI
For applications that wish to display their own custom UI for currency balances, we recommend directly querying on-chain state for the latest data. You'll need to periodically poll these balances to keep them up to date.
You will need to have an existing project, then do the following.
- Passport wallet (or EIP-1193 compatible)
- Non-Passport wallet
1. Set up Passport provider
Passport's zkEVM provider implements the Ethereum EIP-1193 standard, which means that you can use the same logic to interact with a user's Passport wallet as you would with any other Ethereum wallet.
As it implements the EIP-1193 standard, you can create a Passport provider and call a range of RPC methods on it directly. Alternatively, you can use the Passport provider in an Ethers.js provider.
- Passport (EIP-1193)
- Ethers.js
Create the Passport provider:
const passportProvider = passport.connectEvm();
Once a provider is created, you can call various methods on it via the request
function (which is protocol-agnostic), for example:
const accounts = await provider.request({ method: "eth_requestAccounts" });
See the full range of supported RPC methods.
Passport is also compatible with existing Ethereum libraries and products.
For example, you may want to use the Passport provider when creating a new provider with Ethers.js, which abstracts the complexity of interacting directly with an EIP-1193 provider:
import { ethers } from 'ethers';
const passportProvider = passport.connectEvm();
const provider = new ethers.providers.Web3Provider(passportProvider);
and then use the provider as you would any other Ethereum provider:
const signer = provider.getSigner();
const address = await signer.getAddress();
2. Call the requestAccounts
method directly:
const provider = passport.connectEvm();
const accounts = await provider.request({ method: "eth_requestAccounts" });
1. Install the ethers library:
npm install ethers
2. Set up a provider:
const ethers = require('ethers'); // ethers v5
const provider = ethers.getDefaultProvider('mainnet'); // Specifying 'mainnet' allows you to query the Ethereum blockchain. Change to other network if required.
3. Create an instance of the wallet that you want to query:
// Check if MetaMask is installed and available in the browser
if (typeof window.ethereum !== 'undefined') {
// Use the injected provider from MetaMask
const provider = new ethers.providers.Web3Provider(window.ethereum);
// Create a wallet instance using MetaMask's Ethereum account
const wallet = provider.getSigner();
// Now you can use the 'wallet' instance to interact with the Ethereum network.
} else {
console.log("MetaMask not detected. Please install MetaMask to interact with the Ethereum network.");
}
4. Now check token balances:
async function getTokenBalances() {
// Replace 'tokenContractAddresses' with an array of ERC20 token contract addresses you want to query
const tokenContractAddresses = ['TOKEN_1_ADDRESS', 'TOKEN_2_ADDRESS', '...'];
for (const tokenAddress of tokenContractAddresses) {
const tokenContract = new ethers.Contract(tokenAddress, TOKEN_ABI, provider);
const balance = await tokenContract.balanceOf(wallet.address);
console.log(`Token Balance for ${tokenAddress}: ${balance.toString()}`);
}
}
getTokenBalances();
Check out the ethers library documentation for more information on how to use it.
NFTs
Most Web3 game items are unique, non-fungible tokens (NFTs). Immutable's Blockchain Data APIs indexes blockchain activity so you can do the following:
On Immutable, blockchain assets can have metadata stored both on and off chain. See our Metadata docs to learn more about this.
Query for specific data
Check out our Blockchain Data guides for how to query different types of asset and collection data, including getting data on:
Poll for activity data
Use our Blockchain Data Activities endpoint to get data on all events in order to provide your users with the most up to date information when available.
We recommend polling at a frequency of no more than 3 seconds to avoid throttling.