Skip to main content

Interact with any Core contract

About call and callView

The magic contract provides you with a solidity interface to the core contracts. Some functions like [getL2BalanceBaseTokens] are wrapped in the magic contract directly, others you need to call yourself. You can do that with the [call] and [callView] functions.

Example Code

  1. Get the AgentID from the sender by calling ISC.sandbox.getSenderAccount().
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
  1. Initialize the parameters for the call by creating a new [ISCDict]. As you can see in the docs, [getl2balancenativetokens] takes two parameters.: the Agent ID and the native token ID. So, you have to create a dictionary with two key-value pairs. The key of the first pair (Agent ID) has to be a and the key for the second pair (native token ID) N.
ISCDict memory params = ISCDict(new ISCDictItem[](2));
params.items[0] = ISCDictItem("a", agentID.data);
params.items[1] = ISCDictItem("N", nativeTokenID);
  1. Now, you can use [callView] to call our contract. The first parameter is the core contract hname, which we can get with the helper utility [hn], and the second parameter is the function we want to call. The last parameter is the dictionary with all function parameters.
ISCDict memory result = ISC.sandbox.callView(
ISC.util.hn("accounts"),
ISC.util.hn("balanceNativeToken"),
params
);
  1. Next, you can either return or emit the result.
emit NativeTokenBalance(bytesToUint(result.items[0].value));
Return Dictionary

Keep in mind that the call and callView functions will always return a dictionary. The values of this dictionary are always of type byte, so you need to take care of converting it yourself.

Full Example Code

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@iota/iscmagic/ISC.sol";

contract NativeTokenBalance {
event NativeTokenBalance(uint balance);

function getNativeTokenBalance(bytes memory nativeTokenID) public {
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();

ISCDict memory params = ISCDict(new ISCDictItem[](2));
params.items[0] = ISCDictItem("a", agentID.data);
params.items[1] = ISCDictItem("N", nativeTokenID);

ISCDict memory result = ISC.sandbox.callView(
ISC.util.hn("accounts"),
ISC.util.hn("balanceNativeToken"),
params
);

emit NativeTokenBalance(bytesToUint(result.items[0].value));
}

function bytesToUint(bytes memory b) internal pure virtual returns (uint256) {
require(b.length <= 32, "Bytes length exceeds 32.");
return abi.decode(abi.encodePacked(new bytes(32 - b.length), b), (uint256));
}
}