@picon-finance/dlmm-sdk 1.2.0 → 1.4.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 (221) hide show
  1. package/README.md +115 -16
  2. package/dist/entities/admin_config.d.ts +23 -0
  3. package/dist/entities/admin_config.d.ts.map +1 -0
  4. package/dist/entities/admin_config.js +54 -0
  5. package/dist/entities/bin_array.d.ts +37 -0
  6. package/dist/entities/bin_array.d.ts.map +1 -0
  7. package/dist/entities/bin_array.js +63 -0
  8. package/dist/entities/dlmm_program.d.ts +50 -0
  9. package/dist/entities/dlmm_program.d.ts.map +1 -0
  10. package/dist/entities/dlmm_program.js +73 -0
  11. package/dist/entities/index.d.ts +7 -0
  12. package/dist/entities/index.d.ts.map +1 -0
  13. package/dist/entities/index.js +6 -0
  14. package/dist/entities/pool.d.ts +71 -0
  15. package/dist/entities/pool.d.ts.map +1 -0
  16. package/dist/entities/pool.js +189 -0
  17. package/dist/entities/position.d.ts +55 -0
  18. package/dist/entities/position.d.ts.map +1 -0
  19. package/dist/entities/position.js +277 -0
  20. package/dist/entities/position_pool.d.ts +7 -0
  21. package/dist/entities/position_pool.d.ts.map +1 -0
  22. package/dist/entities/position_pool.js +30 -0
  23. package/dist/entities/types.d.ts +5 -0
  24. package/dist/entities/types.d.ts.map +1 -0
  25. package/dist/entities/types.js +0 -0
  26. package/dist/events.d.ts +9 -1
  27. package/dist/events.d.ts.map +1 -1
  28. package/dist/events.js +48 -0
  29. package/dist/index.d.ts +2 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +1 -1
  32. package/dist/program_addresses.d.ts.map +1 -1
  33. package/dist/program_addresses.js +0 -5
  34. package/dist/utils/assert.d.ts +9 -0
  35. package/dist/utils/assert.d.ts.map +1 -0
  36. package/dist/utils/assert.js +10 -0
  37. package/dist/utils/bin_location.d.ts +4 -0
  38. package/dist/utils/bin_location.d.ts.map +1 -1
  39. package/dist/utils/bin_location.js +0 -12
  40. package/dist/utils/chunk.d.ts +1 -0
  41. package/dist/utils/chunk.d.ts.map +1 -1
  42. package/dist/utils/chunk.js +4 -0
  43. package/dist/utils/type_guards.d.ts +2 -0
  44. package/dist/utils/type_guards.d.ts.map +1 -1
  45. package/dist/utils/type_guards.js +7 -0
  46. package/package.json +1 -2
  47. package/src/entities/admin_config.ts +122 -0
  48. package/src/entities/bin_array.ts +157 -0
  49. package/src/entities/dlmm_program.ts +210 -0
  50. package/src/entities/index.ts +6 -0
  51. package/src/entities/pool.ts +392 -0
  52. package/src/entities/position.ts +481 -0
  53. package/src/entities/position_pool.ts +68 -0
  54. package/src/entities/types.ts +3 -0
  55. package/src/events.ts +77 -0
  56. package/src/index.ts +2 -1
  57. package/src/program_addresses.ts +0 -6
  58. package/src/utils/assert.ts +25 -0
  59. package/src/utils/bin_location.ts +5 -12
  60. package/src/utils/chunk.ts +9 -0
  61. package/src/utils/type_guards.ts +12 -0
  62. package/dist/actions/accept_admin_authority.d.ts +0 -13
  63. package/dist/actions/accept_admin_authority.d.ts.map +0 -1
  64. package/dist/actions/accept_admin_authority.js +0 -7
  65. package/dist/actions/claim_fee.d.ts +0 -21
  66. package/dist/actions/claim_fee.d.ts.map +0 -1
  67. package/dist/actions/claim_fee.js +0 -40
  68. package/dist/actions/claim_multiple_fees.d.ts +0 -19
  69. package/dist/actions/claim_multiple_fees.d.ts.map +0 -1
  70. package/dist/actions/claim_multiple_fees.js +0 -81
  71. package/dist/actions/claim_protocol_fee.d.ts +0 -18
  72. package/dist/actions/claim_protocol_fee.d.ts.map +0 -1
  73. package/dist/actions/claim_protocol_fee.js +0 -33
  74. package/dist/actions/close_position.d.ts +0 -14
  75. package/dist/actions/close_position.d.ts.map +0 -1
  76. package/dist/actions/close_position.js +0 -14
  77. package/dist/actions/create_admin_config.d.ts +0 -15
  78. package/dist/actions/create_admin_config.d.ts.map +0 -1
  79. package/dist/actions/create_admin_config.js +0 -30
  80. package/dist/actions/create_bin_array.d.ts +0 -16
  81. package/dist/actions/create_bin_array.d.ts.map +0 -1
  82. package/dist/actions/create_bin_array.js +0 -10
  83. package/dist/actions/create_pool.d.ts +0 -21
  84. package/dist/actions/create_pool.d.ts.map +0 -1
  85. package/dist/actions/create_pool.js +0 -22
  86. package/dist/actions/delete_bin_array.d.ts +0 -16
  87. package/dist/actions/delete_bin_array.d.ts.map +0 -1
  88. package/dist/actions/delete_bin_array.js +0 -13
  89. package/dist/actions/deposit_by_weight.d.ts +0 -27
  90. package/dist/actions/deposit_by_weight.d.ts.map +0 -1
  91. package/dist/actions/deposit_by_weight.js +0 -52
  92. package/dist/actions/get_admin_config.d.ts +0 -4
  93. package/dist/actions/get_admin_config.d.ts.map +0 -1
  94. package/dist/actions/get_admin_config.js +0 -7
  95. package/dist/actions/get_bin_arrays_by_indices.d.ts +0 -5
  96. package/dist/actions/get_bin_arrays_by_indices.d.ts.map +0 -1
  97. package/dist/actions/get_bin_arrays_by_indices.js +0 -6
  98. package/dist/actions/get_pool.d.ts +0 -4
  99. package/dist/actions/get_pool.d.ts.map +0 -1
  100. package/dist/actions/get_pool.js +0 -5
  101. package/dist/actions/get_position.d.ts +0 -4
  102. package/dist/actions/get_position.d.ts.map +0 -1
  103. package/dist/actions/get_position.js +0 -5
  104. package/dist/actions/get_position_by_position_mint.d.ts +0 -4
  105. package/dist/actions/get_position_by_position_mint.d.ts.map +0 -1
  106. package/dist/actions/get_position_by_position_mint.js +0 -6
  107. package/dist/actions/get_positions_by_owner.d.ts +0 -5
  108. package/dist/actions/get_positions_by_owner.d.ts.map +0 -1
  109. package/dist/actions/get_positions_by_owner.js +0 -33
  110. package/dist/actions/get_positions_by_position_mints.d.ts +0 -5
  111. package/dist/actions/get_positions_by_position_mints.d.ts.map +0 -1
  112. package/dist/actions/get_positions_by_position_mints.js +0 -9
  113. package/dist/actions/helpers/build_swap_transaction_message.d.ts +0 -22
  114. package/dist/actions/helpers/build_swap_transaction_message.d.ts.map +0 -1
  115. package/dist/actions/helpers/build_swap_transaction_message.js +0 -50
  116. package/dist/actions/helpers/build_transaction_message.d.ts +0 -20
  117. package/dist/actions/helpers/build_transaction_message.d.ts.map +0 -1
  118. package/dist/actions/helpers/build_transaction_message.js +0 -5
  119. package/dist/actions/helpers/index.d.ts +0 -8
  120. package/dist/actions/helpers/index.d.ts.map +0 -1
  121. package/dist/actions/helpers/index.js +0 -7
  122. package/dist/actions/helpers/resolve_active_bin_and_mints.d.ts +0 -12
  123. package/dist/actions/helpers/resolve_active_bin_and_mints.d.ts.map +0 -1
  124. package/dist/actions/helpers/resolve_active_bin_and_mints.js +0 -13
  125. package/dist/actions/helpers/resolve_admin_config_address.d.ts +0 -3
  126. package/dist/actions/helpers/resolve_admin_config_address.d.ts.map +0 -1
  127. package/dist/actions/helpers/resolve_admin_config_address.js +0 -9
  128. package/dist/actions/helpers/resolve_mint_info.d.ts +0 -10
  129. package/dist/actions/helpers/resolve_mint_info.d.ts.map +0 -1
  130. package/dist/actions/helpers/resolve_mint_info.js +0 -20
  131. package/dist/actions/helpers/resolve_position_range.d.ts +0 -12
  132. package/dist/actions/helpers/resolve_position_range.d.ts.map +0 -1
  133. package/dist/actions/helpers/resolve_position_range.js +0 -12
  134. package/dist/actions/helpers/resolve_token_mints_and_programs.d.ts +0 -15
  135. package/dist/actions/helpers/resolve_token_mints_and_programs.d.ts.map +0 -1
  136. package/dist/actions/helpers/resolve_token_mints_and_programs.js +0 -29
  137. package/dist/actions/index.d.ts +0 -31
  138. package/dist/actions/index.d.ts.map +0 -1
  139. package/dist/actions/index.js +0 -28
  140. package/dist/actions/is_admin_config_active.d.ts +0 -4
  141. package/dist/actions/is_admin_config_active.d.ts.map +0 -1
  142. package/dist/actions/is_admin_config_active.js +0 -7
  143. package/dist/actions/on_event.d.ts +0 -11
  144. package/dist/actions/on_event.d.ts.map +0 -1
  145. package/dist/actions/on_event.js +0 -50
  146. package/dist/actions/open_position.d.ts +0 -17
  147. package/dist/actions/open_position.d.ts.map +0 -1
  148. package/dist/actions/open_position.js +0 -31
  149. package/dist/actions/quote.d.ts +0 -13
  150. package/dist/actions/quote.d.ts.map +0 -1
  151. package/dist/actions/quote.js +0 -43
  152. package/dist/actions/set_metadata_update_authority.d.ts +0 -14
  153. package/dist/actions/set_metadata_update_authority.d.ts.map +0 -1
  154. package/dist/actions/set_metadata_update_authority.js +0 -7
  155. package/dist/actions/set_pool_dynamic_fee_config.d.ts +0 -16
  156. package/dist/actions/set_pool_dynamic_fee_config.d.ts.map +0 -1
  157. package/dist/actions/set_pool_dynamic_fee_config.js +0 -7
  158. package/dist/actions/set_pool_protocol_share.d.ts +0 -15
  159. package/dist/actions/set_pool_protocol_share.d.ts.map +0 -1
  160. package/dist/actions/set_pool_protocol_share.js +0 -7
  161. package/dist/actions/swap.d.ts +0 -22
  162. package/dist/actions/swap.d.ts.map +0 -1
  163. package/dist/actions/swap.js +0 -19
  164. package/dist/actions/sync_pool.d.ts +0 -15
  165. package/dist/actions/sync_pool.d.ts.map +0 -1
  166. package/dist/actions/sync_pool.js +0 -26
  167. package/dist/actions/transfer_admin_authority.d.ts +0 -14
  168. package/dist/actions/transfer_admin_authority.d.ts.map +0 -1
  169. package/dist/actions/transfer_admin_authority.js +0 -7
  170. package/dist/actions/withdraw.d.ts +0 -22
  171. package/dist/actions/withdraw.d.ts.map +0 -1
  172. package/dist/actions/withdraw.js +0 -41
  173. package/dist/utils/admin_config.d.ts +0 -3
  174. package/dist/utils/admin_config.d.ts.map +0 -1
  175. package/dist/utils/admin_config.js +0 -4
  176. package/dist/utils/bin_array.d.ts +0 -8
  177. package/dist/utils/bin_array.d.ts.map +0 -1
  178. package/dist/utils/bin_array.js +0 -29
  179. package/dist/utils/compute_budget.d.ts +0 -11
  180. package/dist/utils/compute_budget.d.ts.map +0 -1
  181. package/dist/utils/compute_budget.js +0 -22
  182. package/src/actions/accept_admin_authority.ts +0 -17
  183. package/src/actions/claim_fee.ts +0 -84
  184. package/src/actions/claim_multiple_fees.ts +0 -123
  185. package/src/actions/claim_protocol_fee.ts +0 -68
  186. package/src/actions/close_position.ts +0 -21
  187. package/src/actions/create_admin_config.ts +0 -51
  188. package/src/actions/create_bin_array.ts +0 -30
  189. package/src/actions/create_pool.ts +0 -49
  190. package/src/actions/delete_bin_array.ts +0 -33
  191. package/src/actions/deposit_by_weight.ts +0 -107
  192. package/src/actions/get_admin_config.ts +0 -13
  193. package/src/actions/get_bin_arrays_by_indices.ts +0 -15
  194. package/src/actions/get_pool.ts +0 -8
  195. package/src/actions/get_position.ts +0 -8
  196. package/src/actions/get_position_by_position_mint.ts +0 -14
  197. package/src/actions/get_positions_by_owner.ts +0 -48
  198. package/src/actions/get_positions_by_position_mints.ts +0 -21
  199. package/src/actions/helpers/build_swap_transaction_message.ts +0 -86
  200. package/src/actions/helpers/build_transaction_message.ts +0 -45
  201. package/src/actions/helpers/index.ts +0 -7
  202. package/src/actions/helpers/resolve_active_bin_and_mints.ts +0 -21
  203. package/src/actions/helpers/resolve_admin_config_address.ts +0 -11
  204. package/src/actions/helpers/resolve_mint_info.ts +0 -36
  205. package/src/actions/helpers/resolve_position_range.ts +0 -21
  206. package/src/actions/helpers/resolve_token_mints_and_programs.ts +0 -56
  207. package/src/actions/index.ts +0 -34
  208. package/src/actions/is_admin_config_active.ts +0 -14
  209. package/src/actions/on_event.ts +0 -78
  210. package/src/actions/open_position.ts +0 -66
  211. package/src/actions/quote.ts +0 -70
  212. package/src/actions/set_metadata_update_authority.ts +0 -17
  213. package/src/actions/set_pool_dynamic_fee_config.ts +0 -17
  214. package/src/actions/set_pool_protocol_share.ts +0 -17
  215. package/src/actions/swap.ts +0 -56
  216. package/src/actions/sync_pool.ts +0 -35
  217. package/src/actions/transfer_admin_authority.ts +0 -17
  218. package/src/actions/withdraw.ts +0 -87
  219. package/src/utils/admin_config.ts +0 -6
  220. package/src/utils/bin_array.ts +0 -45
  221. package/src/utils/compute_budget.ts +0 -58
@@ -0,0 +1,122 @@
1
+ import {
2
+ createSolanaRpc,
3
+ getAddressEncoder,
4
+ getProgramDerivedAddress,
5
+ type Address,
6
+ type Account,
7
+ type Instruction,
8
+ type TransactionSigner,
9
+ type FetchAccountConfig,
10
+ } from "@solana/kit";
11
+ import { SYSTEM_PROGRAM_ADDRESS } from "@solana-program/system";
12
+
13
+ import { generated } from "..";
14
+ import { findAdminConfigPda, type AdminConfig } from "../generated";
15
+ import { type ProgramConfig } from "./types";
16
+
17
+ const LOADER_V3_PROGRAM_ADDRESS = "BPFLoaderUpgradeab1e11111111111111111111111" as Address;
18
+
19
+ async function findProgramDataAddress(programAddress: Address): Promise<Address> {
20
+ const [programData] = await getProgramDerivedAddress({
21
+ programAddress: LOADER_V3_PROGRAM_ADDRESS,
22
+ seeds: [getAddressEncoder().encode(programAddress)],
23
+ });
24
+ return programData;
25
+ }
26
+
27
+ export async function resolveAdminConfigAddress(programAddress: Address): Promise<Address> {
28
+ const [adminConfig] = await findAdminConfigPda({ programAddress });
29
+ return adminConfig;
30
+ }
31
+
32
+ export async function createAdminConfig(
33
+ {
34
+ deployer,
35
+ authority,
36
+ metadataUpdateAuthority,
37
+ }: {
38
+ deployer: TransactionSigner,
39
+ authority: Address,
40
+ metadataUpdateAuthority?: Address,
41
+ },
42
+ config: ProgramConfig,
43
+ ): Promise<Instruction> {
44
+ const [adminConfig, programData] = await Promise.all([
45
+ resolveAdminConfigAddress(config.programAddress),
46
+ findProgramDataAddress(config.programAddress),
47
+ ]);
48
+ return await generated.getCreateAdminConfigInstructionAsync({
49
+ deployer,
50
+ authority,
51
+ metadataUpdateAuthority: metadataUpdateAuthority ?? null,
52
+ adminConfig,
53
+ program: config.programAddress,
54
+ programData,
55
+ }, config);
56
+ }
57
+
58
+ export async function transferAdminAuthority(
59
+ {
60
+ authority,
61
+ newAuthority,
62
+ }: {
63
+ authority: TransactionSigner,
64
+ newAuthority: Address,
65
+ },
66
+ config: ProgramConfig,
67
+ ): Promise<Instruction> {
68
+ const adminConfig = await resolveAdminConfigAddress(config.programAddress);
69
+ return await generated.getTransferAdminAuthorityInstructionAsync(
70
+ { authority, newAuthority, adminConfig },
71
+ config,
72
+ );
73
+ }
74
+
75
+ export async function acceptAdminAuthority(
76
+ { pendingAuthority }: { pendingAuthority: TransactionSigner },
77
+ config: ProgramConfig,
78
+ ): Promise<Instruction> {
79
+ const adminConfig = await resolveAdminConfigAddress(config.programAddress);
80
+ return await generated.getAcceptAdminAuthorityInstructionAsync(
81
+ { pendingAuthority, adminConfig },
82
+ config,
83
+ );
84
+ }
85
+
86
+ export async function setMetadataUpdateAuthority(
87
+ {
88
+ authority,
89
+ metadataUpdateAuthority,
90
+ }: {
91
+ authority: TransactionSigner,
92
+ metadataUpdateAuthority: Address,
93
+ },
94
+ config: ProgramConfig,
95
+ ): Promise<Instruction> {
96
+ const adminConfig = await resolveAdminConfigAddress(config.programAddress);
97
+ return await generated.getSetMetadataUpdateAuthorityInstructionAsync(
98
+ { authority, metadataUpdateAuthority, adminConfig },
99
+ config,
100
+ );
101
+ }
102
+
103
+ export async function getAdminConfig(
104
+ rpc: ReturnType<typeof createSolanaRpc>,
105
+ config: ProgramConfig & FetchAccountConfig,
106
+ ): Promise<Account<generated.AdminConfig>> {
107
+ const adminConfig = await resolveAdminConfigAddress(config.programAddress);
108
+ return await generated.fetchAdminConfig(rpc, adminConfig, config);
109
+ }
110
+
111
+ function isActive(adminConfig: AdminConfig): boolean {
112
+ return adminConfig.authority !== SYSTEM_PROGRAM_ADDRESS;
113
+ }
114
+
115
+ export async function isAdminConfigActive(
116
+ rpc: ReturnType<typeof createSolanaRpc>,
117
+ config: ProgramConfig & FetchAccountConfig,
118
+ ): Promise<boolean> {
119
+ const adminConfig = await resolveAdminConfigAddress(config.programAddress);
120
+ const maybeAdminConfig = await generated.fetchMaybeAdminConfig(rpc, adminConfig, config);
121
+ return maybeAdminConfig.exists && isActive(maybeAdminConfig.data);
122
+ }
@@ -0,0 +1,157 @@
1
+ import {
2
+ decodeAccount,
3
+ createSolanaRpc,
4
+ assertAccountsExist,
5
+ type Address,
6
+ type Account,
7
+ type Instruction,
8
+ type TransactionSigner,
9
+ type FetchAccountConfig,
10
+ type FetchAccountsConfig,
11
+ type MaybeEncodedAccount,
12
+ } from "@solana/kit";
13
+
14
+ import { generated } from "..";
15
+ import { isAccount } from "../utils/type_guards";
16
+ import { fetchInChunks } from "../utils/chunk";
17
+ import { MAX_ACCOUNTS_PER_RPC_BATCH } from "../utils/constants";
18
+ import { resolveAdminConfigAddress } from "./admin_config";
19
+ import { type ProgramConfig } from "./types";
20
+
21
+ export class BinArray {
22
+ #rpc: ReturnType<typeof createSolanaRpc>;
23
+ #account: Account<generated.BinArray>;
24
+ #programAddress: Address;
25
+
26
+ private constructor(
27
+ rpc: ReturnType<typeof createSolanaRpc>,
28
+ account: Account<generated.BinArray>,
29
+ programAddress: Address,
30
+ ) {
31
+ this.#rpc = rpc;
32
+ this.#account = account;
33
+ this.#programAddress = programAddress;
34
+ }
35
+
36
+ public get address() { return this.#account.address; }
37
+ public get data() { return this.#account.data; }
38
+ public get bins() { return this.data.bins; }
39
+
40
+ public async refresh(data: Account<generated.BinArray>): Promise<void>;
41
+ public async refresh(config?: FetchAccountConfig): Promise<void>;
42
+ public async refresh(arg?: Account<generated.BinArray> | FetchAccountConfig) {
43
+ if (!isAccount<generated.BinArray>(arg)) {
44
+ arg = await generated.fetchBinArray(this.#rpc, this.address, arg);
45
+ }
46
+ this.#account = arg;
47
+ }
48
+
49
+ public static async loadAll(
50
+ rpc: ReturnType<typeof createSolanaRpc>,
51
+ pool: Address,
52
+ indices: number[],
53
+ programAddress: Address,
54
+ config?: FetchAccountsConfig,
55
+ ): Promise<BinArray[]> {
56
+ const addresses = await findAllBinArrayAddresses(
57
+ { indices, pool },
58
+ { programAddress },
59
+ );
60
+ const accounts = await fetchAllBinArraysChunked(rpc, addresses, config);
61
+ return accounts.map(account => new BinArray(rpc, account, programAddress));
62
+ }
63
+
64
+ public async delete(authority: TransactionSigner): Promise<Instruction> {
65
+ const adminConfig = await resolveAdminConfigAddress(this.#programAddress);
66
+ return await generated.getDeleteBinArrayInstructionAsync({
67
+ authority,
68
+ pool: this.data.pool,
69
+ index: this.data.index,
70
+ receiver: this.data.payer,
71
+ binArray: this.address,
72
+ adminConfig,
73
+ }, { programAddress: this.#programAddress });
74
+ }
75
+ }
76
+
77
+ export async function createBinArray(
78
+ {
79
+ payer,
80
+ pool,
81
+ index,
82
+ idempotent = true,
83
+ }: {
84
+ payer: TransactionSigner,
85
+ pool: Address,
86
+ index: number,
87
+ idempotent?: boolean,
88
+ },
89
+ config: ProgramConfig,
90
+ ): Promise<Instruction> {
91
+ const binArray = await findBinArrayAddress(
92
+ { pool, index },
93
+ config,
94
+ );
95
+ return await generated.getCreateBinArrayInstructionAsync(
96
+ { payer, pool, index, idempotent, binArray },
97
+ config,
98
+ );
99
+ }
100
+
101
+ export async function createAllBinArrays(
102
+ {
103
+ payer,
104
+ pool,
105
+ indices,
106
+ idempotent = true,
107
+ }: {
108
+ payer: TransactionSigner,
109
+ pool: Address,
110
+ indices: number[],
111
+ idempotent?: boolean,
112
+ },
113
+ config: ProgramConfig,
114
+ ): Promise<Instruction[]> {
115
+ return await Promise.all(
116
+ indices.map(index =>
117
+ createBinArray({ payer, pool, index, idempotent }, config)),
118
+ );
119
+ }
120
+
121
+ export async function findBinArrayAddress(
122
+ seeds: { pool: Address, index: number },
123
+ config: ProgramConfig,
124
+ ): Promise<Address> {
125
+ const [address] = await generated.findBinArrayPda(seeds, config);
126
+ return address;
127
+ }
128
+
129
+ export async function findAllBinArrayAddresses(
130
+ { pool, indices }: { pool: Address, indices: number[] },
131
+ config: ProgramConfig,
132
+ ): Promise<Address[]> {
133
+ return await Promise.all(
134
+ indices.map(index => findBinArrayAddress({ index, pool }, config)),
135
+ );
136
+ }
137
+
138
+ export async function fetchAllBinArraysChunked(
139
+ rpc: ReturnType<typeof createSolanaRpc>,
140
+ addresses: Address[],
141
+ config?: FetchAccountsConfig,
142
+ ) {
143
+ return await fetchInChunks(
144
+ addresses,
145
+ MAX_ACCOUNTS_PER_RPC_BATCH,
146
+ chunkAddresses => generated.fetchAllBinArray(rpc, chunkAddresses, config),
147
+ );
148
+ }
149
+
150
+ const binArrayDecoder = generated.getBinArrayDecoder();
151
+
152
+ export function decodeAllBinArrays(
153
+ maybeAccounts: MaybeEncodedAccount<string>[],
154
+ ): Account<generated.BinArray>[] {
155
+ assertAccountsExist(maybeAccounts);
156
+ return maybeAccounts.map(a => decodeAccount(a, binArrayDecoder));
157
+ }
@@ -0,0 +1,210 @@
1
+ import {
2
+ createSolanaRpc,
3
+ parseBase64RpcAccount,
4
+ type Address,
5
+ type Account,
6
+ type Instruction,
7
+ type AccountInfoBase,
8
+ type TransactionSigner,
9
+ type FetchAccountConfig,
10
+ type FetchAccountsConfig,
11
+ type AccountInfoWithPubkey,
12
+ type AccountInfoWithBase64EncodedData,
13
+ } from "@solana/kit";
14
+ import {
15
+ getTokenDecoder,
16
+ TOKEN_2022_PROGRAM_ADDRESS,
17
+ } from "@solana-program/token-2022";
18
+
19
+ import { MAINNET_PROGRAM_ADDRESS } from "../program_addresses";
20
+ import type { QuoteOutput } from "../quote";
21
+ import {
22
+ Pool,
23
+ createPool,
24
+ } from "./pool";
25
+ import { findAllPositionAddresses } from "./position";
26
+ import { fetchAllMaybePositionPoolsChunked } from "./position_pool";
27
+ import {
28
+ getAdminConfig,
29
+ createAdminConfig,
30
+ isAdminConfigActive,
31
+ acceptAdminAuthority,
32
+ transferAdminAuthority,
33
+ setMetadataUpdateAuthority,
34
+ } from "./admin_config";
35
+ import { generated } from "..";
36
+
37
+ export class DlmmProgram {
38
+ #rpc: ReturnType<typeof createSolanaRpc>;
39
+ #programAddress: Address;
40
+
41
+ public constructor(
42
+ rpc: ReturnType<typeof createSolanaRpc>,
43
+ programAddress: Address = MAINNET_PROGRAM_ADDRESS,
44
+ ) {
45
+ this.#rpc = rpc;
46
+ this.#programAddress = programAddress;
47
+ }
48
+
49
+ public get rpc() { return this.#rpc; }
50
+ public get programAddress() { return this.#programAddress; }
51
+
52
+ public async getPool(
53
+ address: Address,
54
+ config?: FetchAccountConfig,
55
+ ): Promise<Pool> {
56
+ return await Pool.load(this.#rpc, address, this.#programAddress, config);
57
+ }
58
+
59
+ public async createPool(
60
+ authority: TransactionSigner,
61
+ params: {
62
+ binStep: number,
63
+ feeRate: number,
64
+ protocolShare: number,
65
+ activeBinId: number,
66
+ tokenMintX: Address,
67
+ tokenMintY: Address,
68
+ tokenProgramX: Address,
69
+ tokenProgramY: Address,
70
+ },
71
+ ): Promise<Instruction> {
72
+ return await createPool(
73
+ { authority, ...params },
74
+ { programAddress: this.#programAddress },
75
+ );
76
+ }
77
+
78
+ public async swap(
79
+ user: TransactionSigner,
80
+ poolAddress: Address,
81
+ params: { xToY: boolean, amountIn: bigint, minAmountOut: bigint },
82
+ config?: FetchAccountConfig,
83
+ ): Promise<Instruction[]> {
84
+ const pool = await this.getPool(poolAddress, config);
85
+ return await pool.swap(user, params);
86
+ }
87
+
88
+ public async quote(
89
+ poolAddress: Address,
90
+ params: { xToY: boolean, amountIn: bigint, slippageToleranceBps: number },
91
+ config?: FetchAccountConfig,
92
+ ): Promise<QuoteOutput> {
93
+ const pool = await this.getPool(poolAddress, config);
94
+ return await pool.quote(params);
95
+ }
96
+
97
+ public async quoteAndSwap(
98
+ user: TransactionSigner,
99
+ poolAddress: Address,
100
+ params: { xToY: boolean, amountIn: bigint, slippageToleranceBps: number },
101
+ config?: FetchAccountConfig,
102
+ ): Promise<{ quoteOutput: QuoteOutput, instructions: Instruction[] }> {
103
+ const pool = await this.getPool(poolAddress, config);
104
+ return await pool.quoteAndSwap(user, params);
105
+ }
106
+
107
+ public async getAllPositionMintsByOwner(
108
+ owner: Address,
109
+ config?: FetchAccountsConfig,
110
+ ): Promise<Map<Address, Address[]>> {
111
+ const { commitment, minContextSlot, abortSignal } = config ?? {};
112
+ const { value: tokenAccounts } = await this.#rpc
113
+ .getTokenAccountsByOwner(
114
+ owner,
115
+ { programId: TOKEN_2022_PROGRAM_ADDRESS },
116
+ { commitment, minContextSlot, encoding: "base64" },
117
+ )
118
+ .send({ abortSignal });
119
+ const candidateMints = decodeCandidatePositionMints(tokenAccounts);
120
+ if (candidateMints.length === 0) {
121
+ return new Map();
122
+ }
123
+
124
+ const positionAddresses = await findAllPositionAddresses(
125
+ { positionMints: candidateMints },
126
+ { programAddress: this.#programAddress },
127
+ );
128
+ const maybePools = await fetchAllMaybePositionPoolsChunked(
129
+ this.#rpc,
130
+ positionAddresses,
131
+ config,
132
+ );
133
+
134
+ return new Map(
135
+ Map.groupBy(
136
+ maybePools.flatMap((maybePool, i) =>
137
+ maybePool ? [{ pool: maybePool.pool, mint: candidateMints[i] }] : []),
138
+ ({ pool }) => pool,
139
+ ).entries().map(([pool, pairs]) => [pool, pairs.map(({ mint }) => mint)]),
140
+ );
141
+ }
142
+
143
+ public async createAdminConfig(
144
+ deployer: TransactionSigner,
145
+ params: { authority: Address, metadataUpdateAuthority?: Address },
146
+ ): Promise<Instruction> {
147
+ return await createAdminConfig(
148
+ { deployer, ...params },
149
+ { programAddress: this.#programAddress },
150
+ );
151
+ }
152
+
153
+ public async transferAdminAuthority(
154
+ authority: TransactionSigner,
155
+ newAuthority: Address,
156
+ ): Promise<Instruction> {
157
+ return await transferAdminAuthority(
158
+ { authority, newAuthority },
159
+ { programAddress: this.#programAddress },
160
+ );
161
+ }
162
+
163
+ public async acceptAdminAuthority(
164
+ pendingAuthority: TransactionSigner,
165
+ ): Promise<Instruction> {
166
+ return await acceptAdminAuthority(
167
+ { pendingAuthority },
168
+ { programAddress: this.#programAddress },
169
+ );
170
+ }
171
+
172
+ public async setMetadataUpdateAuthority(
173
+ authority: TransactionSigner,
174
+ metadataUpdateAuthority: Address,
175
+ ): Promise<Instruction> {
176
+ return await setMetadataUpdateAuthority(
177
+ { authority, metadataUpdateAuthority },
178
+ { programAddress: this.#programAddress },
179
+ );
180
+ }
181
+
182
+ public async getAdminConfig(
183
+ config?: FetchAccountConfig,
184
+ ): Promise<Account<generated.AdminConfig>> {
185
+ return await getAdminConfig(
186
+ this.#rpc,
187
+ { programAddress: this.#programAddress, ...config },
188
+ );
189
+ }
190
+
191
+ public async isAdminConfigActive(
192
+ config?: FetchAccountConfig,
193
+ ): Promise<boolean> {
194
+ return await isAdminConfigActive(
195
+ this.#rpc,
196
+ { programAddress: this.#programAddress, ...config },
197
+ );
198
+ }
199
+ }
200
+
201
+ const tokenDecoder = getTokenDecoder();
202
+
203
+ function decodeCandidatePositionMints(
204
+ tokenAccounts: readonly AccountInfoWithPubkey<AccountInfoBase & AccountInfoWithBase64EncodedData>[],
205
+ ): Address[] {
206
+ return tokenAccounts
207
+ .map(({ pubkey, account }) => tokenDecoder.decode(parseBase64RpcAccount(pubkey, account).data))
208
+ .filter(token => token.amount === 1n)
209
+ .map(token => token.mint);
210
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./admin_config";
2
+ export * from "./bin_array";
3
+ export * from "./dlmm_program";
4
+ export * from "./pool";
5
+ export * from "./position";
6
+ export * from "./types";