Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated slot subscriber provider #798

Merged
merged 9 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions sdk/src/priorityFee/averageOverSlotsStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import { PriorityFeeStrategy } from './types';

export class AverageOverSlotsStrategy implements PriorityFeeStrategy {
private lookbackSlots: number;

/**
* @param lookbackSlots The number of slots to look back from the max slot in the sample
*/
constructor(lookbackSlots = 10) {
this.lookbackSlots = lookbackSlots;
}

calculate(samples: { slot: number; prioritizationFee: number }[]): number {
if (samples.length === 0) {
return 0;
}
const stopSlot = samples[0].slot - this.lookbackSlots;
let runningSumFees = 0;
let countFees = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is countFees still needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah look down a few lines


for (let i = 0; i < samples.length; i++) {
if (samples[i].slot <= stopSlot) {
return runningSumFees / countFees;
}
runningSumFees += samples[i].prioritizationFee;
countFees++;
}
Expand Down
19 changes: 3 additions & 16 deletions sdk/src/priorityFee/maxOverSlotsStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { PriorityFeeStrategy } from './types';

export class MaxOverSlotsStrategy implements PriorityFeeStrategy {
private lookbackSlots: number;

/**
* @param lookbackSlots The number of slots to look back from the max slot in the sample
*/
constructor(lookbackSlots = 10) {
this.lookbackSlots = lookbackSlots;
}

calculate(samples: { slot: number; prioritizationFee: number }[]): number {

if (samples.length === 0) {
return 0;
}
// Assuming samples are sorted in descending order of slot.
const stopSlot = samples[0].slot - this.lookbackSlots;
let currMaxFee = samples[0].prioritizationFee;

for (let i = 0; i < samples.length; i++) {
if (samples[i].slot <= stopSlot) {
return currMaxFee;
}
if (samples[i].prioritizationFee > currMaxFee) {
currMaxFee = samples[i].prioritizationFee;
}
currMaxFee = Math.max(samples[i].prioritizationFee, currMaxFee);

}
return currMaxFee;
}
Expand Down
48 changes: 22 additions & 26 deletions sdk/src/priorityFee/priorityFeeSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class PriorityFeeSubscriber {
customStrategy?: PriorityFeeStrategy;
averageStrategy = new AverageOverSlotsStrategy();
maxStrategy = new MaxOverSlotsStrategy();
lookbackDistance : number;

intervalId?: ReturnType<typeof setTimeout>;

Expand All @@ -20,6 +21,10 @@ export class PriorityFeeSubscriber {
lastMaxStrategyResult = 0;
lastSlotSeen = 0;

/**
* @param props
* customStrategy : strategy to return the priority fee to use based on recent samples. defaults to AVERAGE.
*/
public constructor({
connection,
frequencyMs,
Expand All @@ -36,28 +41,12 @@ export class PriorityFeeSubscriber {
this.connection = connection;
this.frequencyMs = frequencyMs;
this.addresses = addresses;
if (slotsToCheck) {
this.averageStrategy = new AverageOverSlotsStrategy(slotsToCheck);
this.maxStrategy = new MaxOverSlotsStrategy(slotsToCheck);
if (!customStrategy) {
this.customStrategy = new AverageOverSlotsStrategy();
} else {
this.customStrategy=customStrategy;
}
if (customStrategy) {
this.customStrategy = customStrategy;
}
}

public get avgPriorityFee(): number {
return Math.floor(this.lastAvgStrategyResult);
}

public get maxPriorityFee(): number {
return Math.floor(this.lastMaxStrategyResult);
}

public get customPriorityFee(): number {
if (!this.customStrategy) {
console.error('Custom strategy not set');
}
return Math.floor(this.lastCustomStrategyResult);
this.lookbackDistance = slotsToCheck;
}

public async subscribe(): Promise<void> {
Expand All @@ -75,22 +64,29 @@ export class PriorityFeeSubscriber {
[this.addresses]
);

// getRecentPrioritizationFees returns results unsorted
const results: { slot: number; prioritizationFee: number }[] =
rpcJSONResponse?.result;

if (!results.length) return;
const descResults = results.sort((a, b) => b.slot - a.slot);

// # Sort and filter results based on the slot lookback setting
const descResults = results.sort((a, b) => b.slot - a.slot);
const mostRecentResult = descResults[0];
const cutoffSlot = mostRecentResult.slot - this.lookbackDistance;

const resultsToUse = descResults.filter(result => result.slot >= cutoffSlot);

// # Handle results
this.latestPriorityFee = mostRecentResult.prioritizationFee;
this.lastSlotSeen = mostRecentResult.slot;

this.lastAvgStrategyResult = this.averageStrategy.calculate(descResults);
this.lastMaxStrategyResult = this.maxStrategy.calculate(descResults);
this.lastAvgStrategyResult = this.averageStrategy.calculate(resultsToUse);
this.lastMaxStrategyResult = this.maxStrategy.calculate(resultsToUse);
if (this.customStrategy) {
this.lastCustomStrategyResult =
this.customStrategy.calculate(descResults);
this.customStrategy.calculate(resultsToUse);
}

}

public async unsubscribe(): Promise<void> {
Expand Down
Loading