Skip to content

Commit

Permalink
express fees in sol
Browse files Browse the repository at this point in the history
  • Loading branch information
fengtality committed Jan 26, 2025
1 parent bd3f006 commit 8661331
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
32 changes: 17 additions & 15 deletions src/chains/solana/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ export class Solana {
});

if (!response.ok) {
throw new Error(`Failed to fetch fees: ${response.status}`);
logger.error(`[SOLANA] Failed to fetch priority fees, using minimum fee: ${response.status}`);
return this.config.minPriorityFee * 1_000_000 / this.config.defaultComputeUnits;
}

const data: PriorityFeeResponse = await response.json();
Expand All @@ -464,30 +465,31 @@ export class Solana {
fees.sort((a, b) => a - b);

// Calculate statistics
const minFee = Math.min(...fees);
const maxFee = Math.max(...fees);
const minFee = Math.min(...fees) / 1_000_000; // Convert to lamports
const maxFee = Math.max(...fees) / 1_000_000; // Convert to lamports
const averageFee = Math.floor(
fees.reduce((sum, fee) => sum + fee, 0) / fees.length
fees.reduce((sum, fee) => sum + fee, 0) / fees.length / 1_000_000 // Convert to lamports
);

logger.info(`[PRIORITY FEES] Range: ${minFee} - ${maxFee} microLamports (avg: ${averageFee})`);
logger.info(`[SOLANA] Recent priority fees paid: ${minFee.toFixed(4)} - ${maxFee.toFixed(4)} lamports (avg: ${averageFee.toFixed(4)})`);

// Calculate index for percentile
const percentileIndex = Math.ceil(fees.length * this.config.priorityFeePercentile);
let percentileFee = fees[percentileIndex - 1]; // -1 because array is 0-based
let basePriorityFee = fees[percentileIndex - 1] / 1_000_000; // Convert to lamports

// Ensure fee is not below minimum
percentileFee = Math.max(percentileFee, minimumFee);
// Ensure fee is not below minimum (convert SOL to lamports)
const minimumFeeLamports = Math.floor(this.config.minPriorityFee * 1e9 / this.config.defaultComputeUnits);
basePriorityFee = Math.max(basePriorityFee, minimumFeeLamports);

logger.info(`[PRIORITY FEES] Used: ${percentileFee} microLamports (${percentileFee === minimumFee ? 'minimum' : `${this.config.priorityFeePercentile * 100}th percentile`})`);
logger.info(`[SOLANA] Base priority fee: ${basePriorityFee.toFixed(4)} lamports (${basePriorityFee === minimumFeeLamports ? 'minimum' : `${this.config.priorityFeePercentile * 100}th percentile`})`);

// Cache the result
Solana.lastPriorityFeeEstimate = {
timestamp: Date.now(),
fee: percentileFee,
fee: basePriorityFee,
};

return percentileFee;
return basePriorityFee;

} catch (error: any) {
throw new Error(`Failed to fetch priority fees: ${error.message}`);
Expand Down Expand Up @@ -580,18 +582,18 @@ export class Solana {

logger.info(`Attempting transaction with: Priority Fee: ${currentPriorityFee} microLamports/CU, Compute Units: ${computeUnitsToUse}, Total Fee: ${totalPriorityFeeLamports} lamports`);

// Check if we've exceeded max fee (in lamports)
if (totalPriorityFeeLamports > this.config.maxPriorityFee) {
// Check if we've exceeded max fee (convert maxPriorityFee from SOL to lamports)
if (totalPriorityFeeLamports > this.config.maxPriorityFee * 1e9) {
throw new Error(
`Transaction failed after reaching maximum priority fee of ${
this.config.maxPriorityFee
} lamports`
} SOL`
);
}

// currentPriorityFee is already in microLamports per compute unit
const priorityFeeInstruction = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: Math.floor(currentPriorityFee),
microLamports: Math.floor(currentPriorityFee * 1_000_000),
});
// Remove any existing priority fee instructions and add the new one
tx.instructions = [
Expand Down
7 changes: 4 additions & 3 deletions src/connectors/jupiter/jupiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ export class Jupiter {
async getSwapObj(wallet: Wallet, quote: QuoteResponse, priorityFee?: number): Promise<SwapResponse> {
const prioritizationFeeLamports = priorityFee
? priorityFee
: this.solana.config.minPriorityFee;
: Math.floor(this.solana.config.minPriorityFee * 1e9);

logger.info(`Sending swap with priority fee: ${priorityFee / 1e9} SOL`);
logger.info(`Sending swap with priority fee: ${prioritizationFeeLamports / 1e9} SOL`);

let lastError: Error | null = null;
for (let attempt = 1; attempt <= JUPITER_API_RETRY_COUNT; attempt++) {
Expand Down Expand Up @@ -196,7 +196,8 @@ export class Jupiter {
}> {
let currentPriorityFee = (await this.solana.getGasPrice() * 1e9) - BASE_FEE;

while (currentPriorityFee <= this.solana.config.maxPriorityFee) {
// Convert maxPriorityFee from SOL to lamports for comparison
while (currentPriorityFee <= this.solana.config.maxPriorityFee * 1e9) {
const swapObj = await this.getSwapObj(wallet, quote, currentPriorityFee);

const swapTransactionBuf = Buffer.from(swapObj.swapTransaction, 'base64');
Expand Down
4 changes: 2 additions & 2 deletions src/templates/server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ ipWhitelist: []
# will only be stored in logPath and not printed to stdout.
logToStdOut: true

# If true, Fastify logs will be printed to stdout. If false, they will only be stored in logPath.
fastifyLogs: false
# If true, the server will print detailed Fastify logs for each request and response to stdout. If false, only standard logs will be emitted.
fastifyLogs: true

# Nonce database
nonceDbPath: 'nonce.level'
Expand Down
8 changes: 4 additions & 4 deletions src/templates/solana.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ priorityFeePercentile: 0.75
# Multiplier for increasing priority fee on retry
priorityFeeMultiplier: 2

# Maximum priority fee in lamports
maxPriorityFee: 2000000
# Maximum priority fee in SOL
maxPriorityFee: 0.001

# Minimum priority fee in lamports
minPriorityFee: 100000
# Minimum priority fee in SOL
minPriorityFee: 0.00005

# Retry interval in milliseconds
retryIntervalMs: 500
Expand Down

0 comments on commit 8661331

Please sign in to comment.