Skip to content

Commit

Permalink
feat: slippage passed to new futarchy sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasDeco committed May 17, 2024
1 parent 22ad33b commit f2e8f67
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
15 changes: 13 additions & 2 deletions lib/client/indexer/market-clients/ammMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,26 @@ export class FutarchyIndexerAmmMarketsClient
);
}

/**
*
* @param ammMarket
* @param inputAmount
* @param isBuyBase
* @param slippage - pass this in as a percentage (0.3% would equal 0.3)
* @returns
*/

async getSwapPreview(
ammMarket: AmmMarket,
inputAmount: number,
isBuyBase: boolean
isBuyBase: boolean,
slippage: number
): Promise<SwapPreview> {
return this.rpcMarketsClient.getSwapPreview(
ammMarket,
inputAmount,
isBuyBase
isBuyBase,
slippage
);
}

Expand Down
15 changes: 11 additions & 4 deletions lib/client/rpc/market-clients/ammMarkets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ export class FutarchyAmmMarketsRPCClient implements FutarchyAmmMarketsClient {
outputAmountMin,
outputToken.decimals
);
// TODO don't need to do this if we use new futarchy SDK with slippage on preview calculation
const outputAmountWithSlippage = new BN(
calculateMinWithSlippage(outputAmountMinScaled.toNumber(), slippage)
);
Expand All @@ -376,7 +377,8 @@ export class FutarchyAmmMarketsRPCClient implements FutarchyAmmMarketsClient {
async getSwapPreview(
ammMarket: AmmMarket,
inputAmount: number,
isBuyBase: boolean
isBuyBase: boolean,
slippage: number
): Promise<SwapPreview> {
// TODO we shouldn't need to refetch this if we can build the account type correctly
const ammAccount = await this.ammClient.getAmm(ammMarket.publicKey);
Expand All @@ -386,18 +388,22 @@ export class FutarchyAmmMarketsRPCClient implements FutarchyAmmMarketsClient {
isBuyBase ? ammMarket.quoteToken.decimals : ammMarket.baseToken.decimals
);

const slippageBps = new BN(slippage * 100);

const resp = this.calculateSwapPreview(
ammAccount,
new BN(inputAmountLots),
isBuyBase
isBuyBase,
slippageBps
);
return resp;
}

calculateSwapPreview(
amm: AmmAccount,
inputAmount: BN,
isBuyBase: boolean
isBuyBase: boolean,
slippageBps: BN
): SwapPreview {
const swapType = isBuyBase ? { buy: {} } : { sell: {} };
let startPrice = amm.quoteAmount / amm.baseAmount;
Expand All @@ -407,7 +413,8 @@ export class FutarchyAmmMarketsRPCClient implements FutarchyAmmMarketsClient {
inputAmount,
swapType,
amm.baseAmount,
amm.quoteAmount
amm.quoteAmount,
slippageBps
);

const finalPrice = newQuoteReserves / newBaseReserves;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"dependencies": {
"@coral-xyz/anchor": "^0.28.1-beta.2",
"@metadaoproject/futarchy": "^0.3.0-alpha.2",
"@metadaoproject/futarchy": "^0.3.0-alpha.5",
"@metadaoproject/futarchy-ts": "2.1.0",
"@metaplex-foundation/js": "^0.20.1",
"@metaplex-foundation/mpl-token-metadata": "^3.2.1",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2e8f67

Please sign in to comment.