From 9569b0a9c46bead3ad1d278f7e48b7cea45e9099 Mon Sep 17 00:00:00 2001 From: Sebastian Pape Date: Wed, 7 Dec 2022 15:08:27 +0100 Subject: [PATCH] v10.2.1: fix estimate without params --- dist/esm/index.evm.js | 10 +++++-- dist/esm/index.js | 10 +++++-- dist/umd/index.evm.js | 10 +++++-- dist/umd/index.js | 10 +++++-- package.evm.json | 2 +- package.json | 4 +-- src/platforms/evm/estimate.js | 10 +++++-- .../units/platforms/evm/estimate.evm.spec.js | 28 +++++++++++++++++++ tests/units/platforms/evm/estimate.spec.js | 28 +++++++++++++++++++ yarn.lock | 2 +- 10 files changed, 95 insertions(+), 19 deletions(-) diff --git a/dist/esm/index.evm.js b/dist/esm/index.evm.js index 934ff91..efaabf0 100644 --- a/dist/esm/index.evm.js +++ b/dist/esm/index.evm.js @@ -12,8 +12,6 @@ const getContractArguments = ({ contract, method, params })=>{ return fragment.inputs.map((input) => { return params[input.name] }) - } else { - throw 'Contract params have wrong type!' } }; @@ -22,7 +20,13 @@ var estimateEVM = ({ provider, from, to, value, method, api, params }) => { return provider.estimateGas({ from, to, value }) } else { let contract = new ethers.Contract(to, api, provider); - return contract.estimateGas[method](...getContractArguments({ contract, method, params }), { from, value }) + let contractMethod = contract.estimateGas[method]; + let contractArguments = getContractArguments({ contract, method, params }); + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } } }; diff --git a/dist/esm/index.js b/dist/esm/index.js index fb4ff54..2f6d2b2 100644 --- a/dist/esm/index.js +++ b/dist/esm/index.js @@ -13,8 +13,6 @@ const getContractArguments = ({ contract, method, params })=>{ return fragment.inputs.map((input) => { return params[input.name] }) - } else { - throw 'Contract params have wrong type!' } }; @@ -23,7 +21,13 @@ var estimateEVM = ({ provider, from, to, value, method, api, params }) => { return provider.estimateGas({ from, to, value }) } else { let contract = new ethers.Contract(to, api, provider); - return contract.estimateGas[method](...getContractArguments({ contract, method, params }), { from, value }) + let contractMethod = contract.estimateGas[method]; + let contractArguments = getContractArguments({ contract, method, params }); + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } } }; diff --git a/dist/umd/index.evm.js b/dist/umd/index.evm.js index 093a65b..e631995 100644 --- a/dist/umd/index.evm.js +++ b/dist/umd/index.evm.js @@ -15,8 +15,6 @@ return fragment.inputs.map((input) => { return params[input.name] }) - } else { - throw 'Contract params have wrong type!' } }; @@ -25,7 +23,13 @@ return provider.estimateGas({ from, to, value }) } else { let contract = new ethers.ethers.Contract(to, api, provider); - return contract.estimateGas[method](...getContractArguments({ contract, method, params }), { from, value }) + let contractMethod = contract.estimateGas[method]; + let contractArguments = getContractArguments({ contract, method, params }); + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } } }; diff --git a/dist/umd/index.js b/dist/umd/index.js index e1c09e2..a7ebcae 100644 --- a/dist/umd/index.js +++ b/dist/umd/index.js @@ -15,8 +15,6 @@ return fragment.inputs.map((input) => { return params[input.name] }) - } else { - throw 'Contract params have wrong type!' } }; @@ -25,7 +23,13 @@ return provider.estimateGas({ from, to, value }) } else { let contract = new ethers.ethers.Contract(to, api, provider); - return contract.estimateGas[method](...getContractArguments({ contract, method, params }), { from, value }) + let contractMethod = contract.estimateGas[method]; + let contractArguments = getContractArguments({ contract, method, params }); + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } } }; diff --git a/package.evm.json b/package.evm.json index ea63691..5ea4f90 100644 --- a/package.evm.json +++ b/package.evm.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client-evm", "moduleName": "Web3Client", - "version": "10.2.0", + "version": "10.2.1", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.evm.js", "module": "dist/esm/index.evm.js", diff --git a/package.json b/package.json index 9860c7c..1752bf6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@depay/web3-client", "moduleName": "Web3Client", - "version": "10.2.0", + "version": "10.2.1", "description": "A web3 client to fetch blockchain data just like you are used to with HTTP clients.", "main": "dist/umd/index.js", "module": "dist/esm/index.js", @@ -46,7 +46,7 @@ "@depay/solana-web3.js": "^1.19.0", "@depay/web3-blockchains": "^6.2.3", "@depay/web3-constants": "^6.3.2", - "@depay/web3-mock": "^13.22.10", + "@depay/web3-mock": "^13.22.11", "@rollup/plugin-commonjs": "^22.0.1", "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.3.0", diff --git a/src/platforms/evm/estimate.js b/src/platforms/evm/estimate.js index 3d7937d..8de616b 100644 --- a/src/platforms/evm/estimate.js +++ b/src/platforms/evm/estimate.js @@ -11,8 +11,6 @@ const getContractArguments = ({ contract, method, params })=>{ return fragment.inputs.map((input) => { return params[input.name] }) - } else { - throw 'Contract params have wrong type!' } } @@ -21,6 +19,12 @@ export default ({ provider, from, to, value, method, api, params }) => { return provider.estimateGas({ from, to, value }) } else { let contract = new ethers.Contract(to, api, provider) - return contract.estimateGas[method](...getContractArguments({ contract, method, params }), { from, value }) + let contractMethod = contract.estimateGas[method] + let contractArguments = getContractArguments({ contract, method, params }) + if(contractArguments) { + return contractMethod(...contractArguments, { from, value }) + } else { + return contractMethod({ from, value }) + } } } diff --git a/tests/units/platforms/evm/estimate.evm.spec.js b/tests/units/platforms/evm/estimate.evm.spec.js index 4a39837..c1f4bf7 100644 --- a/tests/units/platforms/evm/estimate.evm.spec.js +++ b/tests/units/platforms/evm/estimate.evm.spec.js @@ -46,6 +46,34 @@ describe('estimate (evm)', () => { expect(gas.toString()).toEqual('200') }) + it('estimates a contract transaction without params', async ()=> { + let mockedGas = '200' + + let api = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}] + let mockedEstimate = mock({ + blockchain, + estimate: { + to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', + api, + from: accounts[0], + method: 'deposit', + value: '1000000000000000', + return: mockedGas + } + }) + let gas = await estimate({ + blockchain, + from: accounts[0], + to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', + method: 'deposit', + value: '1000000000000000', + api + }) + + expect(mockedEstimate).toHaveBeenCalled() + expect(gas.toString()).toEqual('200') + }) + it('estimates a simple transfer', async ()=> { let mockedGas = '200' let mockedEstimate = mock({ diff --git a/tests/units/platforms/evm/estimate.spec.js b/tests/units/platforms/evm/estimate.spec.js index 14b22c4..40c844b 100644 --- a/tests/units/platforms/evm/estimate.spec.js +++ b/tests/units/platforms/evm/estimate.spec.js @@ -46,6 +46,34 @@ describe('estimate', () => { expect(gas.toString()).toEqual('200') }) + it('estimates a contract transaction without params', async ()=> { + let mockedGas = '200' + + let api = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}] + let mockedEstimate = mock({ + blockchain, + estimate: { + to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', + api, + from: accounts[0], + method: 'deposit', + value: '1000000000000000', + return: mockedGas + } + }) + let gas = await estimate({ + blockchain, + from: accounts[0], + to: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', + method: 'deposit', + value: '1000000000000000', + api + }) + + expect(mockedEstimate).toHaveBeenCalled() + expect(gas.toString()).toEqual('200') + }) + it('estimates a simple transfer', async ()=> { let mockedGas = '200' let mockedEstimate = mock({ diff --git a/yarn.lock b/yarn.lock index 5f7d614..fd5db2d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -911,7 +911,7 @@ resolved "https://registry.yarnpkg.com/@depay/web3-constants/-/web3-constants-6.3.2.tgz#13a6066f20baee3ea94b373eebc69bd2c4c9773b" integrity sha512-It2ygcxIrSXMB3ilHmgtq0EMfBGf42sgnaCFbF9Lz80gPjC7O2NifNv8h3gRd/8Fy3XwzCLkjDqbVIgn7kV9tQ== -"@depay/web3-mock@^13.22.10": +"@depay/web3-mock@^13.22.11": version "13.22.11" resolved "https://registry.yarnpkg.com/@depay/web3-mock/-/web3-mock-13.22.11.tgz#36f13c444448284061c73b078fb8f7b388bfeaec" integrity sha512-I5cGu9G6f75fmEJtwQEMRj9zrbLLquOAM31ldTwRrOYpYYLIS2K4DLqZINAdGn+jW8z0dOvSscgMVI/OJ4l2RQ==