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
- Get the AgentIDfrom the sender by calling ISC.sandbox.getSenderAccount().
ISCAgentID memory agentID = ISC.sandbox.getSenderAccount();
- 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 beaand 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);
- Now, you can use [callView] to call our contract. The first parameter is the core contracthname, 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("getL2BalanceBaseTokens"),
    params
);
- 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("getL2BalanceBaseTokens"),
            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));
    }
}