@floorlaunchpad/sdk 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +83 -18
- package/README.md +0 -2
- package/dist/abi/functions.gen.js +1 -1
- package/dist/config/generated.js +77 -0
- package/dist/evm/chains.js +1 -24
- package/dist/evm/router.js +1 -25
- package/dist/evm/squid.js +3 -3
- package/dist/index.js +1 -0
- package/dist/indexer/bodies.js +4 -0
- package/dist/math/v2/book.js +40 -7
- package/dist/math/v2/create.js +1 -1
- package/dist/math/v2/relay.js +201 -1
- package/dist/math/v2/split.js +60 -15
- package/dist/math/v2/trade.js +16 -8
- package/dist/ton/addresses.js +36 -0
- package/dist/ton/book-check.js +23 -0
- package/dist/ton/book.js +119 -0
- package/dist/ton/cell.js +542 -0
- package/dist/ton/consts.generated.js +17 -0
- package/dist/ton/content.js +83 -0
- package/dist/ton/gas.js +198 -0
- package/dist/ton/governance.js +401 -0
- package/dist/ton/index.js +18 -0
- package/dist/ton/messages.js +340 -0
- package/dist/ton/ops.js +37 -0
- package/dist/ton/price.js +51 -0
- package/dist/ton/quote-entry.js +116 -0
- package/dist/ton/quote.js +25 -0
- package/dist/ton/reader.js +378 -0
- package/dist/ton/types.js +43 -0
- package/dist/ton/wallet.js +26 -0
- package/dist/v2/evm.js +3 -1
- package/dist/v2/layouts.js +5 -1
- package/dist/v2/router.js +2 -2
- package/dist/v2/solanaSettle.js +2 -0
- package/docs/aggregator.md +0 -22
- package/docs/bot.md +0 -8
- package/docs/cursor-pages.md +0 -2
- package/docs/frontend.md +0 -2
- package/docs/ton.md +107 -0
- package/docs/wallet.md +0 -2
- package/examples/floor-line.html +1 -1
- package/package.json +6 -1
- package/src/abi/functions.gen.ts +1 -1
- package/src/config/generated.ts +77 -0
- package/src/evm/chains.ts +3 -28
- package/src/evm/router.ts +1 -26
- package/src/evm/squid.ts +3 -3
- package/src/index.ts +1 -0
- package/src/indexer/bodies.ts +4 -0
- package/src/math/v2/book.ts +42 -7
- package/src/math/v2/create.ts +1 -1
- package/src/math/v2/relay.ts +208 -1
- package/src/math/v2/split.ts +70 -16
- package/src/math/v2/trade.ts +16 -8
- package/src/ton/addresses.ts +38 -0
- package/src/ton/book-check.ts +24 -0
- package/src/ton/book.ts +136 -0
- package/src/ton/cell.ts +547 -0
- package/src/ton/consts.generated.ts +17 -0
- package/src/ton/content.ts +97 -0
- package/src/ton/gas.ts +209 -0
- package/src/ton/governance.ts +452 -0
- package/src/ton/index.ts +18 -0
- package/src/ton/messages.ts +343 -0
- package/src/ton/ops.ts +37 -0
- package/src/ton/price.ts +54 -0
- package/src/ton/quote-entry.ts +138 -0
- package/src/ton/quote.ts +67 -0
- package/src/ton/reader.ts +420 -0
- package/src/ton/types.ts +201 -0
- package/src/ton/wallet.ts +30 -0
- package/src/v2/evm.ts +5 -1
- package/src/v2/layouts.ts +5 -1
- package/src/v2/router.ts +2 -2
- package/src/v2/solanaSettle.ts +2 -0
- package/src/v2/types.ts +4 -1
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { beginCell, Builder, snakeCell } from "./cell.js";
|
|
4
|
+
|
|
5
|
+
import { accountAddress, accountData, addr, traderAddress, traderData, launchWalletAddress } from "./addresses.js";
|
|
6
|
+
import { op, STD_OP } from "./ops.js";
|
|
7
|
+
import { getGasParams } from "./reader.js";
|
|
8
|
+
import { tonLaunchIdsCell } from "./governance.js";
|
|
9
|
+
import { defaultClaimValue, defaultReclaimValue, defaultTeamShareBuyValue, fallbackMinValue, traderDeployValue, pushChildMin } from "./gas.js";
|
|
10
|
+
|
|
11
|
+
import { TON_CLAIM_KNOWN, DEFAULT_GAS, REFERRED_HOP, REFERRED_FWD_MIN, REFERRED_SELL_FWD_MIN, REFERRED_GIFT_FWD_MIN, REFERRED_FWD_DEFAULT, REFERRED_SELL_FWD_DEFAULT, REFERRED_GIFT_FWD_DEFAULT, REFERRED_SELL_GIFT_FWD_DEFAULT } from "./types.js";
|
|
12
|
+
|
|
13
|
+
const U32 = 0xffffffff;
|
|
14
|
+
const U16 = 0xffff;
|
|
15
|
+
|
|
16
|
+
const BUY_FWD_FLOOR = 120_000_000n;
|
|
17
|
+
const GAS_WALLET_HOP = 50_000_000n;
|
|
18
|
+
|
|
19
|
+
const GAS_CLAIM = 150_000_000n;
|
|
20
|
+
const GAS_DEPLOY = 500_000_000n;
|
|
21
|
+
const GAS_SMALL = 50_000_000n;
|
|
22
|
+
|
|
23
|
+
const GAS_CHECK_PRESET = 200_000_000n;
|
|
24
|
+
|
|
25
|
+
function u32(v , what ) {
|
|
26
|
+
if (!Number.isInteger(v) || v < 0 || v > U32) throw new RangeError(`${what} out of uint32 range`);
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
function pos(v , what ) {
|
|
30
|
+
if (v < 0n) throw new RangeError(`${what} is negative`);
|
|
31
|
+
return v;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function bodyOf(name , queryId = 0n) {
|
|
35
|
+
return beginCell().storeUint(op(name), 32).storeUint(queryId, 64);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const msg = (to , value , body , bounce = true) => ({ to: addr(to), value, bounce, body });
|
|
39
|
+
const tx = (...messages ) => ({ family: "ton", messages });
|
|
40
|
+
|
|
41
|
+
export function buyBody(a ) {
|
|
42
|
+
return bodyOf("buy", a.queryId)
|
|
43
|
+
.storeCoins(pos(a.money, "money"))
|
|
44
|
+
.storeCoins(pos(a.minTokens, "minTokens"))
|
|
45
|
+
.storeUint(u32(a.deadline, "deadline"), 32)
|
|
46
|
+
.storeAddress(addr(a.recipient))
|
|
47
|
+
.storeMaybeAddress(a.referrer ? addr(a.referrer) : null)
|
|
48
|
+
.endCell();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function floorBuyPayload(a ) {
|
|
52
|
+
return beginCell()
|
|
53
|
+
.storeUint(op("floor_buy"), 32)
|
|
54
|
+
.storeCoins(pos(a.minTokens, "minTokens"))
|
|
55
|
+
.storeUint(u32(a.deadline, "deadline"), 32)
|
|
56
|
+
.storeAddress(addr(a.recipient))
|
|
57
|
+
.storeMaybeAddress(a.referrer ? addr(a.referrer) : null)
|
|
58
|
+
.endCell();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function floorSellPayload(a ) {
|
|
62
|
+
return beginCell()
|
|
63
|
+
.storeUint(op("floor_sell"), 32)
|
|
64
|
+
.storeCoins(pos(a.minOut, "minOut"))
|
|
65
|
+
.storeUint(u32(a.deadline, "deadline"), 32)
|
|
66
|
+
.storeAddress(addr(a.recipient))
|
|
67
|
+
.endCell();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function jettonTransferBody(a
|
|
71
|
+
|
|
72
|
+
) {
|
|
73
|
+
const b = beginCell()
|
|
74
|
+
.storeUint(STD_OP.transfer, 32)
|
|
75
|
+
.storeUint(a.queryId ?? 0n, 64)
|
|
76
|
+
.storeCoins(pos(a.amount, "amount"))
|
|
77
|
+
.storeAddress(addr(a.destination))
|
|
78
|
+
.storeAddress(addr(a.responseDestination))
|
|
79
|
+
.storeBit(0)
|
|
80
|
+
.storeCoins(pos(a.forwardTon, "forwardTon"));
|
|
81
|
+
if (a.payload) b.storeBit(1).storeRef(a.payload);
|
|
82
|
+
else b.storeBit(0);
|
|
83
|
+
return b.endCell();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function buildBuyGram(a ) {
|
|
87
|
+
const gas = a.gas ?? DEFAULT_GAS.buyFwdMin;
|
|
88
|
+
return tx(msg(a.launch, pos(a.money, "money") + gas, buyBody(a)));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const sameAddr = (a , b ) => addr(a).equals(addr(b));
|
|
92
|
+
function doorDeploy(chain , who ) {
|
|
93
|
+
return { to: traderAddress(chain, who), value: traderDeployValue(), bounce: false, body: bodyOf("top_up").endCell(),
|
|
94
|
+
stateInit: { code: chain.codes.trader, data: traderData(chain.factory, who) } };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function buildBuyReferred(chain , a , deployTrader = false) {
|
|
98
|
+
const gas = a.gas ?? DEFAULT_GAS.buyFwdMin;
|
|
99
|
+
const door = a.recipient;
|
|
100
|
+
const gift = sameAddr(door, a.trader) ? 0n : fallbackMinValue();
|
|
101
|
+
const body = bodyOf("buy_referred", a.queryId).storeAddress(addr(a.launch)).storeRef(buyBody(a)).endCell();
|
|
102
|
+
const m = { to: traderAddress(chain, door), value: pos(a.money, "money") + gas + GAS_SMALL + gift, bounce: !deployTrader, body };
|
|
103
|
+
if (deployTrader) m.stateInit = { code: chain.codes.trader, data: traderData(chain.factory, door) };
|
|
104
|
+
return tx(m);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function buildBuyJetton(a ) {
|
|
108
|
+
const fwd = a.forwardTon ?? DEFAULT_GAS.buyFwdMin;
|
|
109
|
+
if (fwd < BUY_FWD_FLOOR && a.forwardTon !== undefined) throw new RangeError("forwardTon under buy_fwd_min");
|
|
110
|
+
const body = jettonTransferBody({
|
|
111
|
+
queryId: a.queryId, amount: a.money, destination: a.launch, responseDestination: a.buyer, forwardTon: fwd, payload: floorBuyPayload(a),
|
|
112
|
+
});
|
|
113
|
+
return tx(msg(a.buyerQuoteWallet, fwd + (a.walletGas ?? GAS_WALLET_HOP), body));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function buildBuyReferredJetton(chain , a ) {
|
|
117
|
+
const own = sameAddr(a.recipient, a.trader);
|
|
118
|
+
const min = own ? REFERRED_FWD_MIN : REFERRED_GIFT_FWD_MIN;
|
|
119
|
+
const fwd = a.forwardTon ?? (own ? REFERRED_FWD_DEFAULT : REFERRED_GIFT_FWD_DEFAULT);
|
|
120
|
+
if (fwd < min) throw new RangeError("forwardTon under referred_fwd_min");
|
|
121
|
+
const payload = bodyOf("buy_referred", a.queryId).storeAddress(addr(a.launch)).storeRef(floorBuyPayload(a)).endCell();
|
|
122
|
+
const body = jettonTransferBody({
|
|
123
|
+
queryId: a.queryId, amount: a.money, destination: traderAddress(chain, a.recipient), responseDestination: a.trader, forwardTon: fwd, payload,
|
|
124
|
+
});
|
|
125
|
+
const m = msg(a.buyerQuoteWallet, fwd + (a.walletGas ?? GAS_WALLET_HOP), body);
|
|
126
|
+
return a.deployTrader ? { family: "ton", messages: [doorDeploy(chain, a.recipient), m] } : tx(m);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function buildSellReferred(chain , a ) {
|
|
130
|
+
if (a.tokens <= 0n) throw new RangeError("tokens must be positive");
|
|
131
|
+
const own = sameAddr(a.recipient, a.seller);
|
|
132
|
+
const min = own ? REFERRED_SELL_FWD_MIN : REFERRED_GIFT_FWD_MIN;
|
|
133
|
+
const fwd = a.forwardTon ?? (own ? REFERRED_SELL_FWD_DEFAULT : REFERRED_SELL_GIFT_FWD_DEFAULT);
|
|
134
|
+
if (fwd < min) throw new RangeError("forwardTon under referred_sell_fwd_min");
|
|
135
|
+
const wallet = a.sellerWallet ?? launchWalletAddress(chain, a.launch, a.seller);
|
|
136
|
+
const payload = bodyOf("sell_referred", a.queryId).storeAddress(addr(a.launch)).storeRef(floorSellPayload(a)).endCell();
|
|
137
|
+
const body = jettonTransferBody({
|
|
138
|
+
queryId: a.queryId, amount: a.tokens, destination: traderAddress(chain, a.recipient), responseDestination: a.seller, forwardTon: fwd, payload,
|
|
139
|
+
});
|
|
140
|
+
const m = msg(wallet, fwd + (a.walletGas ?? GAS_WALLET_HOP), body);
|
|
141
|
+
return a.deployTrader ? { family: "ton", messages: [doorDeploy(chain, a.recipient), m] } : tx(m);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function buildSell(chain , a ) {
|
|
145
|
+
|
|
146
|
+
const floor = a.gasParams?.sellFwdMin;
|
|
147
|
+
const fwd = a.forwardTon ?? floor ?? DEFAULT_GAS.sellFwdMin;
|
|
148
|
+
if (floor !== undefined && fwd < floor) throw new RangeError("forwardTon under sell_fwd_min");
|
|
149
|
+
if (a.tokens <= 0n) throw new RangeError("tokens must be positive");
|
|
150
|
+
const wallet = a.sellerWallet ?? launchWalletAddress(chain, a.launch, a.seller);
|
|
151
|
+
const body = jettonTransferBody({
|
|
152
|
+
queryId: a.queryId, amount: a.tokens, destination: a.launch, responseDestination: a.seller, forwardTon: fwd, payload: floorSellPayload(a),
|
|
153
|
+
});
|
|
154
|
+
return tx(msg(wallet, fwd + (a.walletGas ?? GAS_WALLET_HOP), body));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export async function buildSellFromLaunch(rpc , chain , a ) {
|
|
158
|
+
return buildSell(chain, { ...a, gasParams: a.gasParams ?? (await getGasParams(rpc, a.launch)) });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function buildClaim(chain , a ) {
|
|
162
|
+
if (!Number.isInteger(a.types) || a.types <= 0 || a.types > U16) throw new RangeError("claim types out of uint16 range");
|
|
163
|
+
if ((a.types & ~TON_CLAIM_KNOWN) !== 0) throw new RangeError("unknown claim bit (32 is reserved and refused)");
|
|
164
|
+
const body = bodyOf("claim", a.queryId).storeUint(a.types, 16).storeAddress(a.to ? addr(a.to) : null).endCell();
|
|
165
|
+
const m = msg(accountAddress(chain, a.launch, a.owner), a.gas ?? defaultClaimValue(a.types), body);
|
|
166
|
+
if (a.deploy !== false) m.stateInit = { code: chain.codes.account, data: accountData(a.launch, a.owner) };
|
|
167
|
+
return tx(m);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function launchMeta(a ) {
|
|
171
|
+
if (!a.name || !a.symbol) throw new RangeError("name and symbol are required");
|
|
172
|
+
return beginCell().storeRef(snakeCell(a.name)).storeRef(snakeCell(a.symbol)).storeRef(snakeCell(a.uri)).endCell();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function teamSpec(t ) {
|
|
176
|
+
if (!Number.isInteger(t.slotBps) || t.slotBps < 0 || t.slotBps > U16) throw new RangeError("slotBps out of uint16 range");
|
|
177
|
+
const refs = beginCell();
|
|
178
|
+
for (let i = 0; i < 3; i++) refs.storeMaybeAddress(t.creatorRefs?.[i] ? addr(t.creatorRefs[i] ) : null);
|
|
179
|
+
const b = beginCell().storeUint(t.slotBps, 16);
|
|
180
|
+
if (t.creatorHandle !== undefined) b.storeBit(1).storeUint(t.creatorHandle, 256);
|
|
181
|
+
else b.storeBit(0);
|
|
182
|
+
return b.storeRef(refs.endCell()).endCell();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function createBody(a ) {
|
|
186
|
+
return bodyOf("create_launch", a.queryId)
|
|
187
|
+
.storeUint(u32(a.presetId, "presetId"), 32)
|
|
188
|
+
.storeAddress(a.quote ? addr(a.quote) : null)
|
|
189
|
+
.storeBit(a.listed)
|
|
190
|
+
.storeRef(launchMeta(a))
|
|
191
|
+
.storeRef(teamSpec(a.team))
|
|
192
|
+
.storeCoins(pos(a.teamBuyMoney, "teamBuyMoney"))
|
|
193
|
+
.storeCoins(pos(a.teamBuyMinTokens, "teamBuyMinTokens"))
|
|
194
|
+
.storeMaybeAddress(a.creatorRef ? addr(a.creatorRef) : null)
|
|
195
|
+
.storeUint(u32(a.deadline, "deadline"), 32)
|
|
196
|
+
.endCell();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function buildCreate(chain , a ) {
|
|
200
|
+
const total = pos(a.createFee, "createFee") + pos(a.teamBuyMoney, "teamBuyMoney");
|
|
201
|
+
const body = createBody(a);
|
|
202
|
+
if (!a.quote) return tx(msg(chain.factory, total + (a.deployGas ?? GAS_DEPLOY), body));
|
|
203
|
+
if (!a.creatorQuoteWallet) throw new RangeError("a jetton-quoted create needs the creator's quote wallet");
|
|
204
|
+
const fwd = a.forwardTon ?? GAS_DEPLOY;
|
|
205
|
+
const t = jettonTransferBody({
|
|
206
|
+
queryId: a.queryId, amount: total, destination: chain.factory, responseDestination: a.creator, forwardTon: fwd, payload: body,
|
|
207
|
+
});
|
|
208
|
+
return tx(msg(a.creatorQuoteWallet, fwd + (a.walletGas ?? GAS_WALLET_HOP), t));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function buildOptIn(chain , a ) {
|
|
212
|
+
return tx(msg(launchWalletAddress(chain, a.launch, a.owner), a.gas ?? GAS_SMALL, bodyOf("opt_in", a.queryId).endCell()));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function buildUnlockCreatorBuy(a ) {
|
|
216
|
+
return tx(msg(a.launch, a.gas ?? GAS_CLAIM, bodyOf("unlock_creator_buy", a.queryId).endCell()));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function creditBackingBody(a ) {
|
|
220
|
+
return bodyOf("credit_backing", a.queryId).storeCoins(pos(a.money, "money")).endCell();
|
|
221
|
+
}
|
|
222
|
+
export function buildCreditBacking(a ) {
|
|
223
|
+
return tx(msg(a.launch, a.money + (a.gas ?? GAS_SMALL), creditBackingBody(a)));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function teamShareBuyBody(a ) {
|
|
227
|
+
if (!Number.isInteger(a.place) || a.place < 0 || a.place > 9) throw new RangeError("place out of 0..9");
|
|
228
|
+
return bodyOf("team_share_buy", a.queryId).storeUint(a.place, 8).storeCoins(pos(a.money, "money")).storeCoins(pos(a.minTokens, "minTokens"))
|
|
229
|
+
.storeUint(u32(a.deadline, "deadline"), 32).storeBit(a.thenClaim ?? false).endCell();
|
|
230
|
+
}
|
|
231
|
+
export function buildTeamShareBuy(a ) {
|
|
232
|
+
return tx(msg(a.launch, a.gas ?? defaultTeamShareBuyValue(false, a.thenClaim ?? false), teamShareBuyBody(a)));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function buildTeamShareBuyReferred(chain , a ) {
|
|
236
|
+
const body = bodyOf("team_share_buy_referred", a.queryId).storeAddress(addr(a.launch)).storeRef(teamShareBuyBody(a)).endCell();
|
|
237
|
+
return tx({ to: traderAddress(chain, a.member), value: a.gas === undefined ? defaultTeamShareBuyValue(true, a.thenClaim ?? false) : a.gas + REFERRED_HOP, bounce: true, body });
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function buildTeamSlotAdd(a ) {
|
|
241
|
+
if ((a.member === undefined) === (a.handle === undefined)) throw new RangeError("team_slot_add takes a member or a handle, not both or neither");
|
|
242
|
+
if (a.shares <= 0n || a.shares >= 1n << 64n) throw new RangeError("shares out of 1..2^64-1");
|
|
243
|
+
const b = bodyOf("team_slot_add", a.queryId).storeAddress(a.member === undefined ? null : addr(a.member));
|
|
244
|
+
if (a.handle !== undefined) {
|
|
245
|
+
if (a.handle < 0n || a.handle >= 1n << 256n) throw new RangeError("handle out of uint256 range");
|
|
246
|
+
b.storeBit(1).storeUint(a.handle, 256);
|
|
247
|
+
} else b.storeBit(0);
|
|
248
|
+
b.storeUint(a.shares, 64);
|
|
249
|
+
return tx(msg(a.launch, a.gas ?? GAS_SMALL, b.endCell()));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function buildBurnTeam(a ) {
|
|
253
|
+
return tx(msg(a.launch, a.gas ?? GAS_SMALL, bodyOf("burn_team", a.queryId).storeCoins(pos(a.tokens, "tokens")).endCell()));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function buildSetTraderRef(chain , a ) {
|
|
257
|
+
const m = {
|
|
258
|
+
to: traderAddress(chain, a.trader), value: a.gas ?? GAS_SMALL, bounce: !a.deploy,
|
|
259
|
+
body: bodyOf("set_trader_ref", a.queryId).storeAddress(addr(a.referrer)).endCell(),
|
|
260
|
+
};
|
|
261
|
+
if (a.deploy) m.stateInit = { code: chain.codes.trader, data: traderData(chain.factory, a.trader) };
|
|
262
|
+
return tx(m);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function buildSyncQuote(a ) {
|
|
266
|
+
return tx(msg(a.launch, a.gas ?? GAS_SMALL, bodyOf("sync_quote", a.queryId).endCell()));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function buildReclaimPayout(chain , a ) {
|
|
270
|
+
const body = bodyOf("reclaim_payout", a.queryId).storeUint(a.payoutId, 64).endCell();
|
|
271
|
+
return tx(msg(accountAddress(chain, a.launch, a.payee), a.gas ?? defaultReclaimValue(), body));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function buildTopUp(a ) {
|
|
275
|
+
return tx(msg(a.to, pos(a.value, "value"), bodyOf("top_up", a.queryId).endCell()));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const ADDR_LIST_MAX = 32;
|
|
279
|
+
|
|
280
|
+
export function addrListCell(owners ) {
|
|
281
|
+
if (owners.length === 0 || owners.length > ADDR_LIST_MAX) throw new RangeError(`owners: 1..${ADDR_LIST_MAX}`);
|
|
282
|
+
let next = null;
|
|
283
|
+
for (let i = Math.floor((owners.length - 1) / 3) * 3; i >= 0; i -= 3) {
|
|
284
|
+
const part = owners.slice(i, i + 3).map(addr);
|
|
285
|
+
const b = beginCell().storeUint(part.length, 2).storeAddress(part[0] ).storeMaybeAddress(part[1] ?? null).storeMaybeAddress(part[2] ?? null);
|
|
286
|
+
next = b.storeMaybeRef(next).endCell();
|
|
287
|
+
}
|
|
288
|
+
return next ;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function sig512Cell(sig ) {
|
|
292
|
+
if (sig.length !== 64) throw new RangeError("sig: 64 bytes");
|
|
293
|
+
return beginCell().storeBytes(sig).endCell();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export function buildClearTraderRefSigned(chain , a
|
|
297
|
+
|
|
298
|
+
) {
|
|
299
|
+
if (a.nonce <= 0n || a.nonce >= 1n << 64n) throw new RangeError("nonce out of 1..2^64-1");
|
|
300
|
+
const body = bodyOf("clear_trader_ref_signed", a.queryId).storeAddress(addr(a.trader)).storeUint(a.nonce, 64)
|
|
301
|
+
.storeUint(u32(a.expires, "expires"), 32).storeRef(sig512Cell(a.sig)).endCell();
|
|
302
|
+
return tx(msg(chain.factory, a.gas ?? GAS_SMALL, body));
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function clearRefSignedCell(chain , a ) {
|
|
306
|
+
if (!Number.isInteger(a.globalId) || a.globalId < -(2 ** 31) || a.globalId >= 2 ** 31) throw new RangeError("globalId out of int32 range");
|
|
307
|
+
return beginCell().storeInt(a.globalId, 32).storeAddress(addr(chain.factory)).storeAddress(addr(a.trader))
|
|
308
|
+
.storeUint(a.nonce, 64).storeUint(u32(a.expires, "expires"), 32).endCell();
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function buildReclaimCreateRefund(chain , a ) {
|
|
312
|
+
return tx(msg(chain.factory, a.gas ?? GAS_SMALL, bodyOf("reclaim_create_refund", a.queryId).storeUint(a.refundId, 64).endCell()));
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function buildClaimCreateOwed(chain , a ) {
|
|
316
|
+
const body = bodyOf("claim_create_owed", a.queryId).storeUint(a.refundId, 64).storeAddress(a.to ? addr(a.to) : null).endCell();
|
|
317
|
+
return tx(msg(chain.factory, a.gas ?? GAS_CLAIM, body));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function buildPushChildCode(chain , a ) {
|
|
321
|
+
if (a.family !== 3 && a.family !== 4) throw new RangeError("family: 3 (account) or 4 (trader)");
|
|
322
|
+
const body = bodyOf("push_child_code", a.queryId).storeUint(a.family, 8).storeUint(a.launchId ?? 0n, 64).storeRef(addrListCell(a.owners)).endCell();
|
|
323
|
+
return tx(msg(chain.factory, a.gas ?? (pushChildMin(a.family, a.owners.length) * 11n) / 10n, body));
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function buildCheckPreset(chain , a ) {
|
|
327
|
+
return tx(msg(chain.factory, a.gas ?? GAS_CHECK_PRESET, bodyOf("check_preset", a.queryId).storeUint(u32(a.id, "id"), 32).endCell()));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export function buildBindHandle(chain , a
|
|
331
|
+
|
|
332
|
+
) {
|
|
333
|
+
if (a.value === undefined || a.value === null) throw new RangeError("bind_handle value is required: launches × fan_each + factory_gas, priced from the chain config");
|
|
334
|
+
if (a.handleId < 0n || a.handleId >= 1n << 256n) throw new RangeError("handle out of uint256 range");
|
|
335
|
+
if (a.launches.some((id) => id < 1n)) throw new RangeError("launches: every id is 1 or more");
|
|
336
|
+
const launches = tonLaunchIdsCell(a.launches);
|
|
337
|
+
const body = bodyOf("bind_handle", a.queryId).storeUint(a.handleId, 256).storeAddress(addr(a.wallet))
|
|
338
|
+
.storeRef(sig512Cell(a.sig)).storeRef(launches).endCell();
|
|
339
|
+
return tx(msg(chain.factory, pos(a.value, "value"), body));
|
|
340
|
+
}
|
package/dist/ton/ops.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { crc32 } from "./cell.js";
|
|
4
|
+
|
|
5
|
+
export const STD_OP = {
|
|
6
|
+
transfer: 0x0f8a7ea5,
|
|
7
|
+
internal_transfer: 0x178d4519,
|
|
8
|
+
transfer_notification: 0x7362d09c,
|
|
9
|
+
excesses: 0xd53276db,
|
|
10
|
+
burn: 0x595f07bc,
|
|
11
|
+
burn_notification: 0x7bdd97de,
|
|
12
|
+
provide_wallet_address: 0x2c76b973,
|
|
13
|
+
take_wallet_address: 0xd1735400,
|
|
14
|
+
provide_wallet_balance: 0x7ac8d559,
|
|
15
|
+
take_wallet_balance: 0xca77fdc2,
|
|
16
|
+
} ;
|
|
17
|
+
|
|
18
|
+
const memo = new Map ();
|
|
19
|
+
|
|
20
|
+
export function op(name ) {
|
|
21
|
+
let v = memo.get(name);
|
|
22
|
+
if (v === undefined) {
|
|
23
|
+
v = crc32("floor." + name);
|
|
24
|
+
memo.set(name, v);
|
|
25
|
+
}
|
|
26
|
+
return v;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function logTag(name ) {
|
|
30
|
+
const k = "log:" + name;
|
|
31
|
+
let v = memo.get(k);
|
|
32
|
+
if (v === undefined) {
|
|
33
|
+
v = crc32("floor.log." + name);
|
|
34
|
+
memo.set(k, v);
|
|
35
|
+
}
|
|
36
|
+
return v;
|
|
37
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { beginCell } from "./cell.js";
|
|
4
|
+
|
|
5
|
+
import { addr } from "./addresses.js";
|
|
6
|
+
import { bodyOf, sig512Cell } from "./messages.js";
|
|
7
|
+
|
|
8
|
+
export const TON_PRICE_WINDOW_S = 86_400;
|
|
9
|
+
|
|
10
|
+
export const TON_PRICE_AHEAD_CAP_S = 87_000;
|
|
11
|
+
|
|
12
|
+
export const TON_PRICE_WRITE_VALUE = 20_000_000n;
|
|
13
|
+
|
|
14
|
+
const U128 = (1n << 128n) - 1n;
|
|
15
|
+
const U32 = (1n << 32n) - 1n;
|
|
16
|
+
|
|
17
|
+
function checked(usdCentsX64 , validUntil ) {
|
|
18
|
+
if (typeof usdCentsX64 !== "bigint" || usdCentsX64 < 0n || usdCentsX64 > U128) throw new RangeError("usdCentsX64 out of uint128 range");
|
|
19
|
+
if (typeof validUntil === "number" && !Number.isInteger(validUntil)) throw new RangeError("validUntil out of uint32 range");
|
|
20
|
+
const vu = BigInt(validUntil);
|
|
21
|
+
if (vu < 0n || vu > U32) throw new RangeError("validUntil out of uint32 range");
|
|
22
|
+
return { usd: usdCentsX64, vu };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function tonPriceCell(a ) {
|
|
26
|
+
const { usd, vu } = checked(a.usdCentsX64, a.validUntil);
|
|
27
|
+
|
|
28
|
+
return beginCell().storeAddress(addr(a.factory)).storeAddress(a.quote === null ? null : addr(a.quote)).storeUint(usd, 128).storeUint(vu, 32).endCell();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function tonPriceDigest(a ) {
|
|
32
|
+
return tonPriceCell(a).hash();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function tonSetQuotePriceBody(a ) {
|
|
36
|
+
const { usd, vu } = checked(a.usdCentsX64, a.validUntil);
|
|
37
|
+
const sig = sig512Cell(a.signature);
|
|
38
|
+
return bodyOf("set_quote_price", a.queryId ?? 0n).storeAddress(a.quote === null ? null : addr(a.quote)).storeUint(usd, 128).storeUint(vu, 32).storeRef(sig).endCell();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function buildTonSetQuotePrice(chain , a ) {
|
|
42
|
+
if (a.usdCentsX64 === 0n) throw new RangeError("usdCentsX64 is zero: every create would refuse this price");
|
|
43
|
+
const value = a.value ?? TON_PRICE_WRITE_VALUE;
|
|
44
|
+
if (value <= 0n) throw new RangeError("value must be positive");
|
|
45
|
+
const body = tonSetQuotePriceBody(a);
|
|
46
|
+
return { family: "ton", messages: [{ to: addr(chain.factory), value, bounce: true, body }] };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function tonQuotePriceFresh(p , nowS ) {
|
|
50
|
+
return p.usdCentsX64 > 0n && BigInt(nowS) <= BigInt(p.validUntil);
|
|
51
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { sha256 } from "../sha256.js";
|
|
4
|
+
import { Address, beginCell, contractAddress, hexOf, readSnake } from "./cell.js";
|
|
5
|
+
|
|
6
|
+
import { addr } from "./addresses.js";
|
|
7
|
+
import { StackError, getWalletAddress, parseRefMap } from "./reader.js";
|
|
8
|
+
|
|
9
|
+
export class TonQuoteError extends Error {
|
|
10
|
+
code ;
|
|
11
|
+
constructor(code , message ) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "TonQuoteError";
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const TON_QUOTE_WALLET_KIND = { standard: 1, stable: 3, floor: 4 };
|
|
19
|
+
|
|
20
|
+
export function buildTonQuoteEntry(i ) {
|
|
21
|
+
const walletKind = TON_QUOTE_WALLET_KIND[i.kind];
|
|
22
|
+
if (walletKind === undefined) throw new TonQuoteError("Q-WALLET-KIND", `unknown quote kind "${String(i.kind)}"`);
|
|
23
|
+
return {
|
|
24
|
+
kind: 1, master: i.master, decimals: i.decimals, walletKind,
|
|
25
|
+
walletCodeHash: BigInt("0x" + hexOf(i.walletCode.hash())), walletCode: i.walletCode,
|
|
26
|
+
createFee: i.createFee, issuerCanFreeze: i.issuerCanFreeze, createsEnabled: i.createsEnabled, cap: i.cap ?? 0n,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function checkTonQuoteEntry(e ) {
|
|
31
|
+
if (e.kind === 0) {
|
|
32
|
+
if (e.master !== null || e.walletKind !== 0 || e.decimals !== 9 || e.walletCode !== null || e.issuerCanFreeze) {
|
|
33
|
+
throw new TonQuoteError("Q-NATIVE", "native quote must have no master, wallet_kind 0, decimals 9, no wallet code and issuer_can_freeze false (the factory throws 300)");
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
if (!e.master || e.master.workchain !== 0) throw new TonQuoteError("Q-MASTER-WC", "quote master must be a basechain address (the factory throws 300)");
|
|
37
|
+
if (e.walletKind !== 1 && e.walletKind !== 3 && e.walletKind !== 4) {
|
|
38
|
+
throw new TonQuoteError("Q-WALLET-KIND", `wallet_kind ${e.walletKind} cannot be listed: 1 standard, 3 stablecoin, 4 FLOOR launch token (2, Jetton 2.0, is refused by add_quote)`);
|
|
39
|
+
}
|
|
40
|
+
if (!Number.isInteger(e.decimals) || e.decimals < 0 || e.decimals > 30 || (e.walletKind === 4 && e.decimals !== 6)) {
|
|
41
|
+
throw new TonQuoteError("Q-DECIMALS", "decimals must be at most 30; a FLOOR launch token has 6 decimals");
|
|
42
|
+
}
|
|
43
|
+
if (!e.walletCode || BigInt("0x" + hexOf(e.walletCode.hash())) !== e.walletCodeHash) {
|
|
44
|
+
throw new TonQuoteError("Q-CODE", "wallet_code is missing or its hash differs from wallet_code_hash: pass the library cell itself for a library wallet");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (e.createFee <= 0n || e.createFee >= 1n << 120n) {
|
|
48
|
+
throw new TonQuoteError("Q-FEE", "create_fee must be positive and fit Coins (derive it from the USD fee and the price; never type it)");
|
|
49
|
+
}
|
|
50
|
+
if (e.kind === 1 && e.walletKind === 3 && !e.issuerCanFreeze) {
|
|
51
|
+
throw new TonQuoteError("Q-FREEZE", "a stablecoin wallet (kind 3) can be locked by its admin: set issuer_can_freeze true so the site shows the flag");
|
|
52
|
+
}
|
|
53
|
+
if (e.kind === 1 && e.walletKind === 4 && e.issuerCanFreeze) {
|
|
54
|
+
throw new TonQuoteError("Q-FLOOR-FREEZE", "a FLOOR launch token cannot be frozen: issuer_can_freeze must be false");
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function tonQuoteWalletOf(e , owner ) {
|
|
59
|
+
if (e.kind !== 1 || !e.master || !e.walletCode) throw new TonQuoteError("Q-CODE", "only a jetton entry with a master and a wallet code has a wallet");
|
|
60
|
+
const o = addr(owner);
|
|
61
|
+
const b = beginCell();
|
|
62
|
+
switch (e.walletKind) {
|
|
63
|
+
case 1: b.storeCoins(0).storeAddress(o).storeAddress(e.master).storeRef(e.walletCode); break;
|
|
64
|
+
case 3: b.storeUint(0, 4).storeCoins(0).storeAddress(o).storeAddress(e.master); break;
|
|
65
|
+
case 4: b.storeCoins(0).storeAddress(o).storeAddress(e.master).storeRef(beginCell().endCell()); break;
|
|
66
|
+
default:
|
|
67
|
+
throw new TonQuoteError("Q-WALLET-KIND", `wallet_kind ${e.walletKind} cannot be listed: 1 standard, 3 stablecoin, 4 FLOOR launch token (2, Jetton 2.0, is refused by add_quote)`);
|
|
68
|
+
}
|
|
69
|
+
return contractAddress(e.walletCode, b.endCell());
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function getTonJettonMaster(rpc , master ) {
|
|
73
|
+
const s = await rpc.runGetMethod(addr(master), "get_jetton_data", []);
|
|
74
|
+
if (s.length < 5) throw new StackError(`get_jetton_data: ${s.length} stack items, expected 5`);
|
|
75
|
+
const supply = s[0];
|
|
76
|
+
if (!supply || supply.type !== "int") throw new StackError("total_supply: expected an int at 0");
|
|
77
|
+
const cellAt = (i , name ) => {
|
|
78
|
+
const x = s[i];
|
|
79
|
+
if (!x) throw new StackError(`${name}: missing item ${i}`);
|
|
80
|
+
if (x.type === "null") return null;
|
|
81
|
+
if (x.type !== "cell" && x.type !== "slice") throw new StackError(`${name}: expected a cell at ${i}`);
|
|
82
|
+
return x.cell;
|
|
83
|
+
};
|
|
84
|
+
return { totalSupply: supply.value, content: cellAt(3, "content"), walletCode: cellAt(4, "wallet_code") };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function readTonContentDecimals(content ) {
|
|
88
|
+
const s = content.beginParse();
|
|
89
|
+
if (s.remainingBits < 8) return null;
|
|
90
|
+
if (s.loadUint(8) !== 0n) return null;
|
|
91
|
+
if (s.remainingBits < 1 || !s.loadBit()) return null;
|
|
92
|
+
const map = parseRefMap(s.loadRef(), 256);
|
|
93
|
+
const key = BigInt("0x" + hexOf(sha256(new TextEncoder().encode("decimals"))));
|
|
94
|
+
const v = map.get(key);
|
|
95
|
+
if (!v) return null;
|
|
96
|
+
const bad = (t ) => new TonQuoteError("Q-CONTENT", `the master's on-chain decimals value "${t}" is not an integer from 0 to 30`);
|
|
97
|
+
if (v.len < 8 || v.bits[0] !== 0) throw bad("(not a snake value)");
|
|
98
|
+
const head = new Uint8Array(v.bits.subarray(1, v.len >> 3));
|
|
99
|
+
let text = new TextDecoder().decode(head);
|
|
100
|
+
if (v.refs[0]) text += readSnake(v.refs[0]);
|
|
101
|
+
if (!/^\d{1,3}$/.test(text) || Number(text) > 30) throw bad(text.length > 40 ? text.slice(0, 40) + "…" : text);
|
|
102
|
+
return Number(text);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function deriveTonQuoteWallets(rpc , entry , owners ) {
|
|
106
|
+
if (!entry.master) throw new TonQuoteError("Q-MASTER-WC", "quote master must be a basechain address (the factory throws 300)");
|
|
107
|
+
const out = [];
|
|
108
|
+
for (const o of owners) {
|
|
109
|
+
const owner = addr(o);
|
|
110
|
+
const derived = tonQuoteWalletOf(entry, owner);
|
|
111
|
+
const fromMaster = await getWalletAddress(rpc, entry.master, owner);
|
|
112
|
+
out.push({ owner, derived, fromMaster, match: fromMaster !== null && fromMaster.equals(derived) });
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { buyMinTokens, quoteBuy, quoteSell } from "../math/v2/trade.js";
|
|
4
|
+
|
|
5
|
+
import { minOutAt } from "../v2/press.js";
|
|
6
|
+
import { TON_WALK_STEPS } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export function quoteBuyTon(v , amountIn , slippageBps ) {
|
|
9
|
+
const raw = quoteBuy(v.book, v.row, v.sup, amountIn, TON_WALK_STEPS);
|
|
10
|
+
return {
|
|
11
|
+
amountIn, tokens: raw.tokens, minTokens: buyMinTokens(v.book, v.row, v.sup, amountIn, slippageBps, TON_WALK_STEPS),
|
|
12
|
+
refund: raw.refund, budgetHit: raw.budgetHit, steps: raw.steps, fees: raw.fees, graduates: raw.graduatedNow, raw,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function quoteSellTon(v , tokensIn , slippageBps ) {
|
|
17
|
+
const raw = quoteSell(v.book, v.row, v.sup, tokensIn);
|
|
18
|
+
return {
|
|
19
|
+
tokensIn, out: raw.out, minOut: raw.out > 0n ? minOutAt(raw.out, slippageBps) : 0n, unfilled: raw.unfilled, fees: raw.fees, steps: raw.steps, raw,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function quoteBuyFromCell(decode , bookCell , row , sup , amountIn , slippageBps ) {
|
|
24
|
+
return quoteBuyTon({ book: decode(bookCell), row, sup }, amountIn, slippageBps);
|
|
25
|
+
}
|