Uniswap
與 ethers.js 的 Uniswap V1 工廠契約不起作用
我嘗試了以下程式碼來獲取 DAI(0x6B175474E89094C44Da98b954EedeAC495271d0F) 的交換地址。但它給出了一些奇怪的錯誤。知道出了什麼問題嗎?(我已經驗證了合約地址和ABI)
程式碼:
const { ethers } = require("ethers"); const provider = new ethers.providers.JsonRpcProvider(my_infura_url) const UNISWAP_FACTORY_ABI = [{ "name": "NewExchange", "inputs": [{ "type": "address", "name": "token", "indexed": true }, { "type": "address", "name": "exchange", "indexed": true }], "anonymous": false, "type": "event" }, { "name": "initializeFactory", "outputs": [], "inputs": [{ "type": "address", "name": "template" }], "constant": false, "payable": false, "type": "function", "gas": 35725 }, { "name": "createExchange", "outputs": [{ "type": "address", "name": "out" }], "inputs": [{ "type": "address", "name": "token" }], "constant": false, "payable": false, "type": "function", "gas": 187911 }, { "name": "getExchange", "outputs": [{ "type": "address", "name": "out" }], "inputs": [{ "type": "address", "name": "token" }], "constant": true, "payable": false, "type": "function", "gas": 715 }, { "name": "getToken", "outputs": [{ "type": "address", "name": "out" }], "inputs": [{ "type": "address", "name": "exchange" }], "constant": true, "payable": false, "type": "function", "gas": 745 }, { "name": "getTokenWithId", "outputs": [{ "type": "address", "name": "out" }], "inputs": [{ "type": "uint256", "name": "token_id" }], "constant": true, "payable": false, "type": "function", "gas": 736 }, { "name": "exchangeTemplate", "outputs": [{ "type": "address", "name": "out" }], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 633 }, { "name": "tokenCount", "outputs": [{ "type": "uint256", "name": "out" }], "inputs": [], "constant": true, "payable": false, "type": "function", "gas": 663 }] const UNISWAP_FACTORY_ADDRESS = '0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95' const uniswapFactoryContract = new ethers.Contract(UNISWAP_FACTORY_ADDRESS, UNISWAP_FACTORY_ABI, provider) const main = async () => { const exchangeAddress = await uniswapFactoryContract.getExchange("0x6B175474E89094C44Da98b954EedeAC495271d0F") console.log(exchangeAddress) } main()
錯誤:
C:\Users\rssav\node_modules\@ethersproject\logger\lib\index.js:199 var error = new Error(message); ^ Error: missing revert data in call exception (error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":44,\"error\":{\"code\":-32000,\"message\":\"out of gas\"}}","error":{"code":-32000},"requestBody":"{\"method\":\"eth_call\",\"params\":[{\"gas\":\"0x5b63\",\"to\":\"0xc0a47dfe034b400b47bdad5fecda2621de6c4d95\",\"data\":\"0x06f2bf620000000000000000000000006b175474e89094c44da98b954eedeac495271d0f\"},\"latest\"],\"id\":44,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"https://mainnet.infura.io/v3/b4e8a43f00d34e7590cef3b3eb8c5b82"}, data="0x", code=CALL_EXCEPTION, version=providers/5.5.3) at Logger.makeError (C:\Users\rssav\node_modules\@ethersproject\logger\lib\index.js:199:21) at Logger.throwError (C:\Users\rssav\node_modules\@ethersproject\logger\lib\index.js:208:20) at checkError (C:\Users\rssav\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:76:16) at JsonRpcProvider.<anonymous> (C:\Users\rssav\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:659:47) at step (C:\Users\rssav\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23) at Object.throw (C:\Users\rssav\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53) at rejected (C:\Users\rssav\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65) at processTicksAndRejections (node:internal/process/task_queues:96:5) { reason: 'missing revert data in call exception', code: 'CALL_EXCEPTION', error: Error: processing response error (body="{\"jsonrpc\":\"2.0\",\"id\":44,\"error\":{\"code\":-32000,\"message\":\"out of gas\"}}", error={"code":-32000}, requestBody="{\"method\":\"eth_call\",\"params\":[{\"gas\":\"0x5b63\",\"to\":\"0xc0a47dfe034b400b47bdad5fecda2621de6c4d95\",\"data\":\"0x06f2bf620000000000000000000000006b175474e89094c44da98b954eedeac495271d0f\"},\"latest\"],\"id\":44,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://mainnet.infura.io/v3/b4e8a43f00d34e7590cef3b3eb8c5b82", code=SERVER_ERROR, version=web/5.5.1) at Logger.makeError (C:\Users\rssav\node_modules\@ethersproject\logger\lib\index.js:199:21) at Logger.throwError (C:\Users\rssav\node_modules\@ethersproject\logger\lib\index.js:208:20) at C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:301:32 at step (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:33:23) at Object.next (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:14:53) at fulfilled (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:5:58) at processTicksAndRejections (node:internal/process/task_queues:96:5) { reason: 'processing response error', code: 'SERVER_ERROR', body: '{"jsonrpc":"2.0","id":44,"error":{"code":-32000,"message":"out of gas"}}', error: Error: out of gas at getResult (C:\Users\rssav\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:142:21) at processJsonFunc (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:344:22) at C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:276:46 at step (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:33:23) at Object.next (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:14:53) at fulfilled (C:\Users\rssav\node_modules\@ethersproject\web\lib\index.js:5:58) at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: -32000, data: undefined }, requestBody: '{"method":"eth_call","params":[{"gas":"0x5b63","to":"0xc0a47dfe034b400b47bdad5fecda2621de6c4d95","data":"0x06f2bf620000000000000000000000006b175474e89094c44da98b954eedeac495271d0f"},"latest"],"id":44,"jsonrpc":"2.0"}', requestMethod: 'POST', url: my_infura_url }, data: '0x' }
const abi = [ "function getExchange(address token) view returns (address)" ];
將您的 ABI 修改為上述程式碼。我認為您提供的 json ABI 有問題
錯誤消息說
out of gas
,如果您從 Uniswap 文件中複製了 ABI,則某些函式的氣體定義如下:{ name: "getExchange", outputs: [{ type: "address", name: "out" }], inputs: [{ type: "address", name: "token" }], constant: true, payable: false, type: "function", gas: "715", }
只需刪除氣體屬性。