Skip to content

Commit

Permalink
Add version and standard field to RootNode (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Apr 11, 2024
1 parent 0463a43 commit 5ec114f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/chilly-schools-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@metaplex-foundation/kinobi": minor
---

Add version and standard field to RootNode
17 changes: 17 additions & 0 deletions src/Kinobi.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
assertIsNode,
IdlInputs,
KinobiVersion,
Node,
rootNode,
RootNode,
rootNodeFromIdls,
} from './nodes';
import { KinobiError } from './shared';
import { defaultVisitor, visit, Visitor } from './visitors';

export interface Kinobi {
Expand All @@ -21,6 +23,7 @@ export function createFromRoot(
useDefaultVisitor = true
): Kinobi {
let currentRoot = root;
validateKinobiVersion(currentRoot.version);
if (useDefaultVisitor) {
currentRoot = visit(currentRoot, defaultVisitor());
}
Expand Down Expand Up @@ -58,3 +61,17 @@ export function createFromJson(
): Kinobi {
return createFromRoot(JSON.parse(json) as RootNode, useDefaultVisitor);
}

function validateKinobiVersion(rootVersion: KinobiVersion): void {
// TODO: Replace with __VERSION__ variable when available.
const kinobiVersion = '0.19.0';
if (rootVersion === kinobiVersion) return;
const [rootMajor, rootMinor] = rootVersion.split('.').map(Number);
const [KinobiMajor, KinobiMinor] = kinobiVersion.split('.').map(Number);
const isZeroMajor = rootMajor === 0 && KinobiMajor === 0;
if (isZeroMajor && rootMinor === KinobiMinor) return;
if (rootMajor === KinobiMajor) return;
throw new KinobiError(
`The provided IDL version [${rootVersion}] is not compatible with the installed Kinobi version [${kinobiVersion}]`
);
}
13 changes: 12 additions & 1 deletion src/nodes/RootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,29 @@ import { PdaNode } from './PdaNode';
import { ProgramNode, programNodeFromIdl } from './ProgramNode';

export type IdlInputs = string | Partial<Idl> | (string | Partial<Idl>)[];
export type KinobiVersion = `${number}.${number}.${number}`;

export interface RootNode<TPrograms extends ProgramNode[] = ProgramNode[]> {
readonly kind: 'rootNode';

// Children.
readonly programs: TPrograms;

// Data.
readonly standard: 'kinobi';
readonly version: KinobiVersion;
}

export function rootNode<const TPrograms extends ProgramNode[]>(
programs: TPrograms
): RootNode<TPrograms> {
return { kind: 'rootNode', programs };
return {
kind: 'rootNode',
programs,
standard: 'kinobi',
// TODO: Replace with __VERSION__ variable when available.
version: '0.19.0',
};
}

export function rootNodeFromIdls(idls: IdlInputs): RootNode {
Expand Down

0 comments on commit 5ec114f

Please sign in to comment.