-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ethers-v4] Support for overloaded functions
- Loading branch information
Showing
7 changed files
with
173 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { typedAssert } from 'test-utils' | ||
|
||
import { createNewBlockchain, deployContract } from './common' | ||
import { Overloads } from '../types/Overloads' | ||
import { BigNumber } from 'ethers/utils' | ||
|
||
describe('Overloads', () => { | ||
let contract: Overloads | ||
let ganache: any | ||
beforeEach(async () => { | ||
const { ganache: _ganache, signer } = await createNewBlockchain() | ||
ganache = _ganache | ||
contract = await deployContract<Overloads>(signer, 'Overloads') | ||
}) | ||
|
||
afterEach(() => ganache.close()) | ||
|
||
it('works with 1st overload', async () => { | ||
const result = await contract.functions['overload1(int256)'](1) | ||
typedAssert(result, new BigNumber(1)) | ||
}) | ||
|
||
it('works with 2n overload', async () => { | ||
const result = await contract.functions['overload1(uint256,uint256)'](1, 2) | ||
typedAssert(result, new BigNumber(3)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { FunctionDeclaration, isConstant, isConstantFn, FunctionDocumentation, getSignatureForFn } from 'typechain' | ||
import { generateInputTypes, generateOutputTypes } from './types' | ||
|
||
export function codegenFunctions(fns: FunctionDeclaration[]): string { | ||
if (fns.length === 1) { | ||
return generateFunction(fns[0]) | ||
} | ||
|
||
return codegenForOverloadedFunctions(fns) | ||
} | ||
|
||
export function codegenForOverloadedFunctions(fns: FunctionDeclaration[]): string { | ||
return fns.map((fn) => generateFunction(fn, `"${getSignatureForFn(fn)}"`)).join('\n') | ||
} | ||
|
||
function generateFunction(fn: FunctionDeclaration, overloadedName?: string): string { | ||
return ` | ||
${generateFunctionDocumentation(fn.documentation)} | ||
${overloadedName ?? fn.name}(${generateInputTypes(fn.inputs)}${ | ||
!isConstant(fn) && !isConstantFn(fn) ? 'overrides?: TransactionOverrides' : '' | ||
}): Promise<${ | ||
fn.stateMutability === 'pure' || fn.stateMutability === 'view' | ||
? generateOutputTypes(fn.outputs) | ||
: 'ContractTransaction' | ||
}>; | ||
` | ||
} | ||
|
||
function generateFunctionDocumentation(doc?: FunctionDocumentation): string { | ||
if (!doc) return '' | ||
|
||
let docString = '/**' | ||
if (doc.details) docString += `\n * ${doc.details}` | ||
if (doc.notice) docString += `\n * ${doc.notice}` | ||
|
||
const params = Object.entries(doc.params || {}) | ||
if (params.length) { | ||
params.forEach(([key, value]) => { | ||
docString += `\n * @param ${key} ${value}` | ||
}) | ||
} | ||
|
||
if (doc.return) docString += `\n * @returns ${doc.return}` | ||
|
||
docString += '\n */' | ||
|
||
return docString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { EvmType, EvmOutputType, TupleType, AbiParameter, AbiOutputParameter } from 'typechain' | ||
|
||
export function generateInputTypes(input: Array<AbiParameter>): string { | ||
if (input.length === 0) { | ||
return '' | ||
} | ||
return ( | ||
input.map((input, index) => `${input.name || `arg${index}`}: ${generateInputType(input.type)}`).join(', ') + ', ' | ||
) | ||
} | ||
|
||
export function generateOutputTypes(outputs: Array<AbiOutputParameter>): string { | ||
if (outputs.length === 1) { | ||
return generateOutputType(outputs[0].type) | ||
} else { | ||
return `{ | ||
${outputs.map((t) => t.name && `${t.name}: ${generateOutputType(t.type)}, `).join('')} | ||
${outputs.map((t, i) => `${i}: ${generateOutputType(t.type)}`).join(', ')} | ||
}` | ||
} | ||
} | ||
|
||
// https://docs.ethers.io/ethers.js/html/api-contract.html#types | ||
export function generateInputType(evmType: EvmType): string { | ||
switch (evmType.type) { | ||
case 'integer': | ||
return 'BigNumberish' | ||
case 'uinteger': | ||
return 'BigNumberish' | ||
case 'address': | ||
return 'string' | ||
case 'bytes': | ||
case 'dynamic-bytes': | ||
return 'Arrayish' | ||
case 'array': | ||
return `(${generateInputType(evmType.itemType)})[]` | ||
case 'boolean': | ||
return 'boolean' | ||
case 'string': | ||
return 'string' | ||
case 'tuple': | ||
return generateTupleType(evmType, generateInputType) | ||
} | ||
} | ||
|
||
export function generateOutputType(evmType: EvmOutputType): string { | ||
switch (evmType.type) { | ||
case 'integer': | ||
case 'uinteger': | ||
return evmType.bits <= 48 ? 'number' : 'BigNumber' | ||
case 'address': | ||
return 'string' | ||
case 'void': | ||
return 'void' | ||
case 'bytes': | ||
case 'dynamic-bytes': | ||
return 'string' | ||
case 'array': | ||
return `(${generateOutputType(evmType.itemType)})[]` | ||
case 'boolean': | ||
return 'boolean' | ||
case 'string': | ||
return 'string' | ||
case 'tuple': | ||
return generateOutputTupleType(evmType) | ||
} | ||
} | ||
|
||
export function generateTupleType(tuple: TupleType, generator: (evmType: EvmType) => string) { | ||
return '{' + tuple.components.map((component) => `${component.name}: ${generator(component.type)}`).join(',') + '}' | ||
} | ||
|
||
export function generateOutputTupleType(tuple: TupleType) { | ||
return ( | ||
'{' + | ||
tuple.components.map((component) => `${component.name}: ${generateOutputType(component.type)} ,`).join('\n') + | ||
tuple.components.map((component, index) => `${index}: ${generateOutputType(component.type)}`).join(', ') + | ||
'}' | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters