Skip to content

Commit

Permalink
feat: adds loadExchangeInfoFromSwapAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Jan 3, 2022
1 parent f3ff597 commit 02d8de7
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion packages/stableswap-sdk/src/entities/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { Connection, PublicKey } from "@solana/web3.js";
import type JSBI from "jsbi";

import { SWAP_PROGRAM_ID } from "../constants";
import type { StableSwap } from "../stable-swap";
import { StableSwap } from "../stable-swap";
import type { Fees } from "../state/fees";
import { loadProgramAccount } from "../util/account";

Expand Down Expand Up @@ -187,3 +187,51 @@ export const makeExchange = ({
};
return exchange;
};

/**
* Get exchange info from just the swap account.
* @param connection
* @param swapAccount
* @param tokenA
* @param tokenB
* @returns
*/
export const loadExchangeInfoFromSwapAccount = async (
connection: Connection,
swapAccount: PublicKey,
tokenA: Token | undefined = undefined,
tokenB: Token | undefined = undefined
): Promise<IExchangeInfo | null> => {
const stableSwap = await StableSwap.load(connection, swapAccount);

const theTokenA =
tokenA ??
(await Token.load(connection, stableSwap.state.tokenA.mint))?.info;
if (!theTokenA) {
throw new Error(
`Token ${stableSwap.state.tokenA.mint.toString()} not found`
);
}

const theTokenB =
tokenB ??
(await Token.load(connection, stableSwap.state.tokenB.mint))?.info;
if (!theTokenB) {
throw new Error(
`Token ${stableSwap.state.tokenB.mint.toString()} not found`
);
}

const exchange = makeExchange({
swapAccount,
lpToken: stableSwap.state.poolTokenMint,
tokenA: theTokenA,
tokenB: theTokenB,
});

if (exchange === null) {
return null;
}

return await loadExchangeInfo(connection, exchange, stableSwap);
};

0 comments on commit 02d8de7

Please sign in to comment.