Skip to content

Commit

Permalink
feat: update Enum class to force extending classes to declare enum
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Jun 18, 2024
1 parent 0236b6a commit e64c493
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/transactions/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { PublicKey } from '@near-js/crypto';
import { DelegateAction } from './delegate';
import { Signature } from './signature';

// TODO determine why subclassing is still necessary even though `enum`
// cannot be set in the base class or it will not be borsh-serializable
abstract class Enum {
enum: string;
abstract enum: string;

constructor(properties: any) {
if (Object.keys(properties).length !== 1) {
throw new Error('Enum can only take single value');
}
Object.keys(properties).map((key: string) => {
(this as any)[key] = properties[key];
this.enum = key;
});
}
}
Expand All @@ -32,8 +33,17 @@ export class FunctionCallPermission {
export class FullAccessPermission {}

export class AccessKeyPermission extends Enum {
enum: string;
functionCall?: FunctionCallPermission;
fullAccess?: FullAccessPermission;

constructor(props: any) {
super(props);
for (const [k, v] of Object.entries(props || {})) {
this[k] = v;
this.enum = k;
}
}
}

export class AccessKey {
Expand Down Expand Up @@ -124,6 +134,7 @@ export class SignedDelegate {
* @see {@link https://nomicon.io/RuntimeSpec/Actions.html | Actions Spec}
*/
export class Action extends Enum {
enum: string;
createAccount?: CreateAccount;
deployContract?: DeployContract;
functionCall?: FunctionCall;
Expand All @@ -133,4 +144,12 @@ export class Action extends Enum {
deleteKey?: DeleteKey;
deleteAccount?: DeleteAccount;
signedDelegate?: SignedDelegate;

constructor(props: any) {
super(props);
for (const [k, v] of Object.entries(props || {})) {
this[k] = v;
this.enum = k;
}
}
}

0 comments on commit e64c493

Please sign in to comment.