Skip to content

Commit 9ebd0af

Browse files
committed
Added support for decoding token init parameters
1 parent f46f14e commit 9ebd0af

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

packages/sdk/src/plt/Cbor.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { HexString } from '../types.js';
55
import { cborDecode, cborEncode } from '../types/cbor.js';
66
import { TokenHolder, TokenMetadataUrl } from './index.js';
77
import {
8+
TokenInitializationParameters,
89
TokenListUpdateEventDetails,
910
TokenModuleAccountState,
1011
TokenModuleState,
@@ -196,11 +197,50 @@ function decodeTokenPauseEventDetails(value: Cbor): TokenPauseEventDetails {
196197
return decoded as TokenPauseEventDetails;
197198
}
198199

200+
function decodeTokenInitializationParameters(value: Cbor): TokenInitializationParameters {
201+
const decoded = cborDecode(value.bytes);
202+
if (typeof decoded !== 'object' || decoded === null) {
203+
throw new Error('Invalid CBOR data for TokenInitializationParameters');
204+
}
205+
206+
// Validate required fields
207+
if (!('governanceAccount' in decoded && TokenHolder.instanceOf(decoded.governanceAccount))) {
208+
throw new Error('Invalid TokenInitializationParameters: missing or invalid governanceAccount');
209+
}
210+
if (!('metadata' in decoded)) {
211+
throw new Error('Invalid TokenInitializationParameters: missing metadataUrl');
212+
}
213+
let metadata = TokenMetadataUrl.fromCBORValue(decoded.metadata);
214+
if (!('name' in decoded && typeof decoded.name === 'string')) {
215+
throw new Error('Invalid TokenInitializationParameters: missing or invalid name');
216+
}
217+
218+
// Validate optional fields
219+
if ('allowList' in decoded && typeof decoded.allowList !== 'boolean') {
220+
throw new Error('Invalid TokenInitializationParameters: allowList must be a boolean');
221+
}
222+
if ('denyList' in decoded && typeof decoded.denyList !== 'boolean') {
223+
throw Error('Invalid TokenInitializationParameters: denyList must be a boolean');
224+
}
225+
if ('mintable' in decoded && typeof decoded.mintable !== 'boolean') {
226+
throw new Error('Invalid TokenInitializationParameters: mintable must be a boolean');
227+
}
228+
if ('burnable' in decoded && typeof decoded.burnable !== 'boolean') {
229+
throw new Error('Invalid TokenInitializationParameters: burnable must be a boolean');
230+
}
231+
if ('paused' in decoded && typeof decoded.paused !== 'boolean') {
232+
throw new Error('Invalid TokenInitializationParameters: paused must be a boolean');
233+
}
234+
235+
return { ...decoded, metadata } as TokenInitializationParameters;
236+
}
237+
199238
type DecodeTypeMap = {
200239
TokenModuleState: TokenModuleState;
201240
TokenModuleAccountState: TokenModuleAccountState;
202241
TokenListUpdateEventDetails: TokenListUpdateEventDetails;
203242
TokenPauseEventDetails: TokenPauseEventDetails;
243+
TokenInitializationParameters: TokenInitializationParameters;
204244
};
205245

206246
export function decode<T extends keyof DecodeTypeMap>(cbor: Cbor, type: T): DecodeTypeMap[T];
@@ -222,6 +262,8 @@ export function decode<T extends keyof DecodeTypeMap | undefined>(cbor: Cbor, ty
222262
return decodeTokenListUpdateEventDetails(cbor);
223263
case 'TokenPauseEventDetails':
224264
return decodeTokenPauseEventDetails(cbor);
265+
case 'TokenInitializationParameters':
266+
return decodeTokenInitializationParameters(cbor);
225267
default:
226268
return cborDecode(cbor.bytes);
227269
}

packages/sdk/test/ci/plt/Cbor.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,84 @@ describe('Cbor', () => {
8282
});
8383
});
8484

85+
describe('TokenInitializationParameters', () => {
86+
test('should encode and decode TokenInitializationParameters correctly', () => {
87+
const accountAddress = AccountAddress.fromBase58('3XSLuJcXg6xEua6iBPnWacc3iWh93yEDMCqX8FbE3RDSbEnT9P');
88+
const tokenHolder = TokenHolder.fromAccountAddress(accountAddress);
89+
const metadataUrl = TokenMetadataUrl.fromString('https://example.com/metadata.json');
90+
91+
const params = {
92+
name: 'Test Token',
93+
metadata: metadataUrl,
94+
governanceAccount: tokenHolder,
95+
allowList: true,
96+
denyList: false,
97+
mintable: true,
98+
burnable: true,
99+
customField: 'custom value',
100+
};
101+
102+
const encoded = Cbor.encode(params);
103+
const decoded = Cbor.decode(encoded, 'TokenInitializationParameters');
104+
105+
expect(decoded.name).toBe(params.name);
106+
expect(decoded.metadata).toEqual(params.metadata);
107+
expect(decoded.governanceAccount).toEqual(params.governanceAccount);
108+
expect(decoded.allowList).toBe(params.allowList);
109+
expect(decoded.denyList).toBe(params.denyList);
110+
expect(decoded.mintable).toBe(params.mintable);
111+
expect(decoded.burnable).toBe(params.burnable);
112+
});
113+
114+
test('should throw error if TokenInitializationParameters is missing required fields', () => {
115+
// Missing governanceAccount
116+
const invalidParams1 = {
117+
name: 'Test Token',
118+
metadata: TokenMetadataUrl.fromString('https://example.com/metadata.json'),
119+
// governanceAccount is missing
120+
};
121+
const encoded1 = Cbor.encode(invalidParams1);
122+
expect(() => Cbor.decode(encoded1, 'TokenInitializationParameters')).toThrow(
123+
/missing or invalid governanceAccount/
124+
);
125+
126+
// Missing name
127+
const accountAddress = AccountAddress.fromBase58('3XSLuJcXg6xEua6iBPnWacc3iWh93yEDMCqX8FbE3RDSbEnT9P');
128+
const invalidParams2 = {
129+
// name is missing
130+
metadata: TokenMetadataUrl.fromString('https://example.com/metadata.json'),
131+
governanceAccount: TokenHolder.fromAccountAddress(accountAddress),
132+
};
133+
const encoded2 = Cbor.encode(invalidParams2);
134+
expect(() => Cbor.decode(encoded2, 'TokenInitializationParameters')).toThrow(/missing or invalid name/);
135+
136+
// Missing metadata
137+
const invalidParams3 = {
138+
name: 'Test Token',
139+
// metadata is missing
140+
governanceAccount: TokenHolder.fromAccountAddress(accountAddress),
141+
};
142+
const encoded3 = Cbor.encode(invalidParams3);
143+
expect(() => Cbor.decode(encoded3, 'TokenInitializationParameters')).toThrow(/missing metadataUrl/);
144+
});
145+
146+
test('should throw error if TokenInitializationParameters has invalid field types', () => {
147+
const accountAddress = AccountAddress.fromBase58('3XSLuJcXg6xEua6iBPnWacc3iWh93yEDMCqX8FbE3RDSbEnT9P');
148+
const tokenHolder = TokenHolder.fromAccountAddress(accountAddress);
149+
const metadataUrl = TokenMetadataUrl.fromString('https://example.com/metadata.json');
150+
151+
// Invalid allowList type
152+
const invalidParams = {
153+
name: 'Test Token',
154+
metadata: metadataUrl,
155+
governanceAccount: tokenHolder,
156+
allowList: 'yes', // Should be boolean
157+
};
158+
const encoded = Cbor.encode(invalidParams);
159+
expect(() => Cbor.decode(encoded, 'TokenInitializationParameters')).toThrow(/allowList must be a boolean/);
160+
});
161+
});
162+
85163
describe('TokenModuleAccountState', () => {
86164
test('should encode and decode TokenModuleAccountState correctly', () => {
87165
const state = {

0 commit comments

Comments
 (0)