@picon-finance/dlmm-sdk 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/utils/price.d.ts +4 -0
- package/dist/utils/price.d.ts.map +1 -0
- package/dist/utils/price.js +27 -0
- package/dist/utils/u64x64.d.ts +6 -0
- package/dist/utils/u64x64.d.ts.map +1 -0
- package/dist/utils/u64x64.js +25 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/utils/price.ts +32 -0
- package/src/utils/u64x64.ts +28 -0
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { DistributionMode } from "./generated/types/distributionMode";
|
|
|
3
3
|
export * as events from "./events";
|
|
4
4
|
export * from "./entities";
|
|
5
5
|
export * from "./utils/constants";
|
|
6
|
+
export * from "./utils/price";
|
|
6
7
|
export { NATIVE_MINT_ADDRESS } from "./utils/ata";
|
|
7
8
|
export type { QuoteOutput, TransferFee, FeeRates } from "./quote";
|
|
8
9
|
export * from "./program_addresses";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAClE,cAAc,qBAAqB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAClE,cAAc,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,5 +3,6 @@ export { DistributionMode } from "./generated/types/distributionMode";
|
|
|
3
3
|
export * as events from "./events";
|
|
4
4
|
export * from "./entities";
|
|
5
5
|
export * from "./utils/constants";
|
|
6
|
+
export * from "./utils/price";
|
|
6
7
|
export { NATIVE_MINT_ADDRESS } from "./utils/ata";
|
|
7
8
|
export * from "./program_addresses";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function getBaseBinPrice(binStep: number): bigint;
|
|
2
|
+
export declare function getBinPrice(baseBinPrice: bigint, binId: number): bigint | undefined;
|
|
3
|
+
export declare function getBinPriceFromStep(binStep: number, binId: number): bigint | undefined;
|
|
4
|
+
//# sourceMappingURL=price.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"price.d.ts","sourceRoot":"","sources":["../../src/utils/price.ts"],"names":[],"mappings":"AAGA,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGvD;AAED,wBAAgB,WAAW,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAmBnF;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEtF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MAX_BASIS_POINTS } from "./constants";
|
|
2
|
+
import { expBySquaring, MAX, MAX_EXPONENTIAL, ONE, SCALE_OFFSET } from "./u64x64";
|
|
3
|
+
export function getBaseBinPrice(binStep) {
|
|
4
|
+
const priceStep = (BigInt(binStep) << SCALE_OFFSET) / BigInt(MAX_BASIS_POINTS);
|
|
5
|
+
return ONE + priceStep;
|
|
6
|
+
}
|
|
7
|
+
export function getBinPrice(baseBinPrice, binId) {
|
|
8
|
+
if (baseBinPrice < 0n || baseBinPrice > MAX) {
|
|
9
|
+
throw new Error(`getBinPrice: baseBinPrice out of u128 range (${baseBinPrice})`);
|
|
10
|
+
}
|
|
11
|
+
if (baseBinPrice <= ONE)
|
|
12
|
+
return;
|
|
13
|
+
if (binId === 0)
|
|
14
|
+
return ONE;
|
|
15
|
+
const invert = binId > 0;
|
|
16
|
+
const absBinId = Math.abs(binId);
|
|
17
|
+
if (absBinId >= MAX_EXPONENTIAL)
|
|
18
|
+
return;
|
|
19
|
+
const invertedBase = MAX / baseBinPrice;
|
|
20
|
+
const price = expBySquaring(invertedBase, absBinId);
|
|
21
|
+
if (price === 0n)
|
|
22
|
+
return;
|
|
23
|
+
return invert ? MAX / price : price;
|
|
24
|
+
}
|
|
25
|
+
export function getBinPriceFromStep(binStep, binId) {
|
|
26
|
+
return getBinPrice(getBaseBinPrice(binStep), binId);
|
|
27
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const SCALE_OFFSET = 64n;
|
|
2
|
+
export declare const ONE: bigint;
|
|
3
|
+
export declare const MAX: bigint;
|
|
4
|
+
export declare const MAX_EXPONENTIAL = 524288;
|
|
5
|
+
export declare function expBySquaring(base: bigint, exp: number): bigint;
|
|
6
|
+
//# sourceMappingURL=u64x64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"u64x64.d.ts","sourceRoot":"","sources":["../../src/utils/u64x64.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,MAAM,CAAC;AAChC,eAAO,MAAM,GAAG,QAAqB,CAAC;AACtC,eAAO,MAAM,GAAG,QAAoB,CAAC;AACrC,eAAO,MAAM,eAAe,SAAU,CAAC;AAQvC,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAgB/D"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export const SCALE_OFFSET = 64n;
|
|
2
|
+
export const ONE = 1n << SCALE_OFFSET;
|
|
3
|
+
export const MAX = (1n << 128n) - 1n; // u128::MAX
|
|
4
|
+
export const MAX_EXPONENTIAL = 0x80000; // 2^19
|
|
5
|
+
function mulU128(a, b) {
|
|
6
|
+
const product = a * b;
|
|
7
|
+
if (product > MAX)
|
|
8
|
+
throw new Error("u64x64: u128 multiplication overflow");
|
|
9
|
+
return product;
|
|
10
|
+
}
|
|
11
|
+
export function expBySquaring(base, exp) {
|
|
12
|
+
let result = ONE;
|
|
13
|
+
let remainingBits = 32 - Math.clz32(exp);
|
|
14
|
+
for (let bit = 0x1; bit <= 0x40000; bit <<= 1) {
|
|
15
|
+
if (exp & bit) {
|
|
16
|
+
result = mulU128(result, base) >> SCALE_OFFSET;
|
|
17
|
+
}
|
|
18
|
+
remainingBits -= 1;
|
|
19
|
+
if (remainingBits === 0) {
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
base = mulU128(base, base) >> SCALE_OFFSET;
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`u64x64.expBySquaring: exp out of range (${exp})`);
|
|
25
|
+
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { DistributionMode } from "./generated/types/distributionMode";
|
|
|
3
3
|
export * as events from "./events";
|
|
4
4
|
export * from "./entities";
|
|
5
5
|
export * from "./utils/constants";
|
|
6
|
+
export * from "./utils/price";
|
|
6
7
|
export { NATIVE_MINT_ADDRESS } from "./utils/ata";
|
|
7
8
|
export type { QuoteOutput, TransferFee, FeeRates } from "./quote";
|
|
8
9
|
export * from "./program_addresses";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { MAX_BASIS_POINTS } from "./constants";
|
|
2
|
+
import { expBySquaring, MAX, MAX_EXPONENTIAL, ONE, SCALE_OFFSET } from "./u64x64";
|
|
3
|
+
|
|
4
|
+
export function getBaseBinPrice(binStep: number): bigint {
|
|
5
|
+
const priceStep = (BigInt(binStep) << SCALE_OFFSET) / BigInt(MAX_BASIS_POINTS);
|
|
6
|
+
return ONE + priceStep;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getBinPrice(baseBinPrice: bigint, binId: number): bigint | undefined {
|
|
10
|
+
if (baseBinPrice < 0n || baseBinPrice > MAX) {
|
|
11
|
+
throw new Error(`getBinPrice: baseBinPrice out of u128 range (${baseBinPrice})`);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (baseBinPrice <= ONE) return;
|
|
15
|
+
if (binId === 0) return ONE;
|
|
16
|
+
|
|
17
|
+
const invert = binId > 0;
|
|
18
|
+
const absBinId = Math.abs(binId);
|
|
19
|
+
|
|
20
|
+
if (absBinId >= MAX_EXPONENTIAL) return;
|
|
21
|
+
|
|
22
|
+
const invertedBase = MAX / baseBinPrice;
|
|
23
|
+
const price = expBySquaring(invertedBase, absBinId);
|
|
24
|
+
|
|
25
|
+
if (price === 0n) return;
|
|
26
|
+
|
|
27
|
+
return invert ? MAX / price : price;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getBinPriceFromStep(binStep: number, binId: number): bigint | undefined {
|
|
31
|
+
return getBinPrice(getBaseBinPrice(binStep), binId);
|
|
32
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const SCALE_OFFSET = 64n;
|
|
2
|
+
export const ONE = 1n << SCALE_OFFSET;
|
|
3
|
+
export const MAX = (1n << 128n) - 1n; // u128::MAX
|
|
4
|
+
export const MAX_EXPONENTIAL = 0x80000; // 2^19
|
|
5
|
+
|
|
6
|
+
function mulU128(a: bigint, b: bigint): bigint {
|
|
7
|
+
const product = a * b;
|
|
8
|
+
if (product > MAX) throw new Error("u64x64: u128 multiplication overflow");
|
|
9
|
+
return product;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function expBySquaring(base: bigint, exp: number): bigint {
|
|
13
|
+
let result = ONE;
|
|
14
|
+
let remainingBits = 32 - Math.clz32(exp);
|
|
15
|
+
|
|
16
|
+
for (let bit = 0x1; bit <= 0x40000; bit <<= 1) {
|
|
17
|
+
if (exp & bit) {
|
|
18
|
+
result = mulU128(result, base) >> SCALE_OFFSET;
|
|
19
|
+
}
|
|
20
|
+
remainingBits -= 1;
|
|
21
|
+
if (remainingBits === 0) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
base = mulU128(base, base) >> SCALE_OFFSET;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
throw new Error(`u64x64.expBySquaring: exp out of range (${exp})`);
|
|
28
|
+
}
|