Skip to content

Commit 76fa5d1

Browse files
committed
feat: exclude the gas field from the codegen abi in target-ethers-v5
1 parent 545846e commit 76fa5d1

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

packages/target-ethers-v5/src/codegen/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,18 +241,20 @@ export function codegenAbstractContractFactory(contract: Contract, abi: any): st
241241
}
242242

243243
// Earlier versions of the vyper compiler included the 'gas' estimates as a member
244-
// in the generated abi object.
245-
// This breaks the auto-generation code, so it's manually excluded.
246-
function stringifyAbi(_abi: any): string {
247-
if (typeof _abi === 'object') {
248-
const abiNoGas = _abi.map((element: any) => {
244+
// in the vyper-generated abi object.
245+
// This breaks the typechain auto-generation code, since gas is a number and not stringified.
246+
// Further, even if it were stringified, the gas estimates are generally incorrect.
247+
// So the 'gas' member is manually removed from the abi in the generated typechain.
248+
function stringifyAbi(abi: any): string {
249+
if (typeof abi === 'object') {
250+
const abiNoGas = abi.map((element: any) => {
249251
const { gas: _, ...withoutGas } = element
250252
return withoutGas
251253
})
252254
return JSON.stringify(abiNoGas, null, 2)
253255
}
254256

255-
return JSON.stringify(_abi, null, 2)
257+
return JSON.stringify(abi, null, 2)
256258
}
257259

258260
function codegenCommonContractFactory(contract: Contract, abi: any): { header: string; body: string } {

packages/target-ethers-v5/test/generation.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ describe('Ethers generation edge cases', () => {
3737
expect(source).toEqual(expect.stringMatching(/export class TestContract__factory extends ContractFactory \{/))
3838
expect(source).toEqual(expect.stringMatching(/static linkBytecode\(/))
3939
})
40+
41+
it('should exclude gas field from ABI', () => {
42+
const abi = [
43+
{
44+
stateMutability: 'view',
45+
type: 'function',
46+
name: 'foo',
47+
inputs: [
48+
{
49+
name: 'bar',
50+
type: 'address',
51+
},
52+
],
53+
outputs: [
54+
{
55+
name: '',
56+
type: 'uint256',
57+
},
58+
],
59+
gas: 3135,
60+
},
61+
]
62+
63+
const source = codegenContractFactory(DEFAULT_FLAGS, emptyContract, abi, {
64+
bytecode: '{{BYTECODE}}',
65+
})
66+
67+
expect(source).not.toEqual(expect.stringMatching(/"gas":/))
68+
})
4069
})
4170

4271
describe(generateEventFilters.name, () => {

0 commit comments

Comments
 (0)