资源加载中... loading...

Method of Calling the Smart Contract

The following content is an example of some smart contract method calls.

  • decimals The decimals method is a constant method of ERC20 that does not generate gas consumption, and it can query the precision data of a token. The decimals method has no parameters. Return value: the precision data of token.

    function main(){
        var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302"    // The contract address of the token, in the example the token is 1INCH
        Log(exchange.IO("api", tokenAddress, "decimals"))                  // Query, print 1INCH tokens with precision index of 18
    }
    
  • allowance The allowance method is a constant method of ERC20 that does not generate gas consumption, and it can query the authorized amount of a certain token for a certain contract address. The allowance method need to pass in 2 parameters, the first one is the wallet address, and the second one is the authorized address. Return value: the authorization amount of token.

    function main(){
        // The contract address of the token, in the example the token is 1INCH
        var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302"            
        var owner = ""
        var spender = ""
        
        // For example, the query yields 1000000000000000000, divided by the precision unit of the token 1e18, the current exchange object bound to the wallet to the spender address authorized 1 1INCH.
        Log(exchange.IO("api", tokenAddress, "allowance", owner, spender))
    }
    

    owner: The wallet address is replaced by the string “owner” in the example. In actual use, you need to fill in the address. spender: The authorized contract address is replaced by the string “spender” in the example. In actual use, you need to fill in the address specifically, for example, the address can be Uniswap V3 router v1.

  • approve The approve method is a non-constant method of ERC20 that generates gas consumption, which is used to authorize a token operation amount to a certain contract address. The approve method need to pass in 2 parameters, the first one is the address to be authorized and the second one is the authorized amount. Return value: txid.

    function main(){
        // The contract address of the token, in the example the token is 1INCH
        var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302"
        var spender = ""
        var amount = "0xde0b6b3a7640000"
        
        // The hexadecimal string of the authorization amount: 0xde0b6b3a7640000 , the corresponding decimal string: 1e18 , 1e18 divided by the precision unit of the token, i.e. 1 token amount, so this refers to the authorization of one token.
        Log(exchange.IO("api", tokenAddress, "approve", spender, amount))
    }
    

    spender: The address of the authorized contract, the example is replaced by the string ‘‘spender’’, the actual use needs to fill in the specific address, for example, it can be Uniswap V3 router v1 address. amount: The number of authorizations, represented here using a hexadecimal string, corresponds to a decimal value of 1e18, divided by the token precision unit in the example (i.e., 1e18), yielding 1 token authorized.

    The third parameter of the exchange.IO function is passed the method name approve, which can also be written in the form of methodId, for example: “0x571ac8b0”. It is also possible to write the full standard method name, such as ‘‘approve(address,uint256)’’.

  • multicall The multicall method is a non-constant method of Uniswap V3, which will generate gas consumption and be used to exchange tokens in multiple ways. The multicall method may have multiple methods of passing in parameters. You can query the ABI containing the method for details. You need to register the ABI before calling the method. Return value: txid.

    For specific examples of multicall method calls, please refer to the public “Uniswap V3 Trading Class Library” template of our platform.

    function main() {
        var ABI_Route = ""
        var contractV3SwapRouterV2 = ""
        var value = 0
        var deadline = (new Date().getTime() / 1000) + 3600
        var data = ""
        exchange.IO("abi", contractV3SwapRouterV2, ABI_Route)
        exchange.IO("api", contractV3SwapRouterV2, "multicall(uint256,bytes[])", value, deadline, data)
    }
    

    ABI_Route: The ABI of Uniswap V3’s router v2 contract, it needs to be filled in according to the actual situation. contractV3SwapRouterV2: router v2 address of Uniswap V3, the actual use requires a specific address to be filled in… value: The amount of ETH transferred, set it to 0 if the tokenIn token for the exchange operation is not ETH, it needs to be filled in according to the actual situation. deadline: It can be set to (new Date().getTime() / 1000) + 3600, which means it is valid for one hour. data: The data of the packing operation to be performed, it needs to be filled in according to the actual situation.

    It is also possible to specify the gasLimit/gasPrice/nonce setting for method calls:

    exchange.IO("api", contractV3SwapRouterV2, "multicall(uint256,bytes[])", value, deadline, data, {gasPrice: 5000000000, gasLimit: 21000})
    

    You can set parameter {gasPrice: 5000000000, gasLimit: 21000, nonce: 100} according to your specific needs, the parameter is set to the last parameter of the exchange.IO function. You can omit the nonce and use the system defaults, or do not set gasLimit/gasPrice/nonce and use all the system default values.

    It should be noted that in the example, the attribute of stateMutability in multicall(uint256,bytes[]) method is payable, and the value parameter needs to be passed in. The attribute of stateMutability":"payable" can be viewed from the ABI. The exchange.IO function will determine the required parameters according to the stateMutability attribute in the ABI that has been registered. If the stateMutability attribute is nonpayable, the parameter value does not need to be passed in.

Support for Switching Private Keys Other Function Calls