Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Pool add getDepositQuote, getWithdrawQuote api's #47

Merged
10 commits merged into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/model/orca/pool/orca-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
TransactionPayload,
Percentage,
resolveOrCreateAssociatedTokenAddress,
ZERO,
} from "../../../public";
import {
createApprovalInstruction,
Expand Down Expand Up @@ -184,6 +185,48 @@ export class OrcaPoolImpl implements OrcaPool {
.build();
}

public async getDepositQuote(
maxTokenAIn: Decimal | OrcaU64,
maxTokenBIn: Decimal | OrcaU64,
slippage?: Decimal
): Promise<OrcaU64> {
const slippageTolerance =
slippage === undefined ? defaultSlippagePercentage : Percentage.fromDecimal(slippage);
const maxTokenAIn_U64 = U64Utils.toTokenU64(maxTokenAIn, this.getTokenA(), "maxTokenAIn");
const maxTokenBIn_U64 = U64Utils.toTokenU64(maxTokenBIn, this.getTokenB(), "maxTokenBIn");

const { inputTokenCount: tokenAAmount, outputTokenCount: tokenBAmount } = await getTokenCount(
this.connection,
this.poolParams,
this.getTokenA(),
this.getTokenB()
);
const lpSupply = await this.getLPSupply();

if (tokenAAmount.eq(ZERO) || tokenBAmount.eq(ZERO)) {
return new OrcaU64(ZERO, lpSupply.scale);
}

const poolTokenAmountWithA = maxTokenAIn_U64
.mul(slippageTolerance.denominator)
.mul(lpSupply.toU64())
.div(tokenAAmount)
.div(slippageTolerance.numerator.add(slippageTolerance.denominator));

const poolTokenAmountWithB = maxTokenBIn_U64
.mul(slippageTolerance.denominator)
.mul(lpSupply.toU64())
.div(tokenBAmount)
.div(slippageTolerance.numerator.add(slippageTolerance.denominator));

// Pick the smaller value of the two to calculate the minimum poolTokenAmount out
const minPoolTokenAmountOut = poolTokenAmountWithA.gt(poolTokenAmountWithB)
? poolTokenAmountWithB
: poolTokenAmountWithA;

return new OrcaU64(minPoolTokenAmountOut, lpSupply.scale);
}

public async deposit(
owner: Keypair | PublicKey,
maxTokenAIn: Decimal | OrcaU64,
Expand Down Expand Up @@ -270,6 +313,50 @@ export class OrcaPoolImpl implements OrcaPool {
.build();
}

public async getWithdrawQuote(
poolTokenIn: Decimal | OrcaU64,
slippage?: Decimal
): Promise<{ minTokenAOut: OrcaU64; minTokenBOut: OrcaU64 }> {
const slippageTolerance =
slippage === undefined ? defaultSlippagePercentage : Percentage.fromDecimal(slippage);
const poolTokenIn_U64 = U64Utils.toPoolU64(poolTokenIn, this.poolParams, "poolTokenIn");

const lpSupply = await this.getLPSupply();
const { inputTokenCount: tokenAAmount, outputTokenCount: tokenBAmount } = await getTokenCount(
this.connection,
this.poolParams,
this.getTokenA(),
this.getTokenB()
);

if (poolTokenIn_U64.eq(ZERO)) {
return {
minTokenAOut: new OrcaU64(ZERO, lpSupply.scale),
minTokenBOut: new OrcaU64(ZERO, lpSupply.scale),
};
}

const minTokenAOut = new OrcaU64(
poolTokenIn_U64
.mul(slippageTolerance.denominator)
.mul(tokenAAmount)
.div(lpSupply.toU64())
.div(slippageTolerance.numerator.add(slippageTolerance.denominator)),
this.getTokenA().scale
);

const minTokenBOut = new OrcaU64(
poolTokenIn_U64
.mul(slippageTolerance.denominator)
.mul(tokenBAmount)
.div(lpSupply.toU64())
.div(slippageTolerance.numerator.add(slippageTolerance.denominator)),
this.getTokenB().scale
);

return { minTokenAOut, minTokenBOut };
}

public async withdraw(
owner: Keypair | PublicKey,
poolTokenAmountIn: Decimal | OrcaU64,
Expand Down
26 changes: 25 additions & 1 deletion src/public/pools/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type OrcaPool = {
getQuote: (
inputToken: OrcaToken,
inputAmount: Decimal | OrcaU64,
slippage: Decimal
slippage?: Decimal
) => Promise<Quote>;

/**
Expand All @@ -69,6 +69,19 @@ export type OrcaPool = {
minimumAmountOut: Decimal | OrcaU64
) => Promise<TransactionPayload>;

/**
* Get minPoolTokenAmountOut to be used for deposit.
*
* @param maxTokenAIn The amount of token to deposit for token A
* @param maxTokenBIn The amount of token to deposit for token B
* @param slippage The slippage in percentage you are willing to take in deposit
*/
getDepositQuote: (
maxTokenAIn: Decimal | OrcaU64,
maxTokenBIn: Decimal | OrcaU64,
slippage?: Decimal
) => Promise<OrcaU64>;

/**
* Perform a deposit: send tokenA and tokenB, and receive a poolToken in return.
* Fee for the transaction will be paid by the owner's wallet.
Expand All @@ -90,6 +103,17 @@ export type OrcaPool = {
minPoolTokenAmountOut: Decimal | OrcaU64
) => Promise<TransactionPayload>;

/**
* Get minTokenAOut and mintTokenBOut amounts to be used for withdraw.
*
* @param poolTokenIn The amount of pool tokens to send in
* @param slippage The slippage in percentage you are willing to take in withdraw
*/
getWithdrawQuote: (
poolTokenIn: Decimal | OrcaU64,
slippage?: Decimal
) => Promise<{ minTokenAOut: OrcaU64; minTokenBOut: OrcaU64 }>;

/**
* Perform a withdraw: send poolToken, and receive tokenA and tokenB in return.
* Fee for the transaction will be paid by the owner's wallet.
Expand Down