Skip to content

Commit

Permalink
Remove Hookable
Browse files Browse the repository at this point in the history
It's flexible, but makes the standard more complex than it needs to be.
  • Loading branch information
kantp committed Mar 20, 2024
1 parent 7877c3e commit a5bb8c4
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 167 deletions.
45 changes: 0 additions & 45 deletions src/Hooks.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ export * from './interfaces';
export * from './token';
export * from './errors';
export * from './TokenAccount';
export * from './Hooks';
12 changes: 0 additions & 12 deletions src/interfaces/hookHandler/hooks.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/interfaces/hookHandler/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "reflect-metadata";
import "core-js";
export * from './admin';
export * from './hookHandler';
export * from './tokenAccount';
export * from './token';
7 changes: 0 additions & 7 deletions src/interfaces/token/hookable.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/interfaces/token/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import "reflect-metadata";
import "core-js";
export * from './adminable';
export * from './approvable';
export * from './hookable';
export * from './transferable';
export * from './upgradable';
export * from './viewable';
1 change: 0 additions & 1 deletion src/interfaces/token/viewable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface Viewable {
getTotalSupply: () => UInt64;
getCirculatingSupply: () => UInt64;
getDecimals: () => UInt64;
getHooks: () => PublicKey;
}

export default Viewable;
63 changes: 15 additions & 48 deletions src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,63 @@ import {
state,
State,
VerificationKey,
Int64,
Provable,
TokenContract,
AccountUpdateForest,
} from 'o1js';

import errors from './errors';
import {
AdminAction,
type Burnable,
type Mintable,
type Upgradable,
} from './interfaces/token/adminable';
// eslint-disable-next-line putout/putout
import type Viewable from './interfaces/token/viewable';
// eslint-disable-next-line no-duplicate-imports
import type { Transferable } from './interfaces';
import Hooks from './Hooks';
import type Hookable from './interfaces/token/hookable';
import Approvable from './interfaces/token/approvable';

class Token
extends TokenContract
implements
Approvable,
Hookable,
Mintable,
Burnable,
Viewable,
Transferable,
Upgradable
{

// eslint-disable-next-line no-warning-comments
// TODO: check how many decimals mina has by default
public static defaultDecimals = 9;

@state(PublicKey) public hooks = State<PublicKey>();

@state(PublicKey) public adminAccount = State<PublicKey>();
@state(UInt64) public totalSupply = State<UInt64>();

@state(UInt64) public circulatingSupply = State<UInt64>();

public decimals: UInt64 = UInt64.from(Token.defaultDecimals);

public getHooksContract(): Hooks {
const admin = this.getHooks();
return new Hooks(admin);
}
public decimals: UInt64 = UInt64.from(9);

public assertNotPaused(): void {
this.paused.assertEquals(this.paused.get());
this.paused.get().assertFalse(errors.tokenPaused);
}

@method
public initialize(hooks: PublicKey, totalSupply: UInt64) {
super.init();
this.account.provedState.assertEquals(Bool(false));
public initialize(adminPublicKey: PublicKey, totalSupply: UInt64) {
this.account.provedState.requireEquals(Bool(false));

this.hooks.set(hooks);
this.adminAccount.set(adminPublicKey);
this.totalSupply.set(totalSupply);
this.circulatingSupply.set(UInt64.from(0));
}

requireAdminSignature(): AccountUpdate {
const adminAccount = this.adminAccount.getAndRequireEquals();
const adminAccountUpdate = AccountUpdate.create(adminAccount);
adminAccountUpdate.requireSignature();
return adminAccountUpdate;
}
/**
* Mintable
*/

@method
public mint(address: PublicKey, amount: UInt64): AccountUpdate {
const hooksContract = this.getHooksContract();
hooksContract.canAdmin(AdminAction.fromType(AdminAction.types.mint));
this.requireAdminSignature();

const totalSupply = this.getTotalSupply();
const circulatingSupply = this.getCirculatingSupply();
Expand All @@ -104,10 +88,7 @@ class Token

@method
public setTotalSupply(amount: UInt64) {
const hooksContract = this.getHooksContract();
hooksContract.canAdmin(
AdminAction.fromType(AdminAction.types.setTotalSupply)
);
this.requireAdminSignature();

this.totalSupply.set(amount);
}
Expand All @@ -118,12 +99,8 @@ class Token

@method
public burn(from: PublicKey, amount: UInt64): AccountUpdate {
this.assertNotPaused();

const hooksContract = this.getHooksContract();
hooksContract.canAdmin(AdminAction.fromType(AdminAction.types.burn));
this.requireAdminSignature();

// eslint-disable-next-line putout/putout
return this.token.burn({ address: from, amount });
}

Expand All @@ -133,10 +110,7 @@ class Token

@method
public setVerificationKey(verificationKey: VerificationKey) {
const hooksContract = this.getHooksContract();
hooksContract.canAdmin(
AdminAction.fromType(AdminAction.types.setVerificationKey)
);
this.requireAdminSignature();

this.account.verificationKey.set(verificationKey);
}
Expand Down Expand Up @@ -180,13 +154,6 @@ class Token
return circulatingSupply;
}

public getHooks(): PublicKey {
const hooks = this.hooks.get();
this.hooks.requireEquals(hooks);

return hooks;
}

public getDecimals(): UInt64 {
return this.decimals;
}
Expand Down
56 changes: 8 additions & 48 deletions test/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import ThirdParty from '../test/ThirdParty';

import Token from '../src/token';
import TokenAccount from '../src/TokenAccount';
import Hooks from '../src/Hooks';

const proofsEnabled = false;
const enforceTransactionLimits = false;
Expand All @@ -26,12 +25,8 @@ interface Context {
senderKey: PrivateKey;
senderAccount: PublicKey;

hooksKey: PrivateKey;
hooksAccount: PublicKey;
hooks: Hooks;

directAdminKey: PrivateKey;
directAdminAccount: PublicKey;
tokenAdminKey: PrivateKey;
tokenAdminAccount: PublicKey;

tokenAKey: PrivateKey;
tokenAAccount: PublicKey;
Expand Down Expand Up @@ -69,11 +64,7 @@ describe('token integration', () => {
] = Local.testAccounts;

// Key pairs for non-Mina accounts
const {privateKey: hooksKey, publicKey: hooksAccount} =
PrivateKey.randomKeypair();
const hooks = new Hooks(hooksAccount);

const {privateKey: directAdminKey, publicKey: directAdminAccount} =
const {privateKey: tokenAdminKey, publicKey: tokenAdminAccount} =
PrivateKey.randomKeypair();

const {privateKey: tokenAKey, publicKey: tokenAAccount} =
Expand All @@ -95,7 +86,6 @@ describe('token integration', () => {
const tokenAccountA = new TokenAccount(thirdPartyAccount, tokenA.deriveTokenId());
const tokenAccountB = new TokenAccount(thirdPartyAccount, tokenB.deriveTokenId());

await Hooks.compile();
await Token.compile();

context = {
Expand All @@ -105,12 +95,8 @@ describe('token integration', () => {
senderKey,
senderAccount,

hooksKey,
hooksAccount,
hooks,

directAdminKey,
directAdminAccount,
tokenAdminKey,
tokenAdminAccount,

tokenAKey,
tokenAAccount,
Expand All @@ -137,58 +123,32 @@ describe('token integration', () => {
const totalSupply = UInt64.from(10_000_000_000_000);

describe('deploy', () => {
it('should deploy token hooks', async () => {
const tx = await Mina.transaction(context.deployerAccount, () => {
AccountUpdate.fundNewAccount(context.deployerAccount, 1);
context.hooks.deploy();
});
tx.sign([context.deployerKey, context.hooksKey]);
await tx.prove();
await tx.send();

const tx2 = await Mina.transaction(context.deployerAccount, () => {
context.hooks.initialize(context.directAdminAccount);
});
tx2.sign([context.deployerKey, context.directAdminKey]);
await tx2.prove();
await tx2.send();

});

it('should deploy token contract A', async () => {

const tx = await Mina.transaction(context.deployerAccount, () => {
AccountUpdate.fundNewAccount(context.deployerAccount, 1);
context.tokenA.deploy();
context.tokenA.initialize(context.hooksAccount, totalSupply);
context.tokenA.initialize(context.tokenAdminAccount, totalSupply);
});

tx.sign([context.deployerKey, context.tokenAKey]);

await tx.prove();
await tx.send();

expect(context.tokenA.hooks.get().toBase58()).toBe(
context.hooksAccount.toBase58()
);
});

it('should deploy token contract B', async () => {

const tx = await Mina.transaction(context.deployerAccount, () => {
AccountUpdate.fundNewAccount(context.deployerAccount, 1);
context.tokenB.deploy();
context.tokenB.initialize(context.hooksAccount, totalSupply);
context.tokenB.initialize(context.tokenAdminAccount, totalSupply);
});

tx.sign([context.deployerKey, context.tokenBKey]);

await tx.prove();
await tx.send();

expect(context.tokenB.hooks.get().toBase58()).toBe(
context.hooksAccount.toBase58()
);
});

it('should deploy a third party contract', async () => {
Expand Down Expand Up @@ -245,7 +205,7 @@ describe('token integration', () => {
context.tokenA.mint(context.senderAccount, mintAmount);
});

tx.sign([context.senderKey, context.directAdminKey]);
tx.sign([context.senderKey, context.tokenAdminKey]);

await tx.prove();
await tx.send();
Expand Down

0 comments on commit a5bb8c4

Please sign in to comment.