Skip to main content

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, getL2BalanceCoin and getL2ObjectsCount for the corresponding token types.

Example Code

  1. Get the AgentID from the sender by calling ISC.sandbox.getSenderAccount().
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
  1. To get the base token balance, you can call getL2BalanceBaseTokens using the agentID.
uint64 baseBalance = ISC.accounts.getL2BalanceBaseTokens(agentID);
  1. To get the number coins/Objects, use ISC.accounts.getL2ObjectsCount with the agentID.
uint256 object = ISC.accounts.getL2ObjectsCount(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 GotObjectIDs(uint256 objectBalance);

function getBalanceCoins() public {
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
uint64 baseBalance = ISC.accounts.getL2BalanceBaseTokens(agentID);
emit GotBaseBalance(baseBalance);
}

function getAgentID() public {
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
emit GotAgentID(agentID.data);
}
}