Skip to content

Commit

Permalink
v10.2.1: fix estimate without params
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Dec 7, 2022
1 parent 1c90bf6 commit 9569b0a
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 19 deletions.
10 changes: 7 additions & 3 deletions dist/esm/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
}
};

Expand All @@ -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 })
}
}
};

Expand Down
10 changes: 7 additions & 3 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
}
};

Expand All @@ -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 })
}
}
};

Expand Down
10 changes: 7 additions & 3 deletions dist/umd/index.evm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
return fragment.inputs.map((input) => {
return params[input.name]
})
} else {
throw 'Contract params have wrong type!'
}
};

Expand All @@ -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 })
}
}
};

Expand Down
10 changes: 7 additions & 3 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
return fragment.inputs.map((input) => {
return params[input.name]
})
} else {
throw 'Contract params have wrong type!'
}
};

Expand All @@ -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 })
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion package.evm.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 7 additions & 3 deletions src/platforms/evm/estimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
}
}

Expand All @@ -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 })
}
}
}
28 changes: 28 additions & 0 deletions tests/units/platforms/evm/estimate.evm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
28 changes: 28 additions & 0 deletions tests/units/platforms/evm/estimate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 9569b0a

Please sign in to comment.