Skip to content

Commit

Permalink
chore: add some extra config
Browse files Browse the repository at this point in the history
fix invalid symbol issue
  • Loading branch information
VuongRightHand committed Oct 10, 2024
1 parent 0fabad9 commit ada3d56
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
32 changes: 25 additions & 7 deletions src/chains/solana/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ export class Solana {
// public gasCost: number;
public connection: Connection;
public controller: typeof SolanaController;
private _chainId: number = 900;
private _rpcUrl: string;

constructor(network: string, rpc: string, assetListSource: string) {
this._network = network;
this.connection = new Connection(rpc);
this.controller = SolanaController;
this._assetListSource = assetListSource;
this._rpcUrl = rpc;
}
public get network(): string {
return this._network;
Expand All @@ -43,6 +46,13 @@ export class Solana {
return this._ready;
}

public get chainId(): number {
return this._chainId;
}

public get rpcUrl(): string {
return this._rpcUrl;
}
private async loadAssets(): Promise<void> {
const assetData = await this.getAssetData();
for (const result of assetData) {
Expand Down Expand Up @@ -112,13 +122,17 @@ export class Solana {
}

public async getAssetBalance(account: Keypair, tokenAddress: string) {
const associatedAccount = await this.getAssociatedTokenAccount(
tokenAddress,
account.publicKey.toString(),
);
const assetBalance =
await this.connection.getTokenAccountBalance(associatedAccount);
return assetBalance?.value?.uiAmountString ?? '0';
try {
const associatedAccount = await this.getAssociatedTokenAccount(
tokenAddress,
account.publicKey.toString(),
);
const assetBalance =
await this.connection.getTokenAccountBalance(associatedAccount);
return assetBalance?.value?.uiAmountString ?? '0';
} catch (e) {
return '0';
}
}

public async getKeypairFromPrivateKey(mnemonic: string): Promise<Keypair> {
Expand Down Expand Up @@ -204,4 +218,8 @@ export class Solana {

return this.getKeypairFromPrivateKey(mnemonic);
}

async getCurrentBlockNumber() {
return await this.connection.getSlot();
}
}
13 changes: 8 additions & 5 deletions src/connectors/jupiterswap/jupiter.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Solana } from '../../chains/solana/solana';
import { Jupiter } from './jupiter';
import { PriceRequest, TradeRequest } from '../../amm/amm.requests';
import {
PriceRequest,
TradeRequest,
TradeResponse,
} from '../../amm/amm.requests';
import axios from 'axios';
import { JupiterPriceResponse } from './jupiter.request';
import { logger } from '../../services/logger';
Expand All @@ -13,7 +17,6 @@ import {
SWAP_PRICE_LOWER_THAN_LIMIT_PRICE_ERROR_MESSAGE,
} from '../../services/error-handler';
import { latency } from '../../services/base';

export async function getPairData(base: string, quote: string) {
const baseURL = `https://api.jup.ag/price/v2?ids=${base},${quote}&showExtraInfo=true`;
const response = await axios.get<JupiterPriceResponse>(baseURL);
Expand All @@ -38,7 +41,7 @@ export async function jupiterTrade(
solana: Solana,
jupiter: Jupiter,
req: TradeRequest,
): Promise<any> {
): Promise<TradeResponse> {
const startTimestamp: number = Date.now();
const { address } = req;
const keypair = await solana.getAccountFromAddress(address);
Expand Down Expand Up @@ -86,10 +89,10 @@ export async function jupiterTrade(
rawAmount: req.amount,
expectedIn: String(trade.expectedAmount),
price: String(estimatedPrice),
// gasPrice: solana.gasPrice,
gasPrice: 10,
gasPriceToken: solana.nativeTokenSymbol,
gasLimit: tx.computeUnitLimit,
// gasCost: String(solana.gasCost),
gasCost: String(10),
txHash: tx.txid,
};
}
6 changes: 4 additions & 2 deletions src/connectors/jupiterswap/jupiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ export class Jupiter {
// }
// async estimateTrade(req: PriceRequest) {}
async price(req: PriceRequest) {
const baseSymbol = req.base.replace('_', '');
const quoteSymbol = req.quote.replace('_', '');
const startTimestamp: number = Date.now();
const baseToken = this.chain.getAssetForSymbol(req.base);
const quoteToken = this.chain.getAssetForSymbol(req.quote);
const baseToken = this.chain.getAssetForSymbol(baseSymbol);
const quoteToken = this.chain.getAssetForSymbol(quoteSymbol);
if (!baseToken || !quoteToken) {
throw new Error('INVALID TOKEN');
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"target": "es6",
"module": "commonjs",
"allowJs": true,
"lib": ["es6", "es2020", "esnext.asynciterable", "dom"],
"lib": ["es6", "es2020", "esnext.asynciterable", "dom", "ES2021.String"],
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
Expand Down

0 comments on commit ada3d56

Please sign in to comment.