# Smart Contract API SnippetJS can call external smart contracts written in Solidity by using special built-in functions. Like a normal function call, you can pass JS variables directly to smart contract methods as arguments, and assign return values to JS variables for further processing. ## staticcallContract ``` staticcallContract(contractAddress: string, methodSignature: sting, returnType: object, ...args) ``` Call smart contract methods using Solidity's staticcall opcode. Only view or pure functions can be called. ### Parameters | Name | Type | Description | Example | | -------- | -------- | -------- | -------- | | contractAddress | string | the address of the smart contract to call | '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4' | | methodSignature | string | the signature of the method to call | 'ownerOf(uint256)' | | returnType | object | Specify the return value type of the method in [JSON ABI format](https://docs.soliditylang.org/en/v0.8.11/abi-spec.html#handling-tuple-types). The return value of the staticcallContract function will be a value mapped to the JavaScript type corresponding to the type specified here. Specify undefined if there is no return value. | { type: 'string' }| | ...args | any | Arbitrary number of arguments to pass to the method. Pass in the order of the parameters of the method specified in methodSignature. | staticcallContract('0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(uint256,string,bool)', undefined, 1, 'str', true) ### Supported argument types for methodSignature * uint* / uint*[] uint8 ... uint256 types can be specified. Use 'uint256' instead of 'uint'. Multidimensional arrays are also supported. ```javascript // The example of passing an uint value, 1 staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(uint256)', undefined, 1 ) // The example of passing an uint8[] value, [1, 2, 3] staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(uint8[])', undefined, [1, 2, 3] ) ``` * string / string[] Multidimensional arrays are also supported. ```javascript // The example of passing a string value, 'str' staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(string)', undefined, 'str' ) // The example of passing a string[][] value, [['abc', 'def'], ['ghi', 'jkl']] staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(string[][])', undefined, [['abc', 'def'], ['ghi', 'jkl']] ) ``` * bool / bool[] Multidimensional arrays are also supported. ```javascript // The example of passing a bool value, true staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(bool)', undefined, true ) // The example of passing a bool[] value, [true, false] staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(bool[])', undefined, [true, false] ) ``` * address / address[] Multidimensional arrays are also supported. Note that the address value is passed as a hexadecimal number, not as a string. ```javascript // The example of passing an address value, '0x9175bbea09F865CF034f6430bA4B80c9dDcCc853' staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(address)', undefined, 0x9175bbea09F865CF034f6430bA4B80c9dDcCc853 // pass it as a hexadecimal number ) // The example of passing a bool[] value, ['0x9175bbea09F865CF034f6430bA4B80c9dDcCc853', '0xeb4F5152cE54a9cb0A271DC537Bc7954e4d0dd4b'] staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(bool[])', undefined, [0x9175bbea09F865CF034f6430bA4B80c9dDcCc853, 0xeb4F5152cE54a9cb0A271DC537Bc7954e4d0dd4b] ) ``` * struct / struct[] According to Solidity ABI specification, structs should be mapped to tuple. ```javascript /* The contract in this example receives a struct type defined as the below. * * struct ArgValue { * uint256 numValue; * string strValue; * bool blValue; * } */ staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method((uint256,string,bool))', undefined, { numValue: 123, strValue: 'abc', blValue: true } ) // The example of passing an array of ArgValue staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method((uint256,string,bool)[])', undefined, [ { numValue: 123, strValue: 'abc', blValue: true }, { numValue: 456, strValue: 'def', blValue: false } ] ) ``` * Multiple arguments You can combine the values of the types listed above to call methods with multiple parameters. ```javascript // The example of passing multiple arguments staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method(uint256,string,bool,address,(uint256[],string[]))', undefined, 123, 'abc', true, 0x9175bbea09F865CF034f6430bA4B80c9dDcCc853, { numValues: [456, 789], strValues: ['def', 'ghi'] } ) ``` ### Supported return value types * uint256 / uint256[] A uint type other than 256bit cannot be specified. Use uint256 type instead of uint type other than 256bit. ```javascript // The example of returning a uint256 value var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { type: 'uint256' } ) typeof res; // (='number') res is a value of type number // The example of returning an uint256[] value var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { type: 'uint256[]' } ) res[0]; // (= 123) res is a value of type number array ``` * string Array of string is not supported. ```javascript // The example of returning a string value var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { type: 'string' } ) typeof res; // (='string') res is a value of type string ``` * bool ```javascript // The example of returning a bool value var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { type: 'bool' } ) typeof res; // (='bool') res is a value of type bool // The example of returning an bool[] value var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { type: 'bool[]' } ) res[0]; // (= true) res is a value of type bool array ``` * address Array of string is not supported. ```javascript // The example of returning a address value var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { type: 'address' } ) typeof res; // (='string') res is a value of type string ``` * struct / struct[] According to Solidity ABI specification, structs should be mapped to tuple. The return value of the staticcallContract function will be an Object with properties corresponding to the structure of the tuple specified by returnType. ```javascript /* The contract in this example retuns a struct type defined as the below. * * struct ReturnValue { * uint256 numValue; * string strValue; * bool blValue; * } */ var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { "components": [ { "name": "numValue", "type": "uint256" }, { "name": "strValue", "type": "string" }, { "name": "blValue", "type": "bool" } ], "type": "tuple" } ) // the value of name property in tuple will be mapped to the key value of the object. typeof res.numValue; // = 'number' typeof res.strValue; // = 'string' typeof res.blValue; // = 'bool' // The example of passing an array of ReturnValue. // Specify "tuple[]" type instead of "tuple". var res = staticcallContract( '0x18934649EAe953bEe53d85Bb9d650072b6FB47C4', 'method()', { "components": [ { "name": "numValue", "type": "uint256" }, { "name": "strValue", "type": "string" }, { "name": "blValue", "type": "bool" } ], "type": "tuple[]" } ) res[0]; // { numValue: 123, strValue: 'abc', blValue: true } ```