From 017ea0d6bd22833e9d399ae6b818443786f17884 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Fri, 24 Apr 2020 03:02:57 -0400 Subject: [PATCH] Make default constructor non-payable (#684). --- packages/abi/src.ts/fragments.ts | 42 +++++++++++++++++++++++++++----- packages/abi/src.ts/interface.ts | 7 ++++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/packages/abi/src.ts/fragments.ts b/packages/abi/src.ts/fragments.ts index 5065058661..8ac9258588 100644 --- a/packages/abi/src.ts/fragments.ts +++ b/packages/abi/src.ts/fragments.ts @@ -391,6 +391,14 @@ function parseParams(value: string, allowIndex: boolean): Array { return splitNesting(value).map((param) => ParamType.fromString(param, allowIndex)); } +type TypeCheck = { -readonly [ K in keyof T ]: T[K] }; + +interface _Fragment { + readonly type: string; + readonly name: string; + readonly inputs: Array; +} + export abstract class Fragment { readonly type: string; @@ -465,6 +473,10 @@ export abstract class Fragment { } } +interface _EventFragment extends _Fragment { + readonly anonymous: boolean; +} + export class EventFragment extends Fragment { readonly anonymous: boolean; @@ -516,12 +528,14 @@ export class EventFragment extends Fragment { logger.throwArgumentError("invalid event object", "value", value); } - return new EventFragment(_constructorGuard, { + const params: TypeCheck<_EventFragment> = { name: verifyIdentifier(value.name), anonymous: value.anonymous, inputs: (value.inputs ? value.inputs.map(ParamType.fromObject) : []), type: "event" - }); + }; + + return new EventFragment(_constructorGuard, params); } static fromString(value: string): EventFragment { @@ -678,6 +692,12 @@ function verifyState(value: StateInputValue): StateOutputValue { return result; } +interface _ConstructorFragment extends _Fragment { + stateMutability: string; + payable: boolean; + gas?: BigNumber; +} + export class ConstructorFragment extends Fragment { stateMutability: string; payable: boolean; @@ -735,13 +755,16 @@ export class ConstructorFragment extends Fragment { logger.throwArgumentError("constructor cannot be constant", "value", value); } - return new ConstructorFragment(_constructorGuard, { + const params: TypeCheck<_ConstructorFragment> = { name: null, type: value.type, inputs: (value.inputs ? value.inputs.map(ParamType.fromObject): []), payable: state.payable, + stateMutability: state.stateMutability, gas: (value.gas ? BigNumber.from(value.gas): null) - }); + }; + + return new ConstructorFragment(_constructorGuard, params); } static fromString(value: string): ConstructorFragment { @@ -766,6 +789,11 @@ export class ConstructorFragment extends Fragment { } } +interface _FunctionFragment extends _ConstructorFragment { + constant: boolean; + outputs?: Array; +} + export class FunctionFragment extends ConstructorFragment { constant: boolean; outputs?: Array; @@ -838,7 +866,7 @@ export class FunctionFragment extends ConstructorFragment { let state = verifyState(value); - return new FunctionFragment(_constructorGuard, { + const params: TypeCheck<_FunctionFragment> = { type: value.type, name: verifyIdentifier(value.name), constant: state.constant, @@ -847,7 +875,9 @@ export class FunctionFragment extends ConstructorFragment { payable: state.payable, stateMutability: state.stateMutability, gas: (value.gas ? BigNumber.from(value.gas): null) - }); + }; + + return new FunctionFragment(_constructorGuard, params); } static fromString(value: string): FunctionFragment { diff --git a/packages/abi/src.ts/interface.ts b/packages/abi/src.ts/interface.ts index 260592be19..0e3293c548 100644 --- a/packages/abi/src.ts/interface.ts +++ b/packages/abi/src.ts/interface.ts @@ -108,9 +108,12 @@ export class Interface { bucket[signature] = fragment; }); - // If we do not have a constructor use the default "constructor() payable" + // If we do not have a constructor add a default if (!this.deploy) { - defineReadOnly(this, "deploy", ConstructorFragment.from({ type: "constructor" })); + defineReadOnly(this, "deploy", ConstructorFragment.from({ + payable: false, + type: "constructor" + })); } defineReadOnly(this, "_isInterface", true);