@ickb/sdk 0.0.0 → 1000.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.d.ts +53 -8
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +236 -15
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +53 -8
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +237 -16
- package/dist/esm/index.js.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +405 -28
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { type ScriptDeps } from "@ickb/utils";
|
|
3
|
-
import { LogicManager, OwnedOwnerManager, type
|
|
4
|
-
import { OrderManager, Ratio, type OrderGroup } from "@ickb/order";
|
|
1
|
+
import { ccc, type Epoch } from "@ckb-ccc/core";
|
|
2
|
+
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell } from "@ickb/utils";
|
|
3
|
+
import { IckbManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroups } from "@ickb/core";
|
|
4
|
+
import { OrderManager, Ratio, type OrderCell, type OrderGroup } from "@ickb/order";
|
|
5
5
|
export declare class IckbController {
|
|
6
|
+
ickbManager: IckbManager;
|
|
6
7
|
logicManager: LogicManager;
|
|
7
8
|
ownedOwnerManager: OwnedOwnerManager;
|
|
8
9
|
orderManager: OrderManager;
|
|
9
|
-
constructor(logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager);
|
|
10
|
+
constructor(ickbManager: IckbManager, logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager);
|
|
10
11
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
11
12
|
xudt: ScriptDeps;
|
|
12
13
|
dao: ScriptDeps;
|
|
@@ -24,13 +25,57 @@ export declare class IckbController {
|
|
|
24
25
|
denominator: ccc.Num;
|
|
25
26
|
};
|
|
26
27
|
}): ccc.FixedPoint;
|
|
27
|
-
|
|
28
|
+
convert(options: {
|
|
29
|
+
signer: ccc.Signer;
|
|
30
|
+
isCkb2Udt: boolean;
|
|
31
|
+
amount: ccc.FixedPoint;
|
|
32
|
+
userCells: UserCells;
|
|
33
|
+
minCkbChange: ccc.FixedPoint;
|
|
34
|
+
system: SystemState;
|
|
35
|
+
balance: Balance;
|
|
36
|
+
fee?: {
|
|
37
|
+
numerator: ccc.Num;
|
|
38
|
+
denominator: ccc.Num;
|
|
39
|
+
};
|
|
40
|
+
}): Promise<Conversion | undefined>;
|
|
41
|
+
private attemptConversion;
|
|
42
|
+
getL1State(client: ccc.Client, locks: ccc.ScriptLike[], options?: {
|
|
43
|
+
minLockUp?: ccc.Epoch;
|
|
44
|
+
maxLockUp?: ccc.Epoch;
|
|
45
|
+
}): Promise<{
|
|
46
|
+
system: SystemState;
|
|
47
|
+
user: UserCells;
|
|
48
|
+
}>;
|
|
49
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
28
50
|
}
|
|
29
|
-
export interface
|
|
51
|
+
export interface Balance {
|
|
52
|
+
ckb: ccc.FixedPoint;
|
|
53
|
+
udt: ccc.FixedPoint;
|
|
54
|
+
}
|
|
55
|
+
export interface SystemState {
|
|
56
|
+
feeRate: ccc.Num;
|
|
30
57
|
tip: ccc.ClientBlockHeader;
|
|
31
|
-
|
|
58
|
+
exchangeRatio: {
|
|
59
|
+
ckbScale: ccc.Num;
|
|
60
|
+
udtScale: ccc.Num;
|
|
61
|
+
};
|
|
62
|
+
ckbDepositCap: ccc.FixedPoint;
|
|
63
|
+
udtDepositCap: ccc.FixedPoint;
|
|
64
|
+
deposits: (IckbDepositCell & {
|
|
65
|
+
udtCumulative: bigint;
|
|
66
|
+
})[];
|
|
67
|
+
orders: OrderCell[];
|
|
68
|
+
}
|
|
69
|
+
export interface UserCells {
|
|
70
|
+
capacities: CapacityCell[];
|
|
71
|
+
udts: UdtCell[];
|
|
32
72
|
receipts: ReceiptCell[];
|
|
33
73
|
withdrawalGroups: WithdrawalGroups[];
|
|
34
74
|
orders: OrderGroup[];
|
|
35
75
|
}
|
|
76
|
+
export interface Conversion {
|
|
77
|
+
tx: SmartTransaction;
|
|
78
|
+
ckbFee: ccc.FixedPoint;
|
|
79
|
+
maturity: Epoch;
|
|
80
|
+
}
|
|
36
81
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/cjs/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,KAAK,KAAK,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAIL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,YAAY,EAGlB,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAEL,YAAY,EACZ,KAAK,EACL,KAAK,SAAS,EACd,KAAK,UAAU,EAChB,MAAM,aAAa,CAAC;AAErB,qBAAa,cAAc;IAEhB,WAAW,EAAE,WAAW;IACxB,YAAY,EAAE,YAAY;IAC1B,iBAAiB,EAAE,iBAAiB;IACpC,YAAY,EAAE,YAAY;gBAH1B,WAAW,EAAE,WAAW,EACxB,YAAY,EAAE,YAAY,EAC1B,iBAAiB,EAAE,iBAAiB,EACpC,YAAY,EAAE,YAAY;IAGnC,MAAM,CAAC,QAAQ,CAAC,EACd,IAAI,EACJ,GAAG,EACH,SAAS,EACT,UAAU,EACV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
3
3
|
exports.IckbController = void 0;
|
|
4
|
+
const core_1 = require("@ckb-ccc/core");
|
|
4
5
|
const utils_1 = require("@ickb/utils");
|
|
5
|
-
const
|
|
6
|
+
const core_2 = require("@ickb/core");
|
|
6
7
|
const dao_1 = require("@ickb/dao");
|
|
7
8
|
const order_1 = require("@ickb/order");
|
|
8
9
|
class IckbController {
|
|
9
|
-
constructor(logicManager, ownedOwnerManager, orderManager) {
|
|
10
|
+
constructor(ickbManager, logicManager, ownedOwnerManager, orderManager) {
|
|
11
|
+
this.ickbManager = ickbManager;
|
|
10
12
|
this.logicManager = logicManager;
|
|
11
13
|
this.ownedOwnerManager = ownedOwnerManager;
|
|
12
14
|
this.orderManager = orderManager;
|
|
13
15
|
}
|
|
14
16
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
15
17
|
const daoManager = dao_1.DaoManager.fromDeps(dao);
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
+
const { script: { codeHash, hashType }, cellDeps, } = xudt;
|
|
19
|
+
const ickbXudt = {
|
|
20
|
+
script: new core_1.ccc.Script(codeHash, hashType, [ickbLogic.script.hash(), "00000080"].join("")),
|
|
21
|
+
cellDeps,
|
|
22
|
+
};
|
|
23
|
+
const ickbManager = core_2.IckbManager.fromDeps(ickbXudt, daoManager);
|
|
24
|
+
return new IckbController(ickbManager, core_2.LogicManager.fromDeps(ickbLogic, daoManager, ickbManager), core_2.OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbManager), order_1.OrderManager.fromDeps(order, ickbManager));
|
|
18
25
|
}
|
|
19
26
|
previewConversion(isCkb2Udt, amount, options) {
|
|
20
|
-
|
|
27
|
+
// TODO: make compatible with logic.convert(isCkb2Udt, amount, options)!!!
|
|
28
|
+
const { ckbScale, udtScale } = "ratio" in options ? options.ratio : (0, core_2.ickbExchangeRatio)(options.tip);
|
|
21
29
|
let convertedAmount = isCkb2Udt
|
|
22
30
|
? (amount * ckbScale) / udtScale
|
|
23
31
|
: (amount * udtScale) / ckbScale;
|
|
@@ -27,21 +35,234 @@ class IckbController {
|
|
|
27
35
|
}
|
|
28
36
|
return convertedAmount;
|
|
29
37
|
}
|
|
30
|
-
|
|
38
|
+
// TODO streamline parameters with previewConversion
|
|
39
|
+
async convert(options) {
|
|
40
|
+
const { isCkb2Udt, amount, system, signer } = options;
|
|
41
|
+
// TODO: RatioLike and specify fee!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
42
|
+
// Or let outside specify adjusted rate...
|
|
43
|
+
const info = order_1.Info.create(isCkb2Udt, order_1.Ratio.from(system.exchangeRatio));
|
|
44
|
+
const address = await signer.getRecommendedAddressObj();
|
|
45
|
+
const N = isCkb2Udt
|
|
46
|
+
? Number(amount / system.ckbDepositCap)
|
|
47
|
+
: system.deposits.slice(0, 30).length;
|
|
48
|
+
const cache = Array(N);
|
|
49
|
+
const attempt = (n) => {
|
|
50
|
+
n = N - n;
|
|
51
|
+
return (cache[n] =
|
|
52
|
+
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
53
|
+
};
|
|
54
|
+
return attempt(await asyncBinarySearch(N, async (n) => (await attempt(n)) !== undefined));
|
|
55
|
+
}
|
|
56
|
+
async attemptConversion(n, info, address, { signer, isCkb2Udt, amount, userCells, system: { feeRate, tip, deposits, ckbDepositCap, exchangeRatio }, }) {
|
|
57
|
+
let tx = OfflineTransaction.default();
|
|
58
|
+
const maturities = [];
|
|
59
|
+
let ckbFee = core_1.ccc.Zero;
|
|
60
|
+
if (n > 0) {
|
|
61
|
+
if (isCkb2Udt) {
|
|
62
|
+
amount -= ckbDepositCap * BigInt(n);
|
|
63
|
+
if (amount < 0n) {
|
|
64
|
+
// Too many Deposits respectfully to the amount
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, address.script);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const myDeposits = deposits.slice(0, n);
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
72
|
+
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
73
|
+
if (amount < 0n) {
|
|
74
|
+
// Too many Withdrawal Requests respectfully to the amount
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, address.script);
|
|
78
|
+
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (amount > 0n) {
|
|
82
|
+
this.orderManager.mint(tx, address.script, info, isCkb2Udt ? amount : core_1.ccc.Zero, isCkb2Udt ? core_1.ccc.Zero : amount);
|
|
83
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
84
|
+
const convertedAmount = (0, core_2.convert)(isCkb2Udt, amount, ratio);
|
|
85
|
+
ckbFee = isCkb2Udt
|
|
86
|
+
? amount - (0, core_2.convert)(false, convertedAmount, exchangeRatio)
|
|
87
|
+
: (0, core_2.convert)(true, amount, exchangeRatio) - convertedAmount;
|
|
88
|
+
maturities.push(
|
|
89
|
+
// TODO: use real world data from deposits, open orders and bot balances
|
|
90
|
+
orderMaturityEstimate(isCkb2Udt, amount, tip));
|
|
91
|
+
}
|
|
92
|
+
// Check that order provides enough fee to the bot for being matched
|
|
93
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
94
|
+
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
98
|
+
this.logicManager.completeDeposit(tx, receipts);
|
|
99
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups); //TODO: ignore not isReady cells in method!!!
|
|
100
|
+
for (const order of orders) {
|
|
101
|
+
this.orderManager.melt(tx, order); //TODO: allow melting multiple groups and give choices!!!
|
|
102
|
+
}
|
|
103
|
+
// const outputLen = tx.inputs.length;??
|
|
104
|
+
// Give choice that let user add this cells the way he prefer!! (Either here or at completeFeeBy)
|
|
105
|
+
this.ickbManager.addUdts(tx, udts);
|
|
106
|
+
utils_1.CapacityManager.addCapacities(tx, capacities);
|
|
107
|
+
const outputLen = tx.outputs.length;
|
|
108
|
+
try {
|
|
109
|
+
await tx.completeFeeBy(signer, feeRate);
|
|
110
|
+
// TODO: check in SmartTransaction for 64 output cells in DAO transactions
|
|
111
|
+
if (tx.outputs.length > 64) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
115
|
+
}
|
|
116
|
+
catch (_) {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
// Revert any change cells
|
|
120
|
+
tx.outputs = tx.outputs.slice(outputLen);
|
|
121
|
+
tx = utils_1.SmartTransaction.from(tx);
|
|
122
|
+
const maturity = maturities.reduce((a, b) => (0, utils_1.epochCompare)(a, b) === -1 ? b : a);
|
|
123
|
+
return {
|
|
124
|
+
tx,
|
|
125
|
+
ckbFee,
|
|
126
|
+
maturity,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
// TODO: Allow filtering which cells are fetched
|
|
130
|
+
async getL1State(client, locks, options) {
|
|
31
131
|
const tip = await client.getTipHeader();
|
|
32
132
|
const onChain = true;
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
(0, utils_1.arrayFrom)(this.logicManager.findDeposits(client,
|
|
39
|
-
(0, utils_1.arrayFrom)(
|
|
40
|
-
(0, utils_1.arrayFrom)(this.
|
|
133
|
+
const opts = { ...options, tip, onChain };
|
|
134
|
+
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders,] = await Promise.all([
|
|
135
|
+
// TODO: fetch user NervosDAO cells
|
|
136
|
+
// TODO: fetch bot cells for estimating timings
|
|
137
|
+
client.getFeeRate(),
|
|
138
|
+
(0, utils_1.arrayFrom)(this.logicManager.findDeposits(client, opts)),
|
|
139
|
+
(0, utils_1.arrayFrom)(utils_1.CapacityManager.findCapacities(client, locks, opts)),
|
|
140
|
+
(0, utils_1.arrayFrom)(this.ickbManager.findUdts(client, locks, opts)),
|
|
141
|
+
(0, utils_1.arrayFrom)(this.logicManager.findReceipts(client, locks, opts)),
|
|
142
|
+
(0, utils_1.arrayFrom)(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts)),
|
|
41
143
|
this.orderManager.findOrders(client),
|
|
42
144
|
]);
|
|
43
|
-
|
|
145
|
+
// Sort depositPool by maturity
|
|
146
|
+
depositPool.sort((a, b) => (0, utils_1.epochCompare)(a.maturity, b.maturity));
|
|
147
|
+
// Calculate cumulative value of deposits in the pool
|
|
148
|
+
let udtCumulative = 0n;
|
|
149
|
+
const cumulativeDepositPool = [];
|
|
150
|
+
for (const deposit of depositPool) {
|
|
151
|
+
udtCumulative += deposit.udtValue;
|
|
152
|
+
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
153
|
+
}
|
|
154
|
+
const userOrders = [];
|
|
155
|
+
const nonUserOrders = [];
|
|
156
|
+
for (const group of orders) {
|
|
157
|
+
if (group.isOwner(...locks)) {
|
|
158
|
+
userOrders.push(group);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
nonUserOrders.push(group.order);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const exchangeRatio = (0, core_2.ickbExchangeRatio)(tip);
|
|
165
|
+
const ckbDepositCap = (0, core_2.convert)(false, core_2.ICKB_DEPOSIT_CAP, exchangeRatio); // TODO: Remove export from core
|
|
166
|
+
return {
|
|
167
|
+
system: {
|
|
168
|
+
feeRate,
|
|
169
|
+
tip,
|
|
170
|
+
exchangeRatio,
|
|
171
|
+
ckbDepositCap,
|
|
172
|
+
udtDepositCap: core_2.ICKB_DEPOSIT_CAP,
|
|
173
|
+
deposits: cumulativeDepositPool,
|
|
174
|
+
orders: nonUserOrders,
|
|
175
|
+
},
|
|
176
|
+
user: {
|
|
177
|
+
capacities,
|
|
178
|
+
udts,
|
|
179
|
+
receipts,
|
|
180
|
+
withdrawalGroups,
|
|
181
|
+
orders: userOrders,
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
// TODO: Allow filtering which cells are accounted for
|
|
186
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }) {
|
|
187
|
+
const cells = [
|
|
188
|
+
capacities,
|
|
189
|
+
udts,
|
|
190
|
+
receipts,
|
|
191
|
+
].flat();
|
|
192
|
+
for (const { owner, owned } of withdrawalGroups) {
|
|
193
|
+
cells.push(owned, owner);
|
|
194
|
+
}
|
|
195
|
+
for (const { master, order } of orders) {
|
|
196
|
+
cells.push(master, order);
|
|
197
|
+
}
|
|
198
|
+
let ckb = core_1.ccc.Zero;
|
|
199
|
+
let udt = core_1.ccc.Zero;
|
|
200
|
+
for (const cell of cells) {
|
|
201
|
+
ckb += cell.cell.cellOutput.capacity;
|
|
202
|
+
udt += cell.udtValue ?? core_1.ccc.Zero;
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
ckb,
|
|
206
|
+
udt,
|
|
207
|
+
};
|
|
44
208
|
}
|
|
45
209
|
}
|
|
46
210
|
exports.IckbController = IckbController;
|
|
211
|
+
// Estimate bot ability to fulfill orders:
|
|
212
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
213
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
214
|
+
function orderMaturityEstimate(isCkb2Udt, amount, tip) {
|
|
215
|
+
return (0, utils_1.epochAdd)(tip.epoch, [
|
|
216
|
+
0n,
|
|
217
|
+
1n + amount / core_1.ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
218
|
+
4n * 60n,
|
|
219
|
+
]);
|
|
220
|
+
}
|
|
221
|
+
class OfflineTransaction extends utils_1.SmartTransaction {
|
|
222
|
+
clone() {
|
|
223
|
+
const result = super.clone();
|
|
224
|
+
// Keep inputs metadata when cloning, so it will not refetch that data
|
|
225
|
+
result.inputs = [...this.inputs];
|
|
226
|
+
return new OfflineTransaction(result);
|
|
227
|
+
}
|
|
228
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
229
|
+
async completeInputs(_0, _1, _2, init) {
|
|
230
|
+
// Disable completeInputs, so it will not fetch additional cells
|
|
231
|
+
return {
|
|
232
|
+
addedCount: 0,
|
|
233
|
+
accumulated: init,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
// Reimplement the rest of transformations
|
|
237
|
+
constructor(tx) {
|
|
238
|
+
super(tx.version, tx.cellDeps, tx.headerDeps, tx.inputs, tx.outputs, tx.outputsData, tx.witnesses, tx.udtHandlers, tx.headers);
|
|
239
|
+
}
|
|
240
|
+
static default() {
|
|
241
|
+
return new OfflineTransaction(super.default());
|
|
242
|
+
}
|
|
243
|
+
static fromLumosSkeleton(skeleton) {
|
|
244
|
+
return new OfflineTransaction(super.fromLumosSkeleton(skeleton));
|
|
245
|
+
}
|
|
246
|
+
static from(txLike) {
|
|
247
|
+
return new OfflineTransaction(utils_1.SmartTransaction.from(txLike));
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// TODO: utils.binarySearch is sync, need an async one for await the result!!!!!!!!!!!!
|
|
251
|
+
async function asyncBinarySearch(n, f) {
|
|
252
|
+
// Define f(-1) == false and f(n) == true.
|
|
253
|
+
// Invariant: f(i-1) == false, f(j) == true.
|
|
254
|
+
let [i, j] = [0, n];
|
|
255
|
+
while (i < j) {
|
|
256
|
+
const h = Math.trunc((i + j) / 2);
|
|
257
|
+
// i ≤ h < j
|
|
258
|
+
if (!(await f(h))) {
|
|
259
|
+
i = h + 1; // preserves f(i-1) == false
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
j = h; // preserves f(j) == true
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
|
|
266
|
+
return i;
|
|
267
|
+
}
|
|
47
268
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,wCAAgD;AAChD,uCAUqB;AACrB,qCAUoB;AACpB,mCAAuC;AACvC,uCAMqB;AAErB,MAAa,cAAc;IACzB,YACS,WAAwB,EACxB,YAA0B,EAC1B,iBAAoC,EACpC,YAA0B;QAH1B,gBAAW,GAAX,WAAW,CAAa;QACxB,iBAAY,GAAZ,YAAY,CAAc;QAC1B,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,iBAAY,GAAZ,YAAY,CAAc;IAChC,CAAC;IAEJ,MAAM,CAAC,QAAQ,CAAC,EACd,IAAI,EACJ,GAAG,EACH,SAAS,EACT,UAAU,EACV,KAAK,GAON;QACC,MAAM,UAAU,GAAG,gBAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAE5C,MAAM,EACJ,MAAM,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAC9B,QAAQ,GACT,GAAG,IAAI,CAAC;QACT,MAAM,QAAQ,GAAG;YACf,MAAM,EAAE,IAAI,UAAG,CAAC,MAAM,CACpB,QAAQ,EACR,QAAQ,EACR,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,EAAE,UAAU,CAAC,CAAC,IAAI,CAAC,EAAE,CAAY,CAC1D;YACD,QAAQ;SACT,CAAC;QACF,MAAM,WAAW,GAAG,kBAAW,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC;QAE/D,OAAO,IAAI,cAAc,CACvB,WAAW,EACX,mBAAY,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,WAAW,CAAC,EACzD,wBAAiB,CAAC,QAAQ,CAAC,UAAU,E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,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/D,CAAC;CACF;AAED,uFAAuF;AACvF,KAAK,UAAU,iBAAiB,CAC9B,CAAS,EACT,CAAkC;IAElC,0CAA0C;IAC1C,4CAA4C;IAC5C,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACpB,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACb,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAClC,YAAY;QACZ,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAClB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,4BAA4B;QACzC,CAAC;aAAM,CAAC;YACN,CAAC,GAAG,CAAC,CAAC,CAAC,yBAAyB;QAClC,CAAC;IACH,CAAC;IACD,uEAAuE;IACvE,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { type ScriptDeps } from "@ickb/utils";
|
|
3
|
-
import { LogicManager, OwnedOwnerManager, type
|
|
4
|
-
import { OrderManager, Ratio, type OrderGroup } from "@ickb/order";
|
|
1
|
+
import { ccc, type Epoch } from "@ckb-ccc/core";
|
|
2
|
+
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell } from "@ickb/utils";
|
|
3
|
+
import { IckbManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroups } from "@ickb/core";
|
|
4
|
+
import { OrderManager, Ratio, type OrderCell, type OrderGroup } from "@ickb/order";
|
|
5
5
|
export declare class IckbController {
|
|
6
|
+
ickbManager: IckbManager;
|
|
6
7
|
logicManager: LogicManager;
|
|
7
8
|
ownedOwnerManager: OwnedOwnerManager;
|
|
8
9
|
orderManager: OrderManager;
|
|
9
|
-
constructor(logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager);
|
|
10
|
+
constructor(ickbManager: IckbManager, logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager);
|
|
10
11
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
11
12
|
xudt: ScriptDeps;
|
|
12
13
|
dao: ScriptDeps;
|
|
@@ -24,13 +25,57 @@ export declare class IckbController {
|
|
|
24
25
|
denominator: ccc.Num;
|
|
25
26
|
};
|
|
26
27
|
}): ccc.FixedPoint;
|
|
27
|
-
|
|
28
|
+
convert(options: {
|
|
29
|
+
signer: ccc.Signer;
|
|
30
|
+
isCkb2Udt: boolean;
|
|
31
|
+
amount: ccc.FixedPoint;
|
|
32
|
+
userCells: UserCells;
|
|
33
|
+
minCkbChange: ccc.FixedPoint;
|
|
34
|
+
system: SystemState;
|
|
35
|
+
balance: Balance;
|
|
36
|
+
fee?: {
|
|
37
|
+
numerator: ccc.Num;
|
|
38
|
+
denominator: ccc.Num;
|
|
39
|
+
};
|
|
40
|
+
}): Promise<Conversion | undefined>;
|
|
41
|
+
private attemptConversion;
|
|
42
|
+
getL1State(client: ccc.Client, locks: ccc.ScriptLike[], options?: {
|
|
43
|
+
minLockUp?: ccc.Epoch;
|
|
44
|
+
maxLockUp?: ccc.Epoch;
|
|
45
|
+
}): Promise<{
|
|
46
|
+
system: SystemState;
|
|
47
|
+
user: UserCells;
|
|
48
|
+
}>;
|
|
49
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
28
50
|
}
|
|
29
|
-
export interface
|
|
51
|
+
export interface Balance {
|
|
52
|
+
ckb: ccc.FixedPoint;
|
|
53
|
+
udt: ccc.FixedPoint;
|
|
54
|
+
}
|
|
55
|
+
export interface SystemState {
|
|
56
|
+
feeRate: ccc.Num;
|
|
30
57
|
tip: ccc.ClientBlockHeader;
|
|
31
|
-
|
|
58
|
+
exchangeRatio: {
|
|
59
|
+
ckbScale: ccc.Num;
|
|
60
|
+
udtScale: ccc.Num;
|
|
61
|
+
};
|
|
62
|
+
ckbDepositCap: ccc.FixedPoint;
|
|
63
|
+
udtDepositCap: ccc.FixedPoint;
|
|
64
|
+
deposits: (IckbDepositCell & {
|
|
65
|
+
udtCumulative: bigint;
|
|
66
|
+
})[];
|
|
67
|
+
orders: OrderCell[];
|
|
68
|
+
}
|
|
69
|
+
export interface UserCells {
|
|
70
|
+
capacities: CapacityCell[];
|
|
71
|
+
udts: UdtCell[];
|
|
32
72
|
receipts: ReceiptCell[];
|
|
33
73
|
withdrawalGroups: WithdrawalGroups[];
|
|
34
74
|
orders: OrderGroup[];
|
|
35
75
|
}
|
|
76
|
+
export interface Conversion {
|
|
77
|
+
tx: SmartTransaction;
|
|
78
|
+
ckbFee: ccc.FixedPoint;
|
|
79
|
+
maturity: Epoch;
|
|
80
|
+
}
|
|
36
81
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,KAAK,KAAK,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAIL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,YAAY,EAGlB,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAEL,YAAY,EACZ,KAAK,EACL,KAAK,SAAS,EACd,KAAK,UAAU,EAChB,MAAM,aAAa,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,gBAAgB,EAAE,CAAC;IACrC,MAAM,EAAE,UAAU,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,gBAAgB,CAAC;IACrB,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC;IACvB,QAAQ,EAAE,KAAK,CAAC;CACjB"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,19 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { arrayFrom, epochCompare, CapacityManager, SmartTransaction, epochAdd, } from "@ickb/utils";
|
|
3
|
+
import { convert, ICKB_DEPOSIT_CAP, ickbExchangeRatio, IckbManager, LogicManager, OwnedOwnerManager, } from "@ickb/core";
|
|
3
4
|
import { DaoManager } from "@ickb/dao";
|
|
4
|
-
import { OrderManager } from "@ickb/order";
|
|
5
|
+
import { Info, OrderManager, Ratio, } from "@ickb/order";
|
|
5
6
|
export class IckbController {
|
|
6
|
-
constructor(logicManager, ownedOwnerManager, orderManager) {
|
|
7
|
+
constructor(ickbManager, logicManager, ownedOwnerManager, orderManager) {
|
|
8
|
+
this.ickbManager = ickbManager;
|
|
7
9
|
this.logicManager = logicManager;
|
|
8
10
|
this.ownedOwnerManager = ownedOwnerManager;
|
|
9
11
|
this.orderManager = orderManager;
|
|
10
12
|
}
|
|
11
13
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
12
14
|
const daoManager = DaoManager.fromDeps(dao);
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
+
const { script: { codeHash, hashType }, cellDeps, } = xudt;
|
|
16
|
+
const ickbXudt = {
|
|
17
|
+
script: new ccc.Script(codeHash, hashType, [ickbLogic.script.hash(), "00000080"].join("")),
|
|
18
|
+
cellDeps,
|
|
19
|
+
};
|
|
20
|
+
const ickbManager = IckbManager.fromDeps(ickbXudt, daoManager);
|
|
21
|
+
return new IckbController(ickbManager, LogicManager.fromDeps(ickbLogic, daoManager, ickbManager), OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbManager), OrderManager.fromDeps(order, ickbManager));
|
|
15
22
|
}
|
|
16
23
|
previewConversion(isCkb2Udt, amount, options) {
|
|
24
|
+
// TODO: make compatible with logic.convert(isCkb2Udt, amount, options)!!!
|
|
17
25
|
const { ckbScale, udtScale } = "ratio" in options ? options.ratio : ickbExchangeRatio(options.tip);
|
|
18
26
|
let convertedAmount = isCkb2Udt
|
|
19
27
|
? (amount * ckbScale) / udtScale
|
|
@@ -24,20 +32,233 @@ export class IckbController {
|
|
|
24
32
|
}
|
|
25
33
|
return convertedAmount;
|
|
26
34
|
}
|
|
27
|
-
|
|
35
|
+
// TODO streamline parameters with previewConversion
|
|
36
|
+
async convert(options) {
|
|
37
|
+
const { isCkb2Udt, amount, system, signer } = options;
|
|
38
|
+
// TODO: RatioLike and specify fee!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
39
|
+
// Or let outside specify adjusted rate...
|
|
40
|
+
const info = Info.create(isCkb2Udt, Ratio.from(system.exchangeRatio));
|
|
41
|
+
const address = await signer.getRecommendedAddressObj();
|
|
42
|
+
const N = isCkb2Udt
|
|
43
|
+
? Number(amount / system.ckbDepositCap)
|
|
44
|
+
: system.deposits.slice(0, 30).length;
|
|
45
|
+
const cache = Array(N);
|
|
46
|
+
const attempt = (n) => {
|
|
47
|
+
n = N - n;
|
|
48
|
+
return (cache[n] =
|
|
49
|
+
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
50
|
+
};
|
|
51
|
+
return attempt(await asyncBinarySearch(N, async (n) => (await attempt(n)) !== undefined));
|
|
52
|
+
}
|
|
53
|
+
async attemptConversion(n, info, address, { signer, isCkb2Udt, amount, userCells, system: { feeRate, tip, deposits, ckbDepositCap, exchangeRatio }, }) {
|
|
54
|
+
let tx = OfflineTransaction.default();
|
|
55
|
+
const maturities = [];
|
|
56
|
+
let ckbFee = ccc.Zero;
|
|
57
|
+
if (n > 0) {
|
|
58
|
+
if (isCkb2Udt) {
|
|
59
|
+
amount -= ckbDepositCap * BigInt(n);
|
|
60
|
+
if (amount < 0n) {
|
|
61
|
+
// Too many Deposits respectfully to the amount
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, address.script);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const myDeposits = deposits.slice(0, n);
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
69
|
+
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
70
|
+
if (amount < 0n) {
|
|
71
|
+
// Too many Withdrawal Requests respectfully to the amount
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, address.script);
|
|
75
|
+
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (amount > 0n) {
|
|
79
|
+
this.orderManager.mint(tx, address.script, info, isCkb2Udt ? amount : ccc.Zero, isCkb2Udt ? ccc.Zero : amount);
|
|
80
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
81
|
+
const convertedAmount = convert(isCkb2Udt, amount, ratio);
|
|
82
|
+
ckbFee = isCkb2Udt
|
|
83
|
+
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
84
|
+
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
85
|
+
maturities.push(
|
|
86
|
+
// TODO: use real world data from deposits, open orders and bot balances
|
|
87
|
+
orderMaturityEstimate(isCkb2Udt, amount, tip));
|
|
88
|
+
}
|
|
89
|
+
// Check that order provides enough fee to the bot for being matched
|
|
90
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
91
|
+
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
95
|
+
this.logicManager.completeDeposit(tx, receipts);
|
|
96
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups); //TODO: ignore not isReady cells in method!!!
|
|
97
|
+
for (const order of orders) {
|
|
98
|
+
this.orderManager.melt(tx, order); //TODO: allow melting multiple groups and give choices!!!
|
|
99
|
+
}
|
|
100
|
+
// const outputLen = tx.inputs.length;??
|
|
101
|
+
// Give choice that let user add this cells the way he prefer!! (Either here or at completeFeeBy)
|
|
102
|
+
this.ickbManager.addUdts(tx, udts);
|
|
103
|
+
CapacityManager.addCapacities(tx, capacities);
|
|
104
|
+
const outputLen = tx.outputs.length;
|
|
105
|
+
try {
|
|
106
|
+
await tx.completeFeeBy(signer, feeRate);
|
|
107
|
+
// TODO: check in SmartTransaction for 64 output cells in DAO transactions
|
|
108
|
+
if (tx.outputs.length > 64) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
112
|
+
}
|
|
113
|
+
catch (_) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
// Revert any change cells
|
|
117
|
+
tx.outputs = tx.outputs.slice(outputLen);
|
|
118
|
+
tx = SmartTransaction.from(tx);
|
|
119
|
+
const maturity = maturities.reduce((a, b) => epochCompare(a, b) === -1 ? b : a);
|
|
120
|
+
return {
|
|
121
|
+
tx,
|
|
122
|
+
ckbFee,
|
|
123
|
+
maturity,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
// TODO: Allow filtering which cells are fetched
|
|
127
|
+
async getL1State(client, locks, options) {
|
|
28
128
|
const tip = await client.getTipHeader();
|
|
29
129
|
const onChain = true;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
arrayFrom(this.logicManager.findDeposits(client,
|
|
36
|
-
arrayFrom(
|
|
37
|
-
arrayFrom(this.
|
|
130
|
+
const opts = { ...options, tip, onChain };
|
|
131
|
+
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders,] = await Promise.all([
|
|
132
|
+
// TODO: fetch user NervosDAO cells
|
|
133
|
+
// TODO: fetch bot cells for estimating timings
|
|
134
|
+
client.getFeeRate(),
|
|
135
|
+
arrayFrom(this.logicManager.findDeposits(client, opts)),
|
|
136
|
+
arrayFrom(CapacityManager.findCapacities(client, locks, opts)),
|
|
137
|
+
arrayFrom(this.ickbManager.findUdts(client, locks, opts)),
|
|
138
|
+
arrayFrom(this.logicManager.findReceipts(client, locks, opts)),
|
|
139
|
+
arrayFrom(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts)),
|
|
38
140
|
this.orderManager.findOrders(client),
|
|
39
141
|
]);
|
|
40
|
-
|
|
142
|
+
// Sort depositPool by maturity
|
|
143
|
+
depositPool.sort((a, b) => epochCompare(a.maturity, b.maturity));
|
|
144
|
+
// Calculate cumulative value of deposits in the pool
|
|
145
|
+
let udtCumulative = 0n;
|
|
146
|
+
const cumulativeDepositPool = [];
|
|
147
|
+
for (const deposit of depositPool) {
|
|
148
|
+
udtCumulative += deposit.udtValue;
|
|
149
|
+
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
150
|
+
}
|
|
151
|
+
const userOrders = [];
|
|
152
|
+
const nonUserOrders = [];
|
|
153
|
+
for (const group of orders) {
|
|
154
|
+
if (group.isOwner(...locks)) {
|
|
155
|
+
userOrders.push(group);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
nonUserOrders.push(group.order);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const exchangeRatio = ickbExchangeRatio(tip);
|
|
162
|
+
const ckbDepositCap = convert(false, ICKB_DEPOSIT_CAP, exchangeRatio); // TODO: Remove export from core
|
|
163
|
+
return {
|
|
164
|
+
system: {
|
|
165
|
+
feeRate,
|
|
166
|
+
tip,
|
|
167
|
+
exchangeRatio,
|
|
168
|
+
ckbDepositCap,
|
|
169
|
+
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
170
|
+
deposits: cumulativeDepositPool,
|
|
171
|
+
orders: nonUserOrders,
|
|
172
|
+
},
|
|
173
|
+
user: {
|
|
174
|
+
capacities,
|
|
175
|
+
udts,
|
|
176
|
+
receipts,
|
|
177
|
+
withdrawalGroups,
|
|
178
|
+
orders: userOrders,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
// TODO: Allow filtering which cells are accounted for
|
|
183
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }) {
|
|
184
|
+
const cells = [
|
|
185
|
+
capacities,
|
|
186
|
+
udts,
|
|
187
|
+
receipts,
|
|
188
|
+
].flat();
|
|
189
|
+
for (const { owner, owned } of withdrawalGroups) {
|
|
190
|
+
cells.push(owned, owner);
|
|
191
|
+
}
|
|
192
|
+
for (const { master, order } of orders) {
|
|
193
|
+
cells.push(master, order);
|
|
194
|
+
}
|
|
195
|
+
let ckb = ccc.Zero;
|
|
196
|
+
let udt = ccc.Zero;
|
|
197
|
+
for (const cell of cells) {
|
|
198
|
+
ckb += cell.cell.cellOutput.capacity;
|
|
199
|
+
udt += cell.udtValue ?? ccc.Zero;
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
ckb,
|
|
203
|
+
udt,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Estimate bot ability to fulfill orders:
|
|
208
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
209
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
210
|
+
function orderMaturityEstimate(isCkb2Udt, amount, tip) {
|
|
211
|
+
return epochAdd(tip.epoch, [
|
|
212
|
+
0n,
|
|
213
|
+
1n + amount / ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
214
|
+
4n * 60n,
|
|
215
|
+
]);
|
|
216
|
+
}
|
|
217
|
+
class OfflineTransaction extends SmartTransaction {
|
|
218
|
+
clone() {
|
|
219
|
+
const result = super.clone();
|
|
220
|
+
// Keep inputs metadata when cloning, so it will not refetch that data
|
|
221
|
+
result.inputs = [...this.inputs];
|
|
222
|
+
return new OfflineTransaction(result);
|
|
223
|
+
}
|
|
224
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
225
|
+
async completeInputs(_0, _1, _2, init) {
|
|
226
|
+
// Disable completeInputs, so it will not fetch additional cells
|
|
227
|
+
return {
|
|
228
|
+
addedCount: 0,
|
|
229
|
+
accumulated: init,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
// Reimplement the rest of transformations
|
|
233
|
+
constructor(tx) {
|
|
234
|
+
super(tx.version, tx.cellDeps, tx.headerDeps, tx.inputs, tx.outputs, tx.outputsData, tx.witnesses, tx.udtHandlers, tx.headers);
|
|
235
|
+
}
|
|
236
|
+
static default() {
|
|
237
|
+
return new OfflineTransaction(super.default());
|
|
238
|
+
}
|
|
239
|
+
static fromLumosSkeleton(skeleton) {
|
|
240
|
+
return new OfflineTransaction(super.fromLumosSkeleton(skeleton));
|
|
241
|
+
}
|
|
242
|
+
static from(txLike) {
|
|
243
|
+
return new OfflineTransaction(SmartTransaction.from(txLike));
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
// TODO: utils.binarySearch is sync, need an async one for await the result!!!!!!!!!!!!
|
|
247
|
+
async function asyncBinarySearch(n, f) {
|
|
248
|
+
// Define f(-1) == false and f(n) == true.
|
|
249
|
+
// Invariant: f(i-1) == false, f(j) == true.
|
|
250
|
+
let [i, j] = [0, n];
|
|
251
|
+
while (i < j) {
|
|
252
|
+
const h = Math.trunc((i + j) / 2);
|
|
253
|
+
// i ≤ h < j
|
|
254
|
+
if (!(await f(h))) {
|
|
255
|
+
i = h + 1; // preserves f(i-1) == false
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
j = h; // preserves f(j) == true
|
|
259
|
+
}
|
|
41
260
|
}
|
|
261
|
+
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
|
|
262
|
+
return i;
|
|
42
263
|
}
|
|
43
264
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAmB,MAAM,aAAa,CAAC;AACzD,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,iBAAiB,GAIlB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAA0B,MAAM,aAAa,CAAC;AAEnE,MAAM,OAAO,cAAc;IACzB,YACS,YAA0B,EAC1B,iBAAoC,EACpC,YAA0B;QAF1B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,iBAAY,GAAZ,YAAY,CAAc;IAChC,CAAC;IAEJ,MAAM,CAAC,QAAQ,CAAC,EACd,IAAI,EACJ,GAAG,EACH,SAAS,EACT,UAAU,EACV,KAAK,GAON;QACC,MAAM,UAAU,GAAG,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC5C,MAAM,UAAU,GAAG,cAAc,CAAC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QAE7D,OAAO,IAAI,cAAc,CACvB,YAAY,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU,EAAE,UAAU,CAAC,EACxD,iBAAiB,CAAC,QAAQ,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,EAC9D,YAAY,CAAC,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC,CACzC,CAAC;IACJ,CAAC;IAED,iBAAiB,CACf,SAAkB,EAClB,MAAsB,EACtB,OAKC;QAED,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAC1B,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC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AI,kBAAkB,CAAC,gBAAgB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAC/D,CAAC;CACF;AAED,uFAAuF;AACvF,KAAK,UAAU,iBAAiB,CAC9B,CAAS,EACT,CAAkC;IAElC,0CAA0C;IAC1C,4CAA4C;IAC5C,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACpB,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;QACb,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QAClC,YAAY;QACZ,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAClB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,4BAA4B;QACzC,CAAC;aAAM,CAAC;YACN,CAAC,GAAG,CAAC,CAAC,CAAC,yBAAyB;QAClC,CAAC;IACH,CAAC;IACD,uEAAuE;IACvE,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ickb/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1000.0.26",
|
|
4
4
|
"description": "iCKB SDK built on top of CCC",
|
|
5
5
|
"author": "phroi",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,14 +27,14 @@
|
|
|
27
27
|
"eslint": "^9.25.1",
|
|
28
28
|
"prettier": "^3.5.3",
|
|
29
29
|
"typescript": "^5.8.3",
|
|
30
|
-
"typescript-eslint": "^8.31.
|
|
30
|
+
"typescript-eslint": "^8.31.1"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@ckb-ccc/core": "^1.8.0",
|
|
34
|
-
"@ickb/core": "^1000.0.
|
|
35
|
-
"@ickb/dao": "^1000.0.
|
|
36
|
-
"@ickb/order": "^1000.0.
|
|
37
|
-
"@ickb/utils": "^1000.0.
|
|
34
|
+
"@ickb/core": "^1000.0.26",
|
|
35
|
+
"@ickb/dao": "^1000.0.26",
|
|
36
|
+
"@ickb/order": "^1000.0.26",
|
|
37
|
+
"@ickb/utils": "^1000.0.24"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "pnpm run build:esm && pnpm run build:cjs && node dist/cjs/index.js",
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { arrayFrom, type ScriptDeps } from "@ickb/utils";
|
|
1
|
+
import { ccc, type Epoch } from "@ckb-ccc/core";
|
|
3
2
|
import {
|
|
3
|
+
arrayFrom,
|
|
4
|
+
epochCompare,
|
|
5
|
+
CapacityManager,
|
|
6
|
+
SmartTransaction,
|
|
7
|
+
type ScriptDeps,
|
|
8
|
+
type UdtCell,
|
|
9
|
+
type CapacityCell,
|
|
10
|
+
epochAdd,
|
|
11
|
+
type SmartTransactionLike,
|
|
12
|
+
} from "@ickb/utils";
|
|
13
|
+
import {
|
|
14
|
+
convert,
|
|
15
|
+
ICKB_DEPOSIT_CAP,
|
|
4
16
|
ickbExchangeRatio,
|
|
5
|
-
|
|
17
|
+
IckbManager,
|
|
6
18
|
LogicManager,
|
|
7
19
|
OwnedOwnerManager,
|
|
8
|
-
type
|
|
20
|
+
type IckbDepositCell,
|
|
9
21
|
type ReceiptCell,
|
|
10
22
|
type WithdrawalGroups,
|
|
11
23
|
} from "@ickb/core";
|
|
12
24
|
import { DaoManager } from "@ickb/dao";
|
|
13
|
-
import {
|
|
25
|
+
import {
|
|
26
|
+
Info,
|
|
27
|
+
OrderManager,
|
|
28
|
+
Ratio,
|
|
29
|
+
type OrderCell,
|
|
30
|
+
type OrderGroup,
|
|
31
|
+
} from "@ickb/order";
|
|
14
32
|
|
|
15
33
|
export class IckbController {
|
|
16
34
|
constructor(
|
|
35
|
+
public ickbManager: IckbManager,
|
|
17
36
|
public logicManager: LogicManager,
|
|
18
37
|
public ownedOwnerManager: OwnedOwnerManager,
|
|
19
38
|
public orderManager: OrderManager,
|
|
@@ -33,12 +52,26 @@ export class IckbController {
|
|
|
33
52
|
order: ScriptDeps;
|
|
34
53
|
}): IckbController {
|
|
35
54
|
const daoManager = DaoManager.fromDeps(dao);
|
|
36
|
-
|
|
55
|
+
|
|
56
|
+
const {
|
|
57
|
+
script: { codeHash, hashType },
|
|
58
|
+
cellDeps,
|
|
59
|
+
} = xudt;
|
|
60
|
+
const ickbXudt = {
|
|
61
|
+
script: new ccc.Script(
|
|
62
|
+
codeHash,
|
|
63
|
+
hashType,
|
|
64
|
+
[ickbLogic.script.hash(), "00000080"].join("") as ccc.Hex,
|
|
65
|
+
),
|
|
66
|
+
cellDeps,
|
|
67
|
+
};
|
|
68
|
+
const ickbManager = IckbManager.fromDeps(ickbXudt, daoManager);
|
|
37
69
|
|
|
38
70
|
return new IckbController(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
71
|
+
ickbManager,
|
|
72
|
+
LogicManager.fromDeps(ickbLogic, daoManager, ickbManager),
|
|
73
|
+
OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbManager),
|
|
74
|
+
OrderManager.fromDeps(order, ickbManager),
|
|
42
75
|
);
|
|
43
76
|
}
|
|
44
77
|
|
|
@@ -52,6 +85,8 @@ export class IckbController {
|
|
|
52
85
|
};
|
|
53
86
|
},
|
|
54
87
|
): ccc.FixedPoint {
|
|
88
|
+
// TODO: make compatible with logic.convert(isCkb2Udt, amount, options)!!!
|
|
89
|
+
|
|
55
90
|
const { ckbScale, udtScale } =
|
|
56
91
|
"ratio" in options ? options.ratio : ickbExchangeRatio(options.tip);
|
|
57
92
|
|
|
@@ -67,36 +102,378 @@ export class IckbController {
|
|
|
67
102
|
return convertedAmount;
|
|
68
103
|
}
|
|
69
104
|
|
|
70
|
-
|
|
105
|
+
// TODO streamline parameters with previewConversion
|
|
106
|
+
async convert(options: {
|
|
107
|
+
signer: ccc.Signer;
|
|
108
|
+
isCkb2Udt: boolean;
|
|
109
|
+
amount: ccc.FixedPoint;
|
|
110
|
+
userCells: UserCells;
|
|
111
|
+
minCkbChange: ccc.FixedPoint;
|
|
112
|
+
system: SystemState;
|
|
113
|
+
balance: Balance;
|
|
114
|
+
fee?: {
|
|
115
|
+
numerator: ccc.Num;
|
|
116
|
+
denominator: ccc.Num;
|
|
117
|
+
};
|
|
118
|
+
}): Promise<Conversion | undefined> {
|
|
119
|
+
const { isCkb2Udt, amount, system, signer } = options;
|
|
120
|
+
// TODO: RatioLike and specify fee!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
121
|
+
// Or let outside specify adjusted rate...
|
|
122
|
+
const info = Info.create(isCkb2Udt, Ratio.from(system.exchangeRatio));
|
|
123
|
+
const address = await signer.getRecommendedAddressObj();
|
|
124
|
+
const N = isCkb2Udt
|
|
125
|
+
? Number(amount / system.ckbDepositCap)
|
|
126
|
+
: system.deposits.slice(0, 30).length;
|
|
127
|
+
const cache = Array<ReturnType<IckbController["convert"]>>(N);
|
|
128
|
+
const attempt = (n: number): ReturnType<IckbController["convert"]> => {
|
|
129
|
+
n = N - n;
|
|
130
|
+
return (cache[n] =
|
|
131
|
+
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
132
|
+
};
|
|
133
|
+
return attempt(
|
|
134
|
+
await asyncBinarySearch(
|
|
135
|
+
N,
|
|
136
|
+
async (n: number) => (await attempt(n)) !== undefined,
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async attemptConversion(
|
|
142
|
+
n: number,
|
|
143
|
+
info: Info,
|
|
144
|
+
address: ccc.Address,
|
|
145
|
+
{
|
|
146
|
+
signer,
|
|
147
|
+
isCkb2Udt,
|
|
148
|
+
amount,
|
|
149
|
+
userCells,
|
|
150
|
+
system: { feeRate, tip, deposits, ckbDepositCap, exchangeRatio },
|
|
151
|
+
}: Parameters<IckbController["convert"]>[0],
|
|
152
|
+
): ReturnType<IckbController["convert"]> {
|
|
153
|
+
let tx: SmartTransaction = OfflineTransaction.default();
|
|
154
|
+
const maturities: Epoch[] = [];
|
|
155
|
+
let ckbFee = ccc.Zero;
|
|
156
|
+
|
|
157
|
+
if (n > 0) {
|
|
158
|
+
if (isCkb2Udt) {
|
|
159
|
+
amount -= ckbDepositCap * BigInt(n);
|
|
160
|
+
if (amount < 0n) {
|
|
161
|
+
// Too many Deposits respectfully to the amount
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, address.script);
|
|
165
|
+
} else {
|
|
166
|
+
const myDeposits = deposits.slice(0, n);
|
|
167
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
168
|
+
amount -= myDeposits.slice(-1)[0]!.udtCumulative;
|
|
169
|
+
if (amount < 0n) {
|
|
170
|
+
// Too many Withdrawal Requests respectfully to the amount
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
this.ownedOwnerManager.requestWithdrawal(
|
|
174
|
+
tx,
|
|
175
|
+
myDeposits,
|
|
176
|
+
address.script,
|
|
177
|
+
);
|
|
178
|
+
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (amount > 0n) {
|
|
183
|
+
this.orderManager.mint(
|
|
184
|
+
tx,
|
|
185
|
+
address.script,
|
|
186
|
+
info,
|
|
187
|
+
isCkb2Udt ? amount : ccc.Zero,
|
|
188
|
+
isCkb2Udt ? ccc.Zero : amount,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
192
|
+
const convertedAmount = convert(isCkb2Udt, amount, ratio);
|
|
193
|
+
ckbFee = isCkb2Udt
|
|
194
|
+
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
195
|
+
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
196
|
+
|
|
197
|
+
maturities.push(
|
|
198
|
+
// TODO: use real world data from deposits, open orders and bot balances
|
|
199
|
+
orderMaturityEstimate(isCkb2Udt, amount, tip),
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Check that order provides enough fee to the bot for being matched
|
|
204
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
205
|
+
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
210
|
+
|
|
211
|
+
this.logicManager.completeDeposit(tx, receipts);
|
|
212
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups); //TODO: ignore not isReady cells in method!!!
|
|
213
|
+
for (const order of orders) {
|
|
214
|
+
this.orderManager.melt(tx, order); //TODO: allow melting multiple groups and give choices!!!
|
|
215
|
+
}
|
|
216
|
+
// const outputLen = tx.inputs.length;??
|
|
217
|
+
// Give choice that let user add this cells the way he prefer!! (Either here or at completeFeeBy)
|
|
218
|
+
this.ickbManager.addUdts(tx, udts);
|
|
219
|
+
CapacityManager.addCapacities(tx, capacities);
|
|
220
|
+
const outputLen = tx.outputs.length;
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
await tx.completeFeeBy(signer, feeRate);
|
|
224
|
+
// TODO: check in SmartTransaction for 64 output cells in DAO transactions
|
|
225
|
+
if (tx.outputs.length > 64) {
|
|
226
|
+
return undefined;
|
|
227
|
+
}
|
|
228
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
229
|
+
} catch (_) {
|
|
230
|
+
return undefined;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Revert any change cells
|
|
234
|
+
tx.outputs = tx.outputs.slice(outputLen);
|
|
235
|
+
tx = SmartTransaction.from(tx);
|
|
236
|
+
|
|
237
|
+
const maturity = maturities.reduce((a, b) =>
|
|
238
|
+
epochCompare(a, b) === -1 ? b : a,
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
return {
|
|
242
|
+
tx,
|
|
243
|
+
ckbFee,
|
|
244
|
+
maturity,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// TODO: Allow filtering which cells are fetched
|
|
249
|
+
async getL1State(
|
|
71
250
|
client: ccc.Client,
|
|
72
251
|
locks: ccc.ScriptLike[],
|
|
73
|
-
|
|
252
|
+
options?: {
|
|
253
|
+
minLockUp?: ccc.Epoch;
|
|
254
|
+
maxLockUp?: ccc.Epoch;
|
|
255
|
+
},
|
|
256
|
+
): Promise<{
|
|
257
|
+
system: SystemState;
|
|
258
|
+
user: UserCells;
|
|
259
|
+
}> {
|
|
74
260
|
const tip = await client.getTipHeader();
|
|
75
261
|
const onChain = true;
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
262
|
+
const opts = { ...options, tip, onChain };
|
|
263
|
+
const [
|
|
264
|
+
feeRate,
|
|
265
|
+
depositPool,
|
|
266
|
+
capacities,
|
|
267
|
+
udts,
|
|
268
|
+
receipts,
|
|
269
|
+
withdrawalGroups,
|
|
270
|
+
orders,
|
|
271
|
+
] = await Promise.all([
|
|
272
|
+
// TODO: fetch user NervosDAO cells
|
|
273
|
+
// TODO: fetch bot cells for estimating timings
|
|
274
|
+
client.getFeeRate(),
|
|
275
|
+
arrayFrom(this.logicManager.findDeposits(client, opts)),
|
|
276
|
+
arrayFrom(CapacityManager.findCapacities(client, locks, opts)),
|
|
277
|
+
arrayFrom(this.ickbManager.findUdts(client, locks, opts)),
|
|
278
|
+
arrayFrom(this.logicManager.findReceipts(client, locks, opts)),
|
|
279
|
+
arrayFrom(
|
|
280
|
+
this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts),
|
|
281
|
+
),
|
|
282
|
+
this.orderManager.findOrders(client),
|
|
283
|
+
]);
|
|
80
284
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
285
|
+
// Sort depositPool by maturity
|
|
286
|
+
depositPool.sort((a, b) => epochCompare(a.maturity, b.maturity));
|
|
287
|
+
// Calculate cumulative value of deposits in the pool
|
|
288
|
+
let udtCumulative = 0n;
|
|
289
|
+
const cumulativeDepositPool: SystemState["deposits"] = [];
|
|
290
|
+
for (const deposit of depositPool) {
|
|
291
|
+
udtCumulative += deposit.udtValue;
|
|
292
|
+
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const userOrders: OrderGroup[] = [];
|
|
296
|
+
const nonUserOrders: OrderCell[] = [];
|
|
297
|
+
for (const group of orders) {
|
|
298
|
+
if (group.isOwner(...locks)) {
|
|
299
|
+
userOrders.push(group);
|
|
300
|
+
} else {
|
|
301
|
+
nonUserOrders.push(group.order);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
91
304
|
|
|
92
|
-
|
|
305
|
+
const exchangeRatio = ickbExchangeRatio(tip);
|
|
306
|
+
const ckbDepositCap = convert(false, ICKB_DEPOSIT_CAP, exchangeRatio); // TODO: Remove export from core
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
system: {
|
|
310
|
+
feeRate,
|
|
311
|
+
tip,
|
|
312
|
+
exchangeRatio,
|
|
313
|
+
ckbDepositCap,
|
|
314
|
+
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
315
|
+
deposits: cumulativeDepositPool,
|
|
316
|
+
orders: nonUserOrders,
|
|
317
|
+
},
|
|
318
|
+
user: {
|
|
319
|
+
capacities,
|
|
320
|
+
udts,
|
|
321
|
+
receipts,
|
|
322
|
+
withdrawalGroups,
|
|
323
|
+
orders: userOrders,
|
|
324
|
+
},
|
|
325
|
+
};
|
|
93
326
|
}
|
|
327
|
+
|
|
328
|
+
// TODO: Allow filtering which cells are accounted for
|
|
329
|
+
static getBalance({
|
|
330
|
+
capacities,
|
|
331
|
+
udts,
|
|
332
|
+
receipts,
|
|
333
|
+
withdrawalGroups,
|
|
334
|
+
orders,
|
|
335
|
+
}: UserCells): Balance {
|
|
336
|
+
const cells: { cell: ccc.Cell; udtValue?: ccc.FixedPoint }[] = [
|
|
337
|
+
capacities,
|
|
338
|
+
udts,
|
|
339
|
+
receipts,
|
|
340
|
+
].flat();
|
|
341
|
+
for (const { owner, owned } of withdrawalGroups) {
|
|
342
|
+
cells.push(owned, owner);
|
|
343
|
+
}
|
|
344
|
+
for (const { master, order } of orders) {
|
|
345
|
+
cells.push(master, order);
|
|
346
|
+
}
|
|
347
|
+
let ckb = ccc.Zero;
|
|
348
|
+
let udt = ccc.Zero;
|
|
349
|
+
for (const cell of cells) {
|
|
350
|
+
ckb += cell.cell.cellOutput.capacity;
|
|
351
|
+
udt += cell.udtValue ?? ccc.Zero;
|
|
352
|
+
}
|
|
353
|
+
return {
|
|
354
|
+
ckb,
|
|
355
|
+
udt,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export interface Balance {
|
|
361
|
+
ckb: ccc.FixedPoint;
|
|
362
|
+
udt: ccc.FixedPoint;
|
|
94
363
|
}
|
|
95
364
|
|
|
96
|
-
export interface
|
|
365
|
+
export interface SystemState {
|
|
366
|
+
feeRate: ccc.Num;
|
|
97
367
|
tip: ccc.ClientBlockHeader;
|
|
98
|
-
|
|
368
|
+
exchangeRatio: {
|
|
369
|
+
ckbScale: ccc.Num;
|
|
370
|
+
udtScale: ccc.Num;
|
|
371
|
+
};
|
|
372
|
+
ckbDepositCap: ccc.FixedPoint;
|
|
373
|
+
udtDepositCap: ccc.FixedPoint;
|
|
374
|
+
deposits: (IckbDepositCell & { udtCumulative: bigint })[];
|
|
375
|
+
orders: OrderCell[];
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export interface UserCells {
|
|
379
|
+
capacities: CapacityCell[];
|
|
380
|
+
udts: UdtCell[];
|
|
99
381
|
receipts: ReceiptCell[];
|
|
100
382
|
withdrawalGroups: WithdrawalGroups[];
|
|
101
383
|
orders: OrderGroup[];
|
|
102
384
|
}
|
|
385
|
+
|
|
386
|
+
export interface Conversion {
|
|
387
|
+
tx: SmartTransaction;
|
|
388
|
+
ckbFee: ccc.FixedPoint;
|
|
389
|
+
maturity: Epoch;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Estimate bot ability to fulfill orders:
|
|
393
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
394
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
395
|
+
function orderMaturityEstimate(
|
|
396
|
+
isCkb2Udt: boolean,
|
|
397
|
+
amount: bigint,
|
|
398
|
+
tip: ccc.ClientBlockHeader,
|
|
399
|
+
): ccc.Epoch {
|
|
400
|
+
return epochAdd(tip.epoch, [
|
|
401
|
+
0n,
|
|
402
|
+
1n + amount / ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
403
|
+
4n * 60n,
|
|
404
|
+
]);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
class OfflineTransaction extends SmartTransaction {
|
|
408
|
+
override clone(): OfflineTransaction {
|
|
409
|
+
const result = super.clone();
|
|
410
|
+
// Keep inputs metadata when cloning, so it will not refetch that data
|
|
411
|
+
result.inputs = [...this.inputs];
|
|
412
|
+
return new OfflineTransaction(result);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
416
|
+
override async completeInputs<T>(
|
|
417
|
+
_0: never,
|
|
418
|
+
_1: never,
|
|
419
|
+
_2: never,
|
|
420
|
+
init: T,
|
|
421
|
+
): Promise<{ addedCount: number; accumulated?: T }> {
|
|
422
|
+
// Disable completeInputs, so it will not fetch additional cells
|
|
423
|
+
return {
|
|
424
|
+
addedCount: 0,
|
|
425
|
+
accumulated: init,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Reimplement the rest of transformations
|
|
430
|
+
|
|
431
|
+
constructor(tx: SmartTransaction) {
|
|
432
|
+
super(
|
|
433
|
+
tx.version,
|
|
434
|
+
tx.cellDeps,
|
|
435
|
+
tx.headerDeps,
|
|
436
|
+
tx.inputs,
|
|
437
|
+
tx.outputs,
|
|
438
|
+
tx.outputsData,
|
|
439
|
+
tx.witnesses,
|
|
440
|
+
tx.udtHandlers,
|
|
441
|
+
tx.headers,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
static override default(): OfflineTransaction {
|
|
446
|
+
return new OfflineTransaction(super.default());
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
static override fromLumosSkeleton(
|
|
450
|
+
skeleton: ccc.LumosTransactionSkeletonType,
|
|
451
|
+
): OfflineTransaction {
|
|
452
|
+
return new OfflineTransaction(super.fromLumosSkeleton(skeleton));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
static override from(txLike: SmartTransactionLike): OfflineTransaction {
|
|
456
|
+
return new OfflineTransaction(SmartTransaction.from(txLike));
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// TODO: utils.binarySearch is sync, need an async one for await the result!!!!!!!!!!!!
|
|
461
|
+
async function asyncBinarySearch(
|
|
462
|
+
n: number,
|
|
463
|
+
f: (i: number) => Promise<boolean>,
|
|
464
|
+
): Promise<number> {
|
|
465
|
+
// Define f(-1) == false and f(n) == true.
|
|
466
|
+
// Invariant: f(i-1) == false, f(j) == true.
|
|
467
|
+
let [i, j] = [0, n];
|
|
468
|
+
while (i < j) {
|
|
469
|
+
const h = Math.trunc((i + j) / 2);
|
|
470
|
+
// i ≤ h < j
|
|
471
|
+
if (!(await f(h))) {
|
|
472
|
+
i = h + 1; // preserves f(i-1) == false
|
|
473
|
+
} else {
|
|
474
|
+
j = h; // preserves f(j) == true
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
// i == j, f(i-1) == false, and f(j) (= f(i)) == true => answer is i.
|
|
478
|
+
return i;
|
|
479
|
+
}
|