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

feat: okx fallback for jup #26

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
71 changes: 9 additions & 62 deletions src/auction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { ChainId, isEVMChain } from '@certusone/wormhole-sdk';
import { getAssociatedTokenAddressSync, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { Connection, PublicKey } from '@solana/web3.js';
import axios from 'axios';
import { ethers } from 'ethers6';
import { CHAIN_ID_SOLANA } from './config/chains';
import { RpcConfig } from './config/rpc';
import { Token } from './config/tokens';
import { WalletConfig } from './config/wallet';
import { driverConfig } from './driver.conf';
Expand All @@ -21,7 +19,6 @@ export class AuctionFulfillerConfig {
private readonly forceBid = true;

constructor(
private readonly rpcConfig: RpcConfig,
private readonly connection: Connection,
private readonly evmProviders: EvmProviders,
private readonly walletConfig: WalletConfig,
Expand Down Expand Up @@ -118,15 +115,14 @@ export class AuctionFulfillerConfig {
if (driverToken.contract === toToken.contract) {
output = BigInt(Math.floor(effectiveAmountInDriverToken * 10 ** driverToken.decimals));
} else {
const quoteRes = await this.swapRouters.getQuote(
const quoteRes = await this.swapRouters.getEVMQuote(
{
whChainId: destChain,
srcToken: driverToken.contract,
destToken: toToken.contract,
amountIn: BigInt(Math.floor(effectiveAmountInDriverToken * 10 ** driverToken.decimals)).toString(),
timeout: 2000,
},
true,
3,
);

Expand All @@ -149,18 +145,18 @@ export class AuctionFulfillerConfig {
if (driverToken.contract === toToken.contract) {
output = BigInt(Math.floor(effectiveAmountInDriverToken * 10 ** driverToken.decimals));
} else {
const quoteRes = await this.getJupQuoteWithRetry(
BigInt(Math.floor(effectiveAmountInDriverToken * 10 ** driverToken.decimals)),
driverToken.mint,
toToken.mint,
0.1, // 10%
);

const quoteRes = await this.swapRouters.getSolQuote({
inputMint: driverToken.mint,
outputMint: toToken.mint,
slippageBps: 1000,
amount: BigInt(Math.floor(effectiveAmountInDriverToken * 10 ** driverToken.decimals)).toString(),
maxAccounts: 64 - 7,
});
if (!quoteRes || !quoteRes.raw) {
throw new Error('jupiter quote for bid in swift failed');
}

output = BigInt(Math.floor(Number(quoteRes.expectedAmountOut)));
output = BigInt(Math.floor(Number(quoteRes.outAmount)));
}

return output;
Expand Down Expand Up @@ -219,55 +215,6 @@ export class AuctionFulfillerConfig {
return finalAmountIn;
}

private async getJupQuoteWithRetry(
amountIn: bigint,
fromMint: string,
toMint: string,
slippage: number,
retry: number = 10,
): Promise<any> {
let res;
do {
try {
let params: any = {
inputMint: fromMint,
outputMint: toMint,
slippageBps: slippage * 10000,
maxAccounts: 64 - 7, // 7 accounts reserved for other instructions
amount: amountIn,
};
if (!!this.rpcConfig.jupExcludedDexes) {
params['excludeDexes'] = this.rpcConfig.jupExcludedDexes;
}
if (!!this.rpcConfig.jupApiKey) {
params['token'] = this.rpcConfig.jupApiKey;
}
const { data } = await axios.get(`${this.rpcConfig.jupV6Endpoint}/quote`, {
params: params,
});
res = data;
} catch (err) {
logger.warn(`error in fetch jupiter ${err} try ${retry}`);
} finally {
retry--;
}
} while ((!res || !res.outAmount) && retry > 0);

if (!res) {
logger.error(`juptier quote failed ${fromMint} ${toMint} ${amountIn}`);
return null;
}

return {
effectiveAmountIn: res.inAmount,
expectedAmountOut: res.outAmount,
priceImpact: res.priceImpactPct,
minAmountOut: res.otherAmountThreshold,
route: [],
raw: res,
};
}

private async calcProtocolAndRefBps(
amountIn: bigint,
tokenIn: Token,
Expand Down
6 changes: 6 additions & 0 deletions src/config/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export type RpcConfig = {
jupApiKey: string;
jupExcludedDexes: string;
wormholeGuardianRpcs: string[];
okxApiKey: string;
okxPassPhrase: string;
okxSecretKey: string;
};

export const rpcConfig: RpcConfig = {
Expand Down Expand Up @@ -69,4 +72,7 @@ export const rpcConfig: RpcConfig = {
jupApiKey: process.env.JUP_API_KEY || '',
jupExcludedDexes: process.env.JUP_EXCLUDED_DEXES || '',
wormholeGuardianRpcs: process.env.WORMHOLE_GUARDIAN_RPCS!.split(','),
okxApiKey: process.env.OKX_API_KEY || '',
okxPassPhrase: process.env.OKX_PASSPHRASE || '',
okxSecretKey: process.env.OKX_SECRET_KEY || '',
};
6 changes: 2 additions & 4 deletions src/driver/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class EvmFulfiller {
evmRouterCalldata: string;
expectedAmountOut: bigint;
}> {
const oneInchSwap = await this.swapRouters.getSwap(
const oneInchSwap = await this.swapRouters.getEVMSwap(
{
amountIn: realAmountIn.toString(),
destToken: toToken.contract,
Expand All @@ -325,7 +325,6 @@ export class EvmFulfiller {
srcToken: driverToken.contract,
timeout: 3000,
},
true,
4,
);

Expand All @@ -347,15 +346,14 @@ export class EvmFulfiller {
if (driverToken.contract === toToken.contract) {
bidAmount = BigInt(Math.floor(effectiveAmountInDriverToken * 0.9999 * 10 ** driverToken.decimals));
} else {
const quoteRes = await this.swapRouters.getQuote(
const quoteRes = await this.swapRouters.getEVMQuote(
{
whChainId: destChain,
srcToken: driverToken.contract,
destToken: toToken.contract,
amountIn: BigInt(Math.floor(effectiveAmountInDriverToken * 10 ** driverToken.decimals)).toString(),
timeout: 2000,
},
true,
3,
);

Expand Down
Loading