@moon-cx/sdk 0.0.0-stage → 2.0.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 (155) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +2 -2
  3. package/dist/adapters/sender.d.ts +88 -0
  4. package/dist/adapters/sender.d.ts.map +1 -0
  5. package/dist/adapters/sender.js +62 -0
  6. package/dist/adapters/sender.js.map +1 -0
  7. package/dist/adapters/tonclient.d.ts +13 -0
  8. package/dist/adapters/tonclient.d.ts.map +1 -0
  9. package/dist/adapters/tonclient.js +17 -0
  10. package/dist/adapters/tonclient.js.map +1 -0
  11. package/dist/api/index.d.ts +210 -0
  12. package/dist/api/index.d.ts.map +1 -0
  13. package/dist/api/index.js +364 -0
  14. package/dist/api/index.js.map +1 -0
  15. package/dist/codec/bins.d.ts +26 -0
  16. package/dist/codec/bins.d.ts.map +1 -0
  17. package/dist/codec/bins.js +67 -0
  18. package/dist/codec/bins.js.map +1 -0
  19. package/dist/codec/clmm.d.ts +87 -0
  20. package/dist/codec/clmm.d.ts.map +1 -0
  21. package/dist/codec/clmm.js +119 -0
  22. package/dist/codec/clmm.js.map +1 -0
  23. package/dist/codec/cpmm.d.ts +112 -0
  24. package/dist/codec/cpmm.d.ts.map +1 -0
  25. package/dist/codec/cpmm.js +146 -0
  26. package/dist/codec/cpmm.js.map +1 -0
  27. package/dist/codec/jetton.d.ts +30 -0
  28. package/dist/codec/jetton.d.ts.map +1 -0
  29. package/dist/codec/jetton.js +37 -0
  30. package/dist/codec/jetton.js.map +1 -0
  31. package/dist/codec/mempad.d.ts +109 -0
  32. package/dist/codec/mempad.d.ts.map +1 -0
  33. package/dist/codec/mempad.js +162 -0
  34. package/dist/codec/mempad.js.map +1 -0
  35. package/dist/core/amount.d.ts +33 -0
  36. package/dist/core/amount.d.ts.map +1 -0
  37. package/dist/core/amount.js +81 -0
  38. package/dist/core/amount.js.map +1 -0
  39. package/dist/core/asset.d.ts +82 -0
  40. package/dist/core/asset.d.ts.map +1 -0
  41. package/dist/core/asset.js +143 -0
  42. package/dist/core/asset.js.map +1 -0
  43. package/dist/core/errors.d.ts +70 -0
  44. package/dist/core/errors.d.ts.map +1 -0
  45. package/dist/core/errors.js +114 -0
  46. package/dist/core/errors.js.map +1 -0
  47. package/dist/core/fee.d.ts +36 -0
  48. package/dist/core/fee.d.ts.map +1 -0
  49. package/dist/core/fee.js +40 -0
  50. package/dist/core/fee.js.map +1 -0
  51. package/dist/core/gas.d.ts +112 -0
  52. package/dist/core/gas.d.ts.map +1 -0
  53. package/dist/core/gas.js +265 -0
  54. package/dist/core/gas.js.map +1 -0
  55. package/dist/core/percent.d.ts +27 -0
  56. package/dist/core/percent.d.ts.map +1 -0
  57. package/dist/core/percent.js +71 -0
  58. package/dist/core/percent.js.map +1 -0
  59. package/dist/core/tx.d.ts +36 -0
  60. package/dist/core/tx.d.ts.map +1 -0
  61. package/dist/core/tx.js +17 -0
  62. package/dist/core/tx.js.map +1 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.d.ts.map +1 -0
  65. package/dist/index.js +41 -0
  66. package/dist/index.js.map +1 -0
  67. package/dist/jetton/index.d.ts +53 -0
  68. package/dist/jetton/index.d.ts.map +1 -0
  69. package/dist/jetton/index.js +90 -0
  70. package/dist/jetton/index.js.map +1 -0
  71. package/dist/math/clmm-swap.d.ts +80 -0
  72. package/dist/math/clmm-swap.d.ts.map +1 -0
  73. package/dist/math/clmm-swap.js +277 -0
  74. package/dist/math/clmm-swap.js.map +1 -0
  75. package/dist/math/clmm.d.ts +234 -0
  76. package/dist/math/clmm.d.ts.map +1 -0
  77. package/dist/math/clmm.js +270 -0
  78. package/dist/math/clmm.js.map +1 -0
  79. package/dist/math/cpmm.d.ts +170 -0
  80. package/dist/math/cpmm.d.ts.map +1 -0
  81. package/dist/math/cpmm.js +175 -0
  82. package/dist/math/cpmm.js.map +1 -0
  83. package/dist/math/integer.d.ts +10 -0
  84. package/dist/math/integer.d.ts.map +1 -0
  85. package/dist/math/integer.js +44 -0
  86. package/dist/math/integer.js.map +1 -0
  87. package/dist/math/q128.d.ts +45 -0
  88. package/dist/math/q128.d.ts.map +1 -0
  89. package/dist/math/q128.js +108 -0
  90. package/dist/math/q128.js.map +1 -0
  91. package/dist/moon.d.ts +245 -0
  92. package/dist/moon.d.ts.map +1 -0
  93. package/dist/moon.js +332 -0
  94. package/dist/moon.js.map +1 -0
  95. package/dist/ops/jetton-input.d.ts +3 -0
  96. package/dist/ops/jetton-input.d.ts.map +1 -0
  97. package/dist/ops/jetton-input.js +8 -0
  98. package/dist/ops/jetton-input.js.map +1 -0
  99. package/dist/ops/liquidity.d.ts +157 -0
  100. package/dist/ops/liquidity.d.ts.map +1 -0
  101. package/dist/ops/liquidity.js +413 -0
  102. package/dist/ops/liquidity.js.map +1 -0
  103. package/dist/ops/mempad.d.ts +143 -0
  104. package/dist/ops/mempad.d.ts.map +1 -0
  105. package/dist/ops/mempad.js +248 -0
  106. package/dist/ops/mempad.js.map +1 -0
  107. package/dist/ops/pool.d.ts +75 -0
  108. package/dist/ops/pool.d.ts.map +1 -0
  109. package/dist/ops/pool.js +129 -0
  110. package/dist/ops/pool.js.map +1 -0
  111. package/dist/ops/position.d.ts +47 -0
  112. package/dist/ops/position.d.ts.map +1 -0
  113. package/dist/ops/position.js +73 -0
  114. package/dist/ops/position.js.map +1 -0
  115. package/dist/ops/swap.d.ts +104 -0
  116. package/dist/ops/swap.d.ts.map +1 -0
  117. package/dist/ops/swap.js +248 -0
  118. package/dist/ops/swap.js.map +1 -0
  119. package/dist/pool/amounts.d.ts +36 -0
  120. package/dist/pool/amounts.d.ts.map +1 -0
  121. package/dist/pool/amounts.js +68 -0
  122. package/dist/pool/amounts.js.map +1 -0
  123. package/dist/pool/assets.d.ts +45 -0
  124. package/dist/pool/assets.d.ts.map +1 -0
  125. package/dist/pool/assets.js +57 -0
  126. package/dist/pool/assets.js.map +1 -0
  127. package/dist/pool/clmm.d.ts +309 -0
  128. package/dist/pool/clmm.d.ts.map +1 -0
  129. package/dist/pool/clmm.js +561 -0
  130. package/dist/pool/clmm.js.map +1 -0
  131. package/dist/pool/cpmm.d.ts +228 -0
  132. package/dist/pool/cpmm.d.ts.map +1 -0
  133. package/dist/pool/cpmm.js +332 -0
  134. package/dist/pool/cpmm.js.map +1 -0
  135. package/dist/protocol/generated.d.ts +2752 -0
  136. package/dist/protocol/generated.d.ts.map +1 -0
  137. package/dist/protocol/generated.js +3564 -0
  138. package/dist/protocol/generated.js.map +1 -0
  139. package/dist/protocol/index.d.ts +19 -0
  140. package/dist/protocol/index.d.ts.map +1 -0
  141. package/dist/protocol/index.js +49 -0
  142. package/dist/protocol/index.js.map +1 -0
  143. package/dist/state/address.d.ts +121 -0
  144. package/dist/state/address.d.ts.map +1 -0
  145. package/dist/state/address.js +143 -0
  146. package/dist/state/address.js.map +1 -0
  147. package/dist/state/source.d.ts +26 -0
  148. package/dist/state/source.d.ts.map +1 -0
  149. package/dist/state/source.js +32 -0
  150. package/dist/state/source.js.map +1 -0
  151. package/dist/state/storage.d.ts +218 -0
  152. package/dist/state/storage.d.ts.map +1 -0
  153. package/dist/state/storage.js +273 -0
  154. package/dist/state/storage.js.map +1 -0
  155. package/package.json +74 -4
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Base class for every SDK error. Subclasses carry the numbers involved as
3
+ * fields, so callers can react without parsing the message.
4
+ */
5
+ export class MoonError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = new.target.name;
9
+ }
10
+ }
11
+ /** The built transaction would be rejected or would lose funds. Nothing was sent. */
12
+ export class BuildError extends MoonError {
13
+ }
14
+ /** The requested pool does not exist, or its address could not be derived. */
15
+ export class PoolNotFoundError extends MoonError {
16
+ assets;
17
+ constructor(assetA, assetB) {
18
+ super(`no pool for ${assetA} / ${assetB}`);
19
+ this.assets = [assetA, assetB];
20
+ }
21
+ }
22
+ /** The pool exists but cannot trade yet — usually no liquidity, or wallets not resolved. */
23
+ export class PoolNotReadyError extends MoonError {
24
+ address;
25
+ reason;
26
+ constructor(address, reason) {
27
+ super(`pool ${address.toString()} is not ready: ${reason}`);
28
+ this.address = address;
29
+ this.reason = reason;
30
+ }
31
+ }
32
+ /**
33
+ * The attached TON would not clear an on-chain gas gate. Thrown before
34
+ * building: on a jetton leg the receiving pool or range throws after the
35
+ * jettons are credited.
36
+ */
37
+ export class InsufficientGasError extends BuildError {
38
+ need;
39
+ have;
40
+ gate;
41
+ constructor(need, have, gate) {
42
+ super(`this operation needs at least ${need} nanotons attached, got ${have} ` +
43
+ `(on-chain check: ${gate})`);
44
+ this.need = need;
45
+ this.have = have;
46
+ this.gate = gate;
47
+ }
48
+ }
49
+ /** The amount is below a minimum enforced on chain. */
50
+ export class AmountTooSmallError extends BuildError {
51
+ minimum;
52
+ got;
53
+ constructor(what, minimum, got, gate) {
54
+ super(`${what} must be at least ${minimum}, got ${got} (on-chain check: ${gate})`);
55
+ this.minimum = minimum;
56
+ this.got = got;
57
+ }
58
+ }
59
+ /** An on-chain limit would be exceeded: bins per position, bins per swap, range window. */
60
+ export class LimitExceededError extends BuildError {
61
+ limit;
62
+ got;
63
+ constructor(what, limit, got, hint) {
64
+ super(`${what}: the limit is ${limit}, this request needs ${got}${hint ? `. ${hint}` : ''}`);
65
+ this.limit = limit;
66
+ this.got = got;
67
+ }
68
+ }
69
+ /** The quote moved further than the tolerance allows. */
70
+ export class SlippageError extends MoonError {
71
+ expected;
72
+ got;
73
+ constructor(expected, got) {
74
+ super(`price moved: expected at least ${expected}, would get ${got}`);
75
+ this.expected = expected;
76
+ this.got = got;
77
+ }
78
+ }
79
+ /** On-chain state could not be read or did not parse as the expected pool, range, position or wallet. */
80
+ export class StateError extends MoonError {
81
+ address;
82
+ constructor(address, message) {
83
+ super(`${address.toString()}: ${message}`);
84
+ this.address = address;
85
+ }
86
+ }
87
+ /** A sent message was rejected on chain. */
88
+ export class ContractRejectedError extends MoonError {
89
+ exitCode;
90
+ queryId;
91
+ constructor(exitCode, description, queryId) {
92
+ super(`the message was rejected on chain with exit code ${exitCode}: ${description}`);
93
+ this.exitCode = exitCode;
94
+ this.queryId = queryId;
95
+ }
96
+ }
97
+ /**
98
+ * Plain-language descriptions of the protocol's exit codes (identical in CPMM
99
+ * and CLMM; pinned by the protocol drift test).
100
+ */
101
+ export const EXIT_CODES = {
102
+ 299: 'the pool or position is in a state that does not allow this — a locked position, a frozen one, or a pool with no liquidity',
103
+ 300: 'the sender is not allowed to do this; positions only accept messages from their owner',
104
+ 490: 'ran out of gas while executing',
105
+ 500: 'not enough TON attached to cover the operation',
106
+ 510: 'the amount is below the minimum the receiver accepts',
107
+ 511: 'the swap would return nothing at this size',
108
+ 700: 'the message body did not match what the receiver expected',
109
+ };
110
+ /** Turns an exit code into a sentence, falling back to the bare number. */
111
+ export function describeExitCode(exitCode) {
112
+ return EXIT_CODES[exitCode] ?? `unrecognised exit code ${exitCode}`;
113
+ }
114
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/core/errors.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAClC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAA;IAC7B,CAAC;CACF;AAED,qFAAqF;AACrF,MAAM,OAAO,UAAW,SAAQ,SAAS;CAAG;AAE5C,8EAA8E;AAC9E,MAAM,OAAO,iBAAkB,SAAQ,SAAS;IACrC,MAAM,CAAkB;IAEjC,YAAY,MAAc,EAAE,MAAc;QACxC,KAAK,CAAC,eAAe,MAAM,MAAM,MAAM,EAAE,CAAC,CAAA;QAC1C,IAAI,CAAC,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChC,CAAC;CACF;AAED,4FAA4F;AAC5F,MAAM,OAA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,EAAE,2DAA2D;CACjE,CAAA;AAED,2EAA2E;AAC3E,MAAM,UAAU,gBAAgB,CAAC,QAAgB;IAC/C,OAAO,UAAU,CAAC,QAAQ,CAAC,IAAI,0BAA0B,QAAQ,EAAE,CAAA;AACrE,CAAC"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Pool fee tiers in basis points, shared by the CPMM factory and mempad.
3
+ *
4
+ * The CPMM factory and mempad each enforce the list on their own; it is shared
5
+ * here because a launch's `feeBps` becomes the fee of the CPMM pool it
6
+ * graduates into.
7
+ *
8
+ * Members are named by value so a new tier never renames or reorders others.
9
+ * A const object, not an `enum`, because the package builds with
10
+ * `erasableSyntaxOnly`.
11
+ */
12
+ /** The six tiers. The digits are the value: `Bps30` is `30`. */
13
+ export declare const FeeTier: {
14
+ /** 0.10% — lowest fee. */
15
+ readonly Bps10: 10;
16
+ /** 0.20% — correlated pairs. */
17
+ readonly Bps20: 20;
18
+ /** 0.30% — standard, the usual choice. */
19
+ readonly Bps30: 30;
20
+ /** 0.50% — volatile pairs. */
21
+ readonly Bps50: 50;
22
+ /** 1.00% — exotic pairs. */
23
+ readonly Bps100: 100;
24
+ /** 2.00% — ultra-exotic pairs. */
25
+ readonly Bps200: 200;
26
+ };
27
+ /** Every accepted tier, in the order the factories list them. */
28
+ export declare const FEE_BPS: readonly [10, 20, 30, 50, 100, 200];
29
+ /**
30
+ * A fee tier both factories accept; anything else is `INVALID_PAYLOAD` (700).
31
+ * `feeBps: 30` and `FeeTier.Bps30` are the same value.
32
+ */
33
+ export type FeeBps = (typeof FEE_BPS)[number];
34
+ /** Narrows an arbitrary number to a tier, for values arriving from outside. */
35
+ export declare function isFeeBps(value: number): value is FeeBps;
36
+ //# sourceMappingURL=fee.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fee.d.ts","sourceRoot":"","sources":["../../src/core/fee.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,gEAAgE;AAChE,eAAO,MAAM,OAAO;IAClB,0BAA0B;;IAE1B,gCAAgC;;IAEhC,0CAA0C;;IAE1C,8BAA8B;;IAE9B,4BAA4B;;IAE5B,kCAAkC;;CAE1B,CAAA;AAEV,iEAAiE;AACjE,eAAO,MAAM,OAAO,qCAOV,CAAA;AAEV;;;GAGG;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,OAAO,CAAC,CAAC,MAAM,CAAC,CAAA;AAE7C,+EAA+E;AAC/E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,MAAM,CAEvD"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Pool fee tiers in basis points, shared by the CPMM factory and mempad.
3
+ *
4
+ * The CPMM factory and mempad each enforce the list on their own; it is shared
5
+ * here because a launch's `feeBps` becomes the fee of the CPMM pool it
6
+ * graduates into.
7
+ *
8
+ * Members are named by value so a new tier never renames or reorders others.
9
+ * A const object, not an `enum`, because the package builds with
10
+ * `erasableSyntaxOnly`.
11
+ */
12
+ /** The six tiers. The digits are the value: `Bps30` is `30`. */
13
+ export const FeeTier = {
14
+ /** 0.10% — lowest fee. */
15
+ Bps10: 10,
16
+ /** 0.20% — correlated pairs. */
17
+ Bps20: 20,
18
+ /** 0.30% — standard, the usual choice. */
19
+ Bps30: 30,
20
+ /** 0.50% — volatile pairs. */
21
+ Bps50: 50,
22
+ /** 1.00% — exotic pairs. */
23
+ Bps100: 100,
24
+ /** 2.00% — ultra-exotic pairs. */
25
+ Bps200: 200,
26
+ };
27
+ /** Every accepted tier, in the order the factories list them. */
28
+ export const FEE_BPS = [
29
+ FeeTier.Bps10,
30
+ FeeTier.Bps20,
31
+ FeeTier.Bps30,
32
+ FeeTier.Bps50,
33
+ FeeTier.Bps100,
34
+ FeeTier.Bps200,
35
+ ];
36
+ /** Narrows an arbitrary number to a tier, for values arriving from outside. */
37
+ export function isFeeBps(value) {
38
+ return FEE_BPS.includes(value);
39
+ }
40
+ //# sourceMappingURL=fee.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fee.js","sourceRoot":"","sources":["../../src/core/fee.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,gEAAgE;AAChE,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,0BAA0B;IAC1B,KAAK,EAAE,EAAE;IACT,gCAAgC;IAChC,KAAK,EAAE,EAAE;IACT,0CAA0C;IAC1C,KAAK,EAAE,EAAE;IACT,8BAA8B;IAC9B,KAAK,EAAE,EAAE;IACT,4BAA4B;IAC5B,MAAM,EAAE,GAAG;IACX,kCAAkC;IAClC,MAAM,EAAE,GAAG;CACH,CAAA;AAEV,iEAAiE;AACjE,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;CACN,CAAA;AAQV,+EAA+E;AAC/E,MAAM,UAAU,QAAQ,CAAC,KAAa;IACpC,OAAQ,OAA6B,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AACvD,CAAC"}
@@ -0,0 +1,112 @@
1
+ /**
2
+ * TON each operation must attach, in nanotons.
3
+ *
4
+ * Too little gas on a jetton leg is not a bounce: the pool throws after the
5
+ * jettons are credited, and they are lost.
6
+ */
7
+ /**
8
+ * Gas pricing from config parameter 21.
9
+ *
10
+ * `gasPrice` is nanotons per 65536 gas units, not per unit. The first
11
+ * `flatGasLimit` units cost a flat `flatGasPrice`; units above it cost
12
+ * `gasPrice / 65536` each, rounded up in total.
13
+ */
14
+ export type GasPrices = {
15
+ /** Raw config value: nanotons per 65536 gas units. */
16
+ gasPrice: bigint;
17
+ flatGasLimit: bigint;
18
+ flatGasPrice: bigint;
19
+ };
20
+ /**
21
+ * Snapshot of mainnet basechain config 21 (66.667 nanotons per gas unit). The SDK
22
+ * does not read config 21 at runtime: if the network price changes, pass `prices`.
23
+ */
24
+ export declare const MAINNET_GAS_PRICES: GasPrices;
25
+ /** Previous mainnet basechain config 21 (400 nanotons per gas unit). */
26
+ export declare const LEGACY_GAS_PRICES: GasPrices;
27
+ export declare const DEFAULT_GAS_PRICES: GasPrices;
28
+ /**
29
+ * TON cost of a gas budget, exactly as the node computes it:
30
+ * `flat_gas_price + ceil(gas_price × (used − flat_limit) / 65536)`.
31
+ */
32
+ export declare function gasFee(gasUnits: bigint, prices?: GasPrices): bigint;
33
+ /** A gas requirement and the on-chain check it mirrors. */
34
+ export type GasRequirement = {
35
+ /** Minimum the receiver accepts. */
36
+ minimum: bigint;
37
+ /** What the SDK attaches: usually the minimum plus headroom. */
38
+ attach: bigint;
39
+ /** The check, in words and amounts. */
40
+ gate: string;
41
+ };
42
+ /**
43
+ * 0.02 TON added on top of most gates, so a message built on a threshold still
44
+ * clears it after storage fees or gas prices drift.
45
+ */
46
+ export declare const GAS_HEADROOM = 20000000n;
47
+ /**
48
+ * Native CPMM swap. What is left after gas must be at least 0.05 TON and at
49
+ * least the swap amount, so the floor is the larger of the two, not their sum.
50
+ */
51
+ export declare function cpmmSwapTonGas(swapAmount: bigint): GasRequirement;
52
+ /**
53
+ * Jetton CPMM swap: the forwarded TON amount, not the transfer's own value.
54
+ * Must clear both the gas gate and the 0.06 TON notification gate.
55
+ */
56
+ export declare function cpmmSwapJettonForward(): GasRequirement;
57
+ /** Minimum native deposit the pool accepts, before gas. */
58
+ export declare const CPMM_MIN_TON_DEPOSIT = 1000000000n;
59
+ /**
60
+ * Native CPMM deposit: the deposit itself plus gas on top.
61
+ *
62
+ * Exact, with no headroom: the pool deposits everything above the gas but mints
63
+ * LP for the smaller side, so a surplus enters the reserves without LP. Attaching
64
+ * less shrinks the deposit, and at the 1 TON minimum fails with LOW_AMOUNT (510).
65
+ */
66
+ export declare function cpmmDepositTonGas(depositAmount: bigint): GasRequirement;
67
+ /**
68
+ * CPMM jetton deposit leg: what the notification must carry. Falling short does
69
+ * not throw; the pool refunds the jettons minus gas.
70
+ */
71
+ export declare function cpmmDepositJettonForward(): GasRequirement;
72
+ export declare function cpmmWithdrawGas(): GasRequirement;
73
+ /** CPMM fee collection: 0.01 TON above the withdrawal gate. */
74
+ export declare function cpmmCollectFeesGas(): GasRequirement;
75
+ export declare function cpmmLockGas(): GasRequirement;
76
+ /** CLMM swap: a full gas budget plus one jetton transfer. */
77
+ export declare function clmmSwapGas(prices?: GasPrices): GasRequirement;
78
+ /**
79
+ * Native CLMM swap. What is left after gas must cover the swap and be at least
80
+ * 0.05 TON; otherwise the range takes the reject branch without an error.
81
+ */
82
+ export declare function clmmSwapTonGas(swapAmount: bigint, prices?: GasPrices): GasRequirement;
83
+ export declare function clmmDepositGas(prices?: GasPrices): GasRequirement;
84
+ /** Native CLMM deposit carries the deposited TON on top of the gas. */
85
+ export declare function clmmDepositTonGas(depositAmount: bigint, prices?: GasPrices): GasRequirement;
86
+ /**
87
+ * CLMM withdrawal and fee collection: two gas budgets plus four jetton
88
+ * transfers.
89
+ */
90
+ export declare function clmmWithdrawGas(prices?: GasPrices): GasRequirement;
91
+ export declare const clmmCollectFeesGas: typeof clmmWithdrawGas;
92
+ /** CLMM position lock. The handler has no gas check; the SDK still attaches enough to execute. */
93
+ export declare function clmmLockGas(): GasRequirement;
94
+ /**
95
+ * Gas for a curve buy. `minimum` is the gate; `overhead` is what the curve
96
+ * removes from the message before any curve arithmetic runs.
97
+ */
98
+ export type MempadBuyGas = GasRequirement & {
99
+ /** What the curve subtracts first: `gross = msgValue − overhead`. */
100
+ overhead: bigint;
101
+ };
102
+ /**
103
+ * What a curve buy costs on top of the purchase. The curve computes
104
+ * `overhead = gasFee(buy gas) + jetton transfer reserve` at the live gas price,
105
+ * requires `msgValue >= overhead + minimum buy`, and spends `msgValue − overhead`.
106
+ * No headroom: everything attached above the overhead buys tokens, so `attach`
107
+ * equals `minimum`.
108
+ */
109
+ export declare function mempadBuyGas(prices?: GasPrices): MempadBuyGas;
110
+ /** Refuses to proceed when the attached value would not clear a gate. */
111
+ export declare function assertEnoughGas(requirement: GasRequirement, attached: bigint): void;
112
+ //# sourceMappingURL=gas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gas.d.ts","sourceRoot":"","sources":["../../src/core/gas.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AAEH;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG;IACtB,sDAAsD;IACtD,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,MAAM,CAAA;IACpB,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,kBAAkB,EAAE,SAIhC,CAAA;AAED,wEAAwE;AACxE,eAAO,MAAM,iBAAiB,EAAE,SAI/B,CAAA;AAED,eAAO,MAAM,kBAAkB,WAAqB,CAAA;AAEpD;;;GAGG;AACH,wBAAgB,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAE,SAA8B,GAAG,MAAM,CAMvF;AAED,2DAA2D;AAC3D,MAAM,MAAM,cAAc,GAAG;IAC3B,oCAAoC;IACpC,OAAO,EAAE,MAAM,CAAA;IACf,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAA;IACd,uCAAuC;IACvC,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,YAAc,CAAA;AAmCvC;;;GAGG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,cAAc,CAWjE;AAED;;;GAGG;AACH,wBAAgB,qBAAqB,IAAI,cAAc,CAYtD;AAED,2DAA2D;AAC3D,eAAO,MAAM,oBAAoB,cAAiB,CAAA;AAWlD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,MAAM,GAAG,cAAc,CAQvE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,IAAI,cAAc,CAOzD;AASD,wBAAgB,eAAe,IAAI,cAAc,CAOhD;AAED,+DAA+D;AAC/D,wBAAgB,kBAAkB,IAAI,cAAc,CAOnD;AAED,wBAAgB,WAAW,IAAI,cAAc,CAO5C;AAoBD,6DAA6D;AAC7D,wBAAgB,WAAW,CAAC,MAAM,GAAE,SAA8B,GAAG,cAAc,CAOlF;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,SAA8B,GACrC,cAAc,CAYhB;AAED,wBAAgB,cAAc,CAAC,MAAM,GAAE,SAA8B,GAAG,cAAc,CAOrF;AAED,uEAAuE;AACvE,wBAAgB,iBAAiB,CAC/B,aAAa,EAAE,MAAM,EACrB,MAAM,GAAE,SAA8B,GACrC,cAAc,CAQhB;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,MAAM,GAAE,SAA8B,GAAG,cAAc,CAOtF;AAED,eAAO,MAAM,kBAAkB,wBAAkB,CAAA;AAEjD,kGAAkG;AAClG,wBAAgB,WAAW,IAAI,cAAc,CAG5C;AAMD;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,cAAc,GAAG;IAC1C,qEAAqE;IACrE,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,GAAE,SAA8B,GAAG,YAAY,CAWjF;AAED,yEAAyE;AACzE,wBAAgB,eAAe,CAAC,WAAW,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAInF"}
@@ -0,0 +1,265 @@
1
+ import { constantOf } from "../protocol/index.js";
2
+ import { InsufficientGasError } from "./errors.js";
3
+ /**
4
+ * Snapshot of mainnet basechain config 21 (66.667 nanotons per gas unit). The SDK
5
+ * does not read config 21 at runtime: if the network price changes, pass `prices`.
6
+ */
7
+ export const MAINNET_GAS_PRICES = {
8
+ gasPrice: 4369067n,
9
+ flatGasLimit: 100n,
10
+ flatGasPrice: 6667n,
11
+ };
12
+ /** Previous mainnet basechain config 21 (400 nanotons per gas unit). */
13
+ export const LEGACY_GAS_PRICES = {
14
+ gasPrice: 26214400n,
15
+ flatGasLimit: 100n,
16
+ flatGasPrice: 40000n,
17
+ };
18
+ export const DEFAULT_GAS_PRICES = MAINNET_GAS_PRICES;
19
+ /**
20
+ * TON cost of a gas budget, exactly as the node computes it:
21
+ * `flat_gas_price + ceil(gas_price × (used − flat_limit) / 65536)`.
22
+ */
23
+ export function gasFee(gasUnits, prices = DEFAULT_GAS_PRICES) {
24
+ if (gasUnits <= prices.flatGasLimit)
25
+ return prices.flatGasPrice;
26
+ const scaled = prices.gasPrice * (gasUnits - prices.flatGasLimit);
27
+ const shifted = scaled >> 16n;
28
+ const rounded = (scaled & 0xffffn) === 0n ? shifted : shifted + 1n;
29
+ return prices.flatGasPrice + rounded;
30
+ }
31
+ /**
32
+ * 0.02 TON added on top of most gates, so a message built on a threshold still
33
+ * clears it after storage fees or gas prices drift.
34
+ */
35
+ export const GAS_HEADROOM = 20000000n;
36
+ function withHeadroom(minimum) {
37
+ return minimum + GAS_HEADROOM;
38
+ }
39
+ // Gas UNITS, not nanotons: convert with `gasFee`. Both are bigint, so adding
40
+ // them to nanotons type-checks and silently underfunds the jetton notification.
41
+ const CPMM_SWAP_UNITS = constantOf('cpmm', 'GAS_TON_SWAP');
42
+ const CPMM_JETTON_SWAP_UNITS = constantOf('cpmm', 'GAS_JETTON_SWAP');
43
+ const CPMM_TON_DEPOSIT_UNITS = constantOf('cpmm', 'GAS_TON_DEPOSIT');
44
+ const CPMM_JETTON_DEPOSIT_UNITS = constantOf('cpmm', 'GAS_JETTON_DEPOSIT');
45
+ const CPMM_WITHDRAW_LIQUIDITY_UNITS = constantOf('cpmm', 'GAS_WITHDRAW_LIQUIDITY');
46
+ const CPMM_WITHDRAW_FEES_UNITS = constantOf('cpmm', 'GAS_WITHDRAW_FEES');
47
+ /** Reserve for one outgoing jetton transfer. */
48
+ const CPMM_JETTON_TRANSFER = constantOf('cpmm', 'VALUE_JETTON_TRANSFER');
49
+ /** Reserve for deploying one LP position. */
50
+ const CPMM_DEPLOY_POSITION = constantOf('cpmm', 'VALUE_DEPLOY_POSITION');
51
+ /** The pool charges each declared gas constant twice over. */
52
+ const CPMM_UNITS_MULTIPLIER = 2n;
53
+ /** CPMM gate: `gasFee(units × 2) + jetton transfer reserve × transfers + extra`. */
54
+ function cpmmGate(units, transfers, extra = 0n) {
55
+ return gasFee(units * CPMM_UNITS_MULTIPLIER) + CPMM_JETTON_TRANSFER * transfers + extra;
56
+ }
57
+ /**
58
+ * The pool also requires what is left after gas to be at least 0.05 TON, or it
59
+ * quietly takes the reject branch instead of swapping.
60
+ */
61
+ const CPMM_SWAP_REMAINDER = 50000000n;
62
+ /**
63
+ * Native CPMM swap. What is left after gas must be at least 0.05 TON and at
64
+ * least the swap amount, so the floor is the larger of the two, not their sum.
65
+ */
66
+ export function cpmmSwapTonGas(swapAmount) {
67
+ const required = cpmmGate(CPMM_SWAP_UNITS, 1n);
68
+ const remainder = swapAmount > CPMM_SWAP_REMAINDER ? swapAmount : CPMM_SWAP_REMAINDER;
69
+ const minimum = required + remainder;
70
+ return {
71
+ minimum,
72
+ attach: withHeadroom(minimum),
73
+ gate: `value >= ${required} (swap gas and one jetton transfer), and what remains ` +
74
+ `after gas must be >= max(swapAmount, ${CPMM_SWAP_REMAINDER})`,
75
+ };
76
+ }
77
+ /**
78
+ * Jetton CPMM swap: the forwarded TON amount, not the transfer's own value.
79
+ * Must clear both the gas gate and the 0.06 TON notification gate.
80
+ */
81
+ export function cpmmSwapJettonForward() {
82
+ const required = cpmmGate(CPMM_JETTON_SWAP_UNITS, 1n);
83
+ const notification = 60000000n + 1n; // +1n: older pool code checks strictly greater
84
+ // Keep the max: which gate binds depends on gas prices.
85
+ const minimum = required > notification ? required : notification;
86
+ return {
87
+ minimum,
88
+ attach: withHeadroom(minimum),
89
+ gate: `forward >= ${required} (swap gas and one jetton transfer) and forward > 60000000 ` +
90
+ `(notification minimum)`,
91
+ };
92
+ }
93
+ /** Minimum native deposit the pool accepts, before gas. */
94
+ export const CPMM_MIN_TON_DEPOSIT = 1000000000n;
95
+ /**
96
+ * What the pool withholds from either deposit leg: deposit gas, two position
97
+ * deployments and 0.01 TON. Two positions, because a `split` deposit deploys one
98
+ * per recipient.
99
+ */
100
+ function cpmmDepositRequired(units) {
101
+ return gasFee(units * CPMM_UNITS_MULTIPLIER) + CPMM_DEPLOY_POSITION * 2n + 10000000n;
102
+ }
103
+ /**
104
+ * Native CPMM deposit: the deposit itself plus gas on top.
105
+ *
106
+ * Exact, with no headroom: the pool deposits everything above the gas but mints
107
+ * LP for the smaller side, so a surplus enters the reserves without LP. Attaching
108
+ * less shrinks the deposit, and at the 1 TON minimum fails with LOW_AMOUNT (510).
109
+ */
110
+ export function cpmmDepositTonGas(depositAmount) {
111
+ const required = cpmmDepositRequired(CPMM_TON_DEPOSIT_UNITS);
112
+ const minimum = depositAmount + required;
113
+ return {
114
+ minimum,
115
+ attach: minimum,
116
+ gate: `deposit = value − ${required} (deposit gas and position deployment) must be >= 1000000000`,
117
+ };
118
+ }
119
+ /**
120
+ * CPMM jetton deposit leg: what the notification must carry. Falling short does
121
+ * not throw; the pool refunds the jettons minus gas.
122
+ */
123
+ export function cpmmDepositJettonForward() {
124
+ const required = cpmmDepositRequired(CPMM_JETTON_DEPOSIT_UNITS);
125
+ return {
126
+ minimum: required,
127
+ attach: withHeadroom(required),
128
+ gate: `forward >= ${required} (deposit gas and position deployment)`,
129
+ };
130
+ }
131
+ /**
132
+ * Attached to CPMM withdrawals and fee collection. The gates (~0.104 and
133
+ * ~0.114 TON) are only entry checks; onward messages and up to two jetton
134
+ * transfers are paid from what is attached.
135
+ */
136
+ const CPMM_POSITION_ATTACH = 300000000n;
137
+ export function cpmmWithdrawGas() {
138
+ const required = cpmmGate(CPMM_WITHDRAW_LIQUIDITY_UNITS, 2n);
139
+ return {
140
+ minimum: required,
141
+ attach: CPMM_POSITION_ATTACH,
142
+ gate: `value >= ${required} (withdrawal gas and two jetton transfers)`,
143
+ };
144
+ }
145
+ /** CPMM fee collection: 0.01 TON above the withdrawal gate. */
146
+ export function cpmmCollectFeesGas() {
147
+ const required = cpmmGate(CPMM_WITHDRAW_FEES_UNITS, 2n, 10000000n);
148
+ return {
149
+ minimum: required,
150
+ attach: CPMM_POSITION_ATTACH,
151
+ gate: `value >= ${required} (fee collection gas, two jetton transfers and 0.01 TON)`,
152
+ };
153
+ }
154
+ export function cpmmLockGas() {
155
+ const minimum = 100000000n;
156
+ return {
157
+ minimum,
158
+ attach: withHeadroom(minimum),
159
+ gate: 'value >= 0.1 TON',
160
+ };
161
+ }
162
+ const CLMM_JETTON_TRANSFER = constantOf('clmm', 'VALUE_JETTON_TRANSFER');
163
+ const CLMM_GAS_LIMIT = constantOf('clmm', 'GAS_LIMIT');
164
+ /**
165
+ * Round figures attached to CLMM operations, above gates of about 0.1167,
166
+ * 0.2167 and 0.3333 TON at current gas prices. Unspent TON returns as excesses.
167
+ * The gas tests assert each figure still clears its gate.
168
+ */
169
+ const CLMM_SWAP_ATTACH = 150000000n;
170
+ const CLMM_DEPOSIT_ATTACH = 250000000n;
171
+ const CLMM_POSITION_ATTACH = 350000000n;
172
+ /**
173
+ * What must be left after gas on a native CLMM swap, beyond the swap itself.
174
+ * Less than 0.05 TON takes the reject branch.
175
+ */
176
+ const CLMM_SWAP_REMAINDER = 50000000n;
177
+ /** CLMM swap: a full gas budget plus one jetton transfer. */
178
+ export function clmmSwapGas(prices = DEFAULT_GAS_PRICES) {
179
+ const minimum = gasFee(CLMM_GAS_LIMIT, prices) + CLMM_JETTON_TRANSFER;
180
+ return {
181
+ minimum,
182
+ attach: minimum > CLMM_SWAP_ATTACH ? withHeadroom(minimum) : CLMM_SWAP_ATTACH,
183
+ gate: `value >= ${minimum} (swap gas budget and one jetton transfer)`,
184
+ };
185
+ }
186
+ /**
187
+ * Native CLMM swap. What is left after gas must cover the swap and be at least
188
+ * 0.05 TON; otherwise the range takes the reject branch without an error.
189
+ */
190
+ export function clmmSwapTonGas(swapAmount, prices = DEFAULT_GAS_PRICES) {
191
+ const base = clmmSwapGas(prices);
192
+ const remainder = swapAmount > CLMM_SWAP_REMAINDER ? swapAmount : CLMM_SWAP_REMAINDER;
193
+ const minimum = base.minimum + remainder;
194
+ return {
195
+ minimum,
196
+ // Only the gas part is rounded; the swap amount is added as is.
197
+ attach: base.attach + remainder,
198
+ gate: `value >= ${base.minimum} (swap gas budget and one jetton transfer), and what ` +
199
+ `remains must be >= max(swapAmount, ${CLMM_SWAP_REMAINDER})`,
200
+ };
201
+ }
202
+ export function clmmDepositGas(prices = DEFAULT_GAS_PRICES) {
203
+ const minimum = gasFee(CLMM_GAS_LIMIT, prices) + CLMM_JETTON_TRANSFER * 3n;
204
+ return {
205
+ minimum,
206
+ attach: minimum > CLMM_DEPOSIT_ATTACH ? withHeadroom(minimum) : CLMM_DEPOSIT_ATTACH,
207
+ gate: `value >= ${minimum} (deposit gas budget and three jetton transfers)`,
208
+ };
209
+ }
210
+ /** Native CLMM deposit carries the deposited TON on top of the gas. */
211
+ export function clmmDepositTonGas(depositAmount, prices = DEFAULT_GAS_PRICES) {
212
+ const base = clmmDepositGas(prices);
213
+ return {
214
+ minimum: base.minimum + depositAmount,
215
+ // Only the gas part is rounded; the deposit is added as is.
216
+ attach: base.attach + depositAmount,
217
+ gate: base.gate,
218
+ };
219
+ }
220
+ /**
221
+ * CLMM withdrawal and fee collection: two gas budgets plus four jetton
222
+ * transfers.
223
+ */
224
+ export function clmmWithdrawGas(prices = DEFAULT_GAS_PRICES) {
225
+ const minimum = gasFee(CLMM_GAS_LIMIT * 2n, prices) + CLMM_JETTON_TRANSFER * 4n;
226
+ return {
227
+ minimum,
228
+ attach: minimum > CLMM_POSITION_ATTACH ? withHeadroom(minimum) : CLMM_POSITION_ATTACH,
229
+ gate: `value >= ${minimum} (two gas budgets and four jetton transfers)`,
230
+ };
231
+ }
232
+ export const clmmCollectFeesGas = clmmWithdrawGas;
233
+ /** CLMM position lock. The handler has no gas check; the SDK still attaches enough to execute. */
234
+ export function clmmLockGas() {
235
+ const minimum = 100000000n;
236
+ return { minimum, attach: minimum, gate: 'no gate — the handler does not check gas' };
237
+ }
238
+ const MEMPAD_BUY_UNITS = constantOf('mempad', 'GAS_BUY');
239
+ const MEMPAD_JETTON_TRANSFER = constantOf('mempad', 'VALUE_JETTON_TRANSFER');
240
+ const MEMPAD_MIN_BUY = constantOf('mempad', 'VALUE_MIN_BUY');
241
+ /**
242
+ * What a curve buy costs on top of the purchase. The curve computes
243
+ * `overhead = gasFee(buy gas) + jetton transfer reserve` at the live gas price,
244
+ * requires `msgValue >= overhead + minimum buy`, and spends `msgValue − overhead`.
245
+ * No headroom: everything attached above the overhead buys tokens, so `attach`
246
+ * equals `minimum`.
247
+ */
248
+ export function mempadBuyGas(prices = DEFAULT_GAS_PRICES) {
249
+ const overhead = gasFee(MEMPAD_BUY_UNITS, prices) + MEMPAD_JETTON_TRANSFER;
250
+ const minimum = overhead + MEMPAD_MIN_BUY;
251
+ return {
252
+ overhead,
253
+ minimum,
254
+ attach: minimum,
255
+ gate: `value >= ${overhead} (buy gas and one jetton transfer) + ${MEMPAD_MIN_BUY} ` +
256
+ `(minimum buy), i.e. >= ${minimum}`,
257
+ };
258
+ }
259
+ /** Refuses to proceed when the attached value would not clear a gate. */
260
+ export function assertEnoughGas(requirement, attached) {
261
+ if (attached < requirement.minimum) {
262
+ throw new InsufficientGasError(requirement.minimum, attached, requirement.gate);
263
+ }
264
+ }
265
+ //# sourceMappingURL=gas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gas.js","sourceRoot":"","sources":["../../src/core/gas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAuBlD;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAc;IAC3C,QAAQ,EAAE,QAAU;IACpB,YAAY,EAAE,IAAI;IAClB,YAAY,EAAE,K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CAA8C;KACxE,CAAA;AACH,CAAC;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,eAAe,CAAA;AAEjD,kGAAkG;AAClG,MAAM,UAAU,WAAW;IACzB,MAAM,OAAO,GAAG,UAAY,CAAA;IAC5B,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,0CAA0C,EAAE,CAAA;AACvF,CAAC;AAED,MAAM,gBAAgB,GAAG,UAAU,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAA;AACxD,MAAM,sBAAsB,GAAG,UAAU,CAAC,QAAQ,EAAE,uBAAuB,CAAC,CAAA;AAC5E,MAAM,cAAc,GAAG,UAAU,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAA;AAW5D;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,SAAoB,kBAAkB;IACjE,MAAM,QAAQ,GAAG,MAAM,CAAC,gBAAgB,EAAE,MAAM,CAAC,GAAG,sBAAsB,CAAA;IAC1E,MAAM,OAAO,GAAG,QAAQ,GAAG,cAAc,CAAA;IACzC,OAAO;QACL,QAAQ;QACR,OAAO;QACP,MAAM,EAAE,OAAO;QACf,IAAI,EACF,YAAY,QAAQ,wCAAwC,cAAc,GAAG;YAC7E,0BAA0B,OAAO,EAAE;KACtC,CAAA;AACH,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,eAAe,CAAC,WAA2B,EAAE,QAAgB;IAC3E,IAAI,QAAQ,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC;QACnC,MAAM,IAAI,oBAAoB,CAAC,WAAW,CAAC,OAAO,EAAE,QAAQ,EAAE,WAAW,CAAC,IAAI,CAAC,CAAA;IACjF,CAAC;AACH,CAAC"}
@@ -0,0 +1,27 @@
1
+ /** A slippage tolerance or fee share, held as whole basis points (never a float). */
2
+ export declare class Percent {
3
+ /** Basis points. 1 bps = 0.01%. 10000 bps = 100%. */
4
+ readonly bps: number;
5
+ constructor(bps: number);
6
+ /** `Percent.bps(50)` — half a percent. */
7
+ static bps(value: number): Percent;
8
+ /** `Percent.percent(0.5)` — the same half a percent, written the other way. */
9
+ static percent(value: number): Percent;
10
+ /**
11
+ * Parses a written percentage: `'0.5%'`, `'0.5'` or `'50bps'`.
12
+ *
13
+ * Refuses bare numbers, since `50` could mean bps or percent; use
14
+ * {@link Percent.bps} or {@link Percent.percent} instead.
15
+ */
16
+ static of(value: string | Percent): Percent;
17
+ static zero(): Percent;
18
+ isZero(): boolean;
19
+ /** Lower bound: what you must still accept. Rounds down, in your favour. */
20
+ applyDown(value: bigint): bigint;
21
+ /** Upper bound: the most you are willing to give. Rounds up. */
22
+ applyUp(value: bigint): bigint;
23
+ /** The share itself, rounded down: a fee of `bps` taken from `value`. */
24
+ of(value: bigint): bigint;
25
+ toString(): string;
26
+ }
27
+ //# sourceMappingURL=percent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"percent.d.ts","sourceRoot":"","sources":["../../src/core/percent.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,qBAAa,OAAO;IAClB,qDAAqD;IACrD,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAA;gBAER,GAAG,EAAE,MAAM;IAUvB,0CAA0C;IAC1C,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAIlC,+EAA+E;IAC/E,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAQtC;;;;;OAKG;IACH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO;IAmB3C,MAAM,CAAC,IAAI,IAAI,OAAO;IAItB,MAAM,IAAI,OAAO;IAIjB,4EAA4E;IAC5E,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIhC,gEAAgE;IAChE,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAK9B,yEAAyE;IACzE,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIzB,QAAQ,IAAI,MAAM;CAGnB"}