Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/target-ethers-v5/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ export function codegenAbstractContractFactory(contract: Contract, abi: any): st
`
}

// Earlier versions of the vyper compiler included the 'gas' estimates as a member
// in the vyper-generated abi object.
// This breaks the typechain auto-generation code, since gas is a number and not stringified.
// Further, even if it were stringified, the gas estimates are generally incorrect.
// So the 'gas' member is manually removed from the abi in the generated typechain.
function stringifyAbi(abi: any): string {
if (typeof abi === 'object') {
const abiNoGas = abi.map((element: any) => {
const { gas: _, ...withoutGas } = element
return withoutGas
})
return JSON.stringify(abiNoGas, null, 2)
}

return JSON.stringify(abi, null, 2)
}

function codegenCommonContractFactory(contract: Contract, abi: any): { header: string; body: string } {
const imports: Set<string> = new Set([contract.name, contract.name + 'Interface'])

Expand All @@ -257,7 +274,7 @@ function codegenCommonContractFactory(contract: Contract, abi: any): { header: s
const header = `
import type { ${[...imports.values()].join(', ')} } from "${contractTypesImportPath}";

const _abi = ${JSON.stringify(abi, null, 2)};
const _abi = ${stringifyAbi(abi)};
`.trim()

const body = `
Expand Down
29 changes: 29 additions & 0 deletions packages/target-ethers-v5/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ describe('Ethers generation edge cases', () => {
expect(source).toEqual(expect.stringMatching(/export class TestContract__factory extends ContractFactory \{/))
expect(source).toEqual(expect.stringMatching(/static linkBytecode\(/))
})

it('should exclude gas field from ABI', () => {
const abi = [
{
stateMutability: 'view',
type: 'function',
name: 'foo',
inputs: [
{
name: 'bar',
type: 'address',
},
],
outputs: [
{
name: '',
type: 'uint256',
},
],
gas: 3135,
},
]

const source = codegenContractFactory(DEFAULT_FLAGS, emptyContract, abi, {
bytecode: '{{BYTECODE}}',
})

expect(source).not.toEqual(expect.stringMatching(/"gas":/))
})
})

describe(generateEventFilters.name, () => {
Expand Down