Call Smart Contract Method
The following are examples of smart contract method calls.
-
decimals
decimalsmethod is aconstantmethod ofERC20(no need to register ABI when calling standard ERC20 methods in FMZ quantitative strategy code), which does not consumegasand can query the precision data of atoken.
Thedecimalsmethod has no parameters and returns the precision data of thetoken.javascriptfunction main(){ var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302" // Token contract address, the token in this example is 1INCH Log(exchange.IO("api", tokenAddress, "decimals")) // Query and print that the precision exponent of 1INCH token is 18 } -
allowance
allowancemethod is aconstantmethod ofERC20, which does not consumegasand can query the authorization amount of atokenfor a certain contract address.
Theallowancemethod requires 2 parameters, the first parameter is the wallet address, and the second parameter is the authorized address. The return value is the authorization amount of thetoken.javascriptfunction main(){ // Token contract address, the token in this example is 1INCH var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302" var owner = "" var spender = "" // For example, if the query returns 1000000000000000000, divide by the token's precision unit 1e18 to get that the wallet bound to the current exchange object has authorized 1 1INCH to the spender address Log(exchange.IO("api", tokenAddress, "allowance", owner, spender)) }owner: Wallet address, needs to be filled with the specific address in actual use.
spender: Authorized contract address, needs to be filled with the specific address in actual use, for example, it can be theUniswap V3 router v1address. -
approve
approvemethod is a non-constantmethod ofERC20, which consumesgasand is used to authorize a certain contract address with the operation amount oftoken.
Theapprovemethod requires 2 parameters, the first parameter is the authorized address, and the second parameter is the authorization amount. The return value istxid.javascriptfunction main(){ // Token contract address, the token in this example is 1INCH var tokenAddress = "0x111111111117dC0aa78b770fA6A738034120C302" var spender = "" var amount = "0xde0b6b3a7640000" // Hexadecimal string of authorization amount: 0xde0b6b3a7640000, corresponding decimal string: 1e18, 1e18 divided by the token's precision unit equals 1 token amount, so this authorizes one token Log(exchange.IO("api", tokenAddress, "approve", spender, amount)) }spender: Authorized contract address, needs to be filled with the specific address in actual use, for example, it can be theUniswap V3 router v1address.
amount: Authorization amount, represented here as a hexadecimal string. The corresponding decimal value is1e18, divided by thetokenprecision unit in the example (i.e., 1e18), resulting in authorization of 1token.The third parameter of the
exchange.IO()function passes the method nameapprove, which can also be written in the form ofmethodId, for example: "0x571ac8b0". It can also be written as the complete standard method name, for example: "approve(address,uint256)". -
multicall
multicallmethod is a non-constant method ofUniswap V3, which consumesgasand is used for batch token swaps.
Themulticallmethod may have multiple parameter passing methods, you can check the ABI containing this method for details. The ABI needs to be registered before calling this method. The return value istxid.For specific
multicallmethod call examples, you can refer to the platform's public "Uniswap V3 Trading Library" templatejavascriptfunction 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: ABI of Uniswap V3's router v2 contract, needs to be filled according to actual situation.
contractV3SwapRouterV2: Uniswap V3's router v2 address, needs to be filled with the specific address in actual use.
value: Amount of ETH to transfer, set to 0 if thetokenIntoken for the swap operation is not ETH, needs to be filled according to actual situation.
deadline: Can be set to(new Date().getTime() / 1000) + 3600, indicating validity within one hour.
data: Packed operation data to be executed, needs to be filled according to actual situation.You can also specify
gasLimit/gasPrice/noncesettings for method calls:javascriptexchange.IO("api", contractV3SwapRouterV2, "multicall(uint256,bytes[])", value, deadline, data, {gasPrice: 5000000000, gasLimit: 21000})You can set
{gasPrice: 5000000000, gasLimit: 21000, nonce: 100}parameters according to specific needs, this parameter is set as the last parameter of theexchange.IO()function.
You can omitnonceto use the system default value, or not setgasLimit/gasPrice/nonceto use all system default values.Note that the
stateMutabilityattribute of themulticall(uint256,bytes[])method in the example ispayable, which requires passing thevalueparameter.
ThestateMutability":"payable"attribute can be viewed from theABI, theexchange.IO()function will determine the required parameters based on thestateMutabilityattribute in the registeredABI.
If thestateMutabilityattribute isnonpayable, there is no need to pass thevalueparameter.