@omnipair/dusk-sdk 2.0.1 → 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,5 @@
1
1
  import {
2
- createAssociatedTokenAccountIdempotentInstruction,
3
2
  createTransferCheckedWithTransferHookInstruction,
4
- getAssociatedTokenAddressSync,
5
3
  getMint,
6
4
  getTransferHook,
7
5
  TOKEN_2022_PROGRAM_ID,
@@ -12,52 +10,72 @@ import {
12
10
  type AccountMeta,
13
11
  type Commitment,
14
12
  type Connection,
15
- type TransactionInstruction,
16
13
  } from "@solana/web3.js";
17
14
 
18
15
  import { address, type AddressLike } from "./address.js";
19
- import { deriveReferralProfileAddress } from "./constants.js";
16
+ import {
17
+ deriveReferralAccrualAddress,
18
+ deriveReferralPartnerAddress,
19
+ } from "./constants.js";
20
20
 
21
- export const DEFAULT_REFERRAL_ORIGINATION_FEE_BPS = 10;
22
- 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;
23
23
 
24
24
  export type IntegerLike = bigint | number | string | { toString(): string };
25
25
 
26
- export interface ReferralFeeQuote {
27
- requestedPrincipal: bigint;
28
- configuredFeeBps: number;
29
- feeDebit: bigint;
30
- 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);
31
43
  }
32
44
 
33
- export function quoteReferralOriginationFee(
34
- requestedPrincipal: IntegerLike,
35
- configuredFeeBps: number
36
- ): ReferralFeeQuote {
37
- assertReferralFeeBps(configuredFeeBps);
38
- const requested = BigInt(requestedPrincipal.toString());
39
- if (requested < 0n) {
40
- 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");
41
60
  }
42
- 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);
43
68
  return {
44
- requestedPrincipal: requested,
45
- configuredFeeBps,
46
- feeDebit,
47
- grossDebt: requested + feeDebit,
69
+ interestPaid,
70
+ interestVaultCredit,
71
+ protocolInterestRevenue,
72
+ interestShareBps: params.interestShareBps,
73
+ referralAmount,
48
74
  };
49
75
  }
50
76
 
51
- export function assertReferralFeeBps(configuredFeeBps: number): void {
52
- if (
53
- !Number.isInteger(configuredFeeBps) ||
54
- configuredFeeBps < 0 ||
55
- configuredFeeBps > MAX_REFERRAL_ORIGINATION_FEE_BPS
56
- ) {
57
- throw new Error(
58
- `referral origination fee must be between 0 and ${MAX_REFERRAL_ORIGINATION_FEE_BPS} bps`
59
- );
60
- }
77
+ export function assertReferralInterestShareBps(shareBps: number): void {
78
+ assertBps(shareBps, "referral interest share");
61
79
  }
62
80
 
63
81
  export async function tokenProgramForMint(
@@ -76,50 +94,32 @@ export async function tokenProgramForMint(
76
94
  throw new Error(`Unsupported mint owner: ${account.owner.toBase58()}`);
77
95
  }
78
96
 
79
- export interface ReferralVaultAddresses {
80
- referralProfile: PublicKey;
81
- referralVault: PublicKey;
82
- tokenProgram: PublicKey;
97
+ export interface ReferralAccrualAddresses {
98
+ referralPartner: PublicKey;
99
+ referralAccrual: PublicKey;
83
100
  }
84
101
 
85
- export async function referralVaultAddresses(params: {
86
- connection: Connection;
102
+ export function referralAccrualAddresses(params: {
87
103
  referrer: AddressLike;
88
- mint: AddressLike;
89
- commitment?: Commitment;
90
- }): Promise<ReferralVaultAddresses> {
104
+ market: AddressLike;
105
+ assetMint: AddressLike;
106
+ }): ReferralAccrualAddresses {
91
107
  const referrer = address(params.referrer);
92
- const mint = address(params.mint);
93
- const [referralProfile] = deriveReferralProfileAddress(referrer);
94
- const tokenProgram = await tokenProgramForMint(params.connection, mint, params.commitment);
95
- const referralVault = getAssociatedTokenAddressSync(
96
- mint,
97
- referralProfile,
98
- true,
99
- 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
100
115
  );
101
- return { referralProfile, referralVault, tokenProgram };
116
+ return { referralPartner, referralAccrual };
102
117
  }
103
118
 
104
- export async function buildReferralVaultSetupInstruction(params: {
105
- connection: Connection;
106
- payer: AddressLike;
107
- referrer: AddressLike;
108
- mint: AddressLike;
109
- commitment?: Commitment;
110
- }): Promise<ReferralVaultAddresses & { instruction: TransactionInstruction }> {
111
- const mint = address(params.mint);
112
- const addresses = await referralVaultAddresses(params);
113
- return {
114
- ...addresses,
115
- instruction: createAssociatedTokenAccountIdempotentInstruction(
116
- address(params.payer),
117
- addresses.referralVault,
118
- addresses.referralProfile,
119
- mint,
120
- addresses.tokenProgram
121
- ),
122
- };
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
+ }
123
123
  }
124
124
 
125
125
  export interface TransferHookTransfer {
@@ -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"];