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