Get Balance
Once you have your L1 assets on L2, you might want to check their balance. This guide explains how to do so by calling the three functions getL2BalanceBaseTokens, getL2BalanceNativeTokensand getL2NFTAmount for the corresponding token types.
Example Code
- Get the AgentID from the sender by calling
ISC.sandbox.getSenderAccount().
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
- To get the base token balance, you can call
getL2BalanceBaseTokensusing theagentID.
uint64 baseBalance = ISC.accounts.getL2BalanceBaseTokens(agentID);
- To get the native token balance of a specific
NativeTokenID, useISC.accounts.getL2BalanceNativeTokenswith theidandagentID.
NativeTokenID memory id = NativeTokenID({ data: nativeTokenID});
uint256 nativeTokens = ISC.accounts.getL2BalanceNativeTokens(id, agentID);
- To get the number of NFTs, use
ISC.accounts.getL2NFTAmountwith theagentID.
uint256 nfts = ISC.accounts.getL2NFTAmount(agentID);
Full Example Code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@iota/iscmagic/ISC.sol";
contract GetBalance {
event GotAgentID(bytes agentID);
event GotBaseBalance(uint64 baseBalance);
event GotNativeTokenBalance(uint256 nativeTokenBalance);
event GotNFTIDs(uint256 nftBalance);
function getBalance(bytes memory nativeTokenID) public {
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
emit GotAgentID(agentID.data);
uint64 baseBalance = ISC.accounts.getL2BalanceBaseTokens(agentID);
emit GotBaseBalance(baseBalance);
NativeTokenID memory id = NativeTokenID({ data: nativeTokenID});
uint256 nativeTokens = ISC.accounts.getL2BalanceNativeTokens(id, agentID);
emit GotNativeTokenBalance(nativeTokens);
uint256 nfts = ISC.accounts.getL2NFTAmount(agentID);
emit GotNativeTokenBalance(nfts);
}
}