Skip to content

Commit

Permalink
Merge pull request #314 from ethereum-ts/kk/respect-ethers-reserved-k…
Browse files Browse the repository at this point in the history
…eywords
  • Loading branch information
krzkaczor authored Dec 5, 2020
2 parents da71427 + db5baa5 commit d5548a2
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-ants-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@typechain/ethers-v4': patch
'@typechain/ethers-v5': patch
---

Do not generate typings in contract type itself for reserved keywords that would collide with ethers internals
6 changes: 5 additions & 1 deletion contracts/Name-Mangling.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pragma solidity ^0.6.4;
pragma experimental ABIEncoderV2;

Expand All @@ -7,4 +6,9 @@ contract NAME12mangling {
function works() public view returns (bool) {
return true;
}

// test name collision
function provider() public view returns (bool) {
return true;
}
}
10 changes: 10 additions & 0 deletions packages/target-ethers-v4-test/types/NameMangling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {

interface NameManglingInterface extends Interface {
functions: {
provider: TypedFunctionDescription<{ encode([]: []): string }>;

works: TypedFunctionDescription<{ encode([]: []): string }>;
};

Expand All @@ -36,6 +38,10 @@ export class NameMangling extends Contract {
interface: NameManglingInterface;

functions: {
provider(overrides?: TransactionOverrides): Promise<boolean>;

"provider()"(overrides?: TransactionOverrides): Promise<boolean>;

works(overrides?: TransactionOverrides): Promise<boolean>;

"works()"(overrides?: TransactionOverrides): Promise<boolean>;
Expand All @@ -48,6 +54,10 @@ export class NameMangling extends Contract {
filters: {};

estimate: {
provider(overrides?: TransactionOverrides): Promise<BigNumber>;

"provider()"(overrides?: TransactionOverrides): Promise<BigNumber>;

works(overrides?: TransactionOverrides): Promise<BigNumber>;

"works()"(overrides?: TransactionOverrides): Promise<BigNumber>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export class NameMangling__factory {
}

const _abi = [
{
inputs: [],
name: "provider",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "works",
Expand Down
6 changes: 5 additions & 1 deletion packages/target-ethers-v4/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { generateInputType, generateInputTypes } from './types'
import { codegenFunctions } from './functions'
import { FACTORY_POSTFIX } from '../common'
import { reservedKeywords } from './reserved-keywords'

export function codegenContractTypings(contract: Contract) {
const template = `
Expand Down Expand Up @@ -51,7 +52,10 @@ export function codegenContractTypings(contract: Contract) {
${values(contract.functions).map(codegenFunctions.bind(null, {})).join('\n')}
};
${values(contract.functions).map(codegenFunctions.bind(null, {})).join('\n')}
${values(contract.functions)
.filter((f) => !reservedKeywords.has(f[0].name))
.map(codegenFunctions.bind(null, {}))
.join('\n')}
filters: {
${values(contract.events)
Expand Down
1 change: 1 addition & 0 deletions packages/target-ethers-v4/src/codegen/reserved-keywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const reservedKeywords = new Set(['signer', 'provider', 'deployTransaction', 'deployed', 'fallback', 'connect'])
19 changes: 19 additions & 0 deletions packages/target-ethers-v5-test/types/NameMangling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import { FunctionFragment, EventFragment, Result } from "@ethersproject/abi";

interface NameManglingInterface extends ethers.utils.Interface {
functions: {
"provider()": FunctionFragment;
"works()": FunctionFragment;
};

encodeFunctionData(functionFragment: "provider", values?: undefined): string;
encodeFunctionData(functionFragment: "works", values?: undefined): string;

decodeFunctionResult(functionFragment: "provider", data: BytesLike): Result;
decodeFunctionResult(functionFragment: "works", data: BytesLike): Result;

events: {};
Expand All @@ -45,6 +48,10 @@ export class NameMangling extends Contract {
interface: NameManglingInterface;

functions: {
provider(overrides?: CallOverrides): Promise<[boolean]>;

"provider()"(overrides?: CallOverrides): Promise<[boolean]>;

works(overrides?: CallOverrides): Promise<[boolean]>;

"works()"(overrides?: CallOverrides): Promise<[boolean]>;
Expand All @@ -55,6 +62,10 @@ export class NameMangling extends Contract {
"works()"(overrides?: CallOverrides): Promise<boolean>;

callStatic: {
provider(overrides?: CallOverrides): Promise<boolean>;

"provider()"(overrides?: CallOverrides): Promise<boolean>;

works(overrides?: CallOverrides): Promise<boolean>;

"works()"(overrides?: CallOverrides): Promise<boolean>;
Expand All @@ -63,12 +74,20 @@ export class NameMangling extends Contract {
filters: {};

estimateGas: {
provider(overrides?: CallOverrides): Promise<BigNumber>;

"provider()"(overrides?: CallOverrides): Promise<BigNumber>;

works(overrides?: CallOverrides): Promise<BigNumber>;

"works()"(overrides?: CallOverrides): Promise<BigNumber>;
};

populateTransaction: {
provider(overrides?: CallOverrides): Promise<PopulatedTransaction>;

"provider()"(overrides?: CallOverrides): Promise<PopulatedTransaction>;

works(overrides?: CallOverrides): Promise<PopulatedTransaction>;

"works()"(overrides?: CallOverrides): Promise<PopulatedTransaction>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export class NameMangling__factory {
}

const _abi = [
{
inputs: [],
name: "provider",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "works",
Expand Down
6 changes: 5 additions & 1 deletion packages/target-ethers-v5/src/codegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { generateInputType, generateInputTypes } from './types'
import { codegenFunctions } from './functions'
import { FACTORY_POSTFIX } from '../common'
import { reservedKeywords } from './reserved-keywords'

export function codegenContractTypings(contract: Contract) {
const contractImports: string[] = ['Contract', 'ContractTransaction']
Expand Down Expand Up @@ -77,7 +78,10 @@ export function codegenContractTypings(contract: Contract) {
.join('\n')}
};
${values(contract.functions).map(codegenFunctions.bind(null, {})).join('\n')}
${values(contract.functions)
.filter((f) => !reservedKeywords.has(f[0].name))
.map(codegenFunctions.bind(null, {}))
.join('\n')}
callStatic: {
${values(contract.functions)
Expand Down
1 change: 1 addition & 0 deletions packages/target-ethers-v5/src/codegen/reserved-keywords.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const reservedKeywords = new Set(['signer', 'provider', 'deployTransaction', 'deployed', 'fallback', 'connect'])
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ type AllEvents = never;

export interface NAME12manglingInstance extends Truffle.ContractInstance {
works(txDetails?: Truffle.TransactionDetails): Promise<boolean>;

provider(txDetails?: Truffle.TransactionDetails): Promise<boolean>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ type AllEvents = never;
export interface NAME12manglingInstance extends Truffle.ContractInstance {
works(txDetails?: Truffle.TransactionDetails): Promise<boolean>;

provider(txDetails?: Truffle.TransactionDetails): Promise<boolean>;

methods: {
works(txDetails?: Truffle.TransactionDetails): Promise<boolean>;

provider(txDetails?: Truffle.TransactionDetails): Promise<boolean>;
};

getPastEvents(event: string): Promise<EventData[]>;
Expand Down
2 changes: 2 additions & 0 deletions packages/target-web3-v1-test/types/Name-Mangling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface NameMangling extends BaseContract {
): NameMangling;
clone(): NameMangling;
methods: {
provider(): NonPayableTransactionObject<boolean>;

works(): NonPayableTransactionObject<boolean>;
};
events: {
Expand Down

0 comments on commit d5548a2

Please sign in to comment.