Skip to content

Commit

Permalink
sdk: add max fee clamp to priority fee
Browse files Browse the repository at this point in the history
  • Loading branch information
wphan committed Jan 28, 2024
1 parent b0c1d8a commit 87e9d22
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions sdk/src/priorityFee/priorityFeeSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class PriorityFeeSubscriber {
maxStrategy = new MaxOverSlotsStrategy();
priorityFeeMethod = PriorityFeeMethod.SOLANA;
lookbackDistance: number;
maxFeeMicroLamports?: number;

heliusRpcUrl?: string;
lastHeliusSample?: HeliusPriorityFeeLevels;
Expand Down Expand Up @@ -69,6 +70,8 @@ export class PriorityFeeSubscriber {
);
}
}

this.maxFeeMicroLamports = config.maxFeeMicroLamports;
}

public async subscribe(): Promise<void> {
Expand Down Expand Up @@ -103,6 +106,16 @@ export class PriorityFeeSubscriber {
this.addresses
);
this.lastHeliusSample = sample?.result?.priorityFeeLevels ?? undefined;

if (this.lastHeliusSample) {
this.lastAvgStrategyResult =
this.heliusRpcUrl[HeliusPriorityLevel.MEDIUM];
this.lastMaxStrategyResult =
this.heliusRpcUrl[HeliusPriorityLevel.UNSAFE_MAX];
if (this.customStrategy) {
this.lastCustomStrategyResult = this.customStrategy.calculate(sample!);
}
}
}

public getHeliusPriorityFeeLevel(
Expand All @@ -111,18 +124,30 @@ export class PriorityFeeSubscriber {
if (this.lastHeliusSample === undefined) {
return 0;
}
if (this.maxFeeMicroLamports !== undefined) {
return Math.max(this.maxFeeMicroLamports, this.lastHeliusSample[level]);
}
return this.lastHeliusSample[level];
}

public getCustomStrategyResult(): number {
if (this.maxFeeMicroLamports !== undefined) {
return Math.max(this.maxFeeMicroLamports, this.lastCustomStrategyResult);
}
return this.lastCustomStrategyResult;
}

public getAvgStrategyResult(): number {
if (this.maxFeeMicroLamports !== undefined) {
return Math.max(this.maxFeeMicroLamports, this.lastAvgStrategyResult);
}
return this.lastAvgStrategyResult;
}

public getMaxStrategyResult(): number {
if (this.maxFeeMicroLamports !== undefined) {
return Math.max(this.maxFeeMicroLamports, this.lastMaxStrategyResult);
}
return this.lastMaxStrategyResult;
}

Expand Down
2 changes: 2 additions & 0 deletions sdk/src/priorityFee/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ export type PriorityFeeSubscriberConfig = {
slotsToCheck?: number;
/// url for helius rpc, required if using priorityFeeMethod.HELIUS
heliusRpcUrl?: string;
/// clamp any returned priority fee value to this value.
maxFeeMicroLamports?: number;
};

0 comments on commit 87e9d22

Please sign in to comment.