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

Commit 4399c67

Browse files
authored
Set Prettier line width to 100 (#4)
1 parent 37f4e5a commit 4399c67

10 files changed

+35
-118
lines changed

.prettierrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"printWidth": 100
3+
}

src/constants/pools.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ export const ethUsdcPool: OrcaPoolParams = Object.freeze({
2424
address: new PublicKey("DY8qBwVGLeLJSrWib7L16mL7oB4HNAQ2f9yiYWKof54v"),
2525
nonce: 255,
2626
authority: new PublicKey("82oSibpDKnPZ2Yk1vn6McjCsQQbKfBkGeEh5FsqeVrtU"),
27-
poolTokenAddress: new PublicKey(
28-
"7TYb32qkwYosUQfUspU45cou7Bb3nefJocVMFX2mEGTT"
29-
),
27+
poolTokenAddress: new PublicKey("7TYb32qkwYosUQfUspU45cou7Bb3nefJocVMFX2mEGTT"),
3028
poolTokenMint: new PublicKey("7TYb32qkwYosUQfUspU45cou7Bb3nefJocVMFX2mEGTT"),
3129
poolTokenDecimals: 9,
3230
feeAccount: new PublicKey("AcMaBVt6S43JQXKnEDqdicxYofb5Cj1UgFWF9AsurTp6"),

src/model/orca-pool.ts

+8-30
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import { defaultSlippagePercentage } from "../constants/orca-defaults";
66
import { orcaPoolConfigs } from "../constants/pools";
77
import { deserializeAccount } from "../utils/web3/deserialize-account";
88
import { findAssociatedTokenAddress } from "../utils/web3/find-associated-token-address";
9-
import {
10-
getTokens,
11-
PoolTokenCount,
12-
getTokenCount,
13-
} from "../utils/web3/get-token-count";
9+
import { getTokens, PoolTokenCount, getTokenCount } from "../utils/web3/get-token-count";
1410
import { OrcaPoolParams } from "./orca/orca-types";
1511
import { QuotePoolParams, QuoteBuilderFactory } from "./quote/quote-builder";
1612
import { DecimalUtil } from "../utils/decimal-utils";
@@ -40,10 +36,7 @@ class OrcaPoolImpl implements OrcaPool {
4036
}
4137

4238
public async getLPBalance(user: PublicKey): Promise<number> {
43-
const address = await findAssociatedTokenAddress(
44-
user,
45-
this.poolParams.poolTokenMint
46-
);
39+
const address = await findAssociatedTokenAddress(user, this.poolParams.poolTokenMint);
4740

4841
// TODO: SOL account handling
4942
const accountInfo = await this.connection.getAccountInfo(address);
@@ -57,23 +50,15 @@ class OrcaPoolImpl implements OrcaPool {
5750
throw new Error("Failed to parse user account for LP token.");
5851
}
5952

60-
return DecimalUtil.fromU64(
61-
result.amount,
62-
this.poolParams.poolTokenDecimals
63-
).toNumber();
53+
return DecimalUtil.fromU64(result.amount, this.poolParams.poolTokenDecimals).toNumber();
6454
}
6555

6656
public async getLPSupply(): Promise<number> {
67-
const context = await this.connection.getTokenSupply(
68-
this.poolParams.poolTokenMint
69-
);
57+
const context = await this.connection.getTokenSupply(this.poolParams.poolTokenMint);
7058

7159
const amt = new u64(context.value.amount);
7260

73-
return DecimalUtil.fromU64(
74-
amt,
75-
this.poolParams.poolTokenDecimals
76-
).toNumber();
61+
return DecimalUtil.fromU64(amt, this.poolParams.poolTokenDecimals).toNumber();
7762
}
7863

7964
public async getQuote(
@@ -82,16 +67,11 @@ class OrcaPoolImpl implements OrcaPool {
8267
slippage?: number
8368
): Promise<Quote> {
8469
const slippageTolerance =
85-
slippage === undefined
86-
? defaultSlippagePercentage
87-
: PercentageUtils.fromNumber(slippage);
70+
slippage === undefined ? defaultSlippagePercentage : PercentageUtils.fromNumber(slippage);
8871

8972
const feeStructure = this.poolParams.feeStructure;
9073

91-
const [inputPoolToken, outputPoolToken] = getTokens(
92-
this.poolParams,
93-
inputTokenId
94-
);
74+
const [inputPoolToken, outputPoolToken] = getTokens(this.poolParams, inputTokenId);
9575

9676
const poolTokenCount: PoolTokenCount = await getTokenCount(
9777
this.connection,
@@ -108,9 +88,7 @@ class OrcaPoolImpl implements OrcaPool {
10888
slippageTolerance: slippageTolerance,
10989
};
11090

111-
const quoteBuilder = QuoteBuilderFactory.getBuilder(
112-
this.poolParams.curveType
113-
);
91+
const quoteBuilder = QuoteBuilderFactory.getBuilder(this.poolParams.curveType);
11492

11593
const quote = quoteBuilder?.buildQuote(
11694
quoteParams,

src/model/pools.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ export const ethUsdcPool: OrcaPoolParams = Object.freeze({
2424
address: new PublicKey("DY8qBwVGLeLJSrWib7L16mL7oB4HNAQ2f9yiYWKof54v"),
2525
nonce: 255,
2626
authority: new PublicKey("82oSibpDKnPZ2Yk1vn6McjCsQQbKfBkGeEh5FsqeVrtU"),
27-
poolTokenAddress: new PublicKey(
28-
"7TYb32qkwYosUQfUspU45cou7Bb3nefJocVMFX2mEGTT"
29-
),
27+
poolTokenAddress: new PublicKey("7TYb32qkwYosUQfUspU45cou7Bb3nefJocVMFX2mEGTT"),
3028
poolTokenMint: new PublicKey("7TYb32qkwYosUQfUspU45cou7Bb3nefJocVMFX2mEGTT"),
3129
poolTokenDecimals: 9,
3230
feeAccount: new PublicKey("AcMaBVt6S43JQXKnEDqdicxYofb5Cj1UgFWF9AsurTp6"),

src/model/quote/constant-product-quote.ts

+15-56
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,26 @@ import { QuotePoolParams } from "./quote-builder";
1515
*/
1616

1717
function getRate(inputTradeAmountU64: u64, params: QuotePoolParams): Decimal {
18-
const expectedOutputAmountU64 = getExpectedOutputAmount(
19-
inputTradeAmountU64,
20-
params
21-
);
22-
const inputTradeAmount = DecimalUtil.fromU64(
23-
inputTradeAmountU64,
24-
params.inputToken.decimals
25-
);
18+
const expectedOutputAmountU64 = getExpectedOutputAmount(inputTradeAmountU64, params);
19+
const inputTradeAmount = DecimalUtil.fromU64(inputTradeAmountU64, params.inputToken.decimals);
2620
const outputTradeAmount = DecimalUtil.fromU64(
2721
expectedOutputAmountU64,
2822
params.outputToken.decimals
2923
);
3024
return outputTradeAmount.div(inputTradeAmount);
3125
}
3226

33-
function getPriceImpact(
34-
inputTradeAmount: u64,
35-
params: QuotePoolParams
36-
): Decimal {
37-
const noSlippageOutputCountU64 = getExpectedOutputAmountWithNoSlippage(
38-
inputTradeAmount,
39-
params
40-
);
27+
function getPriceImpact(inputTradeAmount: u64, params: QuotePoolParams): Decimal {
28+
const noSlippageOutputCountU64 = getExpectedOutputAmountWithNoSlippage(inputTradeAmount, params);
4129
const outputCountU64 = getExpectedOutputAmount(inputTradeAmount, params);
4230

4331
const noSlippageOutputCount = DecimalUtil.fromU64(
4432
noSlippageOutputCountU64,
4533
params.outputToken.decimals
4634
);
47-
const outputCount = DecimalUtil.fromU64(
48-
outputCountU64,
49-
params.outputToken.decimals
50-
);
35+
const outputCount = DecimalUtil.fromU64(outputCountU64, params.outputToken.decimals);
5136

52-
const impact = noSlippageOutputCount
53-
.sub(outputCount)
54-
.div(noSlippageOutputCount);
37+
const impact = noSlippageOutputCount.sub(outputCount).div(noSlippageOutputCount);
5538
return impact.mul(100);
5639
}
5740

@@ -68,37 +51,22 @@ function getFees(inputTradeAmount: u64, params: QuotePoolParams): u64 {
6851
return new u64(tradingFee.add(ownerFee).toString());
6952
}
7053

71-
function getExpectedOutputAmount(
72-
inputTradeAmount: u64,
73-
params: QuotePoolParams
74-
): u64 {
75-
const inputTradeLessFees = inputTradeAmount.sub(
76-
getFees(inputTradeAmount, params)
77-
);
54+
function getExpectedOutputAmount(inputTradeAmount: u64, params: QuotePoolParams): u64 {
55+
const inputTradeLessFees = inputTradeAmount.sub(getFees(inputTradeAmount, params));
7856
return getOutputAmount(inputTradeLessFees, params);
7957
}
8058

8159
function getExpectedOutputAmountWithNoSlippage(
8260
inputTradeAmount: u64,
8361
params: QuotePoolParams
8462
): u64 {
85-
const inputTradeLessFees = inputTradeAmount.sub(
86-
getFees(inputTradeAmount, params)
87-
);
88-
return inputTradeLessFees
89-
.mul(params.outputTokenCount)
90-
.div(params.inputTokenCount);
63+
const inputTradeLessFees = inputTradeAmount.sub(getFees(inputTradeAmount, params));
64+
return inputTradeLessFees.mul(params.outputTokenCount).div(params.inputTokenCount);
9165
}
9266

93-
function getMinimumAmountOut(
94-
inputTradeAmount: u64,
95-
params: QuotePoolParams
96-
): u64 {
67+
function getMinimumAmountOut(inputTradeAmount: u64, params: QuotePoolParams): u64 {
9768
const slippageTolerance = params.slippageTolerance;
98-
const expectedOutputAmountFees = getExpectedOutputAmount(
99-
inputTradeAmount,
100-
params
101-
);
69+
const expectedOutputAmountFees = getExpectedOutputAmount(inputTradeAmount, params);
10270
const result = expectedOutputAmountFees
10371
.mul(slippageTolerance.denominator.sub(slippageTolerance.numerator))
10472
.div(slippageTolerance.denominator);
@@ -109,10 +77,7 @@ function getMinimumAmountOut(
10977
// Given k = currInputTokenCount * currOutputTokenCount and k = newInputTokenCount * newOutputTokenCount,
11078
// solve for newOutputTokenCount
11179
function getOutputAmount(inputTradeAmount: u64, params: QuotePoolParams): u64 {
112-
const [poolInputAmount, poolOutputAmount] = [
113-
params.inputTokenCount,
114-
params.outputTokenCount,
115-
];
80+
const [poolInputAmount, poolOutputAmount] = [params.inputTokenCount, params.outputTokenCount];
11681

11782
const invariant = poolInputAmount.mul(poolOutputAmount);
11883

@@ -127,18 +92,12 @@ function getOutputAmount(inputTradeAmount: u64, params: QuotePoolParams): u64 {
12792
}
12893

12994
export class ConstantProductPoolQuoteBuilder {
130-
async buildQuote(
131-
params: QuotePoolParams,
132-
inputTradeAmount: u64
133-
): Promise<Quote> {
95+
async buildQuote(params: QuotePoolParams, inputTradeAmount: u64): Promise<Quote> {
13496
return {
13597
getRate: () => getRate(inputTradeAmount, params),
13698
getPriceImpact: () => getPriceImpact(inputTradeAmount, params),
13799
getFees: () =>
138-
U64Utils.toOrcaU64(
139-
getFees(inputTradeAmount, params),
140-
params.inputToken.decimals
141-
),
100+
U64Utils.toOrcaU64(getFees(inputTradeAmount, params), params.inputToken.decimals),
142101
getExpectedOutputAmount: () =>
143102
U64Utils.toOrcaU64(
144103
getExpectedOutputAmount(inputTradeAmount, params),

src/model/utils/percentage.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ export class PercentageUtils {
99
return PercentageUtils.fromFraction(number * 10, 1000);
1010
}
1111

12-
public static fromFraction(
13-
numerator: u64 | number,
14-
denominator: u64 | number
15-
): Percentage {
12+
public static fromFraction(numerator: u64 | number, denominator: u64 | number): Percentage {
1613
const num = typeof numerator === "number" ? new u64(numerator) : numerator;
17-
const denom =
18-
typeof denominator === "number" ? new u64(denominator) : denominator;
14+
const denom = typeof denominator === "number" ? new u64(denominator) : denominator;
1915
return { numerator: num, denominator: denom };
2016
}
2117
}

src/public/types.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export type OrcaPool = {
4747
* @param slippage The slippage in percentage you are willing to take in this trade
4848
* @return Returns a quote on the exchanged token based on the input token amount
4949
*/
50-
getQuote: (
51-
inputTokenId: string,
52-
inputAmount: Decimal,
53-
slippage: number
54-
) => Promise<Quote>;
50+
getQuote: (inputTokenId: string, inputAmount: Decimal, slippage: number) => Promise<Quote>;
5551
};
5652

5753
export type Quote = {

src/utils/web3/deserialize-account.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { AccountInfo, AccountLayout, u64 } from "@solana/spl-token";
22
import { PublicKey } from "@solana/web3.js";
33

4-
export const deserializeAccount = (
5-
data: Buffer | undefined
6-
): AccountInfo | undefined => {
4+
export const deserializeAccount = (data: Buffer | undefined): AccountInfo | undefined => {
75
if (data == undefined || data.length == 0) {
86
return undefined;
97
}

src/utils/web3/find-associated-token-address.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ export async function findAssociatedTokenAddress(
88
): Promise<PublicKey> {
99
return (
1010
await PublicKey.findProgramAddress(
11-
[
12-
walletAddress.toBuffer(),
13-
TOKEN_PROGRAM_ID.toBuffer(),
14-
tokenMint.toBuffer(),
15-
],
11+
[walletAddress.toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), tokenMint.toBuffer()],
1612
SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID
1713
)
1814
)[0];

src/utils/web3/get-token-count.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ export function getTokens(
2121

2222
const forward = tokenAId == inputTokenId;
2323

24-
const inputOrcaToken = forward
25-
? poolParams.tokens[tokenAId]
26-
: poolParams.tokens[tokenBId];
27-
const outputOrcaToken = forward
28-
? poolParams.tokens[tokenBId]
29-
: poolParams.tokens[tokenAId];
24+
const inputOrcaToken = forward ? poolParams.tokens[tokenAId] : poolParams.tokens[tokenBId];
25+
const outputOrcaToken = forward ? poolParams.tokens[tokenBId] : poolParams.tokens[tokenAId];
3026
return [inputOrcaToken, outputOrcaToken];
3127
}
3228

0 commit comments

Comments
 (0)