Skip to content

Commit

Permalink
Merge pull request #1731 from kantp/kantp/expose-max-account-updates
Browse files Browse the repository at this point in the history
Expose `MAX_ACCOUNT_UPDATES` from `TokenContract.ts`
  • Loading branch information
mitschabaude authored Jul 10, 2024
2 parents 7c8377d + adaa0fd commit f01b351
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/lib/mina/token/token-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import { DeployArgs, SmartContract } from '../zkapp.js';
import { TokenAccountUpdateIterator } from './forest-iterator.js';
import { tokenMethods } from './token-methods.js';

export { TokenContract };

// it's fine to have this restriction, because the protocol also has a limit of ~20
// TODO find out precise protocol limit
const MAX_ACCOUNT_UPDATES = 20;
export { TokenContract, TokenContractV2 };

/**
* Base token contract which
Expand All @@ -27,6 +23,10 @@ const MAX_ACCOUNT_UPDATES = 20;
abstract class TokenContract extends SmartContract {
// change default permissions - important that token contracts use an access permission

/** The maximum number of account updates using the token in a single
* transaction that this contract supports. */
static MAX_ACCOUNT_UPDATES = 20;

/**
* Deploys a {@link TokenContract}.
*
Expand Down Expand Up @@ -96,16 +96,16 @@ abstract class TokenContract extends SmartContract {
this.deriveTokenId()
);

// iterate through the forest and apply user-defined logc
for (let i = 0; i < MAX_ACCOUNT_UPDATES; i++) {
// iterate through the forest and apply user-defined logic
for (let i = 0; i < (this.constructor as typeof TokenContract).MAX_ACCOUNT_UPDATES; i++) {
let { accountUpdate, usesThisToken } = iterator.next();
callback(accountUpdate, usesThisToken);
}

// prove that we checked all updates
iterator.assertFinished(
`Number of account updates to approve exceed ` +
`the supported limit of ${MAX_ACCOUNT_UPDATES}.\n`
`the supported limit of ${(this.constructor as typeof TokenContract).MAX_ACCOUNT_UPDATES}.\n`
);

// skip hashing our child account updates in the method wrapper
Expand Down Expand Up @@ -179,6 +179,15 @@ abstract class TokenContract extends SmartContract {
}
}

/** Version of `TokenContract` with the precise number of `MAX_ACCOUNT_UPDATES`
*
* The value of 20 in `TokenContract` was a rough upper limit, the precise upper
* bound is 9.
*/
abstract class TokenContractV2 extends TokenContract {
static MAX_ACCOUNT_UPDATES = 9;
}

function toForest(
updates: (AccountUpdate | AccountUpdateTree)[]
): AccountUpdateForest {
Expand Down

0 comments on commit f01b351

Please sign in to comment.