@alephium/powfi-sdk 0.0.1-rc.1

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.
Files changed (146) hide show
  1. package/README.md +4 -0
  2. package/clmm/artifacts/BitmapWord.ral.json +125 -0
  3. package/clmm/artifacts/BitmapWordDeployer.ral.json +31 -0
  4. package/clmm/artifacts/CreateConfig.ral.json +37 -0
  5. package/clmm/artifacts/CreateLiquidPool.ral.json +55 -0
  6. package/clmm/artifacts/DexAccount.ral.json +110 -0
  7. package/clmm/artifacts/LiquidityAmountsTest.ral.json +161 -0
  8. package/clmm/artifacts/LiquidityManagmentTest.ral.json +384 -0
  9. package/clmm/artifacts/Pool.ral.json +1530 -0
  10. package/clmm/artifacts/PoolConfig.ral.json +31 -0
  11. package/clmm/artifacts/PoolFactory.ral.json +300 -0
  12. package/clmm/artifacts/PoolRouterDemo.ral.json +49 -0
  13. package/clmm/artifacts/PoolUser.ral.json +89 -0
  14. package/clmm/artifacts/Position.ral.json +183 -0
  15. package/clmm/artifacts/PositionManager.ral.json +416 -0
  16. package/clmm/artifacts/SwapWithoutAccount.ral.json +46 -0
  17. package/clmm/artifacts/TestToken.ral.json +68 -0
  18. package/clmm/artifacts/Tick.ral.json +161 -0
  19. package/clmm/artifacts/TickBitmapTest.ral.json +220 -0
  20. package/clmm/artifacts/constants.ral.json +81 -0
  21. package/clmm/artifacts/structs.ral.json +335 -0
  22. package/clmm/artifacts/ts/BitmapWord.ts +337 -0
  23. package/clmm/artifacts/ts/BitmapWordDeployer.ts +164 -0
  24. package/clmm/artifacts/ts/DexAccount.ts +330 -0
  25. package/clmm/artifacts/ts/LiquidityAmountsTest.ts +464 -0
  26. package/clmm/artifacts/ts/LiquidityManagmentTest.ts +859 -0
  27. package/clmm/artifacts/ts/Pool.ts +2535 -0
  28. package/clmm/artifacts/ts/PoolConfig.ts +179 -0
  29. package/clmm/artifacts/ts/PoolFactory.ts +640 -0
  30. package/clmm/artifacts/ts/PoolUser.ts +237 -0
  31. package/clmm/artifacts/ts/Position.ts +440 -0
  32. package/clmm/artifacts/ts/PositionManager.ts +929 -0
  33. package/clmm/artifacts/ts/TestToken.ts +277 -0
  34. package/clmm/artifacts/ts/Tick.ts +351 -0
  35. package/clmm/artifacts/ts/TickBitmapTest.ts +512 -0
  36. package/clmm/artifacts/ts/constants.ts +17 -0
  37. package/clmm/artifacts/ts/contracts.ts +26 -0
  38. package/clmm/artifacts/ts/deployments.ts +160 -0
  39. package/clmm/artifacts/ts/index.ts +20 -0
  40. package/clmm/artifacts/ts/scripts.ts +76 -0
  41. package/clmm/artifacts/ts/types.ts +105 -0
  42. package/clmm/deployments/.deployments.devnet.json +350 -0
  43. package/clmm/deployments/.deployments.testnet.json +350 -0
  44. package/cpmm/artifacts/dex/DexAccount.ral.json +110 -0
  45. package/cpmm/artifacts/dex/Router.ral.json +361 -0
  46. package/cpmm/artifacts/dex/TokenPair.ral.json +512 -0
  47. package/cpmm/artifacts/dex/TokenPairFactory.ral.json +297 -0
  48. package/cpmm/artifacts/examples/ExampleOracleSimple.ral.json +192 -0
  49. package/cpmm/artifacts/examples/FeeCollectorFactoryImpl.ral.json +185 -0
  50. package/cpmm/artifacts/examples/FeeCollectorPerTokenPairImpl.ral.json +216 -0
  51. package/cpmm/artifacts/examples/FullMathTest.ral.json +123 -0
  52. package/cpmm/artifacts/scripts/AddLiquidity.ral.json +46 -0
  53. package/cpmm/artifacts/scripts/Burn.ral.json +31 -0
  54. package/cpmm/artifacts/scripts/CollectFee.ral.json +25 -0
  55. package/cpmm/artifacts/scripts/CreatePair.ral.json +37 -0
  56. package/cpmm/artifacts/scripts/CreatePairAndAddLiquidity.ral.json +43 -0
  57. package/cpmm/artifacts/scripts/EnableFeeCollector.ral.json +28 -0
  58. package/cpmm/artifacts/scripts/Mint.ral.json +34 -0
  59. package/cpmm/artifacts/scripts/RemoveLiquidity.ral.json +43 -0
  60. package/cpmm/artifacts/scripts/SetFeeCollectorFactory.ral.json +28 -0
  61. package/cpmm/artifacts/scripts/Swap.ral.json +46 -0
  62. package/cpmm/artifacts/scripts/SwapMaxIn.ral.json +46 -0
  63. package/cpmm/artifacts/scripts/SwapMinOut.ral.json +46 -0
  64. package/cpmm/artifacts/test/GetToken.ral.json +31 -0
  65. package/cpmm/artifacts/test/MathTest.ral.json +49 -0
  66. package/cpmm/artifacts/test/TestToken.ral.json +87 -0
  67. package/cpmm/artifacts/ts/DexAccount.ts +329 -0
  68. package/cpmm/artifacts/ts/ExampleOracleSimple.ts +383 -0
  69. package/cpmm/artifacts/ts/FeeCollectorFactoryImpl.ts +227 -0
  70. package/cpmm/artifacts/ts/FeeCollectorPerTokenPairImpl.ts +327 -0
  71. package/cpmm/artifacts/ts/FullMathTest.ts +251 -0
  72. package/cpmm/artifacts/ts/MathTest.ts +183 -0
  73. package/cpmm/artifacts/ts/Router.ts +554 -0
  74. package/cpmm/artifacts/ts/TestToken.ts +312 -0
  75. package/cpmm/artifacts/ts/TokenPair.ts +947 -0
  76. package/cpmm/artifacts/ts/TokenPairFactory.ts +501 -0
  77. package/cpmm/artifacts/ts/contracts.ts +26 -0
  78. package/cpmm/artifacts/ts/deployments.ts +109 -0
  79. package/cpmm/artifacts/ts/index.ts +16 -0
  80. package/cpmm/artifacts/ts/scripts.ts +142 -0
  81. package/cpmm/deployments/.deployments.devnet.json +77 -0
  82. package/cpmm/deployments/.deployments.testnet.json +79 -0
  83. package/lib/index.d.mts +8800 -0
  84. package/lib/index.d.ts +8800 -0
  85. package/lib/index.js +21769 -0
  86. package/lib/index.js.map +1 -0
  87. package/lib/index.mjs +22118 -0
  88. package/lib/index.mjs.map +1 -0
  89. package/package.json +80 -0
  90. package/src/clmm/clmm.ts +607 -0
  91. package/src/clmm/constants.ts +7 -0
  92. package/src/clmm/index.ts +6 -0
  93. package/src/clmm/liquidity.ts +163 -0
  94. package/src/clmm/pool.ts +154 -0
  95. package/src/clmm/tick.ts +335 -0
  96. package/src/clmm/types.ts +155 -0
  97. package/src/common/constants.ts +1 -0
  98. package/src/common/error.ts +46 -0
  99. package/src/common/index.ts +7 -0
  100. package/src/common/logger.ts +82 -0
  101. package/src/common/math.ts +88 -0
  102. package/src/common/numeric.ts +64 -0
  103. package/src/common/types.ts +49 -0
  104. package/src/common/utils.ts +3 -0
  105. package/src/cpmm/constants.ts +2 -0
  106. package/src/cpmm/cpmm.ts +631 -0
  107. package/src/cpmm/index.ts +3 -0
  108. package/src/cpmm/types.ts +113 -0
  109. package/src/index.ts +25 -0
  110. package/src/moduleBase.ts +64 -0
  111. package/src/staking/index.ts +4 -0
  112. package/src/staking/settings.ts +38 -0
  113. package/src/staking/staking.ts +277 -0
  114. package/src/staking/types.ts +15 -0
  115. package/src/staking/utils.ts +25 -0
  116. package/src/token/index.ts +1 -0
  117. package/src/token/token.ts +163 -0
  118. package/src/zeta.ts +105 -0
  119. package/staking/artifacts/AlphStakeAndLock.ral.json +31 -0
  120. package/staking/artifacts/AlphUnstakeVault.ral.json +151 -0
  121. package/staking/artifacts/XAlphStakeVault.ral.json +559 -0
  122. package/staking/artifacts/XAlphToken.ral.json +404 -0
  123. package/staking/artifacts/XAlphUnlockAndStartUnstake.ral.json +31 -0
  124. package/staking/artifacts/examples/GovernanceDemo.ral.json +282 -0
  125. package/staking/artifacts/examples/RewardSharingVault.ral.json +253 -0
  126. package/staking/artifacts/structs.ral.json +47 -0
  127. package/staking/artifacts/ts/AlphUnstakeVault.ts +354 -0
  128. package/staking/artifacts/ts/FullMathTest.ts +175 -0
  129. package/staking/artifacts/ts/GovernanceDemo.ts +726 -0
  130. package/staking/artifacts/ts/RewardSharingVault.ts +559 -0
  131. package/staking/artifacts/ts/TestDynamicArrayByteVec32.ts +431 -0
  132. package/staking/artifacts/ts/TestDynamicSortedArrayForU256.ts +516 -0
  133. package/staking/artifacts/ts/TestMerkleProof.ts +343 -0
  134. package/staking/artifacts/ts/XAlphStakeVault.ts +1120 -0
  135. package/staking/artifacts/ts/XAlphToken.ts +835 -0
  136. package/staking/artifacts/ts/contracts.ts +26 -0
  137. package/staking/artifacts/ts/deployments.ts +109 -0
  138. package/staking/artifacts/ts/index.ts +15 -0
  139. package/staking/artifacts/ts/scripts.ts +35 -0
  140. package/staking/artifacts/ts/types.ts +19 -0
  141. package/staking/artifacts/utils/FullMathTest.ral.json +57 -0
  142. package/staking/artifacts/utils/TestDynamicArrayByteVec32.ral.json +165 -0
  143. package/staking/artifacts/utils/TestDynamicSortedArrayForU256.ral.json +189 -0
  144. package/staking/artifacts/utils/TestMerkleProof.ral.json +134 -0
  145. package/staking/deployments/.deployments.devnet.json +77 -0
  146. package/staking/deployments/.deployments.testnet.json +78 -0
@@ -0,0 +1,155 @@
1
+ import type { TokenInfo } from '@alephium/token-list';
2
+ import { Pool } from 'clmm/artifacts/ts/Pool';
3
+ import { PositionInfo } from 'clmm/artifacts/ts/types';
4
+
5
+ export interface ClmmConfig {
6
+ groupIndex: number;
7
+ factoryId: string;
8
+ positionManagerId: string;
9
+ defaultConfigIndex: bigint;
10
+ accountRoot: string;
11
+ }
12
+
13
+ export interface ClmmSwapParams {
14
+ token0: string;
15
+ token1: string;
16
+ amount: bigint;
17
+ slippage: bigint;
18
+ routePlan: bigint[];
19
+ }
20
+
21
+ export interface SimulateSwap {
22
+ configIndex: bigint;
23
+ token0: string;
24
+ token1: string;
25
+ zeroForOne: boolean;
26
+ amount: bigint;
27
+ }
28
+
29
+ export interface LiquidityForPrice {
30
+ liquidity: bigint;
31
+ sqrtPriceX96: bigint;
32
+ }
33
+
34
+ export interface LiquidityDistribution {
35
+ baseSqrtPriceX96: bigint;
36
+ sqrtPriceX96: bigint;
37
+ liquidity: bigint;
38
+ fee: bigint;
39
+ rows: Array<LiquidityForPrice>;
40
+ }
41
+
42
+ export interface AddLiquidity {
43
+ token0: string;
44
+ token1: string;
45
+ configIndex: bigint;
46
+ owner?: string;
47
+ tickLower: bigint;
48
+ tickUpper: bigint;
49
+ slippage: bigint;
50
+ amount0: bigint;
51
+ amount1: bigint;
52
+ existingPosition?: boolean;
53
+ }
54
+
55
+ export interface RemoveLiquidity {
56
+ token0: string;
57
+ token1: string;
58
+ configIndex: bigint;
59
+ owner: string;
60
+ tickLower: bigint;
61
+ tickUpper: bigint;
62
+ liquidity: bigint;
63
+ base: 'token0' | 'token1';
64
+ baseAmount: bigint;
65
+ otherAmountMax: bigint;
66
+ }
67
+
68
+ export interface CollectTokens {
69
+ token0: string;
70
+ token1: string;
71
+ configIndex: bigint;
72
+ owner: string;
73
+ recipient: string;
74
+ tickLower: bigint;
75
+ tickUpper: bigint;
76
+ liquidity: bigint; // != 0 if also remove liquidity
77
+ amount0Max: bigint;
78
+ amount1Max: bigint;
79
+ }
80
+
81
+ export interface PositionPath {
82
+ poolId: string;
83
+ owner: string;
84
+ tickLower: bigint;
85
+ tickUpper: bigint;
86
+ acc: bigint;
87
+ iacc0: bigint;
88
+ iacc1: bigint;
89
+ t0: bigint;
90
+ acct0: bigint;
91
+ }
92
+
93
+ export interface CollectProtocolFees {
94
+ token0: string;
95
+ token1: string;
96
+ configIndex: bigint;
97
+ recipient: string;
98
+ }
99
+
100
+ export interface SetRewardParams {
101
+ token0: string;
102
+ token1: string;
103
+ configIndex: bigint;
104
+ rewardToken: string;
105
+ payer: string;
106
+ amount: bigint;
107
+ openTime: bigint;
108
+ endTime: bigint;
109
+ }
110
+
111
+ export interface ExtendRewards {
112
+ token0: string;
113
+ token1: string;
114
+ configIndex: bigint;
115
+ rewardToken: string;
116
+ payer: string;
117
+ amount: bigint;
118
+ }
119
+
120
+ export interface ClmmPoolContractState extends ClmmPoolConfig {
121
+ poolId: string;
122
+ token0Info: TokenInfo;
123
+ token1Info: TokenInfo;
124
+ liquidity: bigint;
125
+ sqrtPriceX96: bigint;
126
+ tick: bigint;
127
+ }
128
+
129
+ export interface ClmmPoolConfig {
130
+ configIndex: bigint;
131
+ tickSpacing: bigint;
132
+ tradingFee: bigint;
133
+ protocolFee: bigint;
134
+ }
135
+
136
+ export const MAX_PIPS = Pool.consts.MAX_PIPS;
137
+
138
+
139
+ export type ClmmPositionInfo = PositionInfo
140
+
141
+ export interface GetPositionAmountsFromPriceProps {
142
+ sqrtRatioX96: bigint;
143
+ tokenBaseId: string;
144
+ tokenQuoteId: string;
145
+ lowerTick: bigint;
146
+ upperTick: bigint;
147
+ amountBase: bigint;
148
+ amountQuote: bigint;
149
+ }
150
+
151
+ export interface GetPositionAmountsFromPriceReturn {
152
+ newAmountBase: bigint;
153
+ newAmountQuote: bigint;
154
+ liquidity: bigint;
155
+ }
@@ -0,0 +1 @@
1
+ export const BPS = 10_000n; // basis points denominator
@@ -0,0 +1,46 @@
1
+ export class ZetaSDKError extends Error {
2
+ constructor(message: string, options?: ErrorOptions) {
3
+ super(message, options);
4
+ this.name = 'ZetaSDKError';
5
+ }
6
+ }
7
+
8
+ export class InsufficientLiquidityError extends ZetaSDKError {
9
+ constructor(message: string = 'Insufficient liquidity') {
10
+ super(message);
11
+ this.name = 'InsufficientLiquidityError';
12
+ }
13
+ }
14
+
15
+ export class PriceImpactTooHighError extends ZetaSDKError {
16
+ constructor(priceImpact: number, maxPriceImpact: number) {
17
+ super(`Price impact too high: ${priceImpact.toFixed(2)}% > ${maxPriceImpact}%`);
18
+ this.name = 'PriceImpactTooHighError';
19
+ }
20
+ }
21
+
22
+ export class PoolNotFoundError extends ZetaSDKError {
23
+ constructor(poolId: string) {
24
+ super(`Pool does not exist for ${poolId}`);
25
+ this.name = 'PoolNotFoundError';
26
+ }
27
+ }
28
+
29
+ export class InsufficientBalanceError extends ZetaSDKError {
30
+ constructor(token: string, required: string, available: string) {
31
+ super(`Not enough ${token} balance. Required: ${required}, Available: ${available}`);
32
+ this.name = 'InsufficientBalanceError';
33
+ }
34
+ }
35
+
36
+ export class TokenListFetchError extends ZetaSDKError {
37
+ public readonly status?: number;
38
+
39
+ constructor(url: string, options?: { status?: number; cause?: unknown }) {
40
+ const statusPart = options?.status !== undefined ? ` (status ${options.status})` : '';
41
+ const errorOptions = options?.cause !== undefined ? { cause: options.cause } : undefined;
42
+ super(`Failed to fetch token list from ${url}${statusPart}`, errorOptions);
43
+ this.name = 'TokenListFetchError';
44
+ this.status = options?.status;
45
+ }
46
+ }
@@ -0,0 +1,7 @@
1
+ export * from './types';
2
+ export * from './constants';
3
+ export * from './utils';
4
+ export * from './error';
5
+ export * from './logger';
6
+ export * from './math';
7
+ export * from './numeric';
@@ -0,0 +1,82 @@
1
+ export enum LogLevel {
2
+ Error,
3
+ Warning,
4
+ Info,
5
+ Debug,
6
+ }
7
+
8
+ export class Logger {
9
+ private logLevel: LogLevel;
10
+ private name: string;
11
+
12
+ constructor(params: { name: string; logLevel?: LogLevel }) {
13
+ this.logLevel = params.logLevel !== undefined ? params.logLevel : LogLevel.Error;
14
+ this.name = params.name;
15
+ }
16
+
17
+ set level(logLevel: LogLevel) {
18
+ this.logLevel = logLevel;
19
+ }
20
+
21
+ get time(): string {
22
+ return new Date().toISOString();
23
+ }
24
+
25
+ get moduleName(): string {
26
+ return this.name;
27
+ }
28
+
29
+ private isLogLevel(level: LogLevel): boolean {
30
+ return level <= this.logLevel;
31
+ }
32
+
33
+ public error(...props: unknown[]): Logger {
34
+ if (!this.isLogLevel(LogLevel.Error)) return this;
35
+ console.error(this.time, this.name, 'error', ...props);
36
+ return this;
37
+ }
38
+
39
+ public logWithError(...props: unknown[]): Logger {
40
+ const msg = props
41
+ .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : arg))
42
+ .join(', ');
43
+ throw new Error(msg);
44
+ }
45
+
46
+ public warning(...props: unknown[]): Logger {
47
+ if (!this.isLogLevel(LogLevel.Warning)) return this;
48
+ console.warn(this.time, this.name, 'warning', ...props);
49
+ return this;
50
+ }
51
+
52
+ public info(...props: unknown[]): Logger {
53
+ if (!this.isLogLevel(LogLevel.Info)) return this;
54
+ console.info(this.time, this.name, 'info', ...props);
55
+ return this;
56
+ }
57
+
58
+ public debug(...props: unknown[]): Logger {
59
+ if (!this.isLogLevel(LogLevel.Debug)) return this;
60
+ console.debug(this.time, this.name, 'debug', ...props);
61
+ return this;
62
+ }
63
+ }
64
+
65
+ const moduleLoggers: { [key: string]: Logger } = {};
66
+ const moduleLevels: { [key: string]: LogLevel } = {};
67
+
68
+ export function createLogger(moduleName: string): Logger {
69
+ let logger = moduleLoggers[moduleName];
70
+ if (!logger) {
71
+ const logLevel = moduleLevels[moduleName];
72
+ logger = new Logger({ name: moduleName, logLevel });
73
+ moduleLoggers[moduleName] = logger;
74
+ }
75
+ return logger;
76
+ }
77
+
78
+ export function setLoggerLevel(moduleName: string, level: LogLevel): void {
79
+ moduleLevels[moduleName] = level;
80
+ const logger = moduleLoggers[moduleName];
81
+ if (logger) logger.level = level;
82
+ }
@@ -0,0 +1,88 @@
1
+ export type Rounding = 'floor' | 'ceil';
2
+
3
+ export class MathUtil {
4
+ // Copied from https://github.com/Aisse-258/bigint-isqrt/blob/master/main.js
5
+ static sqrt = function (value: bigint): bigint {
6
+ if (value < 2n) {
7
+ return value;
8
+ }
9
+
10
+ if (value < 16n) {
11
+ return BigInt(Math.sqrt(Number(value)) | 0);
12
+ }
13
+
14
+ let x0, x1;
15
+ if (value < 4503599627370496n) {
16
+ //1n<<52n
17
+ x1 = BigInt(Math.sqrt(Number(value)) | 0) - 3n;
18
+ } else {
19
+ const vlen = value.toString().length;
20
+ if (!(vlen & 1)) {
21
+ x1 = 10n ** BigInt(vlen / 2);
22
+ } else {
23
+ x1 = 4n * 10n ** BigInt((vlen / 2) | 0);
24
+ }
25
+ }
26
+
27
+ do {
28
+ x0 = x1;
29
+ x1 = (value / x0 + x0) >> 1n;
30
+ } while (x0 !== x1 && x0 !== x1 - 1n);
31
+
32
+ return x0;
33
+ };
34
+
35
+ static alphDiv(a: bigint, b: bigint): bigint {
36
+ const result = a / b;
37
+ if (a * b < 0n) return result - BigInt(a % b != 0n);
38
+ return result;
39
+ }
40
+
41
+ static alphCeil(a: bigint, b: bigint): bigint {
42
+ const result = a / b;
43
+ const rem = a % b;
44
+ return result + BigInt(rem != 0n) * BigInt(rem < 0n ? -1n : 1n);
45
+ }
46
+
47
+ static divFloor(a: bigint, b: bigint): bigint {
48
+ return this.divWithRounding(a, b, 'floor');
49
+ }
50
+
51
+ static divCeil(a: bigint, b: bigint): bigint {
52
+ return this.divWithRounding(a, b, 'ceil');
53
+ }
54
+
55
+ static mulDivFloor(a: bigint, b: bigint, d: bigint): bigint {
56
+ if (d === 0n) {
57
+ throw new Error(`Division by zero in mulDivFloor: ${a} * ${b} / ${d}`);
58
+ }
59
+ const product = a * b;
60
+ return this.divFloor(product, d);
61
+ }
62
+
63
+ static mulDivCeil(a: bigint, b: bigint, d: bigint): bigint {
64
+ if (d === 0n) {
65
+ throw new Error(`Division by zero in mulDivCeil: ${a} * ${b} / ${d}`);
66
+ }
67
+ const product = a * b;
68
+ return this.divCeil(product, d);
69
+ }
70
+
71
+ private static divWithRounding = (a: bigint, b: bigint, mode: Rounding): bigint => {
72
+ if (b === 0n) {
73
+ throw new Error(`Division by zero: ${a} / ${b}`);
74
+ }
75
+
76
+ const quotient = a / b;
77
+ const remainder = a % b;
78
+ if (remainder === 0n) {
79
+ return quotient;
80
+ }
81
+
82
+ const signsMatch = a >= 0n === b >= 0n;
83
+ if (mode === 'floor') {
84
+ return signsMatch ? quotient : quotient - 1n;
85
+ }
86
+ return signsMatch ? quotient + 1n : quotient;
87
+ };
88
+ }
@@ -0,0 +1,64 @@
1
+ import Decimal from 'decimal.js';
2
+
3
+ export type NumericLike = Decimal | bigint | number | string | undefined | null;
4
+
5
+ export class NumericUtils {
6
+ static decimalFrom(value: NumericLike): Decimal {
7
+ if (value === undefined || value === null) {
8
+ return new Decimal(0);
9
+ }
10
+ if (Decimal.isDecimal(value)) {
11
+ return value;
12
+ }
13
+ if (typeof value === 'bigint') {
14
+ return new Decimal(value.toString());
15
+ }
16
+ return new Decimal(value);
17
+ }
18
+
19
+ static decimalToString(value: Decimal): string {
20
+ if (!value.isFinite()) {
21
+ return value.toString();
22
+ }
23
+ return value.toFixed(value.decimalPlaces());
24
+ }
25
+
26
+ static scaleDecimal(value: NumericLike, decimals: number): Decimal {
27
+ if (decimals < 0) {
28
+ throw new Error('Decimals must be non-negative');
29
+ }
30
+ if (decimals === 0) {
31
+ return this.decimalFrom(value);
32
+ }
33
+ return this.decimalFrom(value).div(new Decimal(10).pow(decimals));
34
+ }
35
+
36
+ static scaleToString(value: NumericLike, decimals: number): string {
37
+ return this.decimalToString(this.scaleDecimal(value, decimals));
38
+ }
39
+
40
+ static numericToString(value: NumericLike): string {
41
+ return this.decimalToString(this.decimalFrom(value));
42
+ }
43
+
44
+ static numericToBigInt(value: NumericLike): bigint {
45
+ if (value === undefined || value === null) {
46
+ return 0n;
47
+ }
48
+ if (typeof value === 'bigint') {
49
+ return value;
50
+ }
51
+
52
+ const decimalValue = this.decimalFrom(value);
53
+
54
+ if (!decimalValue.isFinite()) {
55
+ throw new Error('Cannot convert non-finite value to bigint');
56
+ }
57
+
58
+ if (!decimalValue.isInteger()) {
59
+ throw new Error('Cannot convert non-integer value to bigint');
60
+ }
61
+
62
+ return BigInt(decimalValue.toFixed(0));
63
+ }
64
+ }
@@ -0,0 +1,49 @@
1
+ import type { SignerProvider, NetworkId } from '@alephium/web3';
2
+
3
+ export interface Network {
4
+ id: NetworkId;
5
+ name: string;
6
+ nodeUrl: string;
7
+ nodeApiKey?: string;
8
+ explorerApiUrl: string;
9
+ explorerUrl: string;
10
+ tokenListUrl: string;
11
+ }
12
+
13
+ export const DEVNET = {
14
+ id: 'devnet' as NetworkId,
15
+ nodeUrl: 'http://127.0.0.1:22973',
16
+ explorerApiUrl: 'http://localhost:9090',
17
+ explorerUrl: 'http://localhost:23000',
18
+ tokenListUrl: 'http://127.0.0.1:4000/token-list/devnet.json', // Temporary solution for devnet
19
+ name: 'Devnet',
20
+ };
21
+
22
+ export const defaultNetworks: Network[] = [
23
+ {
24
+ id: 'mainnet',
25
+ nodeUrl: 'https://node.mainnet.alephium.org',
26
+ explorerApiUrl: 'https://backend.mainnet.alephium.org',
27
+ explorerUrl: 'https://explorer.alephium.org',
28
+ tokenListUrl:
29
+ 'https://raw.githubusercontent.com/alephium/token-list/master/tokens/mainnet.json',
30
+ name: 'Mainnet',
31
+ },
32
+ {
33
+ id: 'testnet',
34
+ nodeUrl: 'https://node.testnet.alephium.org',
35
+ explorerApiUrl: 'https://backend.testnet.alephium.org',
36
+ explorerUrl: 'https://testnet.alephium.org',
37
+ tokenListUrl:
38
+ 'https://raw.githubusercontent.com/alephium/token-list/master/tokens/testnet.json',
39
+ name: 'Testnet',
40
+ },
41
+ DEVNET,
42
+ ];
43
+ export type NetworkOverrides = Partial<Omit<Network, 'id'>>;
44
+
45
+ export interface ZetaLoadParams {
46
+ networkId: NetworkId;
47
+ signer?: SignerProvider;
48
+ networkOverrides?: NetworkOverrides;
49
+ }
@@ -0,0 +1,3 @@
1
+ export function sortTokens(tokenAId: string, tokenBId: string): [string, string] {
2
+ return tokenAId < tokenBId ? [tokenAId, tokenBId] : [tokenBId, tokenAId];
3
+ }
@@ -0,0 +1,2 @@
1
+ export const MAX_PRICE_IMPACT = 5; // 5%
2
+ export const MINIMUM_LIQUIDITY = 1000n;