Skip to content

Commit

Permalink
fix(schema): update handlers schema
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Oct 15, 2024
1 parent 7c54bd9 commit 6ec52e4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
41 changes: 38 additions & 3 deletions src/types/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const IntegerStringSchema = z
},
{ message: 'Must be a non negative integer string' },
);

export const AntRecordSchema = z.object({
transactionId: ArweaveTxIdSchema.describe('The Target ID of the undername'),
ttlSeconds: z.number(),
Expand Down Expand Up @@ -95,6 +96,37 @@ export const AntStateSchema = z.object({
});

export type AoANTState = z.infer<typeof AntStateSchema>;
export const AntHandlerNames = [
'evolve',
'_eval',
'_default',
'transfer',
'balance',
'balances',
'totalSupply',
'info',
'addController',
'removeController',
'controllers',
'setRecord',
'removeRecord',
'record',
'records',
'setName',
'setTicker',
'initializeState',
'state',
];
export const AntHandlersSchema = z
.array(z.string({ description: 'Handler Name' }))
.refine(
(antHandlers: string[]) => {
return AntHandlerNames.every((handler) => antHandlers.includes(handler));
},
{
message: 'ANT is missing required handlers',
},
);

export const AntInfoSchema = z.object({
Name: z.string().describe('The name of the ANT.'),
Expand All @@ -110,9 +142,12 @@ export const AntInfoSchema = z.object({
Denomination: IntegerStringSchema.describe(
'The number of decimal places to use for the ANT. Defaults to 0 if not set representing whole numbers.',
),
HandlerNames: z
.array(z.string({ description: 'Handler Name' }))
.describe('List of handlers for the ANT.'),
Handlers: AntHandlersSchema.optional().describe(
'List of handlers for the ANT.',
),
HandlerNames: AntHandlersSchema.optional().describe(
'Deprecated: List of handlers for the ANT. Use "Handlers" instead.',
),
});

export type AoANTInfo = z.infer<typeof AntInfoSchema>;
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/ant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, it } from 'node:test';
import { z } from 'zod';

import {
AntHandlerNames,
AntInfoSchema,
AntStateSchema,
isAoANTState,
Expand Down Expand Up @@ -66,7 +67,7 @@ describe('ANT Schemas', () => {
['Total-Supply']: '1',
Logo: stub_address,
Denomination: '0',
HandlerNames: ['handler1', 'handler2'],
Handlers: AntHandlerNames,
};
const invalidInfo = {
Name: 'TestToken',
Expand All @@ -76,7 +77,7 @@ describe('ANT Schemas', () => {
['Total-Supply']: 1000,
Logo: stub_address,
Denomination: '1',
HandlerNames: ['handler1', 'handler2'],
Handlers: AntHandlerNames,
};

assert.doesNotThrow(() => AntInfoSchema.parse(validInfo));
Expand Down

0 comments on commit 6ec52e4

Please sign in to comment.