@omnipair/dusk-sdk 2.0.0 → 2.0.2

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/src/referral.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
- createAssociatedTokenAccountIdempotentInstruction,
3
2
  createTransferCheckedWithTransferHookInstruction,
4
- getAssociatedTokenAddressSync,
3
+ getMint,
4
+ getTransferHook,
5
5
  TOKEN_2022_PROGRAM_ID,
6
6
  TOKEN_PROGRAM_ID,
7
7
  } from "@solana/spl-token";
@@ -10,52 +10,72 @@ import {
10
10
  type AccountMeta,
11
11
  type Commitment,
12
12
  type Connection,
13
- type TransactionInstruction,
14
13
  } from "@solana/web3.js";
15
14
 
16
15
  import { address, type AddressLike } from "./address.js";
17
- import { deriveReferralProfileAddress } from "./constants.js";
16
+ import {
17
+ deriveReferralAccrualAddress,
18
+ deriveReferralPartnerAddress,
19
+ } from "./constants.js";
18
20
 
19
- export const DEFAULT_REFERRAL_ORIGINATION_FEE_BPS = 10;
20
- export const MAX_REFERRAL_ORIGINATION_FEE_BPS = 25;
21
+ export const REFERRAL_BPS_DENOMINATOR = 10_000;
22
+ export const MAX_REFERRAL_INTEREST_SHARE_BPS = REFERRAL_BPS_DENOMINATOR;
21
23
 
22
24
  export type IntegerLike = bigint | number | string | { toString(): string };
23
25
 
24
- export interface ReferralFeeQuote {
25
- requestedPrincipal: bigint;
26
- configuredFeeBps: number;
27
- feeDebit: bigint;
28
- grossDebt: bigint;
26
+ export interface ReferralInterestQuote {
27
+ interestPaid: bigint;
28
+ interestVaultCredit: bigint;
29
+ protocolInterestRevenue: bigint;
30
+ interestShareBps: number;
31
+ referralAmount: bigint;
32
+ }
33
+
34
+ export function referralBindingInterestShareBps(params: {
35
+ configuredShareBps: number;
36
+ maxShareBps: number;
37
+ active?: boolean;
38
+ }): number {
39
+ assertReferralInterestShareBps(params.configuredShareBps);
40
+ assertReferralInterestShareBps(params.maxShareBps);
41
+ if (params.active === false) throw new Error("Referral partner is inactive");
42
+ return Math.min(params.configuredShareBps, params.maxShareBps);
29
43
  }
30
44
 
31
- export function quoteReferralOriginationFee(
32
- requestedPrincipal: IntegerLike,
33
- configuredFeeBps: number
34
- ): ReferralFeeQuote {
35
- assertReferralFeeBps(configuredFeeBps);
36
- const requested = BigInt(requestedPrincipal.toString());
37
- if (requested < 0n) {
38
- throw new Error("requestedPrincipal must be non-negative");
45
+ export function quoteReferralInterestShare(params: {
46
+ interestPaid: IntegerLike;
47
+ interestVaultCredit?: IntegerLike;
48
+ protocolInterestBps: number;
49
+ interestShareBps: number;
50
+ }): ReferralInterestQuote {
51
+ assertBps(params.protocolInterestBps, "protocol interest share");
52
+ assertReferralInterestShareBps(params.interestShareBps);
53
+ const interestPaid = BigInt(params.interestPaid.toString());
54
+ if (interestPaid < 0n) throw new Error("interestPaid must be non-negative");
55
+ const interestVaultCredit = BigInt(
56
+ (params.interestVaultCredit ?? params.interestPaid).toString()
57
+ );
58
+ if (interestVaultCredit < 0n || interestVaultCredit > interestPaid) {
59
+ throw new Error("interestVaultCredit must be between 0 and interestPaid");
39
60
  }
40
- const feeDebit = (requested * BigInt(configuredFeeBps) + 9_999n) / 10_000n;
61
+
62
+ const protocolInterestRevenue =
63
+ (interestVaultCredit * BigInt(params.protocolInterestBps)) /
64
+ BigInt(REFERRAL_BPS_DENOMINATOR);
65
+ const referralAmount =
66
+ (protocolInterestRevenue * BigInt(params.interestShareBps)) /
67
+ BigInt(REFERRAL_BPS_DENOMINATOR);
41
68
  return {
42
- requestedPrincipal: requested,
43
- configuredFeeBps,
44
- feeDebit,
45
- grossDebt: requested + feeDebit,
69
+ interestPaid,
70
+ interestVaultCredit,
71
+ protocolInterestRevenue,
72
+ interestShareBps: params.interestShareBps,
73
+ referralAmount,
46
74
  };
47
75
  }
48
76
 
49
- export function assertReferralFeeBps(configuredFeeBps: number): void {
50
- if (
51
- !Number.isInteger(configuredFeeBps) ||
52
- configuredFeeBps < 0 ||
53
- configuredFeeBps > MAX_REFERRAL_ORIGINATION_FEE_BPS
54
- ) {
55
- throw new Error(
56
- `referral origination fee must be between 0 and ${MAX_REFERRAL_ORIGINATION_FEE_BPS} bps`
57
- );
58
- }
77
+ export function assertReferralInterestShareBps(shareBps: number): void {
78
+ assertBps(shareBps, "referral interest share");
59
79
  }
60
80
 
61
81
  export async function tokenProgramForMint(
@@ -74,50 +94,32 @@ export async function tokenProgramForMint(
74
94
  throw new Error(`Unsupported mint owner: ${account.owner.toBase58()}`);
75
95
  }
76
96
 
77
- export interface ReferralVaultAddresses {
78
- referralProfile: PublicKey;
79
- referralVault: PublicKey;
80
- tokenProgram: PublicKey;
97
+ export interface ReferralAccrualAddresses {
98
+ referralPartner: PublicKey;
99
+ referralAccrual: PublicKey;
81
100
  }
82
101
 
83
- export async function referralVaultAddresses(params: {
84
- connection: Connection;
102
+ export function referralAccrualAddresses(params: {
85
103
  referrer: AddressLike;
86
- mint: AddressLike;
87
- commitment?: Commitment;
88
- }): Promise<ReferralVaultAddresses> {
104
+ market: AddressLike;
105
+ assetMint: AddressLike;
106
+ }): ReferralAccrualAddresses {
89
107
  const referrer = address(params.referrer);
90
- const mint = address(params.mint);
91
- const [referralProfile] = deriveReferralProfileAddress(referrer);
92
- const tokenProgram = await tokenProgramForMint(params.connection, mint, params.commitment);
93
- const referralVault = getAssociatedTokenAddressSync(
94
- mint,
95
- referralProfile,
96
- true,
97
- tokenProgram
108
+ const market = address(params.market);
109
+ const assetMint = address(params.assetMint);
110
+ const [referralPartner] = deriveReferralPartnerAddress(referrer);
111
+ const [referralAccrual] = deriveReferralAccrualAddress(
112
+ referralPartner,
113
+ market,
114
+ assetMint
98
115
  );
99
- return { referralProfile, referralVault, tokenProgram };
116
+ return { referralPartner, referralAccrual };
100
117
  }
101
118
 
102
- export async function buildReferralVaultSetupInstruction(params: {
103
- connection: Connection;
104
- payer: AddressLike;
105
- referrer: AddressLike;
106
- mint: AddressLike;
107
- commitment?: Commitment;
108
- }): Promise<ReferralVaultAddresses & { instruction: TransactionInstruction }> {
109
- const mint = address(params.mint);
110
- const addresses = await referralVaultAddresses(params);
111
- return {
112
- ...addresses,
113
- instruction: createAssociatedTokenAccountIdempotentInstruction(
114
- address(params.payer),
115
- addresses.referralVault,
116
- addresses.referralProfile,
117
- mint,
118
- addresses.tokenProgram
119
- ),
120
- };
119
+ function assertBps(value: number, label: string): void {
120
+ if (!Number.isInteger(value) || value < 0 || value > REFERRAL_BPS_DENOMINATOR) {
121
+ throw new Error(`${label} must be between 0 and ${REFERRAL_BPS_DENOMINATOR} bps`);
122
+ }
121
123
  }
122
124
 
123
125
  export interface TransferHookTransfer {
@@ -158,6 +160,9 @@ export async function resolveTransferHookAccountMetas(
158
160
  commitment,
159
161
  tokenProgram
160
162
  );
163
+ const transferHook = getTransferHook(
164
+ await getMint(connection, mint, commitment, tokenProgram)
165
+ );
161
166
  for (const meta of instruction.keys.slice(4)) {
162
167
  const key = meta.pubkey.toBase58();
163
168
  const current = merged.get(key);
@@ -167,6 +172,15 @@ export async function resolveTransferHookAccountMetas(
167
172
  isWritable: Boolean(current?.isWritable || meta.isWritable),
168
173
  });
169
174
  }
175
+ if (transferHook) {
176
+ const key = transferHook.programId.toBase58();
177
+ const current = merged.get(key);
178
+ merged.set(key, {
179
+ pubkey: transferHook.programId,
180
+ isSigner: Boolean(current?.isSigner),
181
+ isWritable: Boolean(current?.isWritable),
182
+ });
183
+ }
170
184
  }
171
185
  return [...merged.values()];
172
186
  }
@@ -7,26 +7,27 @@ export type LeveragePosition = IdlAccounts<Dusk>["leveragePosition"];
7
7
  export type LeverageDelegation = IdlAccounts<Dusk>["leverageDelegation"];
8
8
  export type YieldAccount = IdlAccounts<Dusk>["yieldAccount"];
9
9
  export type FutarchyAuthority = IdlAccounts<Dusk>["futarchyAuthority"];
10
- export type ReferralProfile = IdlAccounts<Dusk>["referralProfile"];
11
- export type V2FutarchyAuthority = FutarchyAuthority;
10
+ export type ReferralPartner = IdlAccounts<Dusk>["referralPartner"];
11
+ export type ReferralAccrual = IdlAccounts<Dusk>["referralAccrual"];
12
12
 
13
13
  export type HlpClosed = IdlEvents<Dusk>["hlpClosed"];
14
14
  export type HlpOpened = IdlEvents<Dusk>["hlpOpened"];
15
- export type HlpRebalanced = IdlEvents<Dusk>["hlpRebalanced"];
16
15
  export type LiquidityAdded = IdlEvents<Dusk>["liquidityAdded"];
17
16
  export type LiquidityRemoved = IdlEvents<Dusk>["liquidityRemoved"];
18
17
  export type MarketCollateralDeposited = IdlEvents<Dusk>["marketCollateralDeposited"];
19
18
  export type MarketCollateralWithdrawn = IdlEvents<Dusk>["marketCollateralWithdrawn"];
20
19
  export type MarketCreated = IdlEvents<Dusk>["marketCreated"];
21
20
  export type MarketDebtUpdated = IdlEvents<Dusk>["marketDebtUpdated"];
22
- export type MarketFeeLiabilityClaimed = IdlEvents<Dusk>["marketFeeLiabilityClaimed"];
21
+ export type ManagerFeesClaimed = IdlEvents<Dusk>["managerFeesClaimed"];
23
22
  export type MarketHealthUpdated = IdlEvents<Dusk>["marketHealthUpdated"];
24
23
  export type MarketUpdated = IdlEvents<Dusk>["marketUpdated"];
25
24
  export type PositionLiquidated = IdlEvents<Dusk>["positionLiquidated"];
26
25
  export type ProtocolAuctionSettled = IdlEvents<Dusk>["protocolAuctionSettled"];
27
- export type ReferralFeesClaimed = IdlEvents<Dusk>["referralFeesClaimed"];
28
- export type ReferralOriginationFeePaid = IdlEvents<Dusk>["referralOriginationFeePaid"];
29
- export type ReferralOriginationFeeUpdated = IdlEvents<Dusk>["referralOriginationFeeUpdated"];
26
+ export type ReferralBound = IdlEvents<Dusk>["referralBound"];
27
+ export type ReferralPartnerConfigured = IdlEvents<Dusk>["referralPartnerConfigured"];
28
+ export type ReferralInterestAccrued = IdlEvents<Dusk>["referralInterestAccrued"];
29
+ export type ReferralInterestClaimed = IdlEvents<Dusk>["referralInterestClaimed"];
30
+ export type ReferralInterestShareCapUpdated = IdlEvents<Dusk>["referralInterestShareCapUpdated"];
30
31
  export type ReferralRecipientUpdated = IdlEvents<Dusk>["referralRecipientUpdated"];
31
32
  export type SwapExecuted = IdlEvents<Dusk>["swapExecuted"];
32
33
  export type YieldClaimed = IdlEvents<Dusk>["yieldClaimed"];