Skip to content

Commit

Permalink
Make default constructor non-payable (#684).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Apr 24, 2020
1 parent 47e4655 commit 017ea0d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
42 changes: 36 additions & 6 deletions packages/abi/src.ts/fragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ function parseParams(value: string, allowIndex: boolean): Array<ParamType> {
return splitNesting(value).map((param) => ParamType.fromString(param, allowIndex));
}

type TypeCheck<T> = { -readonly [ K in keyof T ]: T[K] };

interface _Fragment {
readonly type: string;
readonly name: string;
readonly inputs: Array<ParamType>;
}

export abstract class Fragment {

readonly type: string;
Expand Down Expand Up @@ -465,6 +473,10 @@ export abstract class Fragment {
}
}

interface _EventFragment extends _Fragment {
readonly anonymous: boolean;
}

export class EventFragment extends Fragment {
readonly anonymous: boolean;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -766,6 +789,11 @@ export class ConstructorFragment extends Fragment {
}
}

interface _FunctionFragment extends _ConstructorFragment {
constant: boolean;
outputs?: Array<ParamType>;
}

export class FunctionFragment extends ConstructorFragment {
constant: boolean;
outputs?: Array<ParamType>;
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 017ea0d

Please sign in to comment.