@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.
- package/CHANGELOG.md +18 -0
- package/README.md +2 -2
- package/dist/adapters/sender.d.ts +88 -0
- package/dist/adapters/sender.d.ts.map +1 -0
- package/dist/adapters/sender.js +62 -0
- package/dist/adapters/sender.js.map +1 -0
- package/dist/adapters/tonclient.d.ts +13 -0
- package/dist/adapters/tonclient.d.ts.map +1 -0
- package/dist/adapters/tonclient.js +17 -0
- package/dist/adapters/tonclient.js.map +1 -0
- package/dist/api/index.d.ts +210 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +364 -0
- package/dist/api/index.js.map +1 -0
- package/dist/codec/bins.d.ts +26 -0
- package/dist/codec/bins.d.ts.map +1 -0
- package/dist/codec/bins.js +67 -0
- package/dist/codec/bins.js.map +1 -0
- package/dist/codec/clmm.d.ts +87 -0
- package/dist/codec/clmm.d.ts.map +1 -0
- package/dist/codec/clmm.js +119 -0
- package/dist/codec/clmm.js.map +1 -0
- package/dist/codec/cpmm.d.ts +112 -0
- package/dist/codec/cpmm.d.ts.map +1 -0
- package/dist/codec/cpmm.js +146 -0
- package/dist/codec/cpmm.js.map +1 -0
- package/dist/codec/jetton.d.ts +30 -0
- package/dist/codec/jetton.d.ts.map +1 -0
- package/dist/codec/jetton.js +37 -0
- package/dist/codec/jetton.js.map +1 -0
- package/dist/codec/mempad.d.ts +109 -0
- package/dist/codec/mempad.d.ts.map +1 -0
- package/dist/codec/mempad.js +162 -0
- package/dist/codec/mempad.js.map +1 -0
- package/dist/core/amount.d.ts +33 -0
- package/dist/core/amount.d.ts.map +1 -0
- package/dist/core/amount.js +81 -0
- package/dist/core/amount.js.map +1 -0
- package/dist/core/asset.d.ts +82 -0
- package/dist/core/asset.d.ts.map +1 -0
- package/dist/core/asset.js +143 -0
- package/dist/core/asset.js.map +1 -0
- package/dist/core/errors.d.ts +70 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +114 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/fee.d.ts +36 -0
- package/dist/core/fee.d.ts.map +1 -0
- package/dist/core/fee.js +40 -0
- package/dist/core/fee.js.map +1 -0
- package/dist/core/gas.d.ts +112 -0
- package/dist/core/gas.d.ts.map +1 -0
- package/dist/core/gas.js +265 -0
- package/dist/core/gas.js.map +1 -0
- package/dist/core/percent.d.ts +27 -0
- package/dist/core/percent.d.ts.map +1 -0
- package/dist/core/percent.js +71 -0
- package/dist/core/percent.js.map +1 -0
- package/dist/core/tx.d.ts +36 -0
- package/dist/core/tx.d.ts.map +1 -0
- package/dist/core/tx.js +17 -0
- package/dist/core/tx.js.map +1 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/jetton/index.d.ts +53 -0
- package/dist/jetton/index.d.ts.map +1 -0
- package/dist/jetton/index.js +90 -0
- package/dist/jetton/index.js.map +1 -0
- package/dist/math/clmm-swap.d.ts +80 -0
- package/dist/math/clmm-swap.d.ts.map +1 -0
- package/dist/math/clmm-swap.js +277 -0
- package/dist/math/clmm-swap.js.map +1 -0
- package/dist/math/clmm.d.ts +234 -0
- package/dist/math/clmm.d.ts.map +1 -0
- package/dist/math/clmm.js +270 -0
- package/dist/math/clmm.js.map +1 -0
- package/dist/math/cpmm.d.ts +170 -0
- package/dist/math/cpmm.d.ts.map +1 -0
- package/dist/math/cpmm.js +175 -0
- package/dist/math/cpmm.js.map +1 -0
- package/dist/math/integer.d.ts +10 -0
- package/dist/math/integer.d.ts.map +1 -0
- package/dist/math/integer.js +44 -0
- package/dist/math/integer.js.map +1 -0
- package/dist/math/q128.d.ts +45 -0
- package/dist/math/q128.d.ts.map +1 -0
- package/dist/math/q128.js +108 -0
- package/dist/math/q128.js.map +1 -0
- package/dist/moon.d.ts +245 -0
- package/dist/moon.d.ts.map +1 -0
- package/dist/moon.js +332 -0
- package/dist/moon.js.map +1 -0
- package/dist/ops/jetton-input.d.ts +3 -0
- package/dist/ops/jetton-input.d.ts.map +1 -0
- package/dist/ops/jetton-input.js +8 -0
- package/dist/ops/jetton-input.js.map +1 -0
- package/dist/ops/liquidity.d.ts +157 -0
- package/dist/ops/liquidity.d.ts.map +1 -0
- package/dist/ops/liquidity.js +413 -0
- package/dist/ops/liquidity.js.map +1 -0
- package/dist/ops/mempad.d.ts +143 -0
- package/dist/ops/mempad.d.ts.map +1 -0
- package/dist/ops/mempad.js +248 -0
- package/dist/ops/mempad.js.map +1 -0
- package/dist/ops/pool.d.ts +75 -0
- package/dist/ops/pool.d.ts.map +1 -0
- package/dist/ops/pool.js +129 -0
- package/dist/ops/pool.js.map +1 -0
- package/dist/ops/position.d.ts +47 -0
- package/dist/ops/position.d.ts.map +1 -0
- package/dist/ops/position.js +73 -0
- package/dist/ops/position.js.map +1 -0
- package/dist/ops/swap.d.ts +104 -0
- package/dist/ops/swap.d.ts.map +1 -0
- package/dist/ops/swap.js +248 -0
- package/dist/ops/swap.js.map +1 -0
- package/dist/pool/amounts.d.ts +36 -0
- package/dist/pool/amounts.d.ts.map +1 -0
- package/dist/pool/amounts.js +68 -0
- package/dist/pool/amounts.js.map +1 -0
- package/dist/pool/assets.d.ts +45 -0
- package/dist/pool/assets.d.ts.map +1 -0
- package/dist/pool/assets.js +57 -0
- package/dist/pool/assets.js.map +1 -0
- package/dist/pool/clmm.d.ts +309 -0
- package/dist/pool/clmm.d.ts.map +1 -0
- package/dist/pool/clmm.js +561 -0
- package/dist/pool/clmm.js.map +1 -0
- package/dist/pool/cpmm.d.ts +228 -0
- package/dist/pool/cpmm.d.ts.map +1 -0
- package/dist/pool/cpmm.js +332 -0
- package/dist/pool/cpmm.js.map +1 -0
- package/dist/protocol/generated.d.ts +2752 -0
- package/dist/protocol/generated.d.ts.map +1 -0
- package/dist/protocol/generated.js +3564 -0
- package/dist/protocol/generated.js.map +1 -0
- package/dist/protocol/index.d.ts +19 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +49 -0
- package/dist/protocol/index.js.map +1 -0
- package/dist/state/address.d.ts +121 -0
- package/dist/state/address.d.ts.map +1 -0
- package/dist/state/address.js +143 -0
- package/dist/state/address.js.map +1 -0
- package/dist/state/source.d.ts +26 -0
- package/dist/state/source.d.ts.map +1 -0
- package/dist/state/source.js +32 -0
- package/dist/state/source.js.map +1 -0
- package/dist/state/storage.d.ts +218 -0
- package/dist/state/storage.d.ts.map +1 -0
- package/dist/state/storage.js +273 -0
- package/dist/state/storage.js.map +1 -0
- package/package.json +74 -4
package/dist/moon.js
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { Address } from '@ton/core';
|
|
2
|
+
import { Asset } from "./core/asset.js";
|
|
3
|
+
import { PoolNotFoundError } from "./core/errors.js";
|
|
4
|
+
import { MoonApi, assetsOf, } from "./api/index.js";
|
|
5
|
+
import { rangeOfBin } from "./math/clmm.js";
|
|
6
|
+
import { BuildError } from "./core/errors.js";
|
|
7
|
+
import { buildClmmCreatePool, buildCpmmCreatePool, } from "./ops/pool.js";
|
|
8
|
+
import { clmmPoolAddress, cpmmPoolAddress, mempadCurveAddress, mempadJettonWalletAddress, } from "./state/address.js";
|
|
9
|
+
import { buildCreateCurve, buildCurveBuy, buildCurveSell, } from "./ops/mempad.js";
|
|
10
|
+
import { parseClmmFactory, parseCpmmFactory, parseMempadCurve, parseMempadFactory, } from "./state/storage.js";
|
|
11
|
+
import { readContract } from "./state/source.js";
|
|
12
|
+
import { NoWalletError, prepared } from "./adapters/sender.js";
|
|
13
|
+
import { tonClientSource } from "./adapters/tonclient.js";
|
|
14
|
+
import { CpmmPool } from "./pool/cpmm.js";
|
|
15
|
+
import { ClmmPool } from "./pool/clmm.js";
|
|
16
|
+
import { Jettons } from "./jetton/index.js";
|
|
17
|
+
function toStateSource(client) {
|
|
18
|
+
return 'getAccountState' in client ? client : tonClientSource(client);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The entry point.
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* const moon = new Moon({ client: new TonClient({ endpoint, apiKey }) })
|
|
25
|
+
* const [info] = await moon.api.findPools(TON, USDT, 'cpmm')
|
|
26
|
+
* const pool = await moon.cpmmPool(info)
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class Moon {
|
|
30
|
+
state;
|
|
31
|
+
/** Pool and position discovery. Throws when created with `api: false`. */
|
|
32
|
+
get api() {
|
|
33
|
+
if (this.#api === null) {
|
|
34
|
+
throw new BuildError('this Moon was created with api: false — pass pool addresses directly, ' +
|
|
35
|
+
'or create it with an api option');
|
|
36
|
+
}
|
|
37
|
+
return this.#api;
|
|
38
|
+
}
|
|
39
|
+
/** Whether pool discovery is available at all. */
|
|
40
|
+
get hasApi() {
|
|
41
|
+
return this.#api !== null;
|
|
42
|
+
}
|
|
43
|
+
#api;
|
|
44
|
+
/**
|
|
45
|
+
* Jetton metadata and wallet lookups: `options.jettons`, or built from a client that
|
|
46
|
+
* can open jetton masters and wallets. `null` for a bare state source without
|
|
47
|
+
* `jettons`; then supply decimals and jetton wallet addresses yourself.
|
|
48
|
+
*/
|
|
49
|
+
jettons;
|
|
50
|
+
/**
|
|
51
|
+
* The asset at this master, with decimals, symbol, name and image read from
|
|
52
|
+
* the chain through this Moon's client. Cached per master for the life of
|
|
53
|
+
* the Moon; a failed read is not cached.
|
|
54
|
+
*
|
|
55
|
+
* `null` or `'TON'` gives native TON without a request.
|
|
56
|
+
*
|
|
57
|
+
* @throws BuildError when the client cannot open jetton masters (a bare state
|
|
58
|
+
* source) and no `jettons` were given — build the asset yourself with
|
|
59
|
+
* `Asset.jetton(master, { decimals })`
|
|
60
|
+
* @throws JettonLookupError when the master cannot be read
|
|
61
|
+
*/
|
|
62
|
+
async asset(master) {
|
|
63
|
+
if (master === null || (typeof master === 'string' && master.trim().toUpperCase() === 'TON')) {
|
|
64
|
+
return Asset.native();
|
|
65
|
+
}
|
|
66
|
+
if (this.jettons === null) {
|
|
67
|
+
throw new BuildError('this Moon was created with a client that cannot open jetton masters, so jetton ' +
|
|
68
|
+
'metadata cannot be read — use Asset.jetton(master, { decimals }) instead');
|
|
69
|
+
}
|
|
70
|
+
return this.jettons.asset(typeof master === 'string' ? Address.parse(master) : master);
|
|
71
|
+
}
|
|
72
|
+
/** Factory addresses given at construction. Per-call `factory` wins over these. */
|
|
73
|
+
factories;
|
|
74
|
+
// Only the in-flight request is shared; the answer is not cached, so a factory redeploy is picked up.
|
|
75
|
+
#apiConfig = null;
|
|
76
|
+
#apiMempadConfig = null;
|
|
77
|
+
constructor(options) {
|
|
78
|
+
this.state = toStateSource(options.client);
|
|
79
|
+
this.factories = { ...options.factories };
|
|
80
|
+
this.jettons =
|
|
81
|
+
options.jettons ??
|
|
82
|
+
(typeof options.client.open === 'function'
|
|
83
|
+
? new Jettons(options.client)
|
|
84
|
+
: null);
|
|
85
|
+
this.#api =
|
|
86
|
+
options.api === false
|
|
87
|
+
? null
|
|
88
|
+
: options.api instanceof MoonApi
|
|
89
|
+
? options.api
|
|
90
|
+
: new MoonApi(options.api);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The factory a create* call will use: the one passed to the call, else the one
|
|
94
|
+
* given to the constructor, else the one the API publishes (read per call).
|
|
95
|
+
* Throws a BuildError when none is available.
|
|
96
|
+
*
|
|
97
|
+
* An API-provided factory is only checked to parse as a factory, not to be the
|
|
98
|
+
* genuine moon.cx factory; pin `factories` to avoid trusting the API.
|
|
99
|
+
*/
|
|
100
|
+
async factoryAddress(protocol, given) {
|
|
101
|
+
const configured = given ?? this.factories[protocol];
|
|
102
|
+
if (configured !== undefined)
|
|
103
|
+
return configured;
|
|
104
|
+
if (this.#api === null) {
|
|
105
|
+
throw new BuildError(`no ${protocol} factory: pass 'factory' to this call, or set ` +
|
|
106
|
+
`'factories.${protocol}' when constructing Moon — this Moon has api: false, ` +
|
|
107
|
+
'so there is nowhere to read it from');
|
|
108
|
+
}
|
|
109
|
+
const fromApi = protocol === 'mempad'
|
|
110
|
+
? (await this.#readMempadConfig()).factory
|
|
111
|
+
: (await this.#readConfig())[protocol === 'cpmm' ? 'cpmmFactory' : 'clmmFactory'];
|
|
112
|
+
if (fromApi === null) {
|
|
113
|
+
throw new BuildError(`the API at ${this.#api.baseUrl} reports no ${protocol} factory — ` +
|
|
114
|
+
`pass 'factory' to this call or set 'factories.${protocol}'`);
|
|
115
|
+
}
|
|
116
|
+
return fromApi;
|
|
117
|
+
}
|
|
118
|
+
#readConfig() {
|
|
119
|
+
if (this.#apiConfig === null) {
|
|
120
|
+
this.#apiConfig = this.api.config().finally(() => {
|
|
121
|
+
this.#apiConfig = null;
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return this.#apiConfig;
|
|
125
|
+
}
|
|
126
|
+
#readMempadConfig() {
|
|
127
|
+
if (this.#apiMempadConfig === null) {
|
|
128
|
+
this.#apiMempadConfig = this.api.mempadConfig().finally(() => {
|
|
129
|
+
this.#apiMempadConfig = null;
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return this.#apiMempadConfig;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Opens a CPMM pool by address, or by an entry from the listing.
|
|
136
|
+
*
|
|
137
|
+
* Token decimals are read from the jetton masters when this Moon has jetton lookups.
|
|
138
|
+
* Otherwise a listing entry's decimals are only used for display: they are `'listed'`
|
|
139
|
+
* in `pool.decimalsSource`, and amounts on those sides must be raw `bigint`.
|
|
140
|
+
*
|
|
141
|
+
* A listing entry must name the pool's own tokens in the pool's x/y order, or this throws.
|
|
142
|
+
*/
|
|
143
|
+
async cpmmPool(pool) {
|
|
144
|
+
const { address, assets } = resolve(pool, 'cpmm');
|
|
145
|
+
return CpmmPool.open(this.state, address, {
|
|
146
|
+
...(assets ? { assets } : {}),
|
|
147
|
+
...(this.jettons ? { jettons: this.jettons } : {}),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Opens a CLMM pool by address, or by an entry from the listing. Decimals
|
|
152
|
+
* and the entry's tokens are handled as in {@link cpmmPool}.
|
|
153
|
+
*/
|
|
154
|
+
async clmmPool(pool) {
|
|
155
|
+
const { address, assets } = resolve(pool, 'clmm');
|
|
156
|
+
return ClmmPool.open(this.state, address, {
|
|
157
|
+
...(assets ? { assets } : {}),
|
|
158
|
+
...(this.jettons ? { jettons: this.jettons } : {}),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Opens a position from a listing record, resolving its pool and, for CLMM, its
|
|
163
|
+
* range from the bins it holds.
|
|
164
|
+
*/
|
|
165
|
+
async openPosition(record) {
|
|
166
|
+
if (record.protocol === 'cpmm') {
|
|
167
|
+
const pool = await this.cpmmPool(record.pool);
|
|
168
|
+
const position = await pool.positionAt(record.address);
|
|
169
|
+
if (position === null) {
|
|
170
|
+
throw new BuildError(`no CPMM position at ${record.address.toString()}`);
|
|
171
|
+
}
|
|
172
|
+
return position;
|
|
173
|
+
}
|
|
174
|
+
if (record.bins === null) {
|
|
175
|
+
throw new BuildError(`CLMM position ${record.address.toString()} came back without bins, ` +
|
|
176
|
+
'so its range cannot be worked out');
|
|
177
|
+
}
|
|
178
|
+
const pool = await this.clmmPool(record.pool);
|
|
179
|
+
const range = await pool.range(rangeOfBin(record.bins.low));
|
|
180
|
+
return range.positionAt(record.address);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Creates a CPMM pool; the future pool address is returned in `details`.
|
|
184
|
+
*
|
|
185
|
+
* The pool address depends on the signing wallet, so pass `wallet`, or `sender`
|
|
186
|
+
* when signing elsewhere.
|
|
187
|
+
*/
|
|
188
|
+
async createCpmmPool(args) {
|
|
189
|
+
const initiator = args.sender ?? args.wallet?.address;
|
|
190
|
+
if (initiator === undefined)
|
|
191
|
+
throw new NoWalletError('creating a CPMM pool');
|
|
192
|
+
const factoryAddress = await this.factoryAddress('cpmm', args.factory);
|
|
193
|
+
const factory = await readContract(this.state, factoryAddress, 'CPMM factory', parseCpmmFactory);
|
|
194
|
+
const built = buildCpmmCreatePool({ ...args, factory: factoryAddress });
|
|
195
|
+
const poolAddress = cpmmPoolAddress({
|
|
196
|
+
factory: factoryAddress,
|
|
197
|
+
xMaster: built.details.x.master,
|
|
198
|
+
yMaster: built.details.y.master,
|
|
199
|
+
feeBps: args.feeBps,
|
|
200
|
+
initiator,
|
|
201
|
+
poolCode: factory.poolCode,
|
|
202
|
+
});
|
|
203
|
+
await this.#assertNotDeployed(poolAddress, 'CPMM');
|
|
204
|
+
return prepared({ ...built, details: { ...built.details, poolAddress } }, args.wallet);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Creates a CLMM pool at a starting price. `price` is units of `assetB` per one
|
|
208
|
+
* `assetA`; it is inverted if the factory orders the pair the other way.
|
|
209
|
+
*/
|
|
210
|
+
async createClmmPool(args) {
|
|
211
|
+
const factoryAddress = await this.factoryAddress('clmm', args.factory);
|
|
212
|
+
const factory = await readContract(this.state, factoryAddress, 'CLMM factory', parseClmmFactory);
|
|
213
|
+
const built = buildClmmCreatePool({ ...args, factory: factoryAddress });
|
|
214
|
+
const poolAddress = clmmPoolAddress({
|
|
215
|
+
factory: factoryAddress,
|
|
216
|
+
xMaster: built.details.x.master,
|
|
217
|
+
yMaster: built.details.y.master,
|
|
218
|
+
binStep: args.binStep,
|
|
219
|
+
poolCode: factory.poolCode,
|
|
220
|
+
});
|
|
221
|
+
await this.#assertNotDeployed(poolAddress, 'CLMM');
|
|
222
|
+
return prepared({ ...built, details: { ...built.details, poolAddress } }, args.wallet);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Launches a token on a bonding curve.
|
|
226
|
+
*
|
|
227
|
+
* Reads the launch fee and curve code from the factory, then refuses if the curve
|
|
228
|
+
* address is already taken: a repeated `(factory, creator, salt)` is not refused on
|
|
229
|
+
* chain, and the factory keeps the fee.
|
|
230
|
+
*/
|
|
231
|
+
async createCurve(args) {
|
|
232
|
+
const creator = args.sender ?? args.wallet?.address;
|
|
233
|
+
if (creator === undefined)
|
|
234
|
+
throw new NoWalletError('launching a token');
|
|
235
|
+
const factoryAddress = await this.factoryAddress('mempad', args.factory);
|
|
236
|
+
const factory = await readContract(this.state, factoryAddress, 'mempad factory', parseMempadFactory);
|
|
237
|
+
const built = buildCreateCurve({
|
|
238
|
+
...args,
|
|
239
|
+
factory: factoryAddress,
|
|
240
|
+
creator,
|
|
241
|
+
launchFee: factory.launchFee,
|
|
242
|
+
curveCode: factory.curveCode,
|
|
243
|
+
});
|
|
244
|
+
const curveAddress = built.details.curve;
|
|
245
|
+
await this.#assertNotDeployed(curveAddress, 'mempad');
|
|
246
|
+
return prepared({ ...built, details: { ...built.details, curveAddress } }, args.wallet);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Buys on a curve. Refuses locally when the curve is no longer trading, which on
|
|
250
|
+
* chain would fail with `WRONG_STATE` (555) after gas is spent.
|
|
251
|
+
*/
|
|
252
|
+
async curveBuy(args) {
|
|
253
|
+
const curve = await readContract(this.state, args.curve, 'mempad curve', parseMempadCurve);
|
|
254
|
+
if (curve.state !== 1) {
|
|
255
|
+
throw new BuildError(`mempad buy: this curve is no longer trading (state ${curve.state}) — ` +
|
|
256
|
+
'it would answer WRONG_STATE (555). A graduated token trades on its CPMM pool.');
|
|
257
|
+
}
|
|
258
|
+
return prepared(buildCurveBuy(args), args.wallet);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Sells back to a curve. Derives the seller's jetton wallet from the curve's own
|
|
262
|
+
* stored code (not the factory's), which the curve checks the sender against.
|
|
263
|
+
*/
|
|
264
|
+
async curveSell(args) {
|
|
265
|
+
const owner = args.sender ?? args.wallet?.address;
|
|
266
|
+
if (owner === undefined)
|
|
267
|
+
throw new NoWalletError('selling on a curve');
|
|
268
|
+
const curve = await readContract(this.state, args.curve, 'mempad curve', parseMempadCurve);
|
|
269
|
+
// The curve accepts a sell only while TRADING; otherwise the tokens bounce back
|
|
270
|
+
// but the attached gas stays on the jetton wallet.
|
|
271
|
+
if (curve.state !== 1) {
|
|
272
|
+
throw new BuildError(`mempad sell: this curve is no longer trading (state ${curve.state}) — ` +
|
|
273
|
+
'it would answer WRONG_STATE (555): the tokens would come back, but the GRAM ' +
|
|
274
|
+
'attached for gas would stay on your jetton wallet. A graduated token trades on ' +
|
|
275
|
+
'its CPMM pool.');
|
|
276
|
+
}
|
|
277
|
+
const jettonWallet = mempadJettonWalletAddress({
|
|
278
|
+
owner,
|
|
279
|
+
curve: args.curve,
|
|
280
|
+
jwCode: curve.jwCode,
|
|
281
|
+
});
|
|
282
|
+
const built = buildCurveSell({ ...args, jettonWallet, owner });
|
|
283
|
+
return prepared({ ...built, details: { ...built.details, jettonWallet } }, args.wallet);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Refuses to deploy over an existing pool or curve: the factory does not check, and
|
|
287
|
+
* the attached TON would be spent.
|
|
288
|
+
*/
|
|
289
|
+
async #assertNotDeployed(at, protocol) {
|
|
290
|
+
const existing = await this.state.getAccountState(at);
|
|
291
|
+
if (existing === null)
|
|
292
|
+
return;
|
|
293
|
+
throw new BuildError(protocol === 'mempad'
|
|
294
|
+
? `a curve already exists at ${at.toString()} — this (factory, creator, salt) ` +
|
|
295
|
+
'is taken. Deploying again is not refused on chain: the curve mails a deploy ' +
|
|
296
|
+
'error back and the factory keeps the launch fee. Use a different salt.'
|
|
297
|
+
: `a ${protocol} pool for this pair already exists at ${at.toString()} — ` +
|
|
298
|
+
'deploying again would spend the attached TON for nothing');
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Fetches the pool listing. One request; query the result locally.
|
|
302
|
+
*
|
|
303
|
+
* ```ts
|
|
304
|
+
* const pools = await moon.loadPools() // one request
|
|
305
|
+
* const info = pools.best(TON, USDT, 'cpmm') // in memory
|
|
306
|
+
* ```
|
|
307
|
+
*
|
|
308
|
+
* Throws `PoolIndexWarmingError` (retryable, carries `retryAfterMs`) while the
|
|
309
|
+
* backend is still listing a requested protocol.
|
|
310
|
+
*/
|
|
311
|
+
async loadPools(protocol) {
|
|
312
|
+
return this.api.load(protocol);
|
|
313
|
+
}
|
|
314
|
+
/** Opens the deepest pool for a pair from a listing you already hold, without fetching. */
|
|
315
|
+
async openPair(pools, a, b, protocol) {
|
|
316
|
+
const best = pools.best(a, b, protocol);
|
|
317
|
+
if (best === null)
|
|
318
|
+
throw new PoolNotFoundError(a.toString(), b.toString());
|
|
319
|
+
return protocol === 'cpmm' ? this.cpmmPool(best) : this.clmmPool(best);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function resolve(pool, expected) {
|
|
323
|
+
if ('protocol' in pool) {
|
|
324
|
+
if (pool.protocol !== expected) {
|
|
325
|
+
throw new PoolNotFoundError(pool.tokens.x.symbol, pool.tokens.y.symbol);
|
|
326
|
+
}
|
|
327
|
+
// Listing assets are checked against the pool's own masters in open().
|
|
328
|
+
return { address: pool.address, assets: assetsOf(pool) };
|
|
329
|
+
}
|
|
330
|
+
return { address: pool };
|
|
331
|
+
}
|
|
332
|
+
//# sourceMappingURL=moon.js.map
|
package/dist/moon.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moon.js","sourceRoot":"","sources":["../src/moon.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAA;AACvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EACL,OAAO,EAGP,QAAQ,GAMT,MAAM,gBAAgB,CAAA;AAGvB,OAAO,EAAE,UAAU,EAAgB,MAAM,gBAAgB,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAG7C,OAAO,EACL,m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sB;IAEtB,IAAI,UAAU,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,IAAI,CAAC,QAAQ,KAAK,QAAQ,EAAE,CAAC;YAC/B,MAAM,IAAI,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAA;QACzE,CAAC;QACD,uEAAuE;QACvE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAA;IAC1D,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jetton-input.d.ts","sourceRoot":"","sources":["../../src/ops/jetton-input.ts"],"names":[],"mappings":"AAEA,+GAA+G;AAC/G,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAOjF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AmountTooSmallError } from "../core/errors.js";
|
|
2
|
+
/** Rejects jetton inputs of 10 units or less: both pools assert `msg.amount > 10` before any refund branch. */
|
|
3
|
+
export function assertJettonInput(protocol, amount) {
|
|
4
|
+
if (amount <= 10n) {
|
|
5
|
+
throw new AmountTooSmallError('a jetton input', 11n, amount, `msg.amount > 10, checked by the ${protocol === 'cpmm' ? 'CPMM pool' : 'CLMM range'}`);
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=jetton-input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jetton-input.js","sourceRoot":"","sources":["../../src/ops/jetton-input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AAEvD,+GAA+G;AAC/G,MAAM,UAAU,iBAAiB,CAAC,QAAyB,EAAE,MAAc;IACzE,IAAI,MAAM,IAAI,GAAG,EAAE,CAAC;QAClB,MAAM,IAAI,mBAAmB,CAC3B,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAC7B,mCAAmC,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,YAAY,EAAE,CACtF,CAAA;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import type { Address } from '@ton/core';
|
|
2
|
+
import { Amount } from '../core/amount.ts';
|
|
3
|
+
import type { Asset } from '../core/asset.ts';
|
|
4
|
+
import { Percent } from '../core/percent.ts';
|
|
5
|
+
import { type BuiltOperation } from '../core/tx.ts';
|
|
6
|
+
import { type DepositBin } from '../codec/bins.ts';
|
|
7
|
+
import type { ClmmRangeState, CpmmPoolState, CpmmPositionState } from '../state/storage.ts';
|
|
8
|
+
/** Result of `planCpmmDeposit`. */
|
|
9
|
+
export type CpmmDepositPlan = {
|
|
10
|
+
amounts: {
|
|
11
|
+
x: Amount;
|
|
12
|
+
y: Amount;
|
|
13
|
+
};
|
|
14
|
+
/** LP the deposit is expected to mint. */
|
|
15
|
+
expectedLp: bigint;
|
|
16
|
+
/** LP floor written into the message. */
|
|
17
|
+
minLp: bigint;
|
|
18
|
+
/** True when this is the pool's first deposit and 10000 LP stays behind. */
|
|
19
|
+
firstDeposit: boolean;
|
|
20
|
+
/** Set when one side exceeds the pool's ratio: that excess mints no LP. */
|
|
21
|
+
surplus?: string;
|
|
22
|
+
/**
|
|
23
|
+
* True when the sender's existing position in this pool is locked: the deposit
|
|
24
|
+
* merges into it (`locked = locked || msg.locked` on the LP position)
|
|
25
|
+
* and is locked forever too. Set by `pool.deposit()`; `undefined` when not checked.
|
|
26
|
+
*/
|
|
27
|
+
positionLocked?: boolean;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Works out what a CPMM deposit will mint.
|
|
31
|
+
*
|
|
32
|
+
* The amounts come back exactly as given — nothing is trimmed. Supplying more
|
|
33
|
+
* of one side than the pool's ratio calls for mints no extra LP, and the
|
|
34
|
+
* surplus stays in the pool rather than coming back, so a warning names it.
|
|
35
|
+
* Use `pool.matchDeposit()` or `pool.deposit({ x })` to have the second side
|
|
36
|
+
* worked out for you instead.
|
|
37
|
+
*/
|
|
38
|
+
export declare function planCpmmDeposit(args: {
|
|
39
|
+
state: CpmmPoolState;
|
|
40
|
+
x: Amount;
|
|
41
|
+
y: Amount;
|
|
42
|
+
slippage: Percent;
|
|
43
|
+
}): CpmmDepositPlan;
|
|
44
|
+
/** The amount of the other side that matches, at the pool's current ratio. */
|
|
45
|
+
export declare function matchCpmmDeposit(args: {
|
|
46
|
+
state: CpmmPoolState;
|
|
47
|
+
x?: Amount;
|
|
48
|
+
y?: Amount;
|
|
49
|
+
assetX: Asset;
|
|
50
|
+
assetY: Asset;
|
|
51
|
+
}): {
|
|
52
|
+
x: Amount;
|
|
53
|
+
y: Amount;
|
|
54
|
+
};
|
|
55
|
+
export type CpmmDepositArgs = {
|
|
56
|
+
pool: Address;
|
|
57
|
+
state: CpmmPoolState;
|
|
58
|
+
plan: CpmmDepositPlan;
|
|
59
|
+
sender: Address;
|
|
60
|
+
/** Sender's jetton wallets, by side. Required for whichever side is a jetton. */
|
|
61
|
+
senderJettonWallets: {
|
|
62
|
+
x?: Address;
|
|
63
|
+
y?: Address;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Whether the sender's existing position in this pool is locked, read from
|
|
67
|
+
* its own state (`CpmmPosition.isLocked`). `true` adds a warning and marks
|
|
68
|
+
* `details.positionLocked`: the deposit merges into that position and can
|
|
69
|
+
* never be withdrawn. Leave it out if you have not read the position.
|
|
70
|
+
*/
|
|
71
|
+
positionLocked?: boolean;
|
|
72
|
+
queryId?: bigint;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Builds both legs of a CPMM deposit.
|
|
76
|
+
*
|
|
77
|
+
* They go out under one query id and are meant to be signed together: the pool
|
|
78
|
+
* matches them up in an LP account, and a leg that arrives alone waits there
|
|
79
|
+
* until the other one does — or until it is cancelled.
|
|
80
|
+
*/
|
|
81
|
+
export declare function buildCpmmDeposit(args: CpmmDepositArgs): BuiltOperation<CpmmDepositPlan>;
|
|
82
|
+
/** Builds a CPMM withdrawal. Sent to the position, not the pool. */
|
|
83
|
+
export declare function buildCpmmWithdraw(args: {
|
|
84
|
+
position: Address;
|
|
85
|
+
state: CpmmPositionState;
|
|
86
|
+
poolState: CpmmPoolState;
|
|
87
|
+
lp?: bigint;
|
|
88
|
+
percent?: Percent;
|
|
89
|
+
queryId?: bigint;
|
|
90
|
+
}): BuiltOperation<{
|
|
91
|
+
lp: bigint;
|
|
92
|
+
expected: {
|
|
93
|
+
x: bigint;
|
|
94
|
+
y: bigint;
|
|
95
|
+
};
|
|
96
|
+
}>;
|
|
97
|
+
/** Result of `shapeBins`: the per-bin layout of a CLMM deposit. */
|
|
98
|
+
export type ClmmDepositPlan = {
|
|
99
|
+
bins: DepositBin[];
|
|
100
|
+
totals: {
|
|
101
|
+
x: bigint;
|
|
102
|
+
y: bigint;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Amounts no bin could take at this price and shape. They are never put into
|
|
106
|
+
* the message and stay in the wallet. Bins below the price take only Y and bins
|
|
107
|
+
* above only X, so a two-sided deposit into a one-sided range leaves a remainder.
|
|
108
|
+
*/
|
|
109
|
+
unplaced: {
|
|
110
|
+
x: bigint;
|
|
111
|
+
y: bigint;
|
|
112
|
+
};
|
|
113
|
+
/** True when only one side is supplied and no second leg will follow. */
|
|
114
|
+
isSingle: boolean;
|
|
115
|
+
rangeNum: number;
|
|
116
|
+
};
|
|
117
|
+
export type LiquidityShape = 'spot' | 'curve' | 'bidask';
|
|
118
|
+
/**
|
|
119
|
+
* Spreads a deposit across a bin range according to a shape.
|
|
120
|
+
*
|
|
121
|
+
* `spot` puts the same amount in every bin. `curve` concentrates it at the
|
|
122
|
+
* centre, `bidask` at the edges. The weights are squared distances, so `curve`
|
|
123
|
+
* really is quadratic rather than merely leaning inward.
|
|
124
|
+
*/
|
|
125
|
+
export declare function shapeBins(args: {
|
|
126
|
+
fromBin: number;
|
|
127
|
+
toBin: number;
|
|
128
|
+
shape: LiquidityShape;
|
|
129
|
+
x: bigint;
|
|
130
|
+
y: bigint;
|
|
131
|
+
currentSqrtPrice: bigint;
|
|
132
|
+
binStep: number;
|
|
133
|
+
}): ClmmDepositPlan;
|
|
134
|
+
/** Builds both legs of a CLMM deposit into one range. */
|
|
135
|
+
export declare function buildClmmDeposit(args: {
|
|
136
|
+
range: Address;
|
|
137
|
+
state: ClmmRangeState;
|
|
138
|
+
plan: ClmmDepositPlan;
|
|
139
|
+
sender: Address;
|
|
140
|
+
senderJettonWallets: {
|
|
141
|
+
x?: Address;
|
|
142
|
+
y?: Address;
|
|
143
|
+
};
|
|
144
|
+
xIsNative: boolean;
|
|
145
|
+
queryId?: bigint;
|
|
146
|
+
}): BuiltOperation<ClmmDepositPlan>;
|
|
147
|
+
/** Builds a CLMM withdrawal: which bins to empty. */
|
|
148
|
+
export declare function buildClmmWithdraw(args: {
|
|
149
|
+
position: Address;
|
|
150
|
+
bins: readonly number[];
|
|
151
|
+
locked: boolean;
|
|
152
|
+
pending: boolean;
|
|
153
|
+
queryId?: bigint;
|
|
154
|
+
}): BuiltOperation<{
|
|
155
|
+
bins: number[];
|
|
156
|
+
}>;
|
|
157
|
+
//# sourceMappingURL=liquidity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"liquidity.d.ts","sourceRoot":"","sources":["../../src/ops/liquidity.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC1C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAE7C,OAA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