Skip to content

Commit

Permalink
fix: build error
Browse files Browse the repository at this point in the history
  • Loading branch information
hkey0 committed Nov 15, 2024
1 parent 3e8b989 commit 9667ca6
Showing 1 changed file with 14 additions and 69 deletions.
83 changes: 14 additions & 69 deletions lib/priceUtils.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
import { Connection, Keypair, Transaction, PublicKey } from '@solana/web3.js';
import {
buildWhirlpoolClient,
PoolUtil,
SwapQuote, swapQuoteByInputToken,
Whirlpool,
WhirlpoolContext,
WhirlpoolsConfigExtensionData
} from '@orca-so/whirlpools-sdk';
import { AddressUtil, DecimalUtil, Percentage, Wallet} from '@orca-so/common-sdk';
import { Connection, PublicKey } from '@solana/web3.js';
const BN = require("bn.js")
import Decimal from "decimal.js";

const WHIRLPOOL_PROGRAM_ID = new PublicKey('whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc');

export function getABMints(sourceMint: PublicKey, targetMint: PublicKey): [PublicKey, PublicKey] {
const [addressA, addressB] = PoolUtil.orderMints(sourceMint, targetMint);
return [AddressUtil.toPubKey(addressA), AddressUtil.toPubKey(addressB)];
}

export function getWhirlpoolsContext(connection: Connection): WhirlpoolContext {
const DUMMY_WALLET = {
async signTransaction(tx: Transaction) {
return tx;
},
async signAllTransactions(txs: Transaction[]) {
return txs;
},
publicKey: Keypair.generate().publicKey
};
return WhirlpoolContext.from(connection, DUMMY_WALLET as Wallet, WHIRLPOOL_PROGRAM_ID);
}

const PYTH_PUBLIC_KEY = new PublicKey('HDwcJBJXjL9FpJ7UBsYBtaDjsBUhuLCUYoz3zr8SWWaQ')

function findCorrectPool(mintA: PublicKey, mintB: PublicKey): PublicKey {
// usdc
Expand All @@ -43,46 +16,18 @@ function findCorrectPool(mintA: PublicKey, mintB: PublicKey): PublicKey {
}


export async function getPoolAndQuote(
context: WhirlpoolContext,
mintA: PublicKey,
mintB: PublicKey,
sourceMint: PublicKey,
amount: typeof BN,
slippingTolerance: Percentage
): Promise<[Whirlpool, SwapQuote]> {
const client = buildWhirlpoolClient(context);
const whirlpoolKey = findCorrectPool(mintA, mintB);
const whirlpool = await client.getPool(whirlpoolKey);
const quote = await swapQuoteByInputToken(
whirlpool,
sourceMint,
amount,
slippingTolerance,
WHIRLPOOL_PROGRAM_ID,
context.fetcher,
);
return [whirlpool, quote];
}
export async function fetchTokenPrice() {
const response = await fetch("https://pools-api-eclipse.mainnet.orca.so/prices?amount=100000000");

export async function fetchTokenPrice(connection: Connection, token: string) {
const ctx = getWhirlpoolsContext(connection);
console.log("checkprice")
if (!response.ok) {
throw new Error("Failed to fetch prices.");
}

const [mintA, mintB] = getABMints(
new PublicKey(token),
// USDC mint addr
new PublicKey("AKEWE7Bgh87GPp171b4cJPSSZfmZwQ3KaqYqXoKLNAEE")
);
const [_, quote] = await getPoolAndQuote(
ctx,
mintA,
mintB,
new PublicKey(token),
DecimalUtil.toBN(new Decimal("1"), 6),
Percentage.fromFraction(10, 1000)
);
return (quote.estimatedAmountOut.words[0] / 1000000)
const data = await response.json();
console.log(data, "data")

return [
data.data.GU7NS9xCwgNPiAdJ69iusFrRfawjDDPjeMBovhV1d4kn, //TETH
data.data.BeRUj3h7BqkbdfFU7FBNYbodgf8GCHodzKvF9aVjNNfL // SOL
]
}


0 comments on commit 9667ca6

Please sign in to comment.