rams,name:params,type:tuple],name:exactInput,outputs:[{internalType:uint256,name:amountOut,type:uint256}],stateMutability:payable,type:function},{inputs:[{components:[{internalType:params,type:tuple],name:tokenInput,type:address,outputs:[{internalType:addaddaddAddress,name:tokenOutput,amountOutput,type:uint256],stateMutability:payable,type,type,type,output,type,output,type,output,
var abiPool = [\\inputs\:[],\stateMutability\:\nonpayable\,\type\:\constructor\,\name\:\anonymous\:\false,\inputs\:[\\\indexed\:true,\internalType\:\address\:\:\owner\,\typeType:\address\:\address\:\indexed\:\type:\type:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon
var abiFactory = [\\inputs\:[],\stateMutability\:\\nonpayable\,\type\:\\\constructor\,\anonymous\:\false,\\inputs\:[\\\\indexed\:true,\\internalType\:\uint24\,\\\\\name\:\\\fee\,\type\:\\uint24\,\\\indexed\:\type\:\internalType\:\addaddaddadded\:\uint24\,\tokname\:\tickSaddaddaddaddaddaddadding\:\:false,\input\:\:\:\input\:\:\true,\internalType\:\type\:\type\:\type\:\:\\\\Typed
var kontrakV3FactoryAlamat = 0x1F98431c8aD98523631AE4a59f267346ea31F984
var contractV3SwapRouterV2Address = 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45
fungsi keAmount ((s, perpuluhan) {
pulangkan Nombor (((BigDecimal(BigInt(s)) / BigDecimal ((Math.pow(10, perpuluhan))).toString())
{}
fungsi utama (() {
// Pendaftaran ABI kontrak kilang Uniswap
exchange.IO ((abi, contractV3FactoryAddress, abiFactory) adalah sebuah laman web yang ditubuhkan oleh Microsoft Corporation.
// 注册Uniswap路由合约的ABI
exchange.IO("abi", contractV3SwapRouterV2Address, abiRoute)
// 获取交易对的池地址
var tokenIn = {name : "1INCH", address: "0x111111111117dC0aa78b770fA6A738034120C302", decimals: exchange.IO("api", "0x111111111117dC0aa78b770fA6A738034120C302", "decimals")}
var tokenOut = {name : "WETH", address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", decimals: exchange.IO("api", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", "decimals")}
var poolAddress = exchange.IO("api", contractV3FactoryAddress, "getPool", tokenIn.address, tokenOut.address, 10000)
// 注册池合约ABI
exchange.IO("abi", poolAddress, abiPool)
// 获取当前配置的钱包地址
var walletAddress = exchange.IO("address")
// 1INCH -> ETH, tokenIn: 1INCH
var allowanceAmount = exchange.IO("api", tokenIn.address, "allowance", walletAddress, contractV3SwapRouterV2Address)
Log("allowanceAmount:", allowanceAmount, ", 使用toAmount()函数转换为可读数值:", toAmount(allowanceAmount, tokenIn.decimals))
}
对于代码中的```toAmount()```函数我们应该比较熟悉,前几章课程中我们多次使用了这个自定义函数代码,用于把数据处理为方便人类阅读的数值。
```javascript
function toAmount(s, decimals) {
return Number((BigDecimal(BigInt(s)) / BigDecimal(Math.pow(10, decimals))).toString())
}
Melancarkan kod dan mendapati jumlah lesen semasa adalah 0, maka untuk melakukan pertukaran, jumlah lesen yang mencukupi mesti diberikan kepada kontrak penghala untuk berjalan.
PenguatkuasaanUniswap V3
Kontrak Laluan
Jika digunakanallowance
Apabila permintaan menunjukkan jumlah lesen yang tidak mencukupi, anda perlu menggunakannya.approve
PenguatkuasaanUniswap V3
Kontrak penghalaan yang membenarkan ia mengendalikan sejumlahERC20
Token.
atau1INCH -> ETH
Pertukaran sebagai contoh, penggunaanERC20
Perbezaanapprove
Cara untukUniswap
Kontrak penghalaan membenarkan pengendalian dalam dompet yang kini dikonfigurasi1INCH
Token.
`JavaScript
var abiRoute = [{inputs:[{internalType:address,name:_WETHBYTH9,type:address},{internalType:address,name:factoryV3,type:address},{internalType:address,name:factoryV3,type:address},{internalType:address,name:factoryV3,type:address},{internalType:address,name:positionManager,type:address},{internalType:address:address,type:inputs:inputs,type:address,typeaddress,typeaddress,address,address
var abiPool = [\\inputs\:[],\stateMutability\:\nonpayable\,\type\:\constructor\,\name\:\anonymous\:\false,\inputs\:[\\\indexed\:true,\internalType\:\address\:\:\owner\,\typeType:\address\:\address\:\indexed\:\type:\type:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon:\addon