@temporalabs/treasury 0.1.1 → 0.1.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/NOTICE +4 -2
- package/README.md +100 -44
- package/THIRD_PARTY_NOTICES.connect-page.md +7836 -0
- package/THIRD_PARTY_NOTICES.md +543 -162
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +243 -0
- package/dist/cli.js.map +1 -0
- package/dist/client.d.ts +366 -239
- package/dist/client.js +202 -25
- package/dist/client.js.map +1 -1
- package/dist/config/earn.d.ts +48 -12
- package/dist/config/earn.js +48 -12
- package/dist/config/earn.js.map +1 -1
- package/dist/connect/ack.d.ts +76 -0
- package/dist/connect/ack.js +147 -0
- package/dist/connect/ack.js.map +1 -0
- package/dist/connect/commands.d.ts +81 -0
- package/dist/connect/commands.js +555 -0
- package/dist/connect/commands.js.map +1 -0
- package/dist/connect/config.d.ts +24 -0
- package/dist/connect/config.js +35 -0
- package/dist/connect/config.js.map +1 -0
- package/dist/connect/gate-solana.d.ts +8 -0
- package/dist/connect/gate-solana.js +147 -0
- package/dist/connect/gate-solana.js.map +1 -0
- package/dist/connect/gate.d.ts +37 -0
- package/dist/connect/gate.js +115 -0
- package/dist/connect/gate.js.map +1 -0
- package/dist/connect/http.d.ts +60 -0
- package/dist/connect/http.js +235 -0
- package/dist/connect/http.js.map +1 -0
- package/dist/connect/open.d.ts +2 -0
- package/dist/connect/open.js +35 -0
- package/dist/connect/open.js.map +1 -0
- package/dist/connect/page.d.ts +5 -0
- package/dist/connect/page.js +69 -0
- package/dist/connect/page.js.map +1 -0
- package/dist/connect/session.d.ts +39 -0
- package/dist/connect/session.js +198 -0
- package/dist/connect/session.js.map +1 -0
- package/dist/connect/solana.d.ts +73 -0
- package/dist/connect/solana.js +267 -0
- package/dist/connect/solana.js.map +1 -0
- package/dist/connect/verify.d.ts +37 -0
- package/dist/connect/verify.js +89 -0
- package/dist/connect/verify.js.map +1 -0
- package/dist/connect-page.js +5143 -0
- package/dist/disclosures.d.ts +22 -6
- package/dist/disclosures.js +34 -8
- package/dist/disclosures.js.map +1 -1
- package/dist/earn/commands.d.ts +46 -0
- package/dist/earn/commands.js +556 -0
- package/dist/earn/commands.js.map +1 -0
- package/dist/earn/solana.d.ts +278 -0
- package/dist/earn/solana.js +302 -0
- package/dist/earn/solana.js.map +1 -0
- package/dist/guide.d.ts +15 -0
- package/dist/guide.js +50 -0
- package/dist/guide.js.map +1 -0
- package/dist/links.d.ts +2 -0
- package/dist/links.js +40 -14
- package/dist/links.js.map +1 -1
- package/dist/position.d.ts +9 -6
- package/dist/position.js +39 -19
- package/dist/position.js.map +1 -1
- package/dist/preflight.d.ts +9 -0
- package/dist/preflight.js +14 -3
- package/dist/preflight.js.map +1 -1
- package/dist/quote.js +24 -7
- package/dist/quote.js.map +1 -1
- package/dist/redact.d.ts +3 -3
- package/dist/redact.js +3 -3
- package/dist/redact.js.map +1 -1
- package/dist/registry-schema.d.ts +82 -5
- package/dist/registry-schema.js +105 -15
- package/dist/registry-schema.js.map +1 -1
- package/dist/registry.d.ts +50 -7
- package/dist/registry.js +122 -21
- package/dist/registry.js.map +1 -1
- package/dist/solana/constants.d.ts +49 -0
- package/dist/solana/constants.js +50 -0
- package/dist/solana/constants.js.map +1 -0
- package/dist/solana/kvault.d.ts +99 -0
- package/dist/solana/kvault.js +192 -0
- package/dist/solana/kvault.js.map +1 -0
- package/dist/solana/layout.d.ts +143 -0
- package/dist/solana/layout.js +209 -0
- package/dist/solana/layout.js.map +1 -0
- package/dist/solana/rpc.d.ts +24 -0
- package/dist/solana/rpc.js +66 -0
- package/dist/solana/rpc.js.map +1 -0
- package/dist/solana/vault.d.ts +153 -0
- package/dist/solana/vault.js +324 -0
- package/dist/solana/vault.js.map +1 -0
- package/dist/{mcp-server.mjs → treasury.mjs} +41101 -49486
- package/dist/version.js +5 -10
- package/dist/version.js.map +1 -1
- package/llms.txt +621 -0
- package/package.json +12 -9
- package/registry/vaults.json +218 -4
- package/dist/mcp/server.d.ts +0 -8
- package/dist/mcp/server.js +0 -368
- package/dist/mcp/server.js.map +0 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana constants: the programs a Kamino vault deposit or withdrawal names, and the cluster.
|
|
3
|
+
*
|
|
4
|
+
* Every program id below was read on Solana mainnet-beta. Both Kamino programs are UPGRADEABLE by their
|
|
5
|
+
* upgrade authority, so a later upgrade can change any behaviour this client relies on. The decoders in
|
|
6
|
+
* `layout.ts` refuse an account of the wrong size or type rather than read it, which is the one change this
|
|
7
|
+
* client can notice; anything else is a disclosure, not a check.
|
|
8
|
+
*/
|
|
9
|
+
/** Kamino's vault program (a curated single-asset vault over Kamino lending reserves). */
|
|
10
|
+
export declare const KVAULT_PROGRAM_ID: "KvauGMspG5k6rtzrqqn7WNn3oZdyKqLKwK2XWQ8FLjd";
|
|
11
|
+
/** Kamino's lending program, where the vault's reserves live. */
|
|
12
|
+
export declare const KLEND_PROGRAM_ID: "KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD";
|
|
13
|
+
/** SPL Token: the program that owns USDC and the vault's share mint. */
|
|
14
|
+
export declare const SPL_TOKEN_PROGRAM_ID: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
15
|
+
/** The Associated Token Account program. */
|
|
16
|
+
export declare const ASSOCIATED_TOKEN_PROGRAM_ID: "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
|
|
17
|
+
/** The System Program. */
|
|
18
|
+
export declare const SYSTEM_PROGRAM_ID: "11111111111111111111111111111111";
|
|
19
|
+
/** The instructions sysvar, which a kVault withdrawal from a reserve names. */
|
|
20
|
+
export declare const SYSVAR_INSTRUCTIONS_ID: "Sysvar1nstructions1111111111111111111111111";
|
|
21
|
+
/** The Compute Budget program. */
|
|
22
|
+
export declare const COMPUTE_BUDGET_PROGRAM_ID: "ComputeBudget111111111111111111111111111111";
|
|
23
|
+
/** The all-zero key: an empty allocation slot's reserve. */
|
|
24
|
+
export declare const DEFAULT_PUBKEY: "11111111111111111111111111111111";
|
|
25
|
+
/**
|
|
26
|
+
* Solana mainnet-beta's genesis hash. An RPC endpoint is on mainnet exactly when `getGenesisHash` returns this:
|
|
27
|
+
* the Solana counterpart of an EVM endpoint's `eth_chainId`.
|
|
28
|
+
*/
|
|
29
|
+
export declare const MAINNET_GENESIS_HASH: "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d";
|
|
30
|
+
/** PDA seeds, as Kamino's vault program spells them. */
|
|
31
|
+
export declare const SEEDS: {
|
|
32
|
+
readonly tokenVault: "token_vault";
|
|
33
|
+
readonly ctokenVault: "ctoken_vault";
|
|
34
|
+
readonly baseVaultAuthority: "authority";
|
|
35
|
+
readonly shares: "shares";
|
|
36
|
+
readonly globalConfig: "global_config";
|
|
37
|
+
readonly eventAuthority: "__event_authority";
|
|
38
|
+
/** The lending program's market authority. */
|
|
39
|
+
readonly lendingMarketAuthority: "lma";
|
|
40
|
+
};
|
|
41
|
+
/** Account sizes INCLUDING the 8-byte Anchor discriminator, from Kamino's IDL and measured on chain. */
|
|
42
|
+
export declare const VAULT_STATE_SIZE = 62552;
|
|
43
|
+
export declare const RESERVE_SIZE = 8624;
|
|
44
|
+
/** A vault has this many allocation slots; an empty slot's `reserve` is the default key. */
|
|
45
|
+
export declare const MAX_RESERVES = 25;
|
|
46
|
+
/** Solana's packet limit for one serialized transaction. */
|
|
47
|
+
export declare const MAX_TRANSACTION_BYTES = 1232;
|
|
48
|
+
/** The most compute one transaction may request. */
|
|
49
|
+
export declare const MAX_COMPUTE_UNITS = 1400000;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solana constants: the programs a Kamino vault deposit or withdrawal names, and the cluster.
|
|
3
|
+
*
|
|
4
|
+
* Every program id below was read on Solana mainnet-beta. Both Kamino programs are UPGRADEABLE by their
|
|
5
|
+
* upgrade authority, so a later upgrade can change any behaviour this client relies on. The decoders in
|
|
6
|
+
* `layout.ts` refuse an account of the wrong size or type rather than read it, which is the one change this
|
|
7
|
+
* client can notice; anything else is a disclosure, not a check.
|
|
8
|
+
*/
|
|
9
|
+
/** Kamino's vault program (a curated single-asset vault over Kamino lending reserves). */
|
|
10
|
+
export const KVAULT_PROGRAM_ID = "KvauGMspG5k6rtzrqqn7WNn3oZdyKqLKwK2XWQ8FLjd";
|
|
11
|
+
/** Kamino's lending program, where the vault's reserves live. */
|
|
12
|
+
export const KLEND_PROGRAM_ID = "KLend2g3cP87fffoy8q1mQqGKjrxjC8boSyAYavgmjD";
|
|
13
|
+
/** SPL Token: the program that owns USDC and the vault's share mint. */
|
|
14
|
+
export const SPL_TOKEN_PROGRAM_ID = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
15
|
+
/** The Associated Token Account program. */
|
|
16
|
+
export const ASSOCIATED_TOKEN_PROGRAM_ID = "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL";
|
|
17
|
+
/** The System Program. */
|
|
18
|
+
export const SYSTEM_PROGRAM_ID = "11111111111111111111111111111111";
|
|
19
|
+
/** The instructions sysvar, which a kVault withdrawal from a reserve names. */
|
|
20
|
+
export const SYSVAR_INSTRUCTIONS_ID = "Sysvar1nstructions1111111111111111111111111";
|
|
21
|
+
/** The Compute Budget program. */
|
|
22
|
+
export const COMPUTE_BUDGET_PROGRAM_ID = "ComputeBudget111111111111111111111111111111";
|
|
23
|
+
/** The all-zero key: an empty allocation slot's reserve. */
|
|
24
|
+
export const DEFAULT_PUBKEY = SYSTEM_PROGRAM_ID;
|
|
25
|
+
/**
|
|
26
|
+
* Solana mainnet-beta's genesis hash. An RPC endpoint is on mainnet exactly when `getGenesisHash` returns this:
|
|
27
|
+
* the Solana counterpart of an EVM endpoint's `eth_chainId`.
|
|
28
|
+
*/
|
|
29
|
+
export const MAINNET_GENESIS_HASH = "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d";
|
|
30
|
+
/** PDA seeds, as Kamino's vault program spells them. */
|
|
31
|
+
export const SEEDS = {
|
|
32
|
+
tokenVault: "token_vault",
|
|
33
|
+
ctokenVault: "ctoken_vault",
|
|
34
|
+
baseVaultAuthority: "authority",
|
|
35
|
+
shares: "shares",
|
|
36
|
+
globalConfig: "global_config",
|
|
37
|
+
eventAuthority: "__event_authority",
|
|
38
|
+
/** The lending program's market authority. */
|
|
39
|
+
lendingMarketAuthority: "lma",
|
|
40
|
+
};
|
|
41
|
+
/** Account sizes INCLUDING the 8-byte Anchor discriminator, from Kamino's IDL and measured on chain. */
|
|
42
|
+
export const VAULT_STATE_SIZE = 62_552;
|
|
43
|
+
export const RESERVE_SIZE = 8_624;
|
|
44
|
+
/** A vault has this many allocation slots; an empty slot's `reserve` is the default key. */
|
|
45
|
+
export const MAX_RESERVES = 25;
|
|
46
|
+
/** Solana's packet limit for one serialized transaction. */
|
|
47
|
+
export const MAX_TRANSACTION_BYTES = 1232;
|
|
48
|
+
/** The most compute one transaction may request. */
|
|
49
|
+
export const MAX_COMPUTE_UNITS = 1_400_000;
|
|
50
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/solana/constants.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,0FAA0F;AAC1F,MAAM,CAAC,MAAM,iBAAiB,GAAG,6CAAsD,CAAC;AACxF,iEAAiE;AACjE,MAAM,CAAC,MAAM,gBAAgB,GAAG,6CAAsD,CAAC;AACvF,wEAAwE;AACxE,MAAM,CAAC,MAAM,oBAAoB,GAAG,6CAAsD,CAAC;AAC3F,4CAA4C;AAC5C,MAAM,CAAC,MAAM,2BAA2B,GAAG,8CAAuD,CAAC;AACnG,0BAA0B;AAC1B,MAAM,CAAC,MAAM,iBAAiB,GAAG,kCAA2C,CAAC;AAC7E,+EAA+E;AAC/E,MAAM,CAAC,MAAM,sBAAsB,GAAG,6CAAsD,CAAC;AAC7F,kCAAkC;AAClC,MAAM,CAAC,MAAM,yBAAyB,GAAG,6CAAsD,CAAC;AAEhG,4DAA4D;AAC5D,MAAM,CAAC,MAAM,cAAc,GAAG,iBAAiB,CAAC;AAEhD;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,8CAAuD,CAAC;AAE5F,wDAAwD;AACxD,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,UAAU,EAAE,aAAa;IACzB,WAAW,EAAE,cAAc;IAC3B,kBAAkB,EAAE,WAAW;IAC/B,MAAM,EAAE,QAAQ;IAChB,YAAY,EAAE,eAAe;IAC7B,cAAc,EAAE,mBAAmB;IACnC,8CAA8C;IAC9C,sBAAsB,EAAE,KAAK;CACrB,CAAC;AAEX,wGAAwG;AACxG,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC;AACvC,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,CAAC;AAClC,4FAA4F;AAC5F,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC;AAE/B,4DAA4D;AAC5D,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,CAAC;AAC1C,oDAAoD;AACpD,MAAM,CAAC,MAAM,iBAAiB,GAAG,SAAS,CAAC"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The depositor's side of a Kamino vault: the PDAs a deposit or withdrawal names, the two instructions
|
|
3
|
+
* (`deposit`, `withdraw`), the token-account instruction a first deposit needs, and an UNSIGNED transaction.
|
|
4
|
+
*
|
|
5
|
+
* 🔴 HAND-WRITTEN, PINNED TO THE VENDOR'S BYTES. Each builder must produce exactly the data bytes and the
|
|
6
|
+
* account metas (address, role, order) Kamino's own SDK produces for the same inputs:
|
|
7
|
+
* `tests/solana-kvault.unit.test.ts` asserts byte equality against `vendor/kamino/instruction-fixtures.json`,
|
|
8
|
+
* written by Kamino's SDK (`vendor/kamino/generate-fixtures.mjs`, version in `vendor/kamino/PINNED`).
|
|
9
|
+
*
|
|
10
|
+
* Nothing here signs or sends. The signer slot is an ADDRESS: the operator's wallet signs.
|
|
11
|
+
*/
|
|
12
|
+
import { type Address, type Instruction } from "@solana/kit";
|
|
13
|
+
/** `sha256("global:<name>")[0..8]`, per instruction. */
|
|
14
|
+
export declare const DISCRIMINATORS: {
|
|
15
|
+
readonly deposit: readonly [242, 35, 198, 137, 82, 225, 242, 182];
|
|
16
|
+
readonly withdraw: readonly [183, 18, 70, 156, 148, 109, 161, 34];
|
|
17
|
+
};
|
|
18
|
+
/** Every PDA a vault owns, from its vault-state address alone. */
|
|
19
|
+
export declare function vaultPdas(vaultState: string): Promise<{
|
|
20
|
+
tokenVault: Address;
|
|
21
|
+
baseVaultAuthority: Address;
|
|
22
|
+
sharesMint: Address;
|
|
23
|
+
}>;
|
|
24
|
+
/** The vault's ctoken account for one reserve. */
|
|
25
|
+
export declare const ctokenVaultPda: (vaultState: string, reserve: string) => Promise<Address>;
|
|
26
|
+
export declare const kvaultGlobalConfigPda: () => Promise<Address>;
|
|
27
|
+
/** Anchor's event-CPI authority for the vault program: `deposit` and `withdraw` name it. */
|
|
28
|
+
export declare const kvaultEventAuthorityPda: () => Promise<Address>;
|
|
29
|
+
/** The lending program's authority for one market. */
|
|
30
|
+
export declare const lendingMarketAuthorityPda: (lendingMarket: string) => Promise<Address>;
|
|
31
|
+
/** The owner's associated token account for `mint` (SPL Token). */
|
|
32
|
+
export declare function associatedTokenAccount(owner: string, mint: string): Promise<Address>;
|
|
33
|
+
/** A reserve and its market, as every deposit and withdrawal lists them for the program to refresh. */
|
|
34
|
+
export interface VaultReserve {
|
|
35
|
+
readonly reserve: string;
|
|
36
|
+
readonly lendingMarket: string;
|
|
37
|
+
}
|
|
38
|
+
/** What a withdrawal from one reserve needs to know about it (all read on chain). */
|
|
39
|
+
export interface ReserveAccounts extends VaultReserve {
|
|
40
|
+
readonly liquiditySupplyVault: string;
|
|
41
|
+
readonly collateralMint: string;
|
|
42
|
+
}
|
|
43
|
+
/** The accounts both depositor instructions share, all derived from or read off the vault. */
|
|
44
|
+
export interface VaultAccounts {
|
|
45
|
+
readonly vaultState: string;
|
|
46
|
+
readonly tokenVault: string;
|
|
47
|
+
readonly tokenMint: string;
|
|
48
|
+
readonly baseVaultAuthority: string;
|
|
49
|
+
readonly sharesMint: string;
|
|
50
|
+
readonly vaultReserves: readonly VaultReserve[];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* `deposit(max_amount)`: the USER's tokens in, shares out to the user's own share account. The program
|
|
54
|
+
* refreshes every vault reserve first, which is why each reserve and its market follow the fixed accounts.
|
|
55
|
+
* There is no receiver: the shares can only land in the signer's own account.
|
|
56
|
+
*/
|
|
57
|
+
export declare function depositIx(p: VaultAccounts & {
|
|
58
|
+
user: string;
|
|
59
|
+
userTokenAta: string;
|
|
60
|
+
userSharesAta: string;
|
|
61
|
+
maxAmount: bigint;
|
|
62
|
+
}): Promise<Instruction>;
|
|
63
|
+
/**
|
|
64
|
+
* `withdraw(shares_amount)`: the user burns shares for the vault's token, paid from the vault's idle balance
|
|
65
|
+
* first and then from `reserve`. One reserve per instruction; a withdrawal larger than idle plus what one
|
|
66
|
+
* reserve can release takes one instruction per reserve. Paid to the signer's own token account only.
|
|
67
|
+
*/
|
|
68
|
+
export declare function withdrawIx(p: VaultAccounts & {
|
|
69
|
+
user: string;
|
|
70
|
+
userTokenAta: string;
|
|
71
|
+
userSharesAta: string;
|
|
72
|
+
sharesAmount: bigint;
|
|
73
|
+
reserve: ReserveAccounts;
|
|
74
|
+
}): Promise<Instruction>;
|
|
75
|
+
/**
|
|
76
|
+
* The Associated Token Account program's `CreateIdempotent` (instruction 1): creates `owner`'s token account for
|
|
77
|
+
* `mint` if it does not exist, and does nothing if it does. The payer is the owner, who pays its rent (about
|
|
78
|
+
* 0.002 SOL, returned if the account is ever closed). A first deposit needs it for the share account.
|
|
79
|
+
*/
|
|
80
|
+
export declare function createAtaIdempotentIx(owner: string, mint: string): Promise<Instruction>;
|
|
81
|
+
/**
|
|
82
|
+
* `SetComputeUnitLimit(units)` (Compute Budget instruction 2, a u32). A deposit or withdrawal refreshes every
|
|
83
|
+
* vault reserve inside the program, which can exceed the default 200,000 units.
|
|
84
|
+
*/
|
|
85
|
+
export declare function setComputeUnitLimitIx(units: number): Instruction;
|
|
86
|
+
/** A recent blockhash and the block height after which a transaction naming it can no longer land. */
|
|
87
|
+
export interface Lifetime {
|
|
88
|
+
readonly blockhash: string;
|
|
89
|
+
readonly lastValidBlockHeight: bigint;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A legacy transaction, UNSIGNED (every signature slot zero), as base64 wire bytes. The fee payer is the
|
|
93
|
+
* signer; a wallet adds the signature. It lands only before `lifetime.lastValidBlockHeight`, about a minute
|
|
94
|
+
* after the blockhash was read.
|
|
95
|
+
*/
|
|
96
|
+
export declare function compileUnsigned(instructions: readonly Instruction[], feePayer: string, lifetime: Lifetime): {
|
|
97
|
+
base64: string;
|
|
98
|
+
bytes: number;
|
|
99
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The depositor's side of a Kamino vault: the PDAs a deposit or withdrawal names, the two instructions
|
|
3
|
+
* (`deposit`, `withdraw`), the token-account instruction a first deposit needs, and an UNSIGNED transaction.
|
|
4
|
+
*
|
|
5
|
+
* 🔴 HAND-WRITTEN, PINNED TO THE VENDOR'S BYTES. Each builder must produce exactly the data bytes and the
|
|
6
|
+
* account metas (address, role, order) Kamino's own SDK produces for the same inputs:
|
|
7
|
+
* `tests/solana-kvault.unit.test.ts` asserts byte equality against `vendor/kamino/instruction-fixtures.json`,
|
|
8
|
+
* written by Kamino's SDK (`vendor/kamino/generate-fixtures.mjs`, version in `vendor/kamino/PINNED`).
|
|
9
|
+
*
|
|
10
|
+
* Nothing here signs or sends. The signer slot is an ADDRESS: the operator's wallet signs.
|
|
11
|
+
*/
|
|
12
|
+
import { AccountRole, address, appendTransactionMessageInstructions, compileTransaction, createTransactionMessage, getAddressEncoder, getBase64EncodedWireTransaction, getProgramDerivedAddress, pipe, setTransactionMessageFeePayer, setTransactionMessageLifetimeUsingBlockhash, } from "@solana/kit";
|
|
13
|
+
import { ASSOCIATED_TOKEN_PROGRAM_ID, COMPUTE_BUDGET_PROGRAM_ID, KLEND_PROGRAM_ID, KVAULT_PROGRAM_ID, MAX_COMPUTE_UNITS, SEEDS, SPL_TOKEN_PROGRAM_ID, SYSTEM_PROGRAM_ID, SYSVAR_INSTRUCTIONS_ID, } from "./constants.js";
|
|
14
|
+
/** `sha256("global:<name>")[0..8]`, per instruction. */
|
|
15
|
+
export const DISCRIMINATORS = {
|
|
16
|
+
deposit: [242, 35, 198, 137, 82, 225, 242, 182],
|
|
17
|
+
withdraw: [183, 18, 70, 156, 148, 109, 161, 34],
|
|
18
|
+
};
|
|
19
|
+
const encoder = getAddressEncoder();
|
|
20
|
+
const utf8 = (s) => new TextEncoder().encode(s);
|
|
21
|
+
const ro = (a) => ({ address: address(a), role: AccountRole.READONLY });
|
|
22
|
+
const rw = (a) => ({ address: address(a), role: AccountRole.WRITABLE });
|
|
23
|
+
const signer = (a, writable) => ({
|
|
24
|
+
address: address(a),
|
|
25
|
+
role: writable ? AccountRole.WRITABLE_SIGNER : AccountRole.READONLY_SIGNER,
|
|
26
|
+
});
|
|
27
|
+
function u64le(v) {
|
|
28
|
+
if (v < 0n || v > 0xffffffffffffffffn)
|
|
29
|
+
throw new Error(`${v} does not fit a u64`);
|
|
30
|
+
const b = new Uint8Array(8);
|
|
31
|
+
new DataView(b.buffer).setBigUint64(0, v, true);
|
|
32
|
+
return b;
|
|
33
|
+
}
|
|
34
|
+
function concat(...parts) {
|
|
35
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
36
|
+
let i = 0;
|
|
37
|
+
for (const p of parts) {
|
|
38
|
+
out.set(p, i);
|
|
39
|
+
i += p.length;
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
async function pda(program, seeds) {
|
|
44
|
+
const [found] = await getProgramDerivedAddress({
|
|
45
|
+
programAddress: address(program),
|
|
46
|
+
seeds: seeds.map((s) => (typeof s === "string" ? encoder.encode(address(s)) : utf8(s.text))),
|
|
47
|
+
});
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
/** Every PDA a vault owns, from its vault-state address alone. */
|
|
51
|
+
export async function vaultPdas(vaultState) {
|
|
52
|
+
const [tokenVault, baseVaultAuthority, sharesMint] = await Promise.all([
|
|
53
|
+
pda(KVAULT_PROGRAM_ID, [{ text: SEEDS.tokenVault }, vaultState]),
|
|
54
|
+
pda(KVAULT_PROGRAM_ID, [{ text: SEEDS.baseVaultAuthority }, vaultState]),
|
|
55
|
+
pda(KVAULT_PROGRAM_ID, [{ text: SEEDS.shares }, vaultState]),
|
|
56
|
+
]);
|
|
57
|
+
return { tokenVault, baseVaultAuthority, sharesMint };
|
|
58
|
+
}
|
|
59
|
+
/** The vault's ctoken account for one reserve. */
|
|
60
|
+
export const ctokenVaultPda = (vaultState, reserve) => pda(KVAULT_PROGRAM_ID, [{ text: SEEDS.ctokenVault }, vaultState, reserve]);
|
|
61
|
+
export const kvaultGlobalConfigPda = () => pda(KVAULT_PROGRAM_ID, [{ text: SEEDS.globalConfig }]);
|
|
62
|
+
/** Anchor's event-CPI authority for the vault program: `deposit` and `withdraw` name it. */
|
|
63
|
+
export const kvaultEventAuthorityPda = () => pda(KVAULT_PROGRAM_ID, [{ text: SEEDS.eventAuthority }]);
|
|
64
|
+
/** The lending program's authority for one market. */
|
|
65
|
+
export const lendingMarketAuthorityPda = (lendingMarket) => pda(KLEND_PROGRAM_ID, [{ text: SEEDS.lendingMarketAuthority }, lendingMarket]);
|
|
66
|
+
/** The owner's associated token account for `mint` (SPL Token). */
|
|
67
|
+
export async function associatedTokenAccount(owner, mint) {
|
|
68
|
+
const [found] = await getProgramDerivedAddress({
|
|
69
|
+
programAddress: address(ASSOCIATED_TOKEN_PROGRAM_ID),
|
|
70
|
+
seeds: [encoder.encode(address(owner)), encoder.encode(address(SPL_TOKEN_PROGRAM_ID)), encoder.encode(address(mint))],
|
|
71
|
+
});
|
|
72
|
+
return found;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* `deposit(max_amount)`: the USER's tokens in, shares out to the user's own share account. The program
|
|
76
|
+
* refreshes every vault reserve first, which is why each reserve and its market follow the fixed accounts.
|
|
77
|
+
* There is no receiver: the shares can only land in the signer's own account.
|
|
78
|
+
*/
|
|
79
|
+
export async function depositIx(p) {
|
|
80
|
+
return {
|
|
81
|
+
programAddress: address(KVAULT_PROGRAM_ID),
|
|
82
|
+
accounts: [
|
|
83
|
+
signer(p.user, true),
|
|
84
|
+
rw(p.vaultState),
|
|
85
|
+
rw(p.tokenVault),
|
|
86
|
+
ro(p.tokenMint),
|
|
87
|
+
ro(p.baseVaultAuthority),
|
|
88
|
+
rw(p.sharesMint),
|
|
89
|
+
rw(p.userTokenAta),
|
|
90
|
+
rw(p.userSharesAta),
|
|
91
|
+
ro(KLEND_PROGRAM_ID),
|
|
92
|
+
ro(SPL_TOKEN_PROGRAM_ID),
|
|
93
|
+
ro(SPL_TOKEN_PROGRAM_ID),
|
|
94
|
+
ro(await kvaultEventAuthorityPda()),
|
|
95
|
+
ro(KVAULT_PROGRAM_ID),
|
|
96
|
+
...p.vaultReserves.map((r) => rw(r.reserve)),
|
|
97
|
+
...p.vaultReserves.map((r) => ro(r.lendingMarket)),
|
|
98
|
+
],
|
|
99
|
+
data: concat(DISCRIMINATORS.deposit, u64le(p.maxAmount)),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* `withdraw(shares_amount)`: the user burns shares for the vault's token, paid from the vault's idle balance
|
|
104
|
+
* first and then from `reserve`. One reserve per instruction; a withdrawal larger than idle plus what one
|
|
105
|
+
* reserve can release takes one instruction per reserve. Paid to the signer's own token account only.
|
|
106
|
+
*/
|
|
107
|
+
export async function withdrawIx(p) {
|
|
108
|
+
const [ctokenVault, lma, eventAuthority, globalConfig] = await Promise.all([
|
|
109
|
+
ctokenVaultPda(p.vaultState, p.reserve.reserve),
|
|
110
|
+
lendingMarketAuthorityPda(p.reserve.lendingMarket),
|
|
111
|
+
kvaultEventAuthorityPda(),
|
|
112
|
+
kvaultGlobalConfigPda(),
|
|
113
|
+
]);
|
|
114
|
+
return {
|
|
115
|
+
programAddress: address(KVAULT_PROGRAM_ID),
|
|
116
|
+
accounts: [
|
|
117
|
+
// withdrawFromAvailable
|
|
118
|
+
signer(p.user, false),
|
|
119
|
+
rw(p.vaultState),
|
|
120
|
+
ro(globalConfig),
|
|
121
|
+
rw(p.tokenVault),
|
|
122
|
+
ro(p.baseVaultAuthority),
|
|
123
|
+
rw(p.userTokenAta),
|
|
124
|
+
rw(p.tokenMint),
|
|
125
|
+
rw(p.userSharesAta),
|
|
126
|
+
rw(p.sharesMint),
|
|
127
|
+
ro(SPL_TOKEN_PROGRAM_ID),
|
|
128
|
+
ro(SPL_TOKEN_PROGRAM_ID),
|
|
129
|
+
ro(KLEND_PROGRAM_ID),
|
|
130
|
+
ro(eventAuthority),
|
|
131
|
+
ro(KVAULT_PROGRAM_ID),
|
|
132
|
+
// withdrawFromReserveAccounts
|
|
133
|
+
rw(p.vaultState),
|
|
134
|
+
rw(p.reserve.reserve),
|
|
135
|
+
rw(ctokenVault),
|
|
136
|
+
ro(p.reserve.lendingMarket),
|
|
137
|
+
ro(lma),
|
|
138
|
+
rw(p.reserve.liquiditySupplyVault),
|
|
139
|
+
rw(p.reserve.collateralMint),
|
|
140
|
+
ro(SPL_TOKEN_PROGRAM_ID),
|
|
141
|
+
ro(SYSVAR_INSTRUCTIONS_ID),
|
|
142
|
+
ro(eventAuthority),
|
|
143
|
+
ro(KVAULT_PROGRAM_ID),
|
|
144
|
+
...p.vaultReserves.map((r) => rw(r.reserve)),
|
|
145
|
+
...p.vaultReserves.map((r) => ro(r.lendingMarket)),
|
|
146
|
+
],
|
|
147
|
+
data: concat(DISCRIMINATORS.withdraw, u64le(p.sharesAmount)),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The Associated Token Account program's `CreateIdempotent` (instruction 1): creates `owner`'s token account for
|
|
152
|
+
* `mint` if it does not exist, and does nothing if it does. The payer is the owner, who pays its rent (about
|
|
153
|
+
* 0.002 SOL, returned if the account is ever closed). A first deposit needs it for the share account.
|
|
154
|
+
*/
|
|
155
|
+
export async function createAtaIdempotentIx(owner, mint) {
|
|
156
|
+
return {
|
|
157
|
+
programAddress: address(ASSOCIATED_TOKEN_PROGRAM_ID),
|
|
158
|
+
accounts: [
|
|
159
|
+
signer(owner, true),
|
|
160
|
+
rw(await associatedTokenAccount(owner, mint)),
|
|
161
|
+
ro(owner),
|
|
162
|
+
ro(mint),
|
|
163
|
+
ro(SYSTEM_PROGRAM_ID),
|
|
164
|
+
ro(SPL_TOKEN_PROGRAM_ID),
|
|
165
|
+
],
|
|
166
|
+
data: Uint8Array.from([1]),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* `SetComputeUnitLimit(units)` (Compute Budget instruction 2, a u32). A deposit or withdrawal refreshes every
|
|
171
|
+
* vault reserve inside the program, which can exceed the default 200,000 units.
|
|
172
|
+
*/
|
|
173
|
+
export function setComputeUnitLimitIx(units) {
|
|
174
|
+
if (!Number.isInteger(units) || units <= 0 || units > MAX_COMPUTE_UNITS) {
|
|
175
|
+
throw new Error(`compute unit limit must be an integer in (0, ${MAX_COMPUTE_UNITS}], got ${units}`);
|
|
176
|
+
}
|
|
177
|
+
const data = new Uint8Array(5);
|
|
178
|
+
data[0] = 2;
|
|
179
|
+
new DataView(data.buffer).setUint32(1, units, true);
|
|
180
|
+
return { programAddress: address(COMPUTE_BUDGET_PROGRAM_ID), data };
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* A legacy transaction, UNSIGNED (every signature slot zero), as base64 wire bytes. The fee payer is the
|
|
184
|
+
* signer; a wallet adds the signature. It lands only before `lifetime.lastValidBlockHeight`, about a minute
|
|
185
|
+
* after the blockhash was read.
|
|
186
|
+
*/
|
|
187
|
+
export function compileUnsigned(instructions, feePayer, lifetime) {
|
|
188
|
+
const message = pipe(createTransactionMessage({ version: "legacy" }), (m) => setTransactionMessageFeePayer(address(feePayer), m), (m) => setTransactionMessageLifetimeUsingBlockhash({ blockhash: lifetime.blockhash, lastValidBlockHeight: lifetime.lastValidBlockHeight }, m), (m) => appendTransactionMessageInstructions(instructions, m));
|
|
189
|
+
const base64 = getBase64EncodedWireTransaction(compileTransaction(message));
|
|
190
|
+
return { base64, bytes: Buffer.from(base64, "base64").length };
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=kvault.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kvault.js","sourceRoot":"","sources":["../../src/solana/kvault.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EACL,WAAW,EACX,OAAO,EACP,oCAAoC,EACpC,kBAAkB,EAClB,wBAAwB,EACxB,iBAAiB,EACjB,+BAA+B,EAC/B,wBAAwB,EACxB,IAAI,EACJ,6BAA6B,EAC7B,2CAA2C,GAK5C,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,2BAA2B,EAC3B,yBAAyB,EACzB,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,KAAK,EACL,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,GACvB,MAAM,gBAAgB,CAAC;AAExB,wDAAwD;AACxD,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;IAC/C,QAAQ,EAAE,CAAC,GAAG,EAAE,EAAE,EAA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C,CAAC;IAC/B,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IACZ,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IACpD,OAAO,EAAE,cAAc,EAAE,OAAO,CAAC,yBAAyB,CAAC,EAAE,IAAI,EAAE,CAAC;AACtE,CAAC;AAQD;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,YAAoC,EAAE,QAAgB,EAAE,QAAkB;IACxG,MAAM,OAAO,GAAG,IAAI,CAClB,wBAAwB,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,EAC/C,CAAC,CAAC,EAAE,EAAE,CAAC,6BAA6B,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,EAC1D,CAAC,CAAC,EAAE,EAAE,CAAC,2CAA2C,CAAC,EAAE,SAAS,EAAE,QAAQ,CAAC,SAAsB,EAAE,oBAAoB,EAAE,QAAQ,CAAC,oBAAoB,EAAE,EAAE,CAAC,CAAC,EAC1J,CAAC,CAAC,EAAE,EAAE,CAAC,oCAAoC,CAAC,YAAY,EAAE,CAAC,CAAC,CAC7D,CAAC;IACF,MAAM,MAAM,GAAG,+BAA+B,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC;IAC5E,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC,MAAM,EAAE,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/** klend's fixed-point scale: a `*_sf` field is value × 2^60. */
|
|
2
|
+
export declare const SF_ONE: bigint;
|
|
3
|
+
/** `sha256("account:VaultState")[0..8]` — Anchor's account discriminator. */
|
|
4
|
+
export declare const VAULT_STATE_DISCRIMINATOR: Uint8Array<ArrayBuffer>;
|
|
5
|
+
/** `sha256("account:Reserve")[0..8]`. */
|
|
6
|
+
export declare const RESERVE_DISCRIMINATOR: Uint8Array<ArrayBuffer>;
|
|
7
|
+
/** Byte offsets INTO THE ACCOUNT DATA (the 8-byte discriminator included). */
|
|
8
|
+
export declare const VAULT_STATE_OFFSETS: {
|
|
9
|
+
readonly vaultAdminAuthority: 8;
|
|
10
|
+
readonly baseVaultAuthority: 40;
|
|
11
|
+
readonly tokenMint: 80;
|
|
12
|
+
readonly tokenMintDecimals: 112;
|
|
13
|
+
readonly tokenVault: 120;
|
|
14
|
+
readonly tokenProgram: 152;
|
|
15
|
+
readonly sharesMint: 184;
|
|
16
|
+
readonly sharesMintDecimals: 216;
|
|
17
|
+
readonly tokenAvailable: 224;
|
|
18
|
+
readonly sharesIssued: 232;
|
|
19
|
+
readonly unallocatedWeight: 248;
|
|
20
|
+
readonly performanceFeeBps: 256;
|
|
21
|
+
readonly managementFeeBps: 264;
|
|
22
|
+
readonly pendingFeesSf: 296;
|
|
23
|
+
readonly allocations: 312;
|
|
24
|
+
readonly minInvestAmount: 58424;
|
|
25
|
+
readonly minInvestDelaySlots: 58432;
|
|
26
|
+
readonly pendingAdmin: 58448;
|
|
27
|
+
readonly name: 58528;
|
|
28
|
+
readonly unallocatedTokensCap: 58640;
|
|
29
|
+
readonly allocationAdmin: 58648;
|
|
30
|
+
readonly allowAllocationsInWhitelistedReservesOnly: 58728;
|
|
31
|
+
readonly allowInvestInWhitelistedReservesOnly: 58729;
|
|
32
|
+
readonly depositCap: 58736;
|
|
33
|
+
readonly permissioningAuthority: 58840;
|
|
34
|
+
};
|
|
35
|
+
/** One `VaultAllocation` slot: its size, and each field's offset WITHIN the slot. */
|
|
36
|
+
export declare const ALLOCATION_SLOT: {
|
|
37
|
+
readonly size: 2160;
|
|
38
|
+
readonly reserve: 0;
|
|
39
|
+
readonly ctokenVault: 32;
|
|
40
|
+
readonly targetAllocationWeight: 64;
|
|
41
|
+
readonly tokenAllocationCap: 72;
|
|
42
|
+
readonly ctokenAllocationCap: 88;
|
|
43
|
+
readonly ctokenAllocation: 1104;
|
|
44
|
+
readonly lastInvestSlot: 1112;
|
|
45
|
+
};
|
|
46
|
+
export declare const RESERVE_OFFSETS: {
|
|
47
|
+
readonly lastUpdateSlot: 16;
|
|
48
|
+
readonly lendingMarket: 32;
|
|
49
|
+
readonly liquidityMint: 128;
|
|
50
|
+
readonly liquiditySupplyVault: 160;
|
|
51
|
+
readonly totalAvailableAmount: 224;
|
|
52
|
+
readonly borrowedAmountSf: 232;
|
|
53
|
+
readonly liquidityMintDecimals: 272;
|
|
54
|
+
readonly accumulatedProtocolFeesSf: 344;
|
|
55
|
+
readonly accumulatedReferrerFeesSf: 360;
|
|
56
|
+
readonly pendingReferrerFeesSf: 376;
|
|
57
|
+
readonly collateralMint: 2560;
|
|
58
|
+
readonly collateralMintTotalSupply: 2592;
|
|
59
|
+
readonly depositLimit: 5016;
|
|
60
|
+
};
|
|
61
|
+
export interface VaultAllocation {
|
|
62
|
+
readonly reserve: string;
|
|
63
|
+
readonly ctokenVault: string;
|
|
64
|
+
readonly targetAllocationWeight: bigint;
|
|
65
|
+
readonly tokenAllocationCap: bigint;
|
|
66
|
+
readonly ctokenAllocationCap: bigint;
|
|
67
|
+
readonly ctokenAllocation: bigint;
|
|
68
|
+
readonly lastInvestSlot: bigint;
|
|
69
|
+
}
|
|
70
|
+
export interface VaultStateView {
|
|
71
|
+
readonly vaultAdminAuthority: string;
|
|
72
|
+
readonly baseVaultAuthority: string;
|
|
73
|
+
readonly tokenMint: string;
|
|
74
|
+
readonly tokenMintDecimals: bigint;
|
|
75
|
+
readonly tokenVault: string;
|
|
76
|
+
readonly tokenProgram: string;
|
|
77
|
+
readonly sharesMint: string;
|
|
78
|
+
readonly sharesMintDecimals: bigint;
|
|
79
|
+
readonly tokenAvailable: bigint;
|
|
80
|
+
readonly sharesIssued: bigint;
|
|
81
|
+
readonly unallocatedWeight: bigint;
|
|
82
|
+
readonly performanceFeeBps: bigint;
|
|
83
|
+
readonly managementFeeBps: bigint;
|
|
84
|
+
readonly pendingFeesSf: bigint;
|
|
85
|
+
/** Occupied slots only, in slot order (an empty slot's reserve is the default key). */
|
|
86
|
+
readonly allocations: readonly VaultAllocation[];
|
|
87
|
+
readonly minInvestAmount: bigint;
|
|
88
|
+
readonly minInvestDelaySlots: bigint;
|
|
89
|
+
readonly pendingAdmin: string;
|
|
90
|
+
readonly name: string;
|
|
91
|
+
readonly unallocatedTokensCap: bigint;
|
|
92
|
+
readonly allocationAdmin: string;
|
|
93
|
+
readonly allowAllocationsInWhitelistedReservesOnly: number;
|
|
94
|
+
readonly allowInvestInWhitelistedReservesOnly: number;
|
|
95
|
+
readonly depositCap: bigint;
|
|
96
|
+
readonly permissioningAuthority: string;
|
|
97
|
+
}
|
|
98
|
+
export interface ReserveView {
|
|
99
|
+
readonly lastUpdateSlot: bigint;
|
|
100
|
+
readonly lendingMarket: string;
|
|
101
|
+
readonly liquidityMint: string;
|
|
102
|
+
readonly liquiditySupplyVault: string;
|
|
103
|
+
readonly totalAvailableAmount: bigint;
|
|
104
|
+
readonly borrowedAmountSf: bigint;
|
|
105
|
+
readonly liquidityMintDecimals: bigint;
|
|
106
|
+
readonly accumulatedProtocolFeesSf: bigint;
|
|
107
|
+
readonly accumulatedReferrerFeesSf: bigint;
|
|
108
|
+
readonly pendingReferrerFeesSf: bigint;
|
|
109
|
+
readonly collateralMint: string;
|
|
110
|
+
readonly collateralMintTotalSupply: bigint;
|
|
111
|
+
readonly depositLimit: bigint;
|
|
112
|
+
}
|
|
113
|
+
export declare function decodeVaultState(data: Uint8Array): VaultStateView;
|
|
114
|
+
export declare function decodeReserve(data: Uint8Array): ReserveView;
|
|
115
|
+
/**
|
|
116
|
+
* A reserve's total supply in liquidity units, scaled by 2^60: available + borrowed − every fee bucket.
|
|
117
|
+
* klend-sdk's `Reserve.getTotalSupplySf()`, the numerator of the collateral exchange rate.
|
|
118
|
+
*/
|
|
119
|
+
export declare function totalSupplySf(r: ReserveView): bigint;
|
|
120
|
+
/**
|
|
121
|
+
* What `ctokens` of this reserve are worth in liquidity base units, at the reserve's STORED state
|
|
122
|
+
* (floored): ctokens × totalSupply / ctokenSupply. A reserve accrues interest only when refreshed, so this
|
|
123
|
+
* lags the true value by the interest since `lastUpdateSlot` — conservative, and what klend-sdk's
|
|
124
|
+
* `getCollateralExchangeRate()` (the "stale" rate) also reports.
|
|
125
|
+
*/
|
|
126
|
+
export declare function ctokensToLiquidity(ctokens: bigint, r: ReserveView): bigint;
|
|
127
|
+
/**
|
|
128
|
+
* A reserve's share price — liquidity per ctoken — as an 18-decimal fixed-point bigint. `0n` for a reserve
|
|
129
|
+
* with no ctokens.
|
|
130
|
+
*/
|
|
131
|
+
export declare function reserveSharePrice1e18(r: ReserveView): bigint;
|
|
132
|
+
/** What the vault holds, by reserve and in total: kvault's own AUM rule, at stored reserve state. */
|
|
133
|
+
export interface VaultHoldings {
|
|
134
|
+
readonly idle: bigint;
|
|
135
|
+
readonly byReserve: ReadonlyMap<string, bigint>;
|
|
136
|
+
/** idle + Σ reserves, BEFORE pending fees (klend-sdk's `totalAUMIncludingFees`). */
|
|
137
|
+
readonly aumIncludingFees: bigint;
|
|
138
|
+
/** Pending fees, floored to base units. */
|
|
139
|
+
readonly pendingFees: bigint;
|
|
140
|
+
/** What the depositors own: AUM − pending fees. */
|
|
141
|
+
readonly netAum: bigint;
|
|
142
|
+
}
|
|
143
|
+
export declare function vaultHoldings(vault: VaultStateView, reserves: ReadonlyMap<string, ReserveView>): VaultHoldings;
|