|
| 1 | +import { |
| 2 | + AccountLinkNode, |
| 3 | + AccountNode, |
| 4 | + assertIsNode, |
| 5 | + BytesEncoding, |
| 6 | + DefinedTypeLinkNode, |
| 7 | + DefinedTypeNode, |
| 8 | + InstructionArgumentLinkNode, |
| 9 | + InstructionArgumentNode, |
| 10 | + InstructionLinkNode, |
| 11 | + InstructionNode, |
| 12 | + NumberFormat, |
| 13 | + REGISTERED_TYPE_NODE_KINDS, |
| 14 | + RegisteredTypeNode, |
| 15 | + RootNode, |
| 16 | + structFieldTypeNodeFromInstructionArgumentNode, |
| 17 | + structTypeNodeFromInstructionArgumentNodes, |
| 18 | +} from '@codama/nodes'; |
| 19 | +import { |
| 20 | + getConjunctiveNodeSelectorFunction, |
| 21 | + interceptVisitor, |
| 22 | + LinkableDictionary, |
| 23 | + NodeSelector, |
| 24 | + pipe, |
| 25 | + recordLinkablesVisitor, |
| 26 | + visit, |
| 27 | + Visitor, |
| 28 | + voidVisitor, |
| 29 | +} from '@codama/visitors-core'; |
| 30 | +import { getAddressCodec } from '@solana/addresses'; |
| 31 | +import { |
| 32 | + Codec, |
| 33 | + getBase16Codec, |
| 34 | + getBase58Codec, |
| 35 | + getBase64Codec, |
| 36 | + getBooleanCodec, |
| 37 | + getBytesCodec, |
| 38 | + getF32Codec, |
| 39 | + getF64Codec, |
| 40 | + getI8Codec, |
| 41 | + getI16Codec, |
| 42 | + getI32Codec, |
| 43 | + getI64Codec, |
| 44 | + getI128Codec, |
| 45 | + getShortU16Codec, |
| 46 | + getU8Codec, |
| 47 | + getU16Codec, |
| 48 | + getU32Codec, |
| 49 | + getU64Codec, |
| 50 | + getU128Codec, |
| 51 | + getUtf8Codec, |
| 52 | + NumberCodec, |
| 53 | +} from '@solana/codecs'; |
| 54 | + |
| 55 | +type EncodableNodes = |
| 56 | + | AccountLinkNode |
| 57 | + | AccountNode |
| 58 | + | DefinedTypeLinkNode |
| 59 | + | DefinedTypeNode |
| 60 | + | InstructionArgumentLinkNode |
| 61 | + | InstructionArgumentNode |
| 62 | + | InstructionLinkNode |
| 63 | + | InstructionNode |
| 64 | + | RegisteredTypeNode; |
| 65 | + |
| 66 | +const ENCODABLE_NODE_KINDS = [ |
| 67 | + 'accountLinkNode' as const, |
| 68 | + 'accountNode' as const, |
| 69 | + 'definedTypeLinkNode' as const, |
| 70 | + 'definedTypeNode' as const, |
| 71 | + 'instructionArgumentLinkNode' as const, |
| 72 | + 'instructionArgumentNode' as const, |
| 73 | + 'instructionLinkNode' as const, |
| 74 | + 'instructionNode' as const, |
| 75 | + ...REGISTERED_TYPE_NODE_KINDS, |
| 76 | +]; |
| 77 | + |
| 78 | +export function getTypeNodeCodec(node: EncodableNodes): Codec<unknown> { |
| 79 | + return visit(node, getTypeNodeCodecVisitor(new LinkableDictionary())); |
| 80 | +} |
| 81 | + |
| 82 | +export function getNodeCodec(rootNode: RootNode, selector: NodeSelector | NodeSelector[]): Codec<unknown> { |
| 83 | + const linkables = new LinkableDictionary(); |
| 84 | + const typeNodeVisitor = getTypeNodeCodecVisitor(linkables); |
| 85 | + const isSelected = getConjunctiveNodeSelectorFunction(selector); |
| 86 | + let codec: Codec<unknown> | undefined; |
| 87 | + |
| 88 | + const visitor = pipe( |
| 89 | + voidVisitor(), |
| 90 | + v => |
| 91 | + interceptVisitor(v, (node, next) => { |
| 92 | + if (!isSelected(node, linkables.stack)) return next(node); |
| 93 | + assertIsNode(node, ENCODABLE_NODE_KINDS); |
| 94 | + codec = visit(node, typeNodeVisitor); |
| 95 | + }), |
| 96 | + v => recordLinkablesVisitor(v, linkables), |
| 97 | + ); |
| 98 | + |
| 99 | + visit(rootNode, visitor); |
| 100 | + |
| 101 | + if (!codec) { |
| 102 | + // TODO: Coded error. |
| 103 | + throw new Error('Node Selector not found'); |
| 104 | + } |
| 105 | + |
| 106 | + return codec; |
| 107 | +} |
| 108 | + |
| 109 | +export function getTypeNodeCodecVisitor( |
| 110 | + linkables: LinkableDictionary, |
| 111 | +): Visitor<Codec<unknown>, EncodableNodes['kind']> { |
| 112 | + const errorHandler = (): Codec<unknown> => { |
| 113 | + throw new Error('Not implemented'); |
| 114 | + }; |
| 115 | + return { |
| 116 | + visitAccount(node) { |
| 117 | + return visit(node.data, this); |
| 118 | + }, |
| 119 | + visitAccountLink(node) { |
| 120 | + return visit(linkables.getOrThrow(node), this); |
| 121 | + }, |
| 122 | + visitAmountType(node) { |
| 123 | + return visit(node.number, this); |
| 124 | + }, |
| 125 | + visitArrayType: errorHandler, |
| 126 | + visitBooleanType(node) { |
| 127 | + const size = visit(node.size, this) as NumberCodec; |
| 128 | + return getBooleanCodec({ size }) as Codec<unknown>; |
| 129 | + }, |
| 130 | + visitBytesType() { |
| 131 | + return getBytesCodec() as Codec<unknown>; |
| 132 | + }, |
| 133 | + visitDateTimeType(node) { |
| 134 | + return visit(node.number, this); |
| 135 | + }, |
| 136 | + visitDefinedType(node) { |
| 137 | + return visit(node.type, this); |
| 138 | + }, |
| 139 | + visitDefinedTypeLink(node) { |
| 140 | + return visit(linkables.getOrThrow(node), this); |
| 141 | + }, |
| 142 | + visitEnumType: errorHandler, |
| 143 | + visitEnumEmptyVariantType: errorHandler, |
| 144 | + visitEnumStructVariantType: errorHandler, |
| 145 | + visitEnumTupleVariantType: errorHandler, |
| 146 | + visitFixedSizeType: errorHandler, |
| 147 | + visitHiddenPrefixType: errorHandler, |
| 148 | + visitHiddenSuffixType: errorHandler, |
| 149 | + visitInstruction(node) { |
| 150 | + return visit(structTypeNodeFromInstructionArgumentNodes(node.arguments), this); |
| 151 | + }, |
| 152 | + visitInstructionArgument(node) { |
| 153 | + return visit(structFieldTypeNodeFromInstructionArgumentNode(node), this); |
| 154 | + }, |
| 155 | + visitInstructionArgumentLink(node) { |
| 156 | + return visit(linkables.getOrThrow(node), this); |
| 157 | + }, |
| 158 | + visitInstructionLink(node) { |
| 159 | + return visit(linkables.getOrThrow(node), this); |
| 160 | + }, |
| 161 | + visitMapType: errorHandler, |
| 162 | + visitNumberType(node) { |
| 163 | + return getCodecFromNumberFormat(node.format) as Codec<unknown>; |
| 164 | + }, |
| 165 | + visitOptionType: errorHandler, |
| 166 | + visitPostOffsetType: errorHandler, |
| 167 | + visitPreOffsetType: errorHandler, |
| 168 | + visitPublicKeyType() { |
| 169 | + return getAddressCodec() as Codec<unknown>; |
| 170 | + }, |
| 171 | + visitRemainderOptionType: errorHandler, |
| 172 | + visitSentinelType: errorHandler, |
| 173 | + visitSetType: errorHandler, |
| 174 | + visitSizePrefixType: errorHandler, |
| 175 | + visitSolAmountType(node) { |
| 176 | + return visit(node.number, this); |
| 177 | + }, |
| 178 | + visitStringType(node) { |
| 179 | + return getCodecFromBytesEncoding(node.encoding) as Codec<unknown>; |
| 180 | + }, |
| 181 | + visitStructType: errorHandler, |
| 182 | + visitStructFieldType: errorHandler, |
| 183 | + visitTupleType: errorHandler, |
| 184 | + visitZeroableOptionType: errorHandler, |
| 185 | + }; |
| 186 | +} |
| 187 | + |
| 188 | +function getCodecFromBytesEncoding(encoding: BytesEncoding) { |
| 189 | + switch (encoding) { |
| 190 | + case 'base16': |
| 191 | + return getBase16Codec() as Codec<unknown>; |
| 192 | + case 'base58': |
| 193 | + return getBase58Codec() as Codec<unknown>; |
| 194 | + case 'base64': |
| 195 | + return getBase64Codec() as Codec<unknown>; |
| 196 | + case 'utf8': |
| 197 | + return getUtf8Codec() as Codec<unknown>; |
| 198 | + default: |
| 199 | + // TODO: Coded error. |
| 200 | + throw new Error(`Unsupported bytes encoding: ${encoding satisfies never}`); |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +function getCodecFromNumberFormat(format: NumberFormat) { |
| 205 | + switch (format) { |
| 206 | + case 'u8': |
| 207 | + return getU8Codec(); |
| 208 | + case 'u16': |
| 209 | + return getU16Codec(); |
| 210 | + case 'u32': |
| 211 | + return getU32Codec(); |
| 212 | + case 'u64': |
| 213 | + return getU64Codec(); |
| 214 | + case 'u128': |
| 215 | + return getU128Codec(); |
| 216 | + case 'i8': |
| 217 | + return getI8Codec(); |
| 218 | + case 'i16': |
| 219 | + return getI16Codec(); |
| 220 | + case 'i32': |
| 221 | + return getI32Codec(); |
| 222 | + case 'i64': |
| 223 | + return getI64Codec(); |
| 224 | + case 'i128': |
| 225 | + return getI128Codec(); |
| 226 | + case 'f32': |
| 227 | + return getF32Codec(); |
| 228 | + case 'f64': |
| 229 | + return getF64Codec(); |
| 230 | + case 'shortU16': |
| 231 | + return getShortU16Codec(); |
| 232 | + default: |
| 233 | + // TODO: Coded error. |
| 234 | + throw new Error(`Unsupported number format: ${format satisfies never}`); |
| 235 | + } |
| 236 | +} |
0 commit comments