Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Link nodes #127

Merged
merged 6 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-badgers-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': minor
---

Add Link nodes
8 changes: 4 additions & 4 deletions src/nodes/AccountDataNode.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../shared';
import { LinkTypeNode } from './typeNodes/LinkTypeNode';
import { StructTypeNode } from './typeNodes/StructTypeNode';
import { DefinedTypeLinkNode } from './linkNodes';
import { StructTypeNode } from './typeNodes';

export type AccountDataNode = {
readonly kind: 'accountDataNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
readonly link?: LinkTypeNode;
readonly link?: DefinedTypeLinkNode;
};

export type AccountDataNodeInput = {
readonly name: string;
readonly struct: StructTypeNode;
readonly link?: LinkTypeNode;
readonly link?: DefinedTypeLinkNode;
};

export function accountDataNode(input: AccountDataNodeInput): AccountDataNode {
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/InstructionDataArgsNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../shared';
import { LinkTypeNode } from './typeNodes/LinkTypeNode';
import { StructTypeNode } from './typeNodes/StructTypeNode';
import { DefinedTypeLinkNode } from './linkNodes';
import { StructTypeNode } from './typeNodes';

export type InstructionDataArgsNode = {
readonly kind: 'instructionDataArgsNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
readonly link?: LinkTypeNode;
readonly link?: DefinedTypeLinkNode;
};

export type InstructionDataArgsNodeInput = Omit<
Expand Down
6 changes: 3 additions & 3 deletions src/nodes/InstructionExtraArgsNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../shared';
import { LinkTypeNode } from './typeNodes/LinkTypeNode';
import { StructTypeNode } from './typeNodes/StructTypeNode';
import { DefinedTypeLinkNode } from './linkNodes';
import { StructTypeNode } from './typeNodes';

export type InstructionExtraArgsNode = {
readonly kind: 'instructionExtraArgsNode';
readonly name: MainCaseString;
readonly struct: StructTypeNode;
readonly link?: LinkTypeNode;
readonly link?: DefinedTypeLinkNode;
};

export type InstructionExtraArgsNodeInput = Omit<
Expand Down
2 changes: 2 additions & 0 deletions src/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { InstructionNode } from './InstructionNode';
import type { PdaNode } from './PdaNode';
import type { ProgramNode } from './ProgramNode';
import type { RootNode } from './RootNode';
import { REGISTERED_LINK_NODES } from './linkNodes';
import { REGISTERED_PDA_SEED_NODES } from './pdaSeedNodes';
import { REGISTERED_SIZE_NODES } from './sizeNodes';
import { REGISTERED_TYPE_NODES } from './typeNodes';
Expand All @@ -30,6 +31,7 @@ const REGISTERED_NODES = {
definedTypeNode: {} as DefinedTypeNode,

// Groups.
...REGISTERED_LINK_NODES,
...REGISTERED_PDA_SEED_NODES,
...REGISTERED_SIZE_NODES,
...REGISTERED_TYPE_NODES,
Expand Down
1 change: 1 addition & 0 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './PdaNode';
export * from './ProgramNode';
export * from './RootNode';

export * from './linkNodes';
export * from './pdaSeedNodes';
export * from './sizeNodes';
export * from './typeNodes';
Expand Down
18 changes: 18 additions & 0 deletions src/nodes/linkNodes/AccountLinkNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ImportFrom, MainCaseString, mainCase } from '../../shared';

export type AccountLinkNode = {
readonly kind: 'accountLinkNode';
readonly name: MainCaseString;
readonly importFrom?: ImportFrom;
};

export function accountLinkNode(
name: string,
importFrom?: ImportFrom
): AccountLinkNode {
return {
kind: 'accountLinkNode',
name: mainCase(name),
importFrom,
};
}
18 changes: 18 additions & 0 deletions src/nodes/linkNodes/DefinedTypeLinkNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ImportFrom, MainCaseString, mainCase } from '../../shared';

export type DefinedTypeLinkNode = {
readonly kind: 'definedTypeLinkNode';
readonly name: MainCaseString;
readonly importFrom?: ImportFrom;
};

export function definedTypeLinkNode(
name: string,
importFrom?: ImportFrom
): DefinedTypeLinkNode {
return {
kind: 'definedTypeLinkNode',
name: mainCase(name),
importFrom,
};
}
25 changes: 25 additions & 0 deletions src/nodes/linkNodes/LinkNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ProgramLinkNode } from './ProgramLinkNode';
import type { PdaLinkNode } from './PdaLinkNode';
import type { AccountLinkNode } from './AccountLinkNode';
import type { DefinedTypeLinkNode } from './DefinedTypeLinkNode';

// Node Group Registration.

export const REGISTERED_LINK_NODES = {
programLinkNode: {} as ProgramLinkNode,
pdaLinkNode: {} as PdaLinkNode,
accountLinkNode: {} as AccountLinkNode,
definedTypeLinkNode: {} as DefinedTypeLinkNode,
};

export const REGISTERED_LINK_NODE_KEYS = Object.keys(
REGISTERED_LINK_NODES
) as (keyof typeof REGISTERED_LINK_NODES)[];

export type RegisteredLinkNodes = typeof REGISTERED_LINK_NODES;

// Node Group Helpers.

export type LinkNode = RegisteredLinkNodes[keyof RegisteredLinkNodes];

export const LINK_NODES = REGISTERED_LINK_NODE_KEYS;
18 changes: 18 additions & 0 deletions src/nodes/linkNodes/PdaLinkNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ImportFrom, MainCaseString, mainCase } from '../../shared';

export type PdaLinkNode = {
readonly kind: 'pdaLinkNode';
readonly name: MainCaseString;
readonly importFrom?: ImportFrom;
};

export function pdaLinkNode(
name: string,
importFrom?: ImportFrom
): PdaLinkNode {
return {
kind: 'pdaLinkNode',
name: mainCase(name),
importFrom,
};
}
18 changes: 18 additions & 0 deletions src/nodes/linkNodes/ProgramLinkNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ImportFrom, MainCaseString, mainCase } from '../../shared';

export type ProgramLinkNode = {
readonly kind: 'programLinkNode';
readonly name: MainCaseString;
readonly importFrom?: ImportFrom;
};

export function programLinkNode(
name: string,
importFrom?: ImportFrom
): ProgramLinkNode {
return {
kind: 'programLinkNode',
name: mainCase(name),
importFrom,
};
}
5 changes: 5 additions & 0 deletions src/nodes/linkNodes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './AccountLinkNode';
export * from './DefinedTypeLinkNode';
export * from './LinkNode';
export * from './PdaLinkNode';
export * from './ProgramLinkNode';
14 changes: 8 additions & 6 deletions src/nodes/typeNodes/EnumVariantTypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Mutable } from '../../shared';
import type { EnumEmptyVariantTypeNode } from './EnumEmptyVariantTypeNode';
import type { EnumStructVariantTypeNode } from './EnumStructVariantTypeNode';
import type { EnumTupleVariantTypeNode } from './EnumTupleVariantTypeNode';
Expand All @@ -7,12 +8,13 @@ export type EnumVariantTypeNode =
| EnumStructVariantTypeNode
| EnumTupleVariantTypeNode;

export const ENUM_VARIANT_TYPE_NODES = [
const ENUM_VARIANT_TYPE_NODES_INTERNAL = [
'enumEmptyVariantTypeNode',
'enumStructVariantTypeNode',
'enumTupleVariantTypeNode',
] as [
'enumEmptyVariantTypeNode',
'enumStructVariantTypeNode',
'enumTupleVariantTypeNode'
];
] as const;

export const ENUM_VARIANT_TYPE_NODES =
ENUM_VARIANT_TYPE_NODES_INTERNAL as Mutable<
typeof ENUM_VARIANT_TYPE_NODES_INTERNAL
>;
23 changes: 0 additions & 23 deletions src/nodes/typeNodes/LinkTypeNode.ts

This file was deleted.

60 changes: 39 additions & 21 deletions src/nodes/typeNodes/TypeNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Mutable } from '../../shared';
import { IDL_TYPE_LEAVES, IdlType } from '../../idl';
import { RegisteredNodes } from '../Node';
import { definedTypeLinkNode } from '../linkNodes';
import { prefixedSizeNode } from '../sizeNodes';
import { AmountTypeNode } from './AmountTypeNode';
import { ArrayTypeNode, arrayTypeNodeFromIdl } from './ArrayTypeNode';
Expand All @@ -9,7 +12,6 @@ import { EnumEmptyVariantTypeNode } from './EnumEmptyVariantTypeNode';
import { EnumStructVariantTypeNode } from './EnumStructVariantTypeNode';
import { EnumTupleVariantTypeNode } from './EnumTupleVariantTypeNode';
import { EnumTypeNode, enumTypeNodeFromIdl } from './EnumTypeNode';
import { LinkTypeNode, linkTypeNode } from './LinkTypeNode';
import { MapTypeNode, mapTypeNodeFromIdl } from './MapTypeNode';
import { NumberTypeNode, numberTypeNode } from './NumberTypeNode';
import { OptionTypeNode, optionTypeNodeFromIdl } from './OptionTypeNode';
Expand All @@ -21,18 +23,15 @@ import { StructFieldTypeNode } from './StructFieldTypeNode';
import { StructTypeNode, structTypeNodeFromIdl } from './StructTypeNode';
import { TupleTypeNode, tupleTypeNodeFromIdl } from './TupleTypeNode';

// Type Node Registration.
// This only includes type nodes that can be used as standalone types.
// E.g. this excludes structFieldTypeNode, enumEmptyVariantTypeNode, etc.
// Node Group Registration.

export const STANDALONE_TYPE_NODES = {
export const REGISTERED_TYPE_NODES = {
amountTypeNode: {} as AmountTypeNode,
arrayTypeNode: {} as ArrayTypeNode,
booleanTypeNode: {} as BooleanTypeNode,
bytesTypeNode: {} as BytesTypeNode,
dateTimeTypeNode: {} as DateTimeTypeNode,
enumTypeNode: {} as EnumTypeNode,
linkTypeNode: {} as LinkTypeNode,
mapTypeNode: {} as MapTypeNode,
numberTypeNode: {} as NumberTypeNode,
optionTypeNode: {} as OptionTypeNode,
Expand All @@ -42,20 +41,6 @@ export const STANDALONE_TYPE_NODES = {
stringTypeNode: {} as StringTypeNode,
structTypeNode: {} as StructTypeNode,
tupleTypeNode: {} as TupleTypeNode,
};

export const TYPE_NODES = Object.keys(
STANDALONE_TYPE_NODES
) as (keyof typeof STANDALONE_TYPE_NODES)[];

export type TypeNode =
typeof STANDALONE_TYPE_NODES[keyof typeof STANDALONE_TYPE_NODES];

// Node Group Registration.
// This includes all type nodes.

export const REGISTERED_TYPE_NODES = {
...STANDALONE_TYPE_NODES,

// The following are not valid standalone types.
structFieldTypeNode: {} as StructFieldTypeNode,
Expand All @@ -70,6 +55,39 @@ export const REGISTERED_TYPE_NODE_KEYS = Object.keys(

export type RegisteredTypeNodes = typeof REGISTERED_TYPE_NODES;

// Type Node Helpers.
// This only includes type nodes that can be used as standalone types.
// E.g. this excludes structFieldTypeNode, enumEmptyVariantTypeNode, etc.
// It also includes the definedTypeLinkNode to compose types.

const TYPE_NODES_INTERNAL = [
// Standalone types.
'amountTypeNode',
'arrayTypeNode',
'booleanTypeNode',
'bytesTypeNode',
'dateTimeTypeNode',
'enumTypeNode',
'mapTypeNode',
'numberTypeNode',
'optionTypeNode',
'publicKeyTypeNode',
'setTypeNode',
'solAmountTypeNode',
'stringTypeNode',
'structTypeNode',
'tupleTypeNode',

// Link types.
'definedTypeLinkNode',
] as const;

export const TYPE_NODES = TYPE_NODES_INTERNAL as Mutable<
typeof TYPE_NODES_INTERNAL
>;

export type TypeNode = RegisteredNodes[typeof TYPE_NODES[number]];

// Node Group Helpers.

function isArrayOfSize(array: any, size: number): boolean {
Expand Down Expand Up @@ -104,7 +122,7 @@ export const createTypeNodeFromIdl = (idlType: IdlType): TypeNode => {

// Defined link.
if ('defined' in idlType && typeof idlType.defined === 'string') {
return linkTypeNode(idlType.defined);
return definedTypeLinkNode(idlType.defined);
}

// Enum.
Expand Down
1 change: 0 additions & 1 deletion src/nodes/typeNodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export * from './EnumStructVariantTypeNode';
export * from './EnumTupleVariantTypeNode';
export * from './EnumTypeNode';
export * from './EnumVariantTypeNode';
export * from './LinkTypeNode';
export * from './MapTypeNode';
export * from './NumberTypeNode';
export * from './OptionTypeNode';
Expand Down
5 changes: 4 additions & 1 deletion src/renderers/js-experimental/fragments/accountPdaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { getValueNodeFragment } from './valueNode';
export function getAccountPdaHelpersFragment(scope: {
accountNode: AccountNode;
programNode: ProgramNode;
typeManifestVisitor: Visitor<TypeManifest, keyof RegisteredTypeNodes>;
typeManifestVisitor: Visitor<
TypeManifest,
keyof RegisteredTypeNodes | 'definedTypeLinkNode'
>;
nameApi: NameApi;
}): Fragment {
const { accountNode, programNode, typeManifestVisitor, nameApi } = scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export function getInstructionBytesCreatedOnChainFragment(scope: {

if (bytes?.kind === 'account') {
const functionName = scope.nameApi.accountGetSizeFunction(bytes.name);
const importFrom =
bytes.importFrom === 'generated' ? 'generatedAccounts' : bytes.importFrom;
const importFrom = bytes.importFrom ?? 'generatedAccounts';
bytesFragment.addImports(importFrom, functionName);
} else if (bytes?.kind === 'resolver') {
const functionName = scope.nameApi.resolverFunction(bytes.name);
Expand Down
Loading