@omnipair/dusk-sdk 2.0.1 → 2.16.0-next.20261102
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 +1085 -26
- package/dist/amounts.d.ts +9 -0
- package/dist/amounts.d.ts.map +1 -0
- package/dist/amounts.js +10 -0
- package/dist/amounts.js.map +1 -0
- package/dist/constants.d.ts +80 -33
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +175 -64
- package/dist/constants.js.map +1 -1
- package/dist/delegate.d.ts +94 -0
- package/dist/delegate.d.ts.map +1 -0
- package/dist/delegate.js +131 -0
- package/dist/delegate.js.map +1 -0
- package/dist/dusk.d.ts +14 -0
- package/dist/dusk.d.ts.map +1 -1
- package/dist/dusk.js +22 -0
- package/dist/dusk.js.map +1 -1
- package/dist/get.d.ts +3285 -237
- package/dist/get.d.ts.map +1 -1
- package/dist/get.js +140 -71
- package/dist/get.js.map +1 -1
- package/dist/governance-client.d.ts +169 -0
- package/dist/governance-client.d.ts.map +1 -0
- package/dist/governance-client.js +146 -0
- package/dist/governance-client.js.map +1 -0
- package/dist/governance.d.ts +313 -0
- package/dist/governance.d.ts.map +1 -0
- package/dist/governance.js +902 -0
- package/dist/governance.js.map +1 -0
- package/dist/hash.d.ts +9 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +16 -0
- package/dist/hash.js.map +1 -0
- package/dist/hlp-liquidation-client.d.ts +106 -0
- package/dist/hlp-liquidation-client.d.ts.map +1 -0
- package/dist/hlp-liquidation-client.js +150 -0
- package/dist/hlp-liquidation-client.js.map +1 -0
- package/dist/idl_delegate.d.ts +3 -0
- package/dist/idl_delegate.d.ts.map +1 -0
- package/dist/idl_delegate.js +3 -0
- package/dist/idl_delegate.js.map +1 -0
- package/dist/idl_delegate.json +6119 -0
- package/dist/idl_v2.json +18372 -6121
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -1
- package/dist/interest-accounting.d.ts +49 -0
- package/dist/interest-accounting.d.ts.map +1 -0
- package/dist/interest-accounting.js +72 -0
- package/dist/interest-accounting.js.map +1 -0
- package/dist/leverage-actions.d.ts +122 -0
- package/dist/leverage-actions.d.ts.map +1 -0
- package/dist/leverage-actions.js +228 -0
- package/dist/leverage-actions.js.map +1 -0
- package/dist/leverage-margins.d.ts +12 -0
- package/dist/leverage-margins.d.ts.map +1 -0
- package/dist/leverage-margins.js +45 -0
- package/dist/leverage-margins.js.map +1 -0
- package/dist/leverage-preview.d.ts +587 -0
- package/dist/leverage-preview.d.ts.map +1 -0
- package/dist/leverage-preview.js +89 -0
- package/dist/leverage-preview.js.map +1 -0
- package/dist/liquidation-client.d.ts +230 -0
- package/dist/liquidation-client.d.ts.map +1 -0
- package/dist/liquidation-client.js +239 -0
- package/dist/liquidation-client.js.map +1 -0
- package/dist/liquidation.d.ts +78 -0
- package/dist/liquidation.d.ts.map +1 -0
- package/dist/liquidation.js +122 -0
- package/dist/liquidation.js.map +1 -0
- package/dist/lp-naming.d.ts +43 -0
- package/dist/lp-naming.d.ts.map +1 -0
- package/dist/lp-naming.js +81 -0
- package/dist/lp-naming.js.map +1 -0
- package/dist/lp-vanity.d.ts +33 -0
- package/dist/lp-vanity.d.ts.map +1 -0
- package/dist/lp-vanity.js +94 -0
- package/dist/lp-vanity.js.map +1 -0
- package/dist/market-bootstrap.d.ts +109 -0
- package/dist/market-bootstrap.d.ts.map +1 -0
- package/dist/market-bootstrap.js +158 -0
- package/dist/market-bootstrap.js.map +1 -0
- package/dist/market-launch.d.ts +45 -0
- package/dist/market-launch.d.ts.map +1 -0
- package/dist/market-launch.js +76 -0
- package/dist/market-launch.js.map +1 -0
- package/dist/oracle.d.ts +46 -0
- package/dist/oracle.d.ts.map +1 -0
- package/dist/oracle.js +103 -0
- package/dist/oracle.js.map +1 -0
- package/dist/preview.d.ts +39 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +12 -0
- package/dist/preview.js.map +1 -1
- package/dist/protection.d.ts +48 -0
- package/dist/protection.d.ts.map +1 -0
- package/dist/protection.js +88 -0
- package/dist/protection.js.map +1 -0
- package/dist/referral.d.ts +28 -28
- package/dist/referral.d.ts.map +1 -1
- package/dist/referral.js +41 -33
- package/dist/referral.js.map +1 -1
- package/dist/risk-pricing.d.ts +14 -0
- package/dist/risk-pricing.d.ts.map +1 -0
- package/dist/risk-pricing.js +25 -0
- package/dist/risk-pricing.js.map +1 -0
- package/dist/simulation.d.ts +45 -0
- package/dist/simulation.d.ts.map +1 -0
- package/dist/simulation.js +106 -0
- package/dist/simulation.js.map +1 -0
- package/dist/tranche-client.d.ts +121 -0
- package/dist/tranche-client.d.ts.map +1 -0
- package/dist/tranche-client.js +185 -0
- package/dist/tranche-client.js.map +1 -0
- package/dist/transfer-fees.d.ts +46 -0
- package/dist/transfer-fees.d.ts.map +1 -0
- package/dist/transfer-fees.js +76 -0
- package/dist/transfer-fees.js.map +1 -0
- package/dist/type-aliases.d.ts +271 -30
- package/dist/type-aliases.d.ts.map +1 -1
- package/dist/types_delegate.d.ts +6126 -0
- package/dist/types_delegate.d.ts.map +1 -0
- package/dist/types_delegate.js +2 -0
- package/dist/types_delegate.js.map +1 -0
- package/dist/types_v2.d.ts +18105 -5854
- package/dist/types_v2.d.ts.map +1 -1
- package/dist/virtual-book/index.d.ts +4 -0
- package/dist/virtual-book/index.d.ts.map +1 -0
- package/dist/virtual-book/index.js +4 -0
- package/dist/virtual-book/index.js.map +1 -0
- package/dist/virtual-book/market.d.ts +608 -0
- package/dist/virtual-book/market.d.ts.map +1 -0
- package/dist/virtual-book/market.js +59 -0
- package/dist/virtual-book/market.js.map +1 -0
- package/dist/virtual-book/projection.d.ts +44 -0
- package/dist/virtual-book/projection.d.ts.map +1 -0
- package/dist/virtual-book/projection.js +229 -0
- package/dist/virtual-book/projection.js.map +1 -0
- package/dist/virtual-book/quotes.d.ts +1241 -0
- package/dist/virtual-book/quotes.d.ts.map +1 -0
- package/dist/virtual-book/quotes.js +233 -0
- package/dist/virtual-book/quotes.js.map +1 -0
- package/dist/write.d.ts +584 -14
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +1371 -47
- package/dist/write.js.map +1 -1
- package/dist/yield-client.d.ts +51 -0
- package/dist/yield-client.d.ts.map +1 -0
- package/dist/yield-client.js +98 -0
- package/dist/yield-client.js.map +1 -0
- package/package.json +9 -2
- package/src/amounts.ts +13 -0
- package/src/constants.ts +276 -111
- package/src/delegate.ts +238 -0
- package/src/dusk.ts +28 -0
- package/src/get.ts +267 -90
- package/src/governance-client.ts +234 -0
- package/src/governance.ts +1422 -0
- package/src/hash.ts +15 -0
- package/src/hlp-liquidation-client.ts +195 -0
- package/src/idl_delegate.json +6119 -0
- package/src/idl_delegate.ts +3 -0
- package/src/idl_v2.json +18372 -6121
- package/src/index.ts +31 -0
- package/src/interest-accounting.ts +85 -0
- package/src/leverage-actions.ts +216 -0
- package/src/leverage-margins.ts +48 -0
- package/src/leverage-preview.ts +86 -0
- package/src/liquidation-client.ts +297 -0
- package/src/liquidation.ts +137 -0
- package/src/lp-naming.ts +127 -0
- package/src/lp-vanity.ts +135 -0
- package/src/market-bootstrap.ts +273 -0
- package/src/market-launch.ts +103 -0
- package/src/oracle.ts +120 -0
- package/src/preview.ts +53 -1
- package/src/protection.ts +112 -0
- package/src/referral.ts +71 -71
- package/src/risk-pricing.ts +28 -0
- package/src/simulation.ts +169 -0
- package/src/tranche-client.ts +187 -0
- package/src/transfer-fees.ts +99 -0
- package/src/type-aliases.ts +280 -30
- package/src/types_delegate.ts +6125 -0
- package/src/types_v2.ts +18372 -6121
- package/src/virtual-book/index.ts +3 -0
- package/src/virtual-book/market.ts +76 -0
- package/src/virtual-book/projection.ts +265 -0
- package/src/virtual-book/quotes.ts +308 -0
- package/src/write.ts +2394 -67
- package/src/yield-client.ts +117 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { IdlTypes } from "@coral-xyz/anchor";
|
|
2
|
+
import { PublicKey, type AccountMeta } from "@solana/web3.js";
|
|
3
|
+
import type { LeverageDelegate } from "./types_delegate.js";
|
|
4
|
+
import { type LeverageDelegateProgram } from "./delegate.js";
|
|
5
|
+
import { type RawAmount } from "./write.js";
|
|
6
|
+
export declare const PROTECTION_ACTION: {
|
|
7
|
+
readonly borrowRepay: 0;
|
|
8
|
+
readonly borrowCollateral: 1;
|
|
9
|
+
readonly leverageRepay: 2;
|
|
10
|
+
};
|
|
11
|
+
export declare function deriveProtectionOrderAddress(owner: PublicKey, orderId: RawAmount, programId?: PublicKey): [PublicKey, number];
|
|
12
|
+
type MethodAccounts<T extends keyof LeverageDelegateProgram["methods"]> = Parameters<ReturnType<LeverageDelegateProgram["methods"][T]>["accountsStrict"]>[0];
|
|
13
|
+
type CreateAccounts = Omit<MethodAccounts<"createProtectionOrder">, "order" | "duskEventAuthority" | "duskProgram" | "token2022Program" | "systemProgram"> & {
|
|
14
|
+
owner: PublicKey;
|
|
15
|
+
};
|
|
16
|
+
type ManageAccounts = Omit<MethodAccounts<"fundProtectionOrder">, "token2022Program">;
|
|
17
|
+
type ExecuteAccounts = Omit<MethodAccounts<"executeProtectionOrder">, "duskEventAuthority" | "duskProgram" | "tokenProgram" | "token2022Program">;
|
|
18
|
+
type CreateArgs = IdlTypes<LeverageDelegate>["createProtectionOrderArgs"];
|
|
19
|
+
/** All amounts use raw token units. Health 10000 is the liquidation boundary. */
|
|
20
|
+
export type CreateProtectionOrderParams = {
|
|
21
|
+
[K in keyof CreateArgs]: CreateArgs[K] extends number ? number : RawAmount;
|
|
22
|
+
};
|
|
23
|
+
/** Fund recurring protection for an existing borrowing or leverage position.
|
|
24
|
+
* Setup the order's LP ATA, asset ATAs and both yield accounts first. LP transfers
|
|
25
|
+
* require buildLpTransferHookAccountMetas. The sponsor remains yield recipient.
|
|
26
|
+
* Execution needs a keeper token balance to advance repayment; LP redemption
|
|
27
|
+
* reimburses that balance atomically. Insufficient redemption liquidity reverts.
|
|
28
|
+
*/
|
|
29
|
+
export declare class DuskProtectionOrders {
|
|
30
|
+
readonly program: LeverageDelegateProgram;
|
|
31
|
+
constructor(program: LeverageDelegateProgram);
|
|
32
|
+
private oracleAccounts;
|
|
33
|
+
createInstruction(params: CreateProtectionOrderParams, accounts: CreateAccounts, remainingAccounts?: AccountMeta[]): Promise<import("@solana/web3.js").TransactionInstruction>;
|
|
34
|
+
previewInstruction(accounts: MethodAccounts<"previewProtectionOrder">, remainingAccounts?: AccountMeta[]): Promise<import("@solana/web3.js").TransactionInstruction>;
|
|
35
|
+
fundInstruction(lpAmount: RawAmount, accounts: ManageAccounts, remainingAccounts?: AccountMeta[]): Promise<import("@solana/web3.js").TransactionInstruction>;
|
|
36
|
+
cancelInstruction(accounts: ManageAccounts, remainingAccounts?: AccountMeta[]): Promise<import("@solana/web3.js").TransactionInstruction>;
|
|
37
|
+
/** LP redemptions may need an address lookup table and a larger heap frame.
|
|
38
|
+
* Simulate the complete transaction with the current native market state.
|
|
39
|
+
* LP proceeds must cover payment, the full keeper reward, and the additional
|
|
40
|
+
* 10 bps protocol fee. Supply protocolFee from orderProtocolFeeAccounts.
|
|
41
|
+
*/
|
|
42
|
+
executeInstruction(params: {
|
|
43
|
+
lpAmount: RawAmount;
|
|
44
|
+
paymentAmount: RawAmount;
|
|
45
|
+
}, accounts: ExecuteAccounts, remainingAccounts?: AccountMeta[]): Promise<import("@solana/web3.js").TransactionInstruction>;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=protection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protection.d.ts","sourceRoot":"","sources":["../src/protection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,KAAK,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAG9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MA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OAAO,EAAE,uBAAuB;YAEvC,cAAc;IAUtB,iBAAiB,CAAC,MAAM,EAAE,2BAA2B,EAAE,QAAQ,EAAE,cAAc,EACnF,iBAAiB,GAAE,WAAW,EAAO;IAmBjC,kBAAkB,CAAC,QAAQ,EAAE,cAAc,CAAC,wBAAwB,CAAC,EAAE,iBAAiB,GAAE,WAAW,EAAO;IAK5G,eAAe,CAAC,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,cAAc,EAAE,iBAAiB,GAAE,WAAW,EAAO;IAKpG,iBAAiB,CAAC,QAAQ,EAAE,cAAc,EAAE,iBAAiB,GAAE,WAAW,EAAO;IAKvF;;;;OAIG;IACG,kBAAkB,CAAC,MAAM,EAAE;QAAE,QAAQ,EAAE,SAAS,CAAC;QAAC,aAAa,EAAE,SAAS,CAAA;KAAE,EAChF,QAAQ,EAAE,eAAe,EAAE,iBAAiB,GAAE,WAAW,EAAO;CAiBnE"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
|
2
|
+
import { TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
|
|
3
|
+
import { SystemProgram } from "@solana/web3.js";
|
|
4
|
+
import { LEVERAGE_DELEGATE_PROGRAM_ID } from "./delegate.js";
|
|
5
|
+
import { DUSK_PROGRAM_ID, deriveEventAuthorityAddress } from "./constants.js";
|
|
6
|
+
import { governanceIntegerBN } from "./governance.js";
|
|
7
|
+
import { DuskWrite } from "./write.js";
|
|
8
|
+
import { createDuskProgram } from "./program.js";
|
|
9
|
+
import { oracleReadAccounts } from "./oracle.js";
|
|
10
|
+
export const PROTECTION_ACTION = { borrowRepay: 0, borrowCollateral: 1, leverageRepay: 2 };
|
|
11
|
+
export function deriveProtectionOrderAddress(owner, orderId, programId = LEVERAGE_DELEGATE_PROGRAM_ID) {
|
|
12
|
+
return PublicKey.findProgramAddressSync([Buffer.from("protection_order"), owner.toBuffer(),
|
|
13
|
+
governanceIntegerBN(orderId, "orderId").toArrayLike(Buffer, "le", 8)], programId);
|
|
14
|
+
}
|
|
15
|
+
/** Fund recurring protection for an existing borrowing or leverage position.
|
|
16
|
+
* Setup the order's LP ATA, asset ATAs and both yield accounts first. LP transfers
|
|
17
|
+
* require buildLpTransferHookAccountMetas. The sponsor remains yield recipient.
|
|
18
|
+
* Execution needs a keeper token balance to advance repayment; LP redemption
|
|
19
|
+
* reimburses that balance atomically. Insufficient redemption liquidity reverts.
|
|
20
|
+
*/
|
|
21
|
+
export class DuskProtectionOrders {
|
|
22
|
+
program;
|
|
23
|
+
constructor(program) {
|
|
24
|
+
this.program = program;
|
|
25
|
+
}
|
|
26
|
+
async oracleAccounts(market, remaining) {
|
|
27
|
+
const dusk = createDuskProgram({ connection: this.program.provider.connection });
|
|
28
|
+
const state = await dusk.account.market.fetch(market);
|
|
29
|
+
const accounts = [...remaining];
|
|
30
|
+
for (const meta of oracleReadAccounts(state.oracle.policy)) {
|
|
31
|
+
if (!accounts.some(account => account.pubkey.equals(meta.pubkey)))
|
|
32
|
+
accounts.push(meta);
|
|
33
|
+
}
|
|
34
|
+
return accounts;
|
|
35
|
+
}
|
|
36
|
+
async createInstruction(params, accounts, remainingAccounts = []) {
|
|
37
|
+
const order = deriveProtectionOrderAddress(new PublicKey(accounts.owner), params.orderId, this.program.programId)[0];
|
|
38
|
+
const args = {
|
|
39
|
+
...params,
|
|
40
|
+
orderId: governanceIntegerBN(params.orderId, "orderId"),
|
|
41
|
+
lpAmount: governanceIntegerBN(params.lpAmount, "lpAmount"),
|
|
42
|
+
maxLpPerExecution: governanceIntegerBN(params.maxLpPerExecution, "maxLpPerExecution"),
|
|
43
|
+
maxPaymentPerExecution: governanceIntegerBN(params.maxPaymentPerExecution, "maxPaymentPerExecution"),
|
|
44
|
+
minPaymentPerLpNad: governanceIntegerBN(params.minPaymentPerLpNad, "minPaymentPerLpNad"),
|
|
45
|
+
triggerHealthBps: governanceIntegerBN(params.triggerHealthBps, "triggerHealthBps"),
|
|
46
|
+
targetHealthBps: governanceIntegerBN(params.targetHealthBps, "targetHealthBps"),
|
|
47
|
+
expiresAt: governanceIntegerBN(params.expiresAt, "expiresAt"),
|
|
48
|
+
};
|
|
49
|
+
return this.program.methods.createProtectionOrder(args).accountsStrict({ ...accounts, order,
|
|
50
|
+
duskProgram: DUSK_PROGRAM_ID, duskEventAuthority: deriveEventAuthorityAddress()[0],
|
|
51
|
+
token2022Program: TOKEN_2022_PROGRAM_ID, systemProgram: SystemProgram.programId,
|
|
52
|
+
}).remainingAccounts(remainingAccounts).instruction();
|
|
53
|
+
}
|
|
54
|
+
async previewInstruction(accounts, remainingAccounts = []) {
|
|
55
|
+
return this.program.methods.previewProtectionOrder().accountsStrict(accounts)
|
|
56
|
+
.remainingAccounts(await this.oracleAccounts(new PublicKey(accounts.market), remainingAccounts)).instruction();
|
|
57
|
+
}
|
|
58
|
+
async fundInstruction(lpAmount, accounts, remainingAccounts = []) {
|
|
59
|
+
return this.program.methods.fundProtectionOrder({ lpAmount: governanceIntegerBN(lpAmount, "lpAmount") })
|
|
60
|
+
.accountsStrict({ ...accounts, token2022Program: TOKEN_2022_PROGRAM_ID }).remainingAccounts(remainingAccounts).instruction();
|
|
61
|
+
}
|
|
62
|
+
async cancelInstruction(accounts, remainingAccounts = []) {
|
|
63
|
+
return this.program.methods.cancelProtectionOrder().accountsStrict({ ...accounts, token2022Program: TOKEN_2022_PROGRAM_ID })
|
|
64
|
+
.remainingAccounts(remainingAccounts).instruction();
|
|
65
|
+
}
|
|
66
|
+
/** LP redemptions may need an address lookup table and a larger heap frame.
|
|
67
|
+
* Simulate the complete transaction with the current native market state.
|
|
68
|
+
* LP proceeds must cover payment, the full keeper reward, and the additional
|
|
69
|
+
* 10 bps protocol fee. Supply protocolFee from orderProtocolFeeAccounts.
|
|
70
|
+
*/
|
|
71
|
+
async executeInstruction(params, accounts, remainingAccounts = []) {
|
|
72
|
+
const write = new DuskWrite(createDuskProgram({ connection: this.program.provider.connection }));
|
|
73
|
+
const order = await this.program.account.protectionOrder.fetch(accounts.order);
|
|
74
|
+
const prefix = order.action === PROTECTION_ACTION.borrowCollateral
|
|
75
|
+
? [] : await write.hlpRemainingAccounts(accounts.market);
|
|
76
|
+
// Keep the canonical prefix first even when callers supply transfer-hook
|
|
77
|
+
// accounts. Existing callers may already provide that same prefix.
|
|
78
|
+
const hasPrefix = prefix.length > 0 && prefix.every((meta, i) => remainingAccounts[i]?.pubkey.equals(meta.pubkey) && remainingAccounts[i]?.isWritable === meta.isWritable);
|
|
79
|
+
const completeRemaining = hasPrefix ? remainingAccounts : [...prefix, ...remainingAccounts];
|
|
80
|
+
const remaining = await this.oracleAccounts(new PublicKey(accounts.market), completeRemaining);
|
|
81
|
+
return this.program.methods.executeProtectionOrder({ lpAmount: governanceIntegerBN(params.lpAmount, "lpAmount"),
|
|
82
|
+
paymentAmount: governanceIntegerBN(params.paymentAmount, "paymentAmount") })
|
|
83
|
+
.accountsStrict({ ...accounts, duskProgram: DUSK_PROGRAM_ID, duskEventAuthority: deriveEventAuthorityAddress()[0],
|
|
84
|
+
tokenProgram: TOKEN_PROGRAM_ID, token2022Program: TOKEN_2022_PROGRAM_ID })
|
|
85
|
+
.remainingAccounts(remaining).instruction();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=protection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protection.js","sourceRoot":"","sources":["../src/protection.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAoB,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAgC,4BAA4B,EAAE,MAAM,eAAe,CAAC;AAC3F,OAAO,EAAE,eAAe,EAAE,2BAA2B,EAAE,MAAM,gBAAgB,CAAC;AAC9E,O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AAG,QAAQ,EAAE,WAAW,EAAE,eAAe,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,CAAC,CAAC,CAAC;YAC/G,YAAY,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,CAAC;aAC3E,iBAAiB,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,CAAC;IAChD,CAAC;CACF"}
|
package/dist/referral.d.ts
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { PublicKey, type AccountMeta, type Commitment, type Connection
|
|
1
|
+
import { PublicKey, type AccountMeta, type Commitment, type Connection } from "@solana/web3.js";
|
|
2
2
|
import { type AddressLike } from "./address.js";
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
3
|
+
export declare const REFERRAL_BPS_DENOMINATOR = 10000;
|
|
4
|
+
export declare const MAX_REFERRAL_INTEREST_SHARE_BPS = 10000;
|
|
5
5
|
export type IntegerLike = bigint | number | string | {
|
|
6
6
|
toString(): string;
|
|
7
7
|
};
|
|
8
|
-
export interface
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export interface ReferralInterestQuote {
|
|
9
|
+
interestPaid: bigint;
|
|
10
|
+
interestVaultCredit: bigint;
|
|
11
|
+
protocolInterestRevenue: bigint;
|
|
12
|
+
interestShareBps: number;
|
|
13
|
+
referralAmount: bigint;
|
|
13
14
|
}
|
|
14
|
-
export declare function
|
|
15
|
-
|
|
15
|
+
export declare function referralBindingInterestShareBps(params: {
|
|
16
|
+
configuredShareBps: number;
|
|
17
|
+
maxShareBps: number;
|
|
18
|
+
active?: boolean;
|
|
19
|
+
}): number;
|
|
20
|
+
export declare function quoteReferralInterestShare(params: {
|
|
21
|
+
interestPaid: IntegerLike;
|
|
22
|
+
interestVaultCredit?: IntegerLike;
|
|
23
|
+
protocolInterestBps: number;
|
|
24
|
+
interestShareBps: number;
|
|
25
|
+
}): ReferralInterestQuote;
|
|
26
|
+
export declare function assertReferralInterestShareBps(shareBps: number): void;
|
|
16
27
|
export declare function tokenProgramForMint(connection: Connection, mint: AddressLike, commitment?: Commitment): Promise<PublicKey>;
|
|
17
|
-
export interface
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
tokenProgram: PublicKey;
|
|
28
|
+
export interface ReferralAccrualAddresses {
|
|
29
|
+
referralPartner: PublicKey;
|
|
30
|
+
referralAccrual: PublicKey;
|
|
21
31
|
}
|
|
22
|
-
export declare function
|
|
23
|
-
connection: Connection;
|
|
32
|
+
export declare function referralAccrualAddresses(params: {
|
|
24
33
|
referrer: AddressLike;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}):
|
|
28
|
-
export declare function buildReferralVaultSetupInstruction(params: {
|
|
29
|
-
connection: Connection;
|
|
30
|
-
payer: AddressLike;
|
|
31
|
-
referrer: AddressLike;
|
|
32
|
-
mint: AddressLike;
|
|
33
|
-
commitment?: Commitment;
|
|
34
|
-
}): Promise<ReferralVaultAddresses & {
|
|
35
|
-
instruction: TransactionInstruction;
|
|
36
|
-
}>;
|
|
34
|
+
market: AddressLike;
|
|
35
|
+
assetMint: AddressLike;
|
|
36
|
+
}): ReferralAccrualAddresses;
|
|
37
37
|
export interface TransferHookTransfer {
|
|
38
38
|
source: AddressLike;
|
|
39
39
|
mint: AddressLike;
|
package/dist/referral.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"referral.d.ts","sourceRoot":"","sources":["../src/referral.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"referral.d.ts","sourceRoot":"","sources":["../src/referral.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,SAAS,EACT,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,UAAU,EAChB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EAAW,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAMzD,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAC/C,eAAO,MAAM,+BAA+B,QAA2B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,EAAE,WAAW,CAAC;IACzB,SAAS,EAAE,WAAW,CAAC;IACvB,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,WAAW,CAAC;CAC5B;AAED;;;GAGG;AACH,wBAAsB,+BAA+B,CACnD,UAAU,EAAE,UAAU,EACtB,SAAS,EAAE,SAAS,oBAAoB,EAAE,EAC1C,UAAU,CAAC,EAAE,UAAU,GACtB,OAAO,CAAC,WAAW,EAAE,CAAC,CA2CxB"}
|
package/dist/referral.js
CHANGED
|
@@ -1,28 +1,39 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createTransferCheckedWithTransferHookInstruction, getMint, getTransferHook, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, } from "@solana/spl-token";
|
|
2
2
|
import { address } from "./address.js";
|
|
3
|
-
import {
|
|
4
|
-
export const
|
|
5
|
-
export const
|
|
6
|
-
export function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (
|
|
10
|
-
throw new Error("
|
|
3
|
+
import { deriveReferralAccrualAddress, deriveReferralPartnerAddress, } from "./constants.js";
|
|
4
|
+
export const REFERRAL_BPS_DENOMINATOR = 10_000;
|
|
5
|
+
export const MAX_REFERRAL_INTEREST_SHARE_BPS = REFERRAL_BPS_DENOMINATOR;
|
|
6
|
+
export function referralBindingInterestShareBps(params) {
|
|
7
|
+
assertReferralInterestShareBps(params.configuredShareBps);
|
|
8
|
+
assertReferralInterestShareBps(params.maxShareBps);
|
|
9
|
+
if (params.active === false)
|
|
10
|
+
throw new Error("Referral partner is inactive");
|
|
11
|
+
return Math.min(params.configuredShareBps, params.maxShareBps);
|
|
12
|
+
}
|
|
13
|
+
export function quoteReferralInterestShare(params) {
|
|
14
|
+
assertBps(params.protocolInterestBps, "protocol interest share");
|
|
15
|
+
assertReferralInterestShareBps(params.interestShareBps);
|
|
16
|
+
const interestPaid = BigInt(params.interestPaid.toString());
|
|
17
|
+
if (interestPaid < 0n)
|
|
18
|
+
throw new Error("interestPaid must be non-negative");
|
|
19
|
+
const interestVaultCredit = BigInt((params.interestVaultCredit ?? params.interestPaid).toString());
|
|
20
|
+
if (interestVaultCredit < 0n || interestVaultCredit > interestPaid) {
|
|
21
|
+
throw new Error("interestVaultCredit must be between 0 and interestPaid");
|
|
11
22
|
}
|
|
12
|
-
const
|
|
23
|
+
const protocolInterestRevenue = (interestVaultCredit * BigInt(params.protocolInterestBps)) /
|
|
24
|
+
BigInt(REFERRAL_BPS_DENOMINATOR);
|
|
25
|
+
const referralAmount = (protocolInterestRevenue * BigInt(params.interestShareBps)) /
|
|
26
|
+
BigInt(REFERRAL_BPS_DENOMINATOR);
|
|
13
27
|
return {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
interestPaid,
|
|
29
|
+
interestVaultCredit,
|
|
30
|
+
protocolInterestRevenue,
|
|
31
|
+
interestShareBps: params.interestShareBps,
|
|
32
|
+
referralAmount,
|
|
18
33
|
};
|
|
19
34
|
}
|
|
20
|
-
export function
|
|
21
|
-
|
|
22
|
-
configuredFeeBps < 0 ||
|
|
23
|
-
configuredFeeBps > MAX_REFERRAL_ORIGINATION_FEE_BPS) {
|
|
24
|
-
throw new Error(`referral origination fee must be between 0 and ${MAX_REFERRAL_ORIGINATION_FEE_BPS} bps`);
|
|
25
|
-
}
|
|
35
|
+
export function assertReferralInterestShareBps(shareBps) {
|
|
36
|
+
assertBps(shareBps, "referral interest share");
|
|
26
37
|
}
|
|
27
38
|
export async function tokenProgramForMint(connection, mint, commitment) {
|
|
28
39
|
const mintKey = address(mint);
|
|
@@ -35,21 +46,18 @@ export async function tokenProgramForMint(connection, mint, commitment) {
|
|
|
35
46
|
}
|
|
36
47
|
throw new Error(`Unsupported mint owner: ${account.owner.toBase58()}`);
|
|
37
48
|
}
|
|
38
|
-
export
|
|
49
|
+
export function referralAccrualAddresses(params) {
|
|
39
50
|
const referrer = address(params.referrer);
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
const
|
|
44
|
-
return {
|
|
51
|
+
const market = address(params.market);
|
|
52
|
+
const assetMint = address(params.assetMint);
|
|
53
|
+
const [referralPartner] = deriveReferralPartnerAddress(referrer);
|
|
54
|
+
const [referralAccrual] = deriveReferralAccrualAddress(referralPartner, market, assetMint);
|
|
55
|
+
return { referralPartner, referralAccrual };
|
|
45
56
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
...addresses,
|
|
51
|
-
instruction: createAssociatedTokenAccountIdempotentInstruction(address(params.payer), addresses.referralVault, addresses.referralProfile, mint, addresses.tokenProgram),
|
|
52
|
-
};
|
|
57
|
+
function assertBps(value, label) {
|
|
58
|
+
if (!Number.isInteger(value) || value < 0 || value > REFERRAL_BPS_DENOMINATOR) {
|
|
59
|
+
throw new Error(`${label} must be between 0 and ${REFERRAL_BPS_DENOMINATOR} bps`);
|
|
60
|
+
}
|
|
53
61
|
}
|
|
54
62
|
/**
|
|
55
63
|
* Resolve the union of Token-2022 hook accounts required by one or more CPIs.
|
package/dist/referral.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"referral.js","sourceRoot":"","sources":["../src/referral.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"referral.js","sourceRoot":"","sources":["../src/referral.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gDAAgD,EAChD,OAAO,EACP,eAAe,EACf,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAQ3B,OAAO,EAAE,OAAO,EAAoB,MAAM,cAAc,CAAC;AACzD,OAAO,EACL,4BAA4B,EAC5B,4BAA4B,GAC7B,MAAM,gBAAgB,CAAC;AAExB,MAAM,CAAC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAC/C,MAAM,CAAC,MAAM,+BAA+B,GAAG,wBAAwB,CAAC;AAYxE,MA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1D,CAAC;QACF,KAAK,MAAM,IAAI,IAAI,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7C,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;YACnC,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE;gBACd,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,QAAQ,EAAE,OAAO,CAAC,OAAO,EAAE,QAAQ,IAAI,IAAI,CAAC,QAAQ,CAAC;gBACrD,UAAU,EAAE,OAAO,CAAC,OAAO,EAAE,UAAU,IAAI,IAAI,CAAC,UAAU,CAAC;aAC5D,CAAC,CAAC;QACL,CAAC;QACD,IAAI,YAAY,EAAE,CAAC;YACjB,MAAM,GAAG,GAAG,YAAY,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;YAC9C,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE;gBACd,MAAM,EAAE,YAAY,CAAC,SAAS;gBAC9B,QAAQ,EAAE,OAAO,CAAC,OAAO,EAAE,QAAQ,CAAC;gBACpC,UAAU,EAAE,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC;aACzC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Creation defaults. Governance may replace each market's controller. */
|
|
2
|
+
export declare const RISK_RATE_DEFAULTS: Readonly<{
|
|
3
|
+
lowerSignalNad: 50000000;
|
|
4
|
+
upperSignalNad: 62500000;
|
|
5
|
+
halfLifeMs: number;
|
|
6
|
+
initialAprNad: 40000000;
|
|
7
|
+
minimumAprNad: 20000000;
|
|
8
|
+
maximumAprNad: 0;
|
|
9
|
+
borrowUtilizationLimitBps: 8000;
|
|
10
|
+
}>;
|
|
11
|
+
/** Values use one common numeraire. Returns null when this asset has no debt.
|
|
12
|
+
* This is the controller's coverage/cash signal, not a default probability. */
|
|
13
|
+
export declare function riskCoverageSignalNad(juniorFundedValue: bigint, totalCoveredDebtValue: bigint, assetDebt: bigint, assetCash: bigint): bigint | null;
|
|
14
|
+
//# sourceMappingURL=risk-pricing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"risk-pricing.d.ts","sourceRoot":"","sources":["../src/risk-pricing.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,eAAO,MAAM,kBAAkB;;;;;;;;EAQ7B,CAAC;AAEH;+EAC+E;AAC/E,wBAAgB,qBAAqB,CACnC,iBAAiB,EAAE,MAAM,EACzB,qBAAqB,EAAE,MAAM,EAC7B,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB,MAAM,GAAG,IAAI,CASf"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Creation defaults. Governance may replace each market's controller. */
|
|
2
|
+
export const RISK_RATE_DEFAULTS = Object.freeze({
|
|
3
|
+
lowerSignalNad: 50_000_000,
|
|
4
|
+
upperSignalNad: 62_500_000,
|
|
5
|
+
halfLifeMs: 14 * 24 * 60 * 60 * 1_000,
|
|
6
|
+
initialAprNad: 40_000_000,
|
|
7
|
+
minimumAprNad: 20_000_000,
|
|
8
|
+
maximumAprNad: 0,
|
|
9
|
+
borrowUtilizationLimitBps: 8_000,
|
|
10
|
+
});
|
|
11
|
+
/** Values use one common numeraire. Returns null when this asset has no debt.
|
|
12
|
+
* This is the controller's coverage/cash signal, not a default probability. */
|
|
13
|
+
export function riskCoverageSignalNad(juniorFundedValue, totalCoveredDebtValue, assetDebt, assetCash) {
|
|
14
|
+
if ([juniorFundedValue, totalCoveredDebtValue, assetDebt, assetCash].some(v => v < 0n)) {
|
|
15
|
+
throw new Error("Coverage inputs cannot be negative");
|
|
16
|
+
}
|
|
17
|
+
if (assetDebt === 0n)
|
|
18
|
+
return null;
|
|
19
|
+
if (totalCoveredDebtValue === 0n)
|
|
20
|
+
throw new Error("Covered debt value is zero for an indebted asset");
|
|
21
|
+
// Match the native bps utilization input, including its floor rounding.
|
|
22
|
+
const utilizationBps = assetDebt * 10000n / (assetDebt + assetCash);
|
|
23
|
+
return juniorFundedValue * ((10000n - utilizationBps) * 100000n) / totalCoveredDebtValue;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=risk-pricing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"risk-pricing.js","sourceRoot":"","sources":["../src/risk-pricing.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,MAAM,CAAC,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC;IAC9C,cAAc,EAAE,UAAU;IAC1B,cAAc,EAAE,UAAU;IAC1B,UAAU,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK;IACrC,aAAa,EAAE,UAAU;IACzB,aAAa,EAAE,UAAU;IACzB,aAAa,EAAE,CAAC;IAChB,yBAAyB,EAAE,KAAK;CACjC,CAAC,CAAC;AAEH;+EAC+E;AAC/E,MAAM,UAAU,qBAAqB,CACnC,iBAAyB,EACzB,qBAA6B,EAC7B,SAAiB,EACjB,SAAiB;IAEjB,IAAI,CAAC,iBAAiB,EAAE,qBAAqB,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC;QACvF,MAAM,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;IACxD,CAAC;IACD,IAAI,SAAS,KAAK,EAAE;QAAE,OAAO,IAAI,CAAC;IAClC,IAAI,qBAAqB,KAAK,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAAC;IACtG,wEAAwE;IACxE,MAAM,cAAc,GAAG,SAAS,GAAG,MAAO,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC,CAAC;IACrE,OAAO,iBAAiB,GAAG,CAAC,CAAC,MAAO,GAAG,cAAc,CAAC,GAAG,OAAQ,CAAC,GAAG,qBAAqB,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Program } from "@coral-xyz/anchor";
|
|
2
|
+
import { PublicKey, type AddressLookupTableAccount, type Commitment, type TransactionInstruction, type RpcResponseAndContext, type SimulatedTransactionResponse } from "@solana/web3.js";
|
|
3
|
+
import { type AddressLike } from "./address.js";
|
|
4
|
+
import type { Dusk } from "./types_v2.js";
|
|
5
|
+
export interface SimulateOptions {
|
|
6
|
+
feePayer?: AddressLike;
|
|
7
|
+
commitment?: Commitment;
|
|
8
|
+
/** Defaults to 1,400,000 units to accommodate heavier native previews. */
|
|
9
|
+
computeUnitLimit?: number;
|
|
10
|
+
/** Defaults to 256 KiB for native preview intermediate state. */
|
|
11
|
+
heapFrameBytes?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface PreviewSimulationOptions extends SimulateOptions {
|
|
14
|
+
/** Require a complete budget prefix when other programs bind instruction
|
|
15
|
+
* indices. Dusk flash-liquidation plans are detected automatically. */
|
|
16
|
+
preserveInstructionIndices?: boolean;
|
|
17
|
+
/** Optional active lookup tables for larger position-action transactions. */
|
|
18
|
+
lookupTables?: AddressLookupTableAccount[];
|
|
19
|
+
/** Include CPI events when previewing an actual state-changing instruction. */
|
|
20
|
+
innerInstructions?: boolean;
|
|
21
|
+
/** Reject a bank older than this observed slot. */
|
|
22
|
+
minContextSlot?: number;
|
|
23
|
+
/** Post-simulation accounts, in exactly this order and from the same bank. */
|
|
24
|
+
accounts?: readonly AddressLike[];
|
|
25
|
+
signal?: AbortSignal;
|
|
26
|
+
/** Bounds the caller's wait; the transport may finish an in-flight RPC later. */
|
|
27
|
+
timeoutMs?: number;
|
|
28
|
+
/** Set false when simulating an instruction that succeeds without return data. */
|
|
29
|
+
requireReturnData?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Keeps the actual RPC slot, logs and account snapshots beside return data. */
|
|
32
|
+
export type DuskPreviewSimulation = RpcResponseAndContext<SimulatedTransactionResponse> & {
|
|
33
|
+
/** Request start, so slow simulation never makes old data appear fresh. */
|
|
34
|
+
observedAt: number;
|
|
35
|
+
accountKeys: readonly PublicKey[];
|
|
36
|
+
};
|
|
37
|
+
export declare class DuskSimulationError extends Error {
|
|
38
|
+
readonly simulation: RpcResponseAndContext<SimulatedTransactionResponse>;
|
|
39
|
+
constructor(message: string, simulation: RpcResponseAndContext<SimulatedTransactionResponse>);
|
|
40
|
+
}
|
|
41
|
+
export declare class DuskPreviewTimeoutError extends Error {
|
|
42
|
+
constructor();
|
|
43
|
+
}
|
|
44
|
+
export declare function simulatePreviewWithContext(program: Program<Dusk>, instructions: readonly TransactionInstruction[], options?: PreviewSimulationOptions): Promise<DuskPreviewSimulation>;
|
|
45
|
+
//# sourceMappingURL=simulation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAEL,SAAS,EAIT,KAAK,yBAAyB,EAC9B,KAAK,UAAU,EACf,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,4BAA4B,EAClC,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAwC,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AACtF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAE1C,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,EAAE,WAAW,CAAC;IACvB,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,0EAA0E;IAC1E,gBAAgB,CA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
|
+
import { address, DEFAULT_READONLY_PUBLIC_KEY } from "./address.js";
|
|
3
|
+
export class DuskSimulationError extends Error {
|
|
4
|
+
simulation;
|
|
5
|
+
constructor(message, simulation) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.simulation = simulation;
|
|
8
|
+
this.name = "DuskSimulationError";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class DuskPreviewTimeoutError extends Error {
|
|
12
|
+
constructor() {
|
|
13
|
+
super("Dusk preview timed out");
|
|
14
|
+
this.name = "DuskPreviewTimeoutError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export async function simulatePreviewWithContext(program, instructions, options = {}) {
|
|
18
|
+
options.signal?.throwIfAborted();
|
|
19
|
+
const timeoutMs = options.timeoutMs ?? 10_000;
|
|
20
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs <= 0)
|
|
21
|
+
throw new Error("Invalid preview timeout");
|
|
22
|
+
if (options.minContextSlot !== undefined &&
|
|
23
|
+
(!Number.isSafeInteger(options.minContextSlot) || options.minContextSlot < 0))
|
|
24
|
+
throw new Error("Invalid preview minimum slot");
|
|
25
|
+
if (!instructions.length)
|
|
26
|
+
throw new Error("A preview requires an instruction");
|
|
27
|
+
// A complete flash plan already has its budgets and absolute settle index.
|
|
28
|
+
// Ordinary previews still need a budget prefix: placing budgets after the
|
|
29
|
+
// preview overwrites Solana's final return-data program ID.
|
|
30
|
+
const simulationInstructions = [...instructions];
|
|
31
|
+
const missingBudgets = [];
|
|
32
|
+
const flashDiscriminators = program.idl?.instructions.filter(ix => ix.name === "beginFlashLiquidation" || ix.name === "beginHlpFlash").map(ix => Buffer.from(ix.discriminator)) ?? [];
|
|
33
|
+
const bindsIndices = options.preserveInstructionIndices || instructions.some(ix => ix.programId.equals(program.programId) && flashDiscriminators.some(tag => ix.data.subarray(0, 8).equals(tag)));
|
|
34
|
+
for (const [tag, explicit, budget] of [
|
|
35
|
+
[1, options.heapFrameBytes, ComputeBudgetProgram.requestHeapFrame({ bytes: options.heapFrameBytes ?? 256 * 1024 })],
|
|
36
|
+
[2, options.computeUnitLimit, ComputeBudgetProgram.setComputeUnitLimit({ units: options.computeUnitLimit ?? 1_400_000 })],
|
|
37
|
+
]) {
|
|
38
|
+
const matches = simulationInstructions.flatMap((ix, index) => ix.programId.equals(ComputeBudgetProgram.programId) && ix.data[0] === tag ? [index] : []);
|
|
39
|
+
if (matches.length > 1)
|
|
40
|
+
throw new Error("Duplicate compute-budget instruction in preview plan");
|
|
41
|
+
if (!matches.length)
|
|
42
|
+
missingBudgets.push(budget);
|
|
43
|
+
else if (explicit !== undefined)
|
|
44
|
+
simulationInstructions[matches[0]] = budget;
|
|
45
|
+
}
|
|
46
|
+
if (bindsIndices && missingBudgets.length)
|
|
47
|
+
throw new Error("Index-bound simulation requires planned heap and compute budgets; use buildFlash or include them before binding indices");
|
|
48
|
+
const transaction = new Transaction().add(...missingBudgets, ...simulationInstructions);
|
|
49
|
+
transaction.feePayer = address(options.feePayer ?? program.provider.publicKey ?? DEFAULT_READONLY_PUBLIC_KEY);
|
|
50
|
+
transaction.recentBlockhash = PublicKey.default.toBase58();
|
|
51
|
+
const message = options.lookupTables?.length
|
|
52
|
+
? new TransactionMessage({ payerKey: transaction.feePayer, recentBlockhash: transaction.recentBlockhash,
|
|
53
|
+
instructions: transaction.instructions }).compileToV0Message(options.lookupTables)
|
|
54
|
+
: transaction.compileMessage();
|
|
55
|
+
const wire = new VersionedTransaction(message);
|
|
56
|
+
const keys = message.version === "legacy" ? message.getAccountKeys()
|
|
57
|
+
: message.getAccountKeys({ addressLookupTableAccounts: options.lookupTables ?? [] });
|
|
58
|
+
const accountKeys = Array.from({ length: keys.length }, (_, index) => keys.get(index));
|
|
59
|
+
if (wire.serialize().length > 1232)
|
|
60
|
+
throw new Error("Preview exceeds the transaction packet limit");
|
|
61
|
+
const accounts = options.accounts?.map((account) => address(account).toBase58());
|
|
62
|
+
const observedAt = Date.now();
|
|
63
|
+
let timer;
|
|
64
|
+
let cancel = () => { };
|
|
65
|
+
const deadline = new Promise((_, reject) => {
|
|
66
|
+
cancel = () => reject(options.signal?.reason ?? new DOMException("Preview cancelled", "AbortError"));
|
|
67
|
+
options.signal?.addEventListener("abort", cancel, { once: true });
|
|
68
|
+
timer = setTimeout(() => reject(new DuskPreviewTimeoutError()), timeoutMs);
|
|
69
|
+
});
|
|
70
|
+
try {
|
|
71
|
+
const connection = program.provider.connection;
|
|
72
|
+
const simulation = await Promise.race([
|
|
73
|
+
connection.simulateTransaction(wire, {
|
|
74
|
+
commitment: options.commitment ?? connection.commitment,
|
|
75
|
+
sigVerify: false,
|
|
76
|
+
replaceRecentBlockhash: true,
|
|
77
|
+
...(options.innerInstructions ? { innerInstructions: true } : {}),
|
|
78
|
+
...(options.minContextSlot === undefined ? {} : { minContextSlot: options.minContextSlot }),
|
|
79
|
+
...(accounts === undefined
|
|
80
|
+
? {}
|
|
81
|
+
: { accounts: { encoding: "base64", addresses: accounts } }),
|
|
82
|
+
}),
|
|
83
|
+
deadline,
|
|
84
|
+
]);
|
|
85
|
+
options.signal?.throwIfAborted();
|
|
86
|
+
if (simulation.value.err) {
|
|
87
|
+
const tail = simulation.value.logs?.slice(-8).join("\n");
|
|
88
|
+
throw new DuskSimulationError(`Dusk simulation failed: ${JSON.stringify(simulation.value.err)}${tail ? `\n${tail}` : ""}`, simulation);
|
|
89
|
+
}
|
|
90
|
+
if (options.requireReturnData !== false && !simulation.value.returnData)
|
|
91
|
+
throw new DuskSimulationError("Dusk simulation did not return data", simulation);
|
|
92
|
+
if (options.requireReturnData !== false && simulation.value.returnData && simulation.value.returnData.programId !== program.programId.toBase58())
|
|
93
|
+
throw new DuskSimulationError("Dusk simulation returned data from a different program", simulation);
|
|
94
|
+
if (!Number.isSafeInteger(simulation.context.slot) ||
|
|
95
|
+
simulation.context.slot < (options.minContextSlot ?? 0))
|
|
96
|
+
throw new DuskSimulationError("Dusk simulation returned a stale or invalid slot", simulation);
|
|
97
|
+
if (accounts && simulation.value.accounts?.length !== accounts.length)
|
|
98
|
+
throw new DuskSimulationError("Dusk simulation returned incomplete accounts", simulation);
|
|
99
|
+
return { ...simulation, observedAt, accountKeys };
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
clearTimeout(timer);
|
|
103
|
+
options.signal?.removeEventListener("abort", cancel);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=simulation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.js","sourceRoot":"","sources":["../src/simulation.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EACpB,SAAS,EACT,WAAW,EACX,oBAAoB,EACpB,kBAAkB,GAMnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,OAAO,EAAE,2BAA2B,EAAoB,MAAM,cAAc,CAAC;AAsCtF,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAGjC;IAFX,YACE,OAAe,EACN,UAA+D;QAExE,KAAK,CAAC,OAAO,CAAC,CAAC;QAFN,eAAU,GAAV,UAAU,CAAqD;QAGxE,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAChD;QACE,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAChC,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACxC,CAAC;CACF;AAED,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,OAAsB,EACtB,YAA+C,EAC/C,UAAoC,EAAE;IAEtC,OAAO,CAAC,MAAM,EAAE,cAAc,EAAE,CAAC;IACjC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,MAAM,CAAC;IAC9C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,SAAS,CAAC,IAAI,SAAS,IAAI,CAAC;QACpD,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,IACE,O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