Skip to content

Commit e3db97b

Browse files
committed
clean up account inclusion rules in settle pnl for builder codes
1 parent eb90de5 commit e3db97b

File tree

3 files changed

+104
-114
lines changed

3 files changed

+104
-114
lines changed

programs/drift/src/controller/revenue_share.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn sweep_completed_revenue_share_for_market<'a>(
142142
{
143143
Ok(auth) => auth,
144144
Err(_) => {
145+
msg!("failed to get approved_builder from escrow account");
145146
continue;
146147
}
147148
};

sdk/src/driftClient.ts

Lines changed: 100 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ import { WebSocketDriftClientAccountSubscriber } from './accounts/webSocketDrift
200200
import { hasBuilder } from './math/orders';
201201
import { RevenueShareEscrowMap } from './userMap/revenueShareEscrowMap';
202202
import {
203-
isBuilderOrderAvailable,
204203
isBuilderOrderReferral,
204+
isBuilderOrderCompleted,
205205
} from './math/builder';
206206

207207
type RemainingAccountParams = {
@@ -7656,36 +7656,37 @@ export class DriftClient {
76567656
});
76577657

76587658
if (revenueShareEscrowMap) {
7659-
for (const order of settleeUserAccount.orders) {
7660-
if (hasBuilder(order)) {
7661-
remainingAccounts.push({
7662-
pubkey: getRevenueShareEscrowAccountPublicKey(
7663-
this.program.programId,
7664-
settleeUserAccount.authority
7665-
),
7666-
isSigner: false,
7667-
isWritable: true,
7668-
});
7669-
break;
7670-
}
7671-
}
7672-
7673-
const escrow = await revenueShareEscrowMap.mustGet(
7659+
const escrow = revenueShareEscrowMap.get(
76747660
settleeUserAccount.authority.toBase58()
76757661
);
76767662
if (escrow) {
7663+
const escrowPk = getRevenueShareEscrowAccountPublicKey(
7664+
this.program.programId,
7665+
settleeUserAccount.authority
7666+
);
7667+
76777668
const builders = new Map<number, PublicKey>();
76787669
for (const order of escrow.orders) {
7679-
if (!isBuilderOrderAvailable(order)) {
7680-
if (!builders.has(order.builderIdx)) {
7681-
builders.set(
7682-
order.builderIdx,
7683-
escrow.approvedBuilders[order.builderIdx].authority
7684-
);
7685-
}
7670+
const eligibleBuilder =
7671+
isBuilderOrderCompleted(order) &&
7672+
!isBuilderOrderReferral(order) &&
7673+
order.feesAccrued.gt(ZERO) &&
7674+
order.marketIndex === marketIndex;
7675+
if (eligibleBuilder && !builders.has(order.builderIdx)) {
7676+
builders.set(
7677+
order.builderIdx,
7678+
escrow.approvedBuilders[order.builderIdx].authority
7679+
);
76867680
}
76877681
}
76887682
if (builders.size > 0) {
7683+
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
7684+
remainingAccounts.push({
7685+
pubkey: escrowPk,
7686+
isSigner: false,
7687+
isWritable: true,
7688+
});
7689+
}
76897690
this.addBuilderToRemainingAccounts(
76907691
Array.from(builders.values()),
76917692
remainingAccounts
@@ -7701,44 +7702,37 @@ export class DriftClient {
77017702
);
77027703

77037704
if (hasReferralForMarket) {
7704-
const escrowPk = getRevenueShareEscrowAccountPublicKey(
7705-
this.program.programId,
7706-
settleeUserAccount.authority
7707-
);
77087705
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
77097706
remainingAccounts.push({
77107707
pubkey: escrowPk,
77117708
isSigner: false,
77127709
isWritable: true,
77137710
});
77147711
}
7715-
77167712
if (!escrow.referrer.equals(PublicKey.default)) {
7717-
const refUser = getUserAccountPublicKeySync(
7718-
this.program.programId,
7719-
escrow.referrer,
7720-
0
7713+
this.addBuilderToRemainingAccounts(
7714+
[escrow.referrer],
7715+
remainingAccounts
77217716
);
7722-
if (!remainingAccounts.find((a) => a.pubkey.equals(refUser))) {
7723-
remainingAccounts.push({
7724-
pubkey: refUser,
7725-
isSigner: false,
7726-
isWritable: true,
7727-
});
7728-
}
7729-
const refRevenueShare = getRevenueShareAccountPublicKey(
7717+
}
7718+
}
7719+
} else {
7720+
// Stale-cache fallback: if the user has any builder orders, include escrow PDA. This allows
7721+
// the program to lazily clean up any completed builder orders.
7722+
for (const order of settleeUserAccount.orders) {
7723+
if (hasBuilder(order)) {
7724+
const escrowPk = getRevenueShareEscrowAccountPublicKey(
77307725
this.program.programId,
7731-
escrow.referrer
7726+
settleeUserAccount.authority
77327727
);
7733-
if (
7734-
!remainingAccounts.find((a) => a.pubkey.equals(refRevenueShare))
7735-
) {
7728+
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
77367729
remainingAccounts.push({
7737-
pubkey: refRevenueShare,
7730+
pubkey: escrowPk,
77387731
isSigner: false,
77397732
isWritable: true,
77407733
});
77417734
}
7735+
break;
77427736
}
77437737
}
77447738
}
@@ -7859,98 +7853,90 @@ export class DriftClient {
78597853
});
78607854

78617855
if (revenueShareEscrowMap) {
7862-
for (const order of settleeUserAccount.orders) {
7863-
if (hasBuilder(order)) {
7864-
remainingAccounts.push({
7865-
pubkey: getRevenueShareEscrowAccountPublicKey(
7866-
this.program.programId,
7867-
settleeUserAccount.authority
7868-
),
7869-
isSigner: false,
7870-
isWritable: true,
7871-
});
7872-
break;
7873-
}
7874-
}
7875-
7876-
const escrow = await revenueShareEscrowMap.mustGet(
7856+
const escrow = revenueShareEscrowMap.get(
78777857
settleeUserAccount.authority.toBase58()
78787858
);
78797859
const builders = new Map<number, PublicKey>();
7880-
for (const order of escrow.orders) {
7881-
if (!isBuilderOrderAvailable(order)) {
7882-
if (!builders.has(order.builderIdx)) {
7860+
if (escrow) {
7861+
for (const order of escrow.orders) {
7862+
const eligibleBuilder =
7863+
isBuilderOrderCompleted(order) &&
7864+
!isBuilderOrderReferral(order) &&
7865+
order.feesAccrued.gt(ZERO) &&
7866+
marketIndexes.includes(order.marketIndex);
7867+
if (eligibleBuilder && !builders.has(order.builderIdx)) {
78837868
builders.set(
78847869
order.builderIdx,
78857870
escrow.approvedBuilders[order.builderIdx].authority
78867871
);
78877872
}
78887873
}
7889-
}
7890-
if (builders.size > 0) {
7891-
this.addBuilderToRemainingAccounts(
7892-
Array.from(builders.values()),
7893-
remainingAccounts
7894-
);
7895-
}
7896-
7897-
// Include escrow and referrer accounts when there are referral rewards
7898-
// for any of the markets we are settling, so on-chain sweep can find them.
7899-
const hasReferralForRequestedMarkets = escrow.orders.some(
7900-
(o) =>
7901-
isBuilderOrderReferral(o) &&
7902-
o.feesAccrued.gt(ZERO) &&
7903-
marketIndexes.includes(o.marketIndex)
7904-
);
7905-
console.log(
7906-
'hasReferralForRequestedMarkets',
7907-
hasReferralForRequestedMarkets
7908-
);
7909-
7910-
if (hasReferralForRequestedMarkets) {
7911-
// Ensure the user's RevenueShareEscrow is included if not already
7912-
const escrowPk = getRevenueShareEscrowAccountPublicKey(
7913-
this.program.programId,
7914-
settleeUserAccount.authority
7915-
);
7916-
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
7917-
remainingAccounts.push({
7918-
pubkey: escrowPk,
7919-
isSigner: false,
7920-
isWritable: true,
7921-
});
7922-
}
7923-
7924-
// Add referrer's User and RevenueShare accounts
7925-
if (!escrow.referrer.equals(PublicKey.default)) {
7926-
const refUser = getUserAccountPublicKeySync(
7874+
if (builders.size > 0) {
7875+
const escrowPk = getRevenueShareEscrowAccountPublicKey(
79277876
this.program.programId,
7928-
escrow.referrer,
7929-
0
7930-
);
7931-
console.log(
7932-
`adding referrer user and rev share ${escrow.referrer.toBase58()} to remaining accounts`
7877+
settleeUserAccount.authority
79337878
);
7934-
if (!remainingAccounts.find((a) => a.pubkey.equals(refUser))) {
7879+
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
79357880
remainingAccounts.push({
7936-
pubkey: refUser,
7881+
pubkey: escrowPk,
79377882
isSigner: false,
79387883
isWritable: true,
79397884
});
79407885
}
7941-
const refRevenueShare = getRevenueShareAccountPublicKey(
7886+
this.addBuilderToRemainingAccounts(
7887+
Array.from(builders.values()),
7888+
remainingAccounts
7889+
);
7890+
}
7891+
7892+
// Include escrow and referrer accounts when there are referral rewards
7893+
// for any of the markets we are settling, so on-chain sweep can find them.
7894+
const hasReferralForRequestedMarkets = escrow.orders.some(
7895+
(o) =>
7896+
isBuilderOrderReferral(o) &&
7897+
o.feesAccrued.gt(ZERO) &&
7898+
marketIndexes.includes(o.marketIndex)
7899+
);
7900+
7901+
if (hasReferralForRequestedMarkets) {
7902+
const escrowPk = getRevenueShareEscrowAccountPublicKey(
79427903
this.program.programId,
7943-
escrow.referrer
7904+
settleeUserAccount.authority
79447905
);
7945-
if (
7946-
!remainingAccounts.find((a) => a.pubkey.equals(refRevenueShare))
7947-
) {
7906+
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
79487907
remainingAccounts.push({
7949-
pubkey: refRevenueShare,
7908+
pubkey: escrowPk,
79507909
isSigner: false,
79517910
isWritable: true,
79527911
});
79537912
}
7913+
7914+
// Add referrer's User and RevenueShare accounts
7915+
if (!escrow.referrer.equals(PublicKey.default)) {
7916+
this.addBuilderToRemainingAccounts(
7917+
[escrow.referrer],
7918+
remainingAccounts
7919+
);
7920+
}
7921+
}
7922+
} else {
7923+
// Stale-cache fallback: if the user has any builder orders, include escrow PDA. This allows
7924+
// the program to lazily clean up any completed builder orders.
7925+
for (const order of settleeUserAccount.orders) {
7926+
if (hasBuilder(order)) {
7927+
const escrowPk = getRevenueShareEscrowAccountPublicKey(
7928+
this.program.programId,
7929+
settleeUserAccount.authority
7930+
);
7931+
if (!remainingAccounts.find((a) => a.pubkey.equals(escrowPk))) {
7932+
remainingAccounts.push({
7933+
pubkey: escrowPk,
7934+
isSigner: false,
7935+
isWritable: true,
7936+
});
7937+
}
7938+
break;
7939+
}
79547940
}
79557941
}
79567942
}

tests/builderCodes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,7 @@ describe('builder codes', () => {
921921
usdcPos.balanceType
922922
);
923923

924+
await userClient.fetchAccounts();
924925
const settleTx = await builderClient.settlePNL(
925926
await userClient.getUserAccountPublicKey(),
926927
userClient.getUserAccount(),
@@ -1195,6 +1196,7 @@ describe('builder codes', () => {
11951196
usdcPos.balanceType
11961197
);
11971198

1199+
await userClient.fetchAccounts();
11981200
await builderClient.settlePNL(
11991201
await userClient.getUserAccountPublicKey(),
12001202
userClient.getUserAccount(),
@@ -1355,6 +1357,7 @@ describe('builder codes', () => {
13551357
usdcPos.balanceType
13561358
);
13571359

1360+
await userClient.fetchAccounts();
13581361
const settleTx = await builderClient.settlePNL(
13591362
await userClient.getUserAccountPublicKey(),
13601363
userClient.getUserAccount(),

0 commit comments

Comments
 (0)