Skip to content

Commit

Permalink
Check token balance before bid (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: mrlotfi <[email protected]>
  • Loading branch information
mrlotfi and mrlotfi authored Oct 31, 2024
1 parent ef1ee41 commit 6127271
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
53 changes: 52 additions & 1 deletion src/auction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
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, WhChainIdToEvm } from './config/chains';
import { RpcConfig } from './config/rpc';
import { Token } from './config/tokens';
import { WalletConfig } from './config/wallet';
import { driverConfig } from './driver.conf';
import { get1InchQuote } from './driver/routers';
import { Swap } from './swap.dto';
import { getErc20Balance } from './utils/erc20';
import { EvmProviders } from './utils/evm-providers';
import { SwiftCosts } from './utils/fees';
import logger from './utils/logger';

Expand All @@ -13,14 +20,26 @@ export class AuctionFulfillerConfig {
private readonly fulfillAggressionPercent = driverConfig.fulfillAggressionPercent;
private readonly forceBid = true;

constructor(private readonly rpcConfig: RpcConfig) {}
constructor(
private readonly rpcConfig: RpcConfig,
private readonly connection: Connection,
private readonly evmProviders: EvmProviders,
private readonly walletConfig: WalletConfig,
) {}

async normalizedBidAmount(
driverToken: Token,
effectiveAmountIn: number,
swap: Swap,
costs: SwiftCosts,
): Promise<bigint> {
const balance = await this.getTokenBalance(driverToken);
if (balance < effectiveAmountIn) {
throw new Error(`Insufficient balance for ${swap.sourceTxHash}. Dropping bid`);
} else {
logger.info(`Balance is ${balance} for ${swap.sourceTxHash}`);
}

if (swap.fromAmount.toNumber() * costs.fromTokenPrice > driverConfig.volumeLimitUsd) {
throw new Error(`Volume limit exceeded for ${swap.sourceTxHash} and dropping bid`);
}
Expand Down Expand Up @@ -260,4 +279,36 @@ export class AuctionFulfillerConfig {
return BigInt(3 + referrerBps);
}
}

private async getTokenBalance(token: Token): Promise<number> {
if (token.wChainId === CHAIN_ID_SOLANA) {
if (token.contract === ethers.ZeroAddress) {
const balance = await this.connection.getBalance(this.walletConfig.solana.publicKey);
return balance / 10 ** 9;
} else {
const ataKey = getAssociatedTokenAddressSync(
new PublicKey(token.contract),
this.walletConfig.solana.publicKey,
false,
token.standard === 'spl2022' ? TOKEN_2022_PROGRAM_ID : TOKEN_PROGRAM_ID,
);
const balance = await this.connection.getTokenAccountBalance(ataKey);
return balance?.value?.uiAmount || 0;
}
} else if (isEVMChain(token.wChainId as ChainId)) {
if (token.contract === ethers.ZeroAddress) {
const balance = await this.evmProviders[token.wChainId!].getBalance(this.walletConfig.evm.address);
return Number(balance) / 10 ** 18;
} else {
const balance64 = await getErc20Balance(
this.evmProviders[token.wChainId!],
token.contract,
this.walletConfig.evm.address,
);
return Number(balance64) / 10 ** token.decimals;
}
} else {
throw new Error(`Unsupported chain ${token.wChainId}`);
}
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export async function main() {
await evmFulFiller.init();
const driverSvc = new DriverService(
new SimpleFulfillerConfig(),
new AuctionFulfillerConfig(rpcConfig),
new AuctionFulfillerConfig(rpcConfig, solanaConnection, evmProviders, walletConf),
solanaConnection,
walletConf,
rpcConfig,
Expand Down

0 comments on commit 6127271

Please sign in to comment.