Skip to content

Commit

Permalink
refactor(account-update.ts): move MayUseToken type and related functi…
Browse files Browse the repository at this point in the history
…ons to the top-level scope

The MayUseToken type and its related functions were previously defined within the AccountUpdate class. Moving them to the top-level scope improves code organization and makes them more accessible.

The MayUseToken type is now imported from the '../../bindings/mina-transaction/transaction-leaves.js' module as BaseMayUseToken and then extended with additional properties and functions.

This change enhances code readability and maintainability by separating concerns and promoting a more modular structure.
  • Loading branch information
MartinMinkov committed Jul 10, 2024
1 parent df603bc commit 5a27a22
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/lib/mina/account-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { Memo } from '../../mina-signer/src/memo.js';
import {
Events as BaseEvents,
Actions as BaseActions,
MayUseToken as BaseMayUseToken,
} from '../../bindings/mina-transaction/transaction-leaves.js';
import { TokenId as Base58TokenId } from './base58-encodings.js';
import {
Expand Down Expand Up @@ -131,8 +132,6 @@ type AuthRequired = Types.Json.AuthRequired;
type AccountUpdateBody = Types.AccountUpdate['body'];
type Update = AccountUpdateBody['update'];

type MayUseToken = AccountUpdateBody['mayUseToken'];

type Events = BaseEvents;
const Events = {
...BaseEvents,
Expand All @@ -158,6 +157,33 @@ const Actions = {
},
};

type MayUseToken = BaseMayUseToken;
const MayUseToken = {
...BaseMayUseToken,
type: provablePure({ parentsOwnToken: Bool, inheritFromParent: Bool }),
No: {
parentsOwnToken: Bool(false),
inheritFromParent: Bool(false),
},
ParentsOwnToken: {
parentsOwnToken: Bool(true),
inheritFromParent: Bool(false),
},
InheritFromParent: {
parentsOwnToken: Bool(false),
inheritFromParent: Bool(true),
},
isNo: ({
body: {
mayUseToken: { parentsOwnToken, inheritFromParent },
},
}: AccountUpdate) => parentsOwnToken.or(inheritFromParent).not(),
isParentsOwnToken: (accountUpdate: AccountUpdate) =>
accountUpdate.body.mayUseToken.parentsOwnToken,
isInheritFromParent: (mayUseToken: AccountUpdate) =>
mayUseToken.body.mayUseToken.inheritFromParent,
};

/**
* Either set a value or keep it the same.
*/
Expand Down Expand Up @@ -675,6 +701,7 @@ class AccountUpdate implements Types.AccountUpdate {

static Actions = Actions;
static Events = Events;
static MayUseToken = MayUseToken;

constructor(body: Body, authorization?: Control);
constructor(body: Body, authorization: Control = {}, isSelf = false) {
Expand Down Expand Up @@ -1173,34 +1200,6 @@ class AccountUpdate implements Types.AccountUpdate {
return Provable.witnessAsync(combinedType, compute);
}

static get MayUseToken() {
return {
type: provablePure({ parentsOwnToken: Bool, inheritFromParent: Bool }),
No: { parentsOwnToken: Bool(false), inheritFromParent: Bool(false) },
ParentsOwnToken: {
parentsOwnToken: Bool(true),
inheritFromParent: Bool(false),
},
InheritFromParent: {
parentsOwnToken: Bool(false),
inheritFromParent: Bool(true),
},
isNo({
body: {
mayUseToken: { parentsOwnToken, inheritFromParent },
},
}: AccountUpdate) {
return parentsOwnToken.or(inheritFromParent).not();
},
isParentsOwnToken(a: AccountUpdate) {
return a.body.mayUseToken.parentsOwnToken;
},
isInheritFromParent(a: AccountUpdate) {
return a.body.mayUseToken.inheritFromParent;
},
};
}

/**
* Returns a JSON representation of only the fields that differ from the
* default {@link AccountUpdate}.
Expand Down

0 comments on commit 5a27a22

Please sign in to comment.