Skip to content

Commit

Permalink
Add PdaNode (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva authored Dec 31, 2023
1 parent 22ce9ee commit fc32421
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-yaks-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Add PdaNode
2 changes: 2 additions & 0 deletions src/nodes/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { InstructionAccountNode } from './InstructionAccountNode';
import type { InstructionDataArgsNode } from './InstructionDataArgsNode';
import type { InstructionExtraArgsNode } from './InstructionExtraArgsNode';
import type { InstructionNode } from './InstructionNode';
import type { PdaNode } from './PdaNode';
import type { ProgramNode } from './ProgramNode';
import type { RootNode } from './RootNode';
import { REGISTERED_PDA_SEED_NODES } from './pdaSeedNodes';
Expand All @@ -18,6 +19,7 @@ import { REGISTERED_VALUE_NODES } from './valueNodes';
const REGISTERED_NODES = {
rootNode: {} as RootNode,
programNode: {} as ProgramNode,
pdaNode: {} as PdaNode,
accountNode: {} as AccountNode,
accountDataNode: {} as AccountDataNode,
instructionNode: {} as InstructionNode,
Expand Down
15 changes: 15 additions & 0 deletions src/nodes/PdaNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InvalidKinobiTreeError, MainCaseString, mainCase } from '../shared';
import { PdaSeedNode } from './pdaSeedNodes';

export type PdaNode = {
readonly kind: 'pdaNode';
readonly name: MainCaseString;
readonly seeds: PdaSeedNode[];
};

export function pdaNode(name: string, seeds: PdaSeedNode[]): PdaNode {
if (!name) {
throw new InvalidKinobiTreeError('PdaNode must have a name.');
}
return { kind: 'pdaNode', name: mainCase(name), seeds };
}
3 changes: 3 additions & 0 deletions src/nodes/ProgramNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { AccountNode, accountNodeFromIdl } from './AccountNode';
import { DefinedTypeNode, definedTypeNodeFromIdl } from './DefinedTypeNode';
import { ErrorNode, errorNodeFromIdl } from './ErrorNode';
import { InstructionNode, instructionNodeFromIdl } from './InstructionNode';
import { PdaNode } from './PdaNode';

export type ProgramNode = {
readonly kind: 'programNode';
readonly pdas: PdaNode[];
readonly accounts: AccountNode[];
readonly instructions: InstructionNode[];
readonly definedTypes: DefinedTypeNode[];
Expand Down Expand Up @@ -38,6 +40,7 @@ export type ProgramNodeInput = Omit<
export function programNode(input: ProgramNodeInput): ProgramNode {
return {
kind: 'programNode',
pdas: input.pdas ?? [],
accounts: input.accounts,
instructions: input.instructions,
definedTypes: input.definedTypes,
Expand Down
1 change: 1 addition & 0 deletions src/nodes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './InstructionDataArgsNode';
export * from './InstructionExtraArgsNode';
export * from './InstructionNode';
export * from './Node';
export * from './PdaNode';
export * from './ProgramNode';
export * from './RootNode';

Expand Down
15 changes: 15 additions & 0 deletions src/visitors/identityVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
mapTypeNode,
mapValueNode,
optionTypeNode,
pdaNode,
prefixedSizeNode,
programNode,
removeNullAndAssertIsNodeFilter,
Expand Down Expand Up @@ -76,6 +77,9 @@ export function identityVisitor<
visitor.visitProgram = function visitProgram(node) {
return programNode({
...node,
pdas: node.pdas
.map((account) => visit(this)(account))
.filter(removeNullAndAssertIsNodeFilter('pdaNode')),
accounts: node.accounts
.map((account) => visit(this)(account))
.filter(removeNullAndAssertIsNodeFilter('accountNode')),
Expand All @@ -92,6 +96,17 @@ export function identityVisitor<
};
}

if (castedNodeKeys.includes('pdaNode')) {
visitor.visitPda = function visitPda(node) {
return pdaNode(
node.name,
node.seeds
.map((type) => visit(this)(type))
.filter(removeNullAndAssertIsNodeFilter(PDA_SEED_NODES))
);
};
}

if (castedNodeKeys.includes('accountNode')) {
visitor.visitAccount = function visitAccount(node) {
const data = visit(this)(node.data);
Expand Down
7 changes: 7 additions & 0 deletions src/visitors/mergeVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function mergeVisitor<
if (castedNodeKeys.includes('programNode')) {
visitor.visitProgram = function visitProgram(node) {
return merge(node, [
...node.pdas.flatMap(visit(this)),
...node.accounts.flatMap(visit(this)),
...node.instructions.flatMap(visit(this)),
...node.definedTypes.flatMap(visit(this)),
Expand All @@ -34,6 +35,12 @@ export function mergeVisitor<
};
}

if (castedNodeKeys.includes('pdaNode')) {
visitor.visitPda = function visitPda(node) {
return merge(node, node.seeds.flatMap(visit(this)));
};
}

if (castedNodeKeys.includes('accountNode')) {
visitor.visitAccount = function visitAccount(node) {
return merge(node, [
Expand Down

0 comments on commit fc32421

Please sign in to comment.