@omnipair/dusk-sdk 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/README.md +170 -0
  2. package/dist/address.d.ts +7 -0
  3. package/dist/address.d.ts.map +1 -0
  4. package/dist/address.js +29 -0
  5. package/dist/address.js.map +1 -0
  6. package/dist/constants.d.ts +132 -0
  7. package/dist/constants.d.ts.map +1 -0
  8. package/dist/constants.js +330 -0
  9. package/dist/constants.js.map +1 -0
  10. package/dist/dusk.d.ts +24 -0
  11. package/dist/dusk.d.ts.map +1 -0
  12. package/dist/dusk.js +26 -0
  13. package/dist/dusk.js.map +1 -0
  14. package/dist/get.d.ts +340 -0
  15. package/dist/get.d.ts.map +1 -0
  16. package/dist/get.js +152 -0
  17. package/dist/get.js.map +1 -0
  18. package/dist/idl_v2.d.ts +3 -0
  19. package/dist/idl_v2.d.ts.map +1 -0
  20. package/dist/idl_v2.js +3 -0
  21. package/dist/idl_v2.js.map +1 -0
  22. package/dist/idl_v2.json +12630 -0
  23. package/dist/index.d.ts +14 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +15 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/indexer.d.ts +122 -0
  28. package/dist/indexer.d.ts.map +1 -0
  29. package/dist/indexer.js +203 -0
  30. package/dist/indexer.js.map +1 -0
  31. package/dist/preview.d.ts +39 -0
  32. package/dist/preview.d.ts.map +1 -0
  33. package/dist/preview.js +68 -0
  34. package/dist/preview.js.map +1 -0
  35. package/dist/program.d.ts +19 -0
  36. package/dist/program.d.ts.map +1 -0
  37. package/dist/program.js +29 -0
  38. package/dist/program.js.map +1 -0
  39. package/dist/referral.d.ts +51 -0
  40. package/dist/referral.d.ts.map +1 -0
  41. package/dist/referral.js +80 -0
  42. package/dist/referral.js.map +1 -0
  43. package/dist/type-aliases.d.ts +32 -0
  44. package/dist/type-aliases.d.ts.map +1 -0
  45. package/dist/type-aliases.js +2 -0
  46. package/dist/type-aliases.js.map +1 -0
  47. package/dist/types_v2.d.ts +12637 -0
  48. package/dist/types_v2.d.ts.map +1 -0
  49. package/dist/types_v2.js +2 -0
  50. package/dist/types_v2.js.map +1 -0
  51. package/dist/write.d.ts +61 -0
  52. package/dist/write.d.ts.map +1 -0
  53. package/dist/write.js +129 -0
  54. package/dist/write.js.map +1 -0
  55. package/package.json +76 -0
  56. package/src/address.ts +38 -0
  57. package/src/constants.ts +562 -0
  58. package/src/dusk.ts +43 -0
  59. package/src/get.ts +279 -0
  60. package/src/idl_v2.json +12630 -0
  61. package/src/idl_v2.ts +3 -0
  62. package/src/index.ts +18 -0
  63. package/src/indexer.ts +424 -0
  64. package/src/preview.ts +139 -0
  65. package/src/program.ts +54 -0
  66. package/src/referral.ts +172 -0
  67. package/src/type-aliases.ts +33 -0
  68. package/src/types_v2.ts +12636 -0
  69. package/src/write.ts +219 -0
package/src/write.ts ADDED
@@ -0,0 +1,219 @@
1
+ import type { Program } from "@coral-xyz/anchor";
2
+ import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from "@solana/spl-token";
3
+ import { SystemProgram, Transaction, type AccountMeta, type TransactionInstruction } from "@solana/web3.js";
4
+
5
+ import { address, normalizeAccountKeys, type AddressLike } from "./address.js";
6
+ import { deriveReferralProfileAddress } from "./constants.js";
7
+ import {
8
+ buildReferralVaultSetupInstruction,
9
+ resolveTransferHookAccountMetas,
10
+ type TransferHookTransfer,
11
+ } from "./referral.js";
12
+ import type { Dusk } from "./types_v2.js";
13
+
14
+ export type DuskInstructionName = Dusk["instructions"][number]["name"];
15
+ export type DuskInstructionArgs = unknown[] | unknown | undefined;
16
+ export type DuskAccounts = Record<string, unknown>;
17
+
18
+ export interface DuskBuildOptions {
19
+ accounts?: DuskAccounts;
20
+ remainingAccounts?: AccountMeta[];
21
+ }
22
+
23
+ export type ReferredActionName = "borrow" | "openLeverage" | "increaseLeverage";
24
+
25
+ export interface ReferredActionOptions extends DuskBuildOptions {
26
+ accounts: DuskAccounts;
27
+ payer: AddressLike;
28
+ referrer: AddressLike;
29
+ debtMint: AddressLike;
30
+ maxAcceptableReferralFeeBps: number;
31
+ transferHookTransfers?: readonly TransferHookTransfer[];
32
+ }
33
+
34
+ export interface ReferredActionBuild {
35
+ referralProfile: ReturnType<typeof deriveReferralProfileAddress>[0];
36
+ referralVault: ReturnType<typeof deriveReferralProfileAddress>[0];
37
+ setupInstruction: TransactionInstruction;
38
+ actionInstruction: TransactionInstruction;
39
+ transaction: Transaction;
40
+ }
41
+
42
+ type AnchorMethodBuilder = {
43
+ accounts(accounts: DuskAccounts): AnchorMethodBuilder;
44
+ remainingAccounts(accounts: AccountMeta[]): AnchorMethodBuilder;
45
+ instruction(): Promise<TransactionInstruction>;
46
+ transaction(): Promise<Transaction>;
47
+ rpc(): Promise<string>;
48
+ };
49
+
50
+ type AnchorMethods = Record<string, (...args: unknown[]) => AnchorMethodBuilder>;
51
+
52
+ export class DuskWrite {
53
+ constructor(readonly program: Program<Dusk>) {}
54
+
55
+ method(name: DuskInstructionName, args?: DuskInstructionArgs): AnchorMethodBuilder {
56
+ const method = (this.program.methods as unknown as AnchorMethods)[name];
57
+ if (!method) {
58
+ throw new Error(`Unknown Dusk instruction: ${name}`);
59
+ }
60
+ return method(...normalizeArgs(args));
61
+ }
62
+
63
+ builder(
64
+ name: DuskInstructionName,
65
+ args?: DuskInstructionArgs,
66
+ options: DuskBuildOptions = {}
67
+ ): AnchorMethodBuilder {
68
+ let builder = this.method(name, args);
69
+ if (options.accounts) {
70
+ builder = builder.accounts(normalizeAccountKeys(options.accounts));
71
+ }
72
+ if (options.remainingAccounts?.length) {
73
+ builder = builder.remainingAccounts(options.remainingAccounts);
74
+ }
75
+ return builder;
76
+ }
77
+
78
+ instruction(
79
+ name: DuskInstructionName,
80
+ args?: DuskInstructionArgs,
81
+ options?: DuskBuildOptions
82
+ ): Promise<TransactionInstruction> {
83
+ return this.builder(name, args, options).instruction();
84
+ }
85
+
86
+ transaction(
87
+ name: DuskInstructionName,
88
+ args?: DuskInstructionArgs,
89
+ options?: DuskBuildOptions
90
+ ): Promise<Transaction> {
91
+ return this.builder(name, args, options).transaction();
92
+ }
93
+
94
+ rpc(name: DuskInstructionName, args?: DuskInstructionArgs, options?: DuskBuildOptions) {
95
+ return this.builder(name, args, options).rpc();
96
+ }
97
+
98
+ async referredAction(
99
+ name: ReferredActionName,
100
+ args: Record<string, unknown>,
101
+ options: ReferredActionOptions
102
+ ): Promise<ReferredActionBuild> {
103
+ const setup = await buildReferralVaultSetupInstruction({
104
+ connection: this.program.provider.connection,
105
+ payer: options.payer,
106
+ referrer: options.referrer,
107
+ mint: options.debtMint,
108
+ });
109
+ const hookAccounts = options.transferHookTransfers?.length
110
+ ? await resolveTransferHookAccountMetas(
111
+ this.program.provider.connection,
112
+ options.transferHookTransfers
113
+ )
114
+ : [];
115
+ const actionInstruction = await this.instruction(
116
+ name,
117
+ {
118
+ ...args,
119
+ referrer: address(options.referrer),
120
+ maxAcceptableReferralFeeBps: options.maxAcceptableReferralFeeBps,
121
+ },
122
+ {
123
+ accounts: {
124
+ ...options.accounts,
125
+ referralProfile: setup.referralProfile,
126
+ referralVault: setup.referralVault,
127
+ },
128
+ remainingAccounts: mergeAccountMetas(options.remainingAccounts ?? [], hookAccounts),
129
+ }
130
+ );
131
+ return {
132
+ referralProfile: setup.referralProfile,
133
+ referralVault: setup.referralVault,
134
+ setupInstruction: setup.instruction,
135
+ actionInstruction,
136
+ transaction: new Transaction().add(setup.instruction, actionInstruction),
137
+ };
138
+ }
139
+
140
+ referredBorrow(args: Record<string, unknown>, options: ReferredActionOptions) {
141
+ return this.referredAction("borrow", args, options);
142
+ }
143
+
144
+ referredOpenLeverage(args: Record<string, unknown>, options: ReferredActionOptions) {
145
+ return this.referredAction("openLeverage", args, options);
146
+ }
147
+
148
+ referredIncreaseLeverage(args: Record<string, unknown>, options: ReferredActionOptions) {
149
+ return this.referredAction("increaseLeverage", args, options);
150
+ }
151
+
152
+ async setReferralRecipientInstruction(params: {
153
+ authority: AddressLike;
154
+ recipient: AddressLike;
155
+ }): Promise<TransactionInstruction> {
156
+ const authority = address(params.authority);
157
+ return this.instruction(
158
+ "setReferralRecipient",
159
+ { recipient: address(params.recipient) },
160
+ {
161
+ accounts: {
162
+ authority,
163
+ referralProfile: deriveReferralProfileAddress(authority)[0],
164
+ systemProgram: SystemProgram.programId,
165
+ },
166
+ }
167
+ );
168
+ }
169
+
170
+ async claimReferralFeesInstruction(params: {
171
+ authority: AddressLike;
172
+ mint: AddressLike;
173
+ recipientTokenAccount: AddressLike;
174
+ remainingAccounts?: AccountMeta[];
175
+ }): Promise<TransactionInstruction> {
176
+ const authority = address(params.authority);
177
+ const setup = await buildReferralVaultSetupInstruction({
178
+ connection: this.program.provider.connection,
179
+ payer: authority,
180
+ referrer: authority,
181
+ mint: params.mint,
182
+ });
183
+ return this.instruction("claimReferralFees", undefined, {
184
+ accounts: {
185
+ authority,
186
+ referralProfile: setup.referralProfile,
187
+ assetMint: address(params.mint),
188
+ referralVault: setup.referralVault,
189
+ recipientTokenAccount: address(params.recipientTokenAccount),
190
+ tokenProgram: TOKEN_PROGRAM_ID,
191
+ token2022Program: TOKEN_2022_PROGRAM_ID,
192
+ },
193
+ remainingAccounts: params.remainingAccounts,
194
+ });
195
+ }
196
+ }
197
+
198
+ function normalizeArgs(args: DuskInstructionArgs): unknown[] {
199
+ if (args === undefined) {
200
+ return [];
201
+ }
202
+ return Array.isArray(args) ? args : [args];
203
+ }
204
+
205
+ function mergeAccountMetas(...groups: readonly AccountMeta[][]): AccountMeta[] {
206
+ const merged = new Map<string, AccountMeta>();
207
+ for (const group of groups) {
208
+ for (const meta of group) {
209
+ const key = meta.pubkey.toBase58();
210
+ const current = merged.get(key);
211
+ merged.set(key, {
212
+ pubkey: meta.pubkey,
213
+ isSigner: Boolean(current?.isSigner || meta.isSigner),
214
+ isWritable: Boolean(current?.isWritable || meta.isWritable),
215
+ });
216
+ }
217
+ }
218
+ return [...merged.values()];
219
+ }