Skip to content

Commit

Permalink
Move common-sdk into monorepo (#523)
Browse files Browse the repository at this point in the history
* Move common-sdk into monorepo

* Tweak

* Tweaks

* Build fix

* Tweak

* update tx-builder to handle fixed

* Lint

---------

Co-authored-by: zach225 <[email protected]>
  • Loading branch information
wjthieme and zach225 authored Jan 3, 2025
1 parent aa05b79 commit b446521
Show file tree
Hide file tree
Showing 43 changed files with 4,171 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Install dependencies
run: yarn install
- name: Run tests
run: yarn test --exclude legacy-sdk/whirlpool --output-style static
run: yarn test --exclude legacy-sdk/integration --output-style static

lint:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cluster = "localnet"
wallet = "~/.config/solana/id.json"

[scripts]
test = "yarn vitest run --test-timeout 1000000 --no-file-parallelism --globals legacy-sdk/whirlpool/tests"
test = "yarn vitest run --test-timeout 1000000 --no-file-parallelism --globals legacy-sdk"

[test.validator]
slots_per_epoch = "33"
Expand Down
3 changes: 1 addition & 2 deletions legacy-sdk/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
},
"dependencies": {
"@coral-xyz/anchor": "0.29.0",
"@orca-so/common-sdk": "0.6.4",
"@orca-so/orca-sdk": "0.2.0",
"@orca-so/common-sdk": "*",
"@orca-so/whirlpools-sdk": "*",
"@solana/spl-token": "0.4.1",
"@solana/web3.js": "^1.90.0",
Expand Down
2 changes: 2 additions & 0 deletions legacy-sdk/common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Orca Common SDK
This package contains a set of utility functions used by other Typescript components in Orca.
33 changes: 33 additions & 0 deletions legacy-sdk/common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@orca-so/common-sdk",
"version": "0.6.5",
"description": "Common Typescript components across Orca",
"repository": "https://github.com/orca-so/orca-sdks",
"author": "Orca Foundation",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"peerDependencies": {
"@solana/spl-token": "^0.4.1",
"@solana/web3.js": "^1.90.0",
"decimal.js": "^10.4.3"
},
"dependencies": {
"tiny-invariant": "^1.3.1"
},
"devDependencies": {
"@solana/spl-token": "^0.4.1",
"@solana/web3.js": "^1.90.0",
"decimal.js": "^10.4.3",
"typescript": "^5.6.3"
},
"scripts": {
"build": "tsc",
"clean": "rimraf dist",
"prepublishOnly": "cd ../.. && yarn build legacy-sdk/common"
},
"files": [
"/dist",
"README.md"
]
}
2 changes: 2 additions & 0 deletions legacy-sdk/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./math";
export * from "./web3";
28 changes: 28 additions & 0 deletions legacy-sdk/common/src/math/decimal-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BN from "bn.js";
import Decimal from "decimal.js";

export class DecimalUtil {
public static adjustDecimals(input: Decimal, shift = 0): Decimal {
return input.div(Decimal.pow(10, shift));
}

public static fromBN(input: BN, shift = 0): Decimal {
return new Decimal(input.toString()).div(new Decimal(10).pow(shift));
}

public static fromNumber(input: number, shift = 0): Decimal {
return new Decimal(input).div(new Decimal(10).pow(shift));
}

public static toBN(input: Decimal, shift = 0): BN {
if (input.isNeg()) {
throw new Error(
"Negative decimal value ${input} cannot be converted to BN.",
);
}

const shiftedValue = input.mul(new Decimal(10).pow(shift));
const zeroDecimalValue = shiftedValue.trunc();
return new BN(zeroDecimalValue.toString());
}
}
3 changes: 3 additions & 0 deletions legacy-sdk/common/src/math/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./decimal-util";
export * from "./math-util";
export * from "./percentage";
79 changes: 79 additions & 0 deletions legacy-sdk/common/src/math/math-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import BN from "bn.js";
import Decimal from "decimal.js";

/**
* @category Math
*/
export const ZERO = new BN(0);

/**
* @category Math
*/
export const ONE = new BN(1);

/**
* @category Math
*/
export const TWO = new BN(2);

/**
* @category Math
*/
export const U128 = TWO.pow(new BN(128));

/**
* @category Math
*/
export const U64_MAX = TWO.pow(new BN(64)).sub(ONE);

/**
* @category Math
*/
export class MathUtil {
public static toX64_BN(num: BN): BN {
return num.mul(new BN(2).pow(new BN(64)));
}

public static toX64_Decimal(num: Decimal): Decimal {
return num.mul(Decimal.pow(2, 64));
}

public static toX64(num: Decimal): BN {
return new BN(num.mul(Decimal.pow(2, 64)).floor().toFixed());
}

public static fromX64(num: BN): Decimal {
return new Decimal(num.toString()).mul(Decimal.pow(2, -64));
}

public static fromX64_Decimal(num: Decimal): Decimal {
return num.mul(Decimal.pow(2, -64));
}

public static fromX64_BN(num: BN): BN {
return num.div(new BN(2).pow(new BN(64)));
}

public static shiftRightRoundUp(n: BN): BN {
let result = n.shrn(64);

if (n.mod(U64_MAX).gt(ZERO)) {
result = result.add(ONE);
}

return result;
}

public static divRoundUp(n0: BN, n1: BN): BN {
const hasRemainder = !n0.mod(n1).eq(ZERO);
if (hasRemainder) {
return n0.div(n1).add(new BN(1));
} else {
return n0.div(n1);
}
}

public static subUnderflowU128(n0: BN, n1: BN): BN {
return n0.add(U128).sub(n1).mod(U128);
}
}
63 changes: 63 additions & 0 deletions legacy-sdk/common/src/math/percentage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import BN from "bn.js";
import Decimal from "decimal.js";

/**
* @category Math
*/
export class Percentage {
readonly numerator: BN;
readonly denominator: BN;

constructor(numerator: BN, denominator: BN) {
this.numerator = numerator;
this.denominator = denominator;
}

public static fromDecimal(number: Decimal): Percentage {
return Percentage.fromFraction(number.mul(100000).toNumber(), 10000000);
}

public static fromFraction(
numerator: BN | number,
denominator: BN | number,
): Percentage {
const num =
typeof numerator === "number" ? new BN(numerator.toString()) : numerator;
const denom =
typeof denominator === "number"
? new BN(denominator.toString())
: denominator;
return new Percentage(num, denom);
}

public toString = (): string => {
return `${this.numerator.toString()}/${this.denominator.toString()}`;
};

public toDecimal() {
if (this.denominator.eq(new BN(0))) {
return new Decimal(0);
}
return new Decimal(this.numerator.toString()).div(
new Decimal(this.denominator.toString()),
);
}

public add(p2: Percentage): Percentage {
const denomGcd = this.denominator.gcd(p2.denominator);
const denomLcm = this.denominator.div(denomGcd).mul(p2.denominator);

const p1DenomAdjustment = denomLcm.div(this.denominator);
const p2DenomAdjustment = denomLcm.div(p2.denominator);

const p1NumeratorAdjusted = this.numerator.mul(p1DenomAdjustment);
const p2NumeratorAdjusted = p2.numerator.mul(p2DenomAdjustment);

const newNumerator = p1NumeratorAdjusted.add(p2NumeratorAdjusted);

return new Percentage(
new BN(newNumerator.toString()),
new BN(denomLcm.toString()),
);
}
}
43 changes: 43 additions & 0 deletions legacy-sdk/common/src/web3/address-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { PublicKey } from "@solana/web3.js";

export declare type Address = PublicKey | string;

/**
* @category Util
*/
export type PDA = { publicKey: PublicKey; bump: number };

/**
* @category Util
*/
export class AddressUtil {
public static toPubKey(address: Address): PublicKey {
return address instanceof PublicKey ? address : new PublicKey(address);
}

public static toPubKeys(addresses: Address[]): PublicKey[] {
return addresses.map((address) => AddressUtil.toPubKey(address));
}

public static toString(address: Address): string {
if (typeof address === "string") {
return address;
}
return AddressUtil.toPubKey(address).toBase58();
}

public static toStrings(addresses: Address[]): string[] {
return addresses.map((address) => AddressUtil.toString(address));
}

public static findProgramAddress(
seeds: (Uint8Array | Buffer)[],
programId: PublicKey,
): PDA {
const [publicKey, bump] = PublicKey.findProgramAddressSync(
seeds,
programId,
);
return { publicKey, bump };
}
}
Loading

0 comments on commit b446521

Please sign in to comment.