-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move common-sdk into monorepo (#523)
* 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
Showing
43 changed files
with
4,171 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./math"; | ||
export * from "./web3"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
Oops, something went wrong.