|
1 | | -import { InstructionAccountNode, instructionAccountNode, publicKeyValueNode } from '@kinobi-so/nodes'; |
| 1 | +import { |
| 2 | + KINOBI_ERROR__ANCHOR__ACCOUNT_TYPE_MISSING, |
| 3 | + KINOBI_ERROR__ANCHOR__ARGUMENT_TYPE_MISSING, |
| 4 | + KINOBI_ERROR__ANCHOR__SEED_KIND_UNIMPLEMENTED, |
| 5 | + KINOBI_ERROR__ANCHOR__TYPE_PATH_MISSING, |
| 6 | + KinobiError, |
| 7 | +} from '@kinobi-so/errors'; |
| 8 | +import { |
| 9 | + AccountNode, |
| 10 | + accountValueNode, |
| 11 | + argumentValueNode, |
| 12 | + camelCase, |
| 13 | + constantPdaSeedNodeFromBytes, |
| 14 | + InstructionAccountNode, |
| 15 | + instructionAccountNode, |
| 16 | + InstructionArgumentNode, |
| 17 | + pdaNode, |
| 18 | + PdaSeedNode, |
| 19 | + PdaSeedValueNode, |
| 20 | + pdaSeedValueNode, |
| 21 | + PdaValueNode, |
| 22 | + pdaValueNode, |
| 23 | + publicKeyTypeNode, |
| 24 | + PublicKeyValueNode, |
| 25 | + publicKeyValueNode, |
| 26 | + resolveNestedTypeNode, |
| 27 | + variablePdaSeedNode, |
| 28 | +} from '@kinobi-so/nodes'; |
2 | 29 |
|
3 | | -import { IdlV01InstructionAccount, IdlV01InstructionAccountItem } from './idl'; |
| 30 | +import { hex } from '../utils'; |
| 31 | +import { IdlV01InstructionAccount, IdlV01InstructionAccountItem, IdlV01Seed } from './idl'; |
4 | 32 |
|
5 | | -export function instructionAccountNodesFromAnchorV01(idl: IdlV01InstructionAccountItem[]): InstructionAccountNode[] { |
| 33 | +export function instructionAccountNodesFromAnchorV01( |
| 34 | + allAccounts: AccountNode[], |
| 35 | + instructionArguments: InstructionArgumentNode[], |
| 36 | + idl: IdlV01InstructionAccountItem[], |
| 37 | +): InstructionAccountNode[] { |
6 | 38 | return idl.flatMap(account => |
7 | 39 | 'accounts' in account |
8 | | - ? instructionAccountNodesFromAnchorV01(account.accounts) |
9 | | - : [instructionAccountNodeFromAnchorV01(account)], |
| 40 | + ? instructionAccountNodesFromAnchorV01(allAccounts, instructionArguments, account.accounts) |
| 41 | + : [instructionAccountNodeFromAnchorV01(allAccounts, instructionArguments, account)], |
10 | 42 | ); |
11 | 43 | } |
12 | 44 |
|
13 | | -export function instructionAccountNodeFromAnchorV01(idl: IdlV01InstructionAccount): InstructionAccountNode { |
| 45 | +export function instructionAccountNodeFromAnchorV01( |
| 46 | + allAccounts: AccountNode[], |
| 47 | + instructionArguments: InstructionArgumentNode[], |
| 48 | + idl: IdlV01InstructionAccount, |
| 49 | +): InstructionAccountNode { |
14 | 50 | const isOptional = idl.optional ?? false; |
15 | 51 | const docs = idl.docs ?? []; |
16 | 52 | const isSigner = idl.signer ?? false; |
17 | 53 | const isWritable = idl.writable ?? false; |
18 | 54 | const name = idl.name ?? ''; |
19 | | - let defaultValue = undefined; |
| 55 | + let defaultValue: PdaValueNode | PublicKeyValueNode | undefined; |
20 | 56 |
|
21 | 57 | if (idl.address) { |
22 | 58 | defaultValue = publicKeyValueNode(idl.address, name); |
| 59 | + } else if (idl.pda) { |
| 60 | + // TODO: Handle seeds with nested paths. |
| 61 | + // Currently, we gracefully ignore PDA default values if we encounter seeds with nested paths. |
| 62 | + const seedsWithNestedPaths = idl.pda.seeds.some(seed => 'path' in seed && seed.path.includes('.')); |
| 63 | + if (!seedsWithNestedPaths) { |
| 64 | + const [seeds, lookups] = idl.pda.seeds.reduce( |
| 65 | + ([seeds, lookups], seed: IdlV01Seed) => { |
| 66 | + const kind = seed.kind; |
| 67 | + |
| 68 | + switch (kind) { |
| 69 | + case 'const': |
| 70 | + return [[...seeds, constantPdaSeedNodeFromBytes('base16', hex(seed.value))], lookups]; |
| 71 | + case 'account': { |
| 72 | + const path = seed.path.split('.'); |
| 73 | + if (path.length === 1) { |
| 74 | + return [ |
| 75 | + [...seeds, variablePdaSeedNode(seed.path, publicKeyTypeNode())], |
| 76 | + [...lookups, pdaSeedValueNode(seed.path, accountValueNode(seed.path))], |
| 77 | + ]; |
| 78 | + } else if (path.length === 2) { |
| 79 | + // TODO: Handle nested account paths. |
| 80 | + // Currently, this scenario is never reached. |
| 81 | + |
| 82 | + const accountName = camelCase(seed.account ?? ''); |
| 83 | + const accountNode = allAccounts.find(({ name }) => name === accountName); |
| 84 | + if (!accountNode) { |
| 85 | + throw new KinobiError(KINOBI_ERROR__ANCHOR__ACCOUNT_TYPE_MISSING, { kind }); |
| 86 | + } |
| 87 | + |
| 88 | + const fieldName = camelCase(path[1]); |
| 89 | + const accountFields = resolveNestedTypeNode(accountNode.data).fields; |
| 90 | + const fieldNode = accountFields.find(({ name }) => name === fieldName); |
| 91 | + if (!fieldNode) { |
| 92 | + throw new KinobiError(KINOBI_ERROR__ANCHOR__TYPE_PATH_MISSING, { |
| 93 | + idlType: seed.account, |
| 94 | + path: seed.path, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + const seedName = camelCase(seed.path); |
| 99 | + return [[...seeds, variablePdaSeedNode(seedName, fieldNode.type)], []]; |
| 100 | + } else { |
| 101 | + throw new KinobiError(KINOBI_ERROR__ANCHOR__TYPE_PATH_MISSING, { |
| 102 | + idlType: seed, |
| 103 | + path: seed.path, |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + case 'arg': { |
| 108 | + const argumentNode = instructionArguments.find(({ name }) => name === seed.path); |
| 109 | + if (!argumentNode) { |
| 110 | + throw new KinobiError(KINOBI_ERROR__ANCHOR__ARGUMENT_TYPE_MISSING, { name: seed.path }); |
| 111 | + } |
| 112 | + |
| 113 | + return [ |
| 114 | + [...seeds, variablePdaSeedNode(seed.path, argumentNode.type)], |
| 115 | + [...lookups, pdaSeedValueNode(seed.path, argumentValueNode(seed.path))], |
| 116 | + ]; |
| 117 | + } |
| 118 | + default: |
| 119 | + throw new KinobiError(KINOBI_ERROR__ANCHOR__SEED_KIND_UNIMPLEMENTED, { kind }); |
| 120 | + } |
| 121 | + }, |
| 122 | + <[PdaSeedNode[], PdaSeedValueNode[]]>[[], []], |
| 123 | + ); |
| 124 | + |
| 125 | + defaultValue = pdaValueNode(pdaNode({ name, seeds }), lookups); |
| 126 | + } |
23 | 127 | } |
24 | 128 |
|
25 | 129 | return instructionAccountNode({ |
|
0 commit comments