Skip to content

Commit

Permalink
using numeric version to represent v5 & refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
1cedrus committed Jul 23, 2024
1 parent 20a180d commit 5269d64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 12 additions & 7 deletions packages/contracts/src/TypinkRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AccountId32, Bytes, TypeRegistry } from '@dedot/codecs';
import * as $ from '@dedot/shape';
import { IEventRecord, IRuntimeEvent } from '@dedot/types';
import { assert, hexToU8a, stringCamelCase, stringPascalCase } from '@dedot/utils';
import { DedotError, assert, hexToU8a, stringCamelCase, stringPascalCase } from '@dedot/utils';
import { ContractEvent, ContractEventMeta, ContractMetadata } from './types/index.js';
import { extractContractTypes } from './utils.js';

Expand Down Expand Up @@ -30,11 +30,15 @@ export class TypinkRegistry extends TypeRegistry {
}

decodeEvent(eventRecord: IEventRecord): ContractEvent {
if (this.#metadata.version === '4') {
return this.#decodeEventV4(eventRecord);
} else {
// Latest version
return this.#decodeEventV5(eventRecord);
const { version } = this.#metadata;

switch (version) {
case 5:
return this.#decodeEventV5(eventRecord);
case '4':
return this.#decodeEventV4(eventRecord);
default:
throw new DedotError('Unsupported metadata version!');
}
}

Expand All @@ -44,6 +48,7 @@ export class TypinkRegistry extends TypeRegistry {
}

#decodeEventV4(eventRecord: IEventRecord): ContractEvent {
assert(this.#metadata.version === '4', 'Invalid metadata version!');
assert(this.#isContractEmittedEvent(eventRecord.event), 'Event Record is not valid!');

const data = hexToU8a(eventRecord.event.palletEvent.data.data);
Expand All @@ -57,7 +62,7 @@ export class TypinkRegistry extends TypeRegistry {
}

#decodeEventV5(eventRecord: IEventRecord): ContractEvent {
assert(this.#metadata.version == '5', 'Invalid metadata version!');
assert(this.#metadata.version === 5, 'Invalid metadata version!');
assert(this.#isContractEmittedEvent(eventRecord.event), 'Event Record is not valid!');

const data = hexToU8a(eventRecord.event.palletEvent.data.data);
Expand Down
5 changes: 2 additions & 3 deletions packages/contracts/src/types/v5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export interface ContractMetadataV5 {
spec: ContractSpecV5;
storage: ContractStorage;
types: ContractType[];
// This is a numberic field in v5 metadata, but it is a string in v4 metadata
// TODO: Verify this!
version: '5';
// This is a numberic field in v5 metadata, but it is a string in v4 metadata
version: 5;
}

export interface ContractSpecV5 extends ContractSpecV4 {
Expand Down

0 comments on commit 5269d64

Please sign in to comment.