-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtokenWrap.ts
More file actions
228 lines (215 loc) · 9.35 KB
/
Copy pathtokenWrap.ts
File metadata and controls
228 lines (215 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* This code was AUTOGENERATED using the Codama library.
* Please DO NOT EDIT THIS FILE, instead use visitors
* to add features, then rerun Codama to update it.
*
* @see https://github.com/codama-idl/codama
*/
import {
assertIsInstructionWithAccounts,
containsBytes,
extendClient,
getU8Encoder,
SOLANA_ERROR__PROGRAM_CLIENTS__FAILED_TO_IDENTIFY_INSTRUCTION,
SOLANA_ERROR__PROGRAM_CLIENTS__UNRECOGNIZED_INSTRUCTION_TYPE,
SolanaError,
type Address,
type ClientWithRpc,
type ClientWithTransactionPlanning,
type ClientWithTransactionSending,
type ExtendedClient,
type GetAccountInfoApi,
type GetMultipleAccountsApi,
type Instruction,
type InstructionWithData,
type ReadonlyUint8Array,
} from '@solana/kit';
import {
addSelfFetchFunctions,
addSelfPlanAndSendFunctions,
type SelfFetchFunctions,
type SelfPlanAndSendFunctions,
} from '@solana/program-client-core';
import { getBackpointerCodec, type Backpointer, type BackpointerArgs } from '../accounts';
import {
getCloseStuckEscrowInstruction,
getCreateMintInstruction,
getSyncMetadataToSplTokenInstruction,
getSyncMetadataToToken2022Instruction,
getUnwrapInstruction,
getWrapInstruction,
parseCloseStuckEscrowInstruction,
parseCreateMintInstruction,
parseSyncMetadataToSplTokenInstruction,
parseSyncMetadataToToken2022Instruction,
parseUnwrapInstruction,
parseWrapInstruction,
type CloseStuckEscrowInput,
type CreateMintInput,
type ParsedCloseStuckEscrowInstruction,
type ParsedCreateMintInstruction,
type ParsedSyncMetadataToSplTokenInstruction,
type ParsedSyncMetadataToToken2022Instruction,
type ParsedUnwrapInstruction,
type ParsedWrapInstruction,
type SyncMetadataToSplTokenInput,
type SyncMetadataToToken2022Input,
type UnwrapInput,
type WrapInput,
} from '../instructions';
import { findBackpointerPda, findWrappedMintAuthorityPda, findWrappedMintPda } from '../pdas';
export const TOKEN_WRAP_PROGRAM_ADDRESS =
'TwRapQCDhWkZRrDaHfZGuHxkZ91gHDRkyuzNqeU5MgR' as Address<'TwRapQCDhWkZRrDaHfZGuHxkZ91gHDRkyuzNqeU5MgR'>;
export enum TokenWrapAccount {
Backpointer,
}
export enum TokenWrapInstruction {
CreateMint,
Wrap,
Unwrap,
CloseStuckEscrow,
SyncMetadataToToken2022,
SyncMetadataToSplToken,
}
export function identifyTokenWrapInstruction(
instruction: { data: ReadonlyUint8Array } | ReadonlyUint8Array,
): TokenWrapInstruction {
const data = 'data' in instruction ? instruction.data : instruction;
if (containsBytes(data, getU8Encoder().encode(0), 0)) {
return TokenWrapInstruction.CreateMint;
}
if (containsBytes(data, getU8Encoder().encode(1), 0)) {
return TokenWrapInstruction.Wrap;
}
if (containsBytes(data, getU8Encoder().encode(2), 0)) {
return TokenWrapInstruction.Unwrap;
}
if (containsBytes(data, getU8Encoder().encode(3), 0)) {
return TokenWrapInstruction.CloseStuckEscrow;
}
if (containsBytes(data, getU8Encoder().encode(4), 0)) {
return TokenWrapInstruction.SyncMetadataToToken2022;
}
if (containsBytes(data, getU8Encoder().encode(5), 0)) {
return TokenWrapInstruction.SyncMetadataToSplToken;
}
throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__FAILED_TO_IDENTIFY_INSTRUCTION, {
instructionData: data,
programName: 'tokenWrap',
});
}
export type ParsedTokenWrapInstruction<TProgram extends string = 'TwRapQCDhWkZRrDaHfZGuHxkZ91gHDRkyuzNqeU5MgR'> =
| ({ instructionType: TokenWrapInstruction.CreateMint } & ParsedCreateMintInstruction<TProgram>)
| ({ instructionType: TokenWrapInstruction.Wrap } & ParsedWrapInstruction<TProgram>)
| ({ instructionType: TokenWrapInstruction.Unwrap } & ParsedUnwrapInstruction<TProgram>)
| ({ instructionType: TokenWrapInstruction.CloseStuckEscrow } & ParsedCloseStuckEscrowInstruction<TProgram>)
| ({
instructionType: TokenWrapInstruction.SyncMetadataToToken2022;
} & ParsedSyncMetadataToToken2022Instruction<TProgram>)
| ({
instructionType: TokenWrapInstruction.SyncMetadataToSplToken;
} & ParsedSyncMetadataToSplTokenInstruction<TProgram>);
export function parseTokenWrapInstruction<TProgram extends string>(
instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
): ParsedTokenWrapInstruction<TProgram> {
const instructionType = identifyTokenWrapInstruction(instruction);
switch (instructionType) {
case TokenWrapInstruction.CreateMint: {
assertIsInstructionWithAccounts(instruction);
return { instructionType: TokenWrapInstruction.CreateMint, ...parseCreateMintInstruction(instruction) };
}
case TokenWrapInstruction.Wrap: {
assertIsInstructionWithAccounts(instruction);
return { instructionType: TokenWrapInstruction.Wrap, ...parseWrapInstruction(instruction) };
}
case TokenWrapInstruction.Unwrap: {
assertIsInstructionWithAccounts(instruction);
return { instructionType: TokenWrapInstruction.Unwrap, ...parseUnwrapInstruction(instruction) };
}
case TokenWrapInstruction.CloseStuckEscrow: {
assertIsInstructionWithAccounts(instruction);
return {
instructionType: TokenWrapInstruction.CloseStuckEscrow,
...parseCloseStuckEscrowInstruction(instruction),
};
}
case TokenWrapInstruction.SyncMetadataToToken2022: {
assertIsInstructionWithAccounts(instruction);
return {
instructionType: TokenWrapInstruction.SyncMetadataToToken2022,
...parseSyncMetadataToToken2022Instruction(instruction),
};
}
case TokenWrapInstruction.SyncMetadataToSplToken: {
assertIsInstructionWithAccounts(instruction);
return {
instructionType: TokenWrapInstruction.SyncMetadataToSplToken,
...parseSyncMetadataToSplTokenInstruction(instruction),
};
}
default:
throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNRECOGNIZED_INSTRUCTION_TYPE, {
instructionType: instructionType as string,
programName: 'tokenWrap',
});
}
}
export type TokenWrapPlugin = {
accounts: TokenWrapPluginAccounts;
instructions: TokenWrapPluginInstructions;
pdas: TokenWrapPluginPdas;
identifyInstruction: typeof identifyTokenWrapInstruction;
parseInstruction: typeof parseTokenWrapInstruction;
};
export type TokenWrapPluginAccounts = {
backpointer: ReturnType<typeof getBackpointerCodec> & SelfFetchFunctions<BackpointerArgs, Backpointer>;
};
export type TokenWrapPluginInstructions = {
createMint: (input: CreateMintInput) => ReturnType<typeof getCreateMintInstruction> & SelfPlanAndSendFunctions;
wrap: (input: WrapInput) => ReturnType<typeof getWrapInstruction> & SelfPlanAndSendFunctions;
unwrap: (input: UnwrapInput) => ReturnType<typeof getUnwrapInstruction> & SelfPlanAndSendFunctions;
closeStuckEscrow: (
input: CloseStuckEscrowInput,
) => ReturnType<typeof getCloseStuckEscrowInstruction> & SelfPlanAndSendFunctions;
syncMetadataToToken2022: (
input: SyncMetadataToToken2022Input,
) => ReturnType<typeof getSyncMetadataToToken2022Instruction> & SelfPlanAndSendFunctions;
syncMetadataToSplToken: (
input: SyncMetadataToSplTokenInput,
) => ReturnType<typeof getSyncMetadataToSplTokenInstruction> & SelfPlanAndSendFunctions;
};
export type TokenWrapPluginPdas = {
backpointer: typeof findBackpointerPda;
wrappedMint: typeof findWrappedMintPda;
wrappedMintAuthority: typeof findWrappedMintAuthorityPda;
};
export type TokenWrapPluginRequirements = ClientWithRpc<GetAccountInfoApi & GetMultipleAccountsApi> &
ClientWithTransactionPlanning &
ClientWithTransactionSending;
export function tokenWrapProgram() {
return <T extends TokenWrapPluginRequirements>(client: T): ExtendedClient<T, { tokenWrap: TokenWrapPlugin }> => {
return extendClient(client, {
tokenWrap: <TokenWrapPlugin>{
accounts: { backpointer: addSelfFetchFunctions(client, getBackpointerCodec()) },
instructions: {
createMint: input => addSelfPlanAndSendFunctions(client, getCreateMintInstruction(input)),
wrap: input => addSelfPlanAndSendFunctions(client, getWrapInstruction(input)),
unwrap: input => addSelfPlanAndSendFunctions(client, getUnwrapInstruction(input)),
closeStuckEscrow: input =>
addSelfPlanAndSendFunctions(client, getCloseStuckEscrowInstruction(input)),
syncMetadataToToken2022: input =>
addSelfPlanAndSendFunctions(client, getSyncMetadataToToken2022Instruction(input)),
syncMetadataToSplToken: input =>
addSelfPlanAndSendFunctions(client, getSyncMetadataToSplTokenInstruction(input)),
},
pdas: {
backpointer: findBackpointerPda,
wrappedMint: findWrappedMintPda,
wrappedMintAuthority: findWrappedMintAuthorityPda,
},
identifyInstruction: identifyTokenWrapInstruction,
parseInstruction: parseTokenWrapInstruction,
},
});
};
}