@sanctumso/inf1 0.0.1-dev-1 → 0.0.1-dev-3

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.
package/README.md CHANGED
@@ -6,8 +6,9 @@ Typescript + WASM SDK for Sanctum Infinity program V1.
6
6
 
7
7
  ```ts
8
8
  import {
9
+ assertAccountsExist,
9
10
  createSolanaRpc,
10
- getBase64Encoder,
11
+ fetchEncodedAccounts,
11
12
  type Address,
12
13
  type IInstruction,
13
14
  type Rpc,
@@ -15,6 +16,7 @@ import {
15
16
  } from "@solana/kit";
16
17
  import {
17
18
  accountsToUpdateForTrade,
19
+ init,
18
20
  initPks,
19
21
  quoteTradeExactIn,
20
22
  tradeExactInIx,
@@ -22,14 +24,23 @@ import {
22
24
  type AccountMap,
23
25
  type SplPoolAccounts,
24
26
  } from "@sanctumso/inf1";
27
+ import initSdk from "@sanctumso/inf1";
28
+
29
+ // The SDK needs to be initialized once globally before it can be used (idempotent).
30
+ // For nodejs environments, use
31
+ // `import { initSyncEmbed } from "@sanctumso/inf1"; initSyncEmbed();`
32
+ // instead
33
+ await initSdk();
25
34
 
26
35
  const LAINESOL = "LAinEtNLgpmCP9Rvsf5Hn8W6EhNiKLZQti1xfWMLy6X";
27
36
  const WSOL = "So11111111111111111111111111111111111111112";
28
- const SPL_POOL_ACCOUNTS: SplPoolAccounts = {
37
+ const SPL_POOL_ACCOUNTS: SplPoolAccounts = new Map(Object.entries({
29
38
  [LAINESOL]: "2qyEeSAWKfU18AFthrF7JA8z8ZCi1yt76Tqs917vwQTV",
30
39
  // ...populate rest of `spl lst mint: stake pool addr` data
31
- // for every single spl lst mint in the INF pool (all 3 spl program deploys)
32
- };
40
+ // for every spl lst mints in the INF pool (all 3 spl program deploys).
41
+ // To support SPL LSTs that are added later on, the `appendSplLsts` fn
42
+ // can be used to add data
43
+ }));
33
44
 
34
45
  // If out === INF mint, then below code will work the same,
35
46
  // but the quote and instruction will be for AddLiquidity instead of SwapExactIn.
@@ -42,34 +53,23 @@ async function fetchAccountMap(
42
53
  rpc: Rpc<SolanaRpcApi>,
43
54
  accounts: string[]
44
55
  ): Promise<AccountMap> {
45
- const map = new Map<string, Account>();
46
- await Promise.all(
47
- accounts.map(async (account) => {
48
- const accountInfo = await rpc
49
- .getAccountInfo(account as Address, {
50
- encoding: "base64",
51
- })
52
- .send();
53
- const acc = accountInfo.value!;
54
- map.set(account, {
55
- data: new Uint8Array(getBase64Encoder().encode(acc.data[0])),
56
- owner: acc.owner,
57
- });
58
- })
56
+ const fetched = await fetchEncodedAccounts(rpc, accounts);
57
+ assertAccountsExist(fetched);
58
+ return new Map(
59
+ fetched.map(({ address, data, programAddress }) => [
60
+ address,
61
+ { data, owner: programAddress },
62
+ ])
59
63
  );
60
- return map;
61
64
  }
62
65
 
63
66
  const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
64
67
 
65
68
  // init
66
- const { poolState: poolStateAddr, lstStateList: lstStateListAddr } = initPks();
67
- const initAccs = await fetchAccountMap(rpc, [poolStateAddr, lstStateListAddr]);
69
+ const ipks = initPks();
70
+ const initAccs = await fetchAccountMap(rpc, ipks);
68
71
  const inf = init(
69
- {
70
- poolState: initAccs.get(poolStateAddr)!,
71
- lstStateList: initAccs.get(lstStateListAddr)!,
72
- },
72
+ initAccs,
73
73
  SPL_POOL_ACCOUNTS
74
74
  );
75
75
 
package/index.d.ts CHANGED
@@ -1,19 +1,73 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
+ /**
4
+ * @throws
5
+ */
3
6
  export function tradeExactInIx(inf: Inf, arg1: TradeArgs): Instruction;
7
+ /**
8
+ * @throws
9
+ */
4
10
  export function tradeExactOutIx(inf: Inf, arg1: TradeArgs): Instruction;
5
- export function findPoolReservesAta(arg0: Bs58Array): FoundPda;
6
- export function findProtocolFeeAccumulatorAta(arg0: Bs58Array): FoundPda;
7
- export function initPks(): InitPks;
8
- export function init(arg0: InitAccounts, arg1: SplPoolAccounts): Inf;
9
11
  /**
10
- * Update SPL LSTs auxiliary data to support new SPL LSTs that may have previously not been covered
12
+ * Add SPL LSTs auxiliary data to support new SPL LSTs that may have previously not been covered
13
+ */
14
+ export function appendSplLsts(inf: Inf, arg1: SplPoolAccounts): void;
15
+ /**
16
+ * Returns if the given SPL LST mints have their {@link SplPoolAccounts} present in the object.
17
+ *
18
+ * Returns a byte array where ret[i] corresponds to the result for `mints[i]`.
19
+ * 0 - false, 1 - true.
20
+ *
21
+ * If false is returned, then the data needs to be added via {@link appendSplLsts}
22
+ *
23
+ * This fn returns a byte array instead of `boolean` array because wasm_bindgen's type
24
+ * conversion doesnt work with bool arrays.
25
+ */
26
+ export function hasSplData(inf: Inf, mints: Bs58Array[]): Uint8Array;
27
+ /**
28
+ * Returned accounts are deduped
29
+ *
30
+ * @throws
11
31
  */
12
- export function updateSplLsts(inf: Inf, arg1: SplPoolAccounts): void;
13
- export function quoteTradeExactIn(inf: Inf, arg1: QuoteArgs): Quote;
14
- export function quoteTradeExactOut(arg0: Inf, arg1: QuoteArgs): Quote;
15
32
  export function accountsToUpdateForTrade(inf: Inf, arg1: PkPair): Bs58Array[];
33
+ /**
34
+ * @throws
35
+ */
16
36
  export function updateForTrade(inf: Inf, arg1: PkPair, arg2: AccountMap): void;
37
+ /**
38
+ * @throws
39
+ */
40
+ export function quoteTradeExactIn(inf: Inf, arg1: QuoteArgs): Quote;
41
+ /**
42
+ * @throws
43
+ */
44
+ export function quoteTradeExactOut(inf: Inf, arg1: QuoteArgs): Quote;
45
+ /**
46
+ * Returns the array of all possible {@link InfErr}s
47
+ */
48
+ export function allInfErrs(): AllInfErrs;
49
+ /**
50
+ * @throws if not valid PDA found
51
+ */
52
+ export function findPoolReservesAta(arg0: Bs58Array): FoundPda;
53
+ /**
54
+ * @throws if not valid PDA found
55
+ */
56
+ export function findProtocolFeeAccumulatorAta(arg0: Bs58Array): FoundPda;
57
+ /**
58
+ * Returns the pubkeys of the accounts that need ot be fetched to initialize
59
+ * a new {@link Inf} object
60
+ */
61
+ export function initPks(): Bs58Array[];
62
+ /**
63
+ * Initialize a new {@link Inf} object.
64
+ *
65
+ * The returned object must be updated for a mint pair before it is ready to
66
+ * quote and operate for trades involving that pair
67
+ *
68
+ * @throws
69
+ */
70
+ export function init(arg0: AccountMap, arg1: SplPoolAccounts): Inf;
17
71
  export interface TradeArgs {
18
72
  amt: bigint;
19
73
  limit: bigint;
@@ -22,60 +76,6 @@ export interface TradeArgs {
22
76
  tokenAccs: PkPair;
23
77
  }
24
78
 
25
- export interface AccountMeta {
26
- address: B58PK;
27
- /**
28
- * Represents the role of an account in a transaction:
29
- * - Readonly: 0
30
- * - Writable: 1
31
- * - ReadonlySigner: 2
32
- * - WritableSigner: 3
33
- */
34
- role: 0 | 1 | 2 | 3;
35
- }
36
-
37
- export interface Instruction {
38
- data: Uint8Array;
39
- accounts: AccountMeta[];
40
- programAddress: B58PK;
41
- }
42
-
43
- export type FoundPda = [B58PK, number];
44
-
45
- export interface Account {
46
- data: Uint8Array;
47
- owner: B58PK;
48
- }
49
-
50
- /**
51
- * Map of `mint: stake pool account` for spl (all deploys) LSTs.
52
- *
53
- * This data is required to determine how to properly initialize the corresponding
54
- * sol value calculator data since which stake pool account corresponds to which mint
55
- * is not available onchain (yet)
56
- */
57
- export type SplPoolAccounts = Map<B58PK, B58PK>;
58
-
59
- export type AccountMap = Map<B58PK, Account>;
60
-
61
- export type B58PK = Bs58Array;
62
-
63
- export interface Pair<T> {
64
- inp: T;
65
- out: T;
66
- }
67
-
68
- export type PkPair = Pair<B58PK>;
69
-
70
- export interface Init<T> {
71
- poolState: T;
72
- lstStateList: T;
73
- }
74
-
75
- export type InitPks = Init<B58PK>;
76
-
77
- export type InitAccounts = Init<Account>;
78
-
79
79
  export type FeeMint = "inp" | "out";
80
80
 
81
81
  export interface QuoteArgs {
@@ -116,9 +116,126 @@ export interface Quote {
116
116
  mints: PkPair;
117
117
  }
118
118
 
119
+ export interface Pair<T> {
120
+ inp: T;
121
+ out: T;
122
+ }
123
+
124
+ export type PkPair = Pair<B58PK>;
125
+
126
+ export interface Account {
127
+ data: Uint8Array;
128
+ owner: B58PK;
129
+ }
130
+
131
+ /**
132
+ * Map of `mint: stake pool account` for spl (all deploys) LSTs.
133
+ *
134
+ * This data is required to determine how to properly initialize the corresponding
135
+ * sol value calculator data since which stake pool account corresponds to which mint
136
+ * is not available onchain (yet)
137
+ */
138
+ export type SplPoolAccounts = Map<B58PK, B58PK>;
139
+
140
+ export type AccountMap = Map<B58PK, Account>;
141
+
142
+ export type B58PK = Bs58Array;
143
+
144
+ /**
145
+ * All {@link Error} objects thrown by SDK functions will start with
146
+ * `{InfErr}:`, so that the `InfErr` error code can be
147
+ * extracted by splitting on the first colon `:`
148
+ */
149
+ export type InfErr = "AccDeserErr" | "InternalErr" | "MissingAccErr" | "MissingSplDataErr" | "MissingSvcDataErr" | "NoValidPdaErr" | "PoolErr" | "UnknownSvcErr" | "UnsupportedMintErr" | "UserErr";
150
+
151
+ export type AllInfErrs = InfErr[];
152
+
153
+ export type FoundPda = [B58PK, number];
154
+
155
+ export interface AccountMeta {
156
+ address: B58PK;
157
+ /**
158
+ * Represents the role of an account in a transaction:
159
+ * - Readonly: 0
160
+ * - Writable: 1
161
+ * - ReadonlySigner: 2
162
+ * - WritableSigner: 3
163
+ */
164
+ role: 0 | 1 | 2 | 3;
165
+ }
166
+
167
+ export interface Instruction {
168
+ data: Uint8Array;
169
+ accounts: AccountMeta[];
170
+ programAddress: B58PK;
171
+ }
172
+
119
173
  export type Bs58Array = string
120
174
 
121
175
  export class Inf {
122
176
  private constructor();
123
177
  free(): void;
124
178
  }
179
+
180
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
181
+
182
+ export interface InitOutput {
183
+ readonly memory: WebAssembly.Memory;
184
+ readonly tradeExactInIx: (a: number, b: any) => [number, number, number];
185
+ readonly tradeExactOutIx: (a: number, b: any) => [number, number, number];
186
+ readonly appendSplLsts: (a: number, b: any) => void;
187
+ readonly hasSplData: (a: number, b: number, c: number) => [number, number];
188
+ readonly accountsToUpdateForTrade: (a: number, b: any) => [number, number, number, number];
189
+ readonly updateForTrade: (a: number, b: any, c: any) => [number, number];
190
+ readonly quoteTradeExactIn: (a: number, b: any) => [number, number, number];
191
+ readonly quoteTradeExactOut: (a: number, b: any) => [number, number, number];
192
+ readonly __wbg_inf_free: (a: number, b: number) => void;
193
+ readonly allInfErrs: () => any;
194
+ readonly findPoolReservesAta: (a: any) => [number, number, number];
195
+ readonly findProtocolFeeAccumulatorAta: (a: any) => [number, number, number];
196
+ readonly initPks: () => [number, number];
197
+ readonly init: (a: any, b: any) => [number, number, number];
198
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
199
+ readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
200
+ readonly __wbindgen_exn_store: (a: number) => void;
201
+ readonly __externref_table_alloc: () => number;
202
+ readonly __wbindgen_export_4: WebAssembly.Table;
203
+ readonly __externref_table_dealloc: (a: number) => void;
204
+ readonly __wbindgen_free: (a: number, b: number, c: number) => void;
205
+ readonly __externref_drop_slice: (a: number, b: number) => void;
206
+ readonly __wbindgen_start: () => void;
207
+ }
208
+
209
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
210
+ /**
211
+ * Instantiates the given `module`, which can either be bytes or
212
+ * a precompiled `WebAssembly.Module`.
213
+ *
214
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
215
+ *
216
+ * @returns {InitOutput}
217
+ */
218
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
219
+
220
+ /**
221
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
222
+ * for everything else, calls `WebAssembly.instantiate` directly.
223
+ *
224
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
225
+ *
226
+ * @returns {Promise<InitOutput>}
227
+ */
228
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
229
+
230
+ /**
231
+ * Instantiates this `module` using the embedded
232
+ * {@link WASM_BIN_B64}. This is what works for nodejs envs.
233
+ *
234
+ * @returns {InitOutput}
235
+ */
236
+ export function initSyncEmbed(): InitOutput;
237
+
238
+ /**
239
+ * Embedded base64-encoded wasm binary bytes
240
+ */
241
+ export const WASM_BIN_B64: string;