@ickb/sdk 0.0.0 → 1000.0.40
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 +60 -17
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +188 -22
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +60 -17
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +190 -24
- package/dist/esm/index.js.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +343 -46
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { type ScriptDeps } from "@ickb/utils";
|
|
3
|
-
import { LogicManager, OwnedOwnerManager, type
|
|
4
|
-
import {
|
|
1
|
+
import { ccc, type Epoch } from "@ckb-ccc/core";
|
|
2
|
+
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell } from "@ickb/utils";
|
|
3
|
+
import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroups } from "@ickb/core";
|
|
4
|
+
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
5
|
+
import { OrderManager, type OrderCell, type OrderGroup, type RatioLike } from "@ickb/order";
|
|
5
6
|
export declare class IckbController {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
readonly ickbUdtManager: IckbUdtManager;
|
|
8
|
+
readonly logicManager: LogicManager;
|
|
9
|
+
readonly ownedOwnerManager: OwnedOwnerManager;
|
|
10
|
+
readonly orderManager: OrderManager;
|
|
11
|
+
readonly daoManager: DaoManager;
|
|
12
|
+
constructor(ickbUdtManager: IckbUdtManager, logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager, daoManager: DaoManager);
|
|
10
13
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
11
14
|
xudt: ScriptDeps;
|
|
12
15
|
dao: ScriptDeps;
|
|
@@ -14,23 +17,63 @@ export declare class IckbController {
|
|
|
14
17
|
ownedOwner: ScriptDeps;
|
|
15
18
|
order: ScriptDeps;
|
|
16
19
|
}): IckbController;
|
|
17
|
-
previewConversion(isCkb2Udt: boolean, amount: ccc.FixedPoint,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
previewConversion(isCkb2Udt: boolean, amount: ccc.FixedPoint, rate: RatioLike | ccc.ClientBlockHeader, fee?: {
|
|
21
|
+
numerator: ccc.Num;
|
|
22
|
+
denominator: ccc.Num;
|
|
23
|
+
}): ccc.FixedPoint;
|
|
24
|
+
convert(options: {
|
|
25
|
+
signer: ccc.Signer;
|
|
26
|
+
isCkb2Udt: boolean;
|
|
27
|
+
amount: ccc.FixedPoint;
|
|
28
|
+
userCells: UserCells;
|
|
29
|
+
minCkbChange: ccc.FixedPoint;
|
|
30
|
+
system: SystemState;
|
|
31
|
+
balance: Balance;
|
|
22
32
|
fee?: {
|
|
23
33
|
numerator: ccc.Num;
|
|
24
34
|
denominator: ccc.Num;
|
|
25
35
|
};
|
|
26
|
-
}):
|
|
27
|
-
|
|
36
|
+
}): Promise<Conversion | undefined>;
|
|
37
|
+
private attemptConversion;
|
|
38
|
+
getL1State(client: ccc.Client, locks: ccc.Script[], options?: {
|
|
39
|
+
minLockUp?: ccc.Epoch;
|
|
40
|
+
maxLockUp?: ccc.Epoch;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
system: SystemState;
|
|
43
|
+
user: UserCells;
|
|
44
|
+
}>;
|
|
45
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
46
|
+
}
|
|
47
|
+
export interface Balance {
|
|
48
|
+
ckb: ccc.FixedPoint;
|
|
49
|
+
udt: ccc.FixedPoint;
|
|
28
50
|
}
|
|
29
|
-
export interface
|
|
51
|
+
export interface SystemState {
|
|
52
|
+
feeRate: ccc.Num;
|
|
30
53
|
tip: ccc.ClientBlockHeader;
|
|
31
|
-
|
|
54
|
+
exchangeRatio: {
|
|
55
|
+
ckbScale: ccc.Num;
|
|
56
|
+
udtScale: ccc.Num;
|
|
57
|
+
};
|
|
58
|
+
ckbDepositCap: ccc.FixedPoint;
|
|
59
|
+
udtDepositCap: ccc.FixedPoint;
|
|
60
|
+
deposits: (IckbDepositCell & {
|
|
61
|
+
udtCumulative: bigint;
|
|
62
|
+
})[];
|
|
63
|
+
orders: OrderCell[];
|
|
64
|
+
}
|
|
65
|
+
export interface UserCells {
|
|
66
|
+
capacities: CapacityCell[];
|
|
67
|
+
udts: UdtCell[];
|
|
32
68
|
receipts: ReceiptCell[];
|
|
33
69
|
withdrawalGroups: WithdrawalGroups[];
|
|
34
70
|
orders: OrderGroup[];
|
|
71
|
+
deposits: DaoCell[];
|
|
72
|
+
withdrawalRequests: DaoCell[];
|
|
73
|
+
}
|
|
74
|
+
export interface Conversion {
|
|
75
|
+
tx: SmartTransaction;
|
|
76
|
+
ckbFee: ccc.FixedPoint;
|
|
77
|
+
maturity: Epoch;
|
|
35
78
|
}
|
|
36
79
|
//# 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,EAIlB,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,cAAc,EACd,YAAY,EACZ,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,Y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;IACxB,UAAU,EAAE,YAAY,EAAE,CAAC;IAC3B,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,gBAAgB,EAAE,gBAAgB,EAAE,CAAC;IACrC,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,kBAAkB,EAAE,OAAO,EAAE,CAAC;CAC/B;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,EAAE,gBAAgB,CAAC;IACrB,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC;IACvB,QAAQ,EAAE,KAAK,CAAC;CACjB"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,47 +1,213 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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(ickbUdtManager, logicManager, ownedOwnerManager, orderManager, daoManager) {
|
|
11
|
+
this.ickbUdtManager = ickbUdtManager;
|
|
10
12
|
this.logicManager = logicManager;
|
|
11
13
|
this.ownedOwnerManager = ownedOwnerManager;
|
|
12
14
|
this.orderManager = orderManager;
|
|
15
|
+
this.daoManager = daoManager;
|
|
13
16
|
}
|
|
14
17
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
15
18
|
const daoManager = dao_1.DaoManager.fromDeps(dao);
|
|
16
|
-
const
|
|
17
|
-
|
|
19
|
+
const { script: { codeHash, hashType }, cellDeps, } = xudt;
|
|
20
|
+
const ickbXudt = {
|
|
21
|
+
script: new core_1.ccc.Script(codeHash, hashType, [ickbLogic.script.hash(), "00000080"].join("")),
|
|
22
|
+
cellDeps,
|
|
23
|
+
};
|
|
24
|
+
const ickbUdtManager = core_2.IckbUdtManager.fromDeps(ickbXudt, daoManager);
|
|
25
|
+
return new IckbController(ickbUdtManager, core_2.LogicManager.fromDeps(ickbLogic, daoManager, ickbUdtManager), core_2.OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbUdtManager), order_1.OrderManager.fromDeps(order, ickbUdtManager), daoManager);
|
|
18
26
|
}
|
|
19
|
-
previewConversion(isCkb2Udt, amount,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
: (amount * udtScale) / ckbScale;
|
|
24
|
-
if (options.fee) {
|
|
25
|
-
const { numerator, denominator } = options.fee;
|
|
27
|
+
previewConversion(isCkb2Udt, amount, rate, fee) {
|
|
28
|
+
let convertedAmount = (0, core_2.convert)(isCkb2Udt, amount, rate);
|
|
29
|
+
if (fee) {
|
|
30
|
+
const { numerator, denominator } = fee;
|
|
26
31
|
convertedAmount -= (convertedAmount * numerator) / denominator;
|
|
27
32
|
}
|
|
28
33
|
return convertedAmount;
|
|
29
34
|
}
|
|
30
|
-
|
|
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 = order_1.Info.create(isCkb2Udt, order_1.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 (0, utils_1.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
|
+
try {
|
|
55
|
+
const tx = utils_1.SmartTransaction.default();
|
|
56
|
+
const maturities = [];
|
|
57
|
+
let ckbFee = core_1.ccc.Zero;
|
|
58
|
+
if (n > 0) {
|
|
59
|
+
if (isCkb2Udt) {
|
|
60
|
+
amount -= ckbDepositCap * BigInt(n);
|
|
61
|
+
if (amount < 0n) {
|
|
62
|
+
// Too many Deposits respectfully to the amount
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, address.script);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const myDeposits = deposits.slice(0, n);
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
70
|
+
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
71
|
+
if (amount < 0n) {
|
|
72
|
+
// Too many Withdrawal Requests respectfully to the amount
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, address.script);
|
|
76
|
+
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (amount > 0n) {
|
|
80
|
+
this.orderManager.mint(tx, address.script, info, isCkb2Udt ? amount : core_1.ccc.Zero, isCkb2Udt ? core_1.ccc.Zero : amount);
|
|
81
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
82
|
+
const convertedAmount = (0, core_2.convert)(isCkb2Udt, amount, ratio);
|
|
83
|
+
ckbFee = isCkb2Udt
|
|
84
|
+
? amount - (0, core_2.convert)(false, convertedAmount, exchangeRatio)
|
|
85
|
+
: (0, core_2.convert)(true, amount, exchangeRatio) - convertedAmount;
|
|
86
|
+
maturities.push(
|
|
87
|
+
// TODO: use real world data from deposits, open orders and bot balances
|
|
88
|
+
orderMaturityEstimate(isCkb2Udt, amount, tip));
|
|
89
|
+
}
|
|
90
|
+
// Check that order provides enough fee to the bot for being matched
|
|
91
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
92
|
+
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
96
|
+
this.logicManager.completeDeposit(tx, receipts);
|
|
97
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, {
|
|
98
|
+
isReadyOnly: true,
|
|
99
|
+
});
|
|
100
|
+
this.orderManager.melt(tx, orders);
|
|
101
|
+
this.ickbUdtManager.addUdts(tx, udts);
|
|
102
|
+
utils_1.CapacityManager.addCapacities(tx, capacities);
|
|
103
|
+
// Throw an error if inputs do not cover for outputs
|
|
104
|
+
await new utils_1.RestrictedTransaction(tx).completeFeeBy(signer, feeRate);
|
|
105
|
+
// Find the maturity most distant in the future
|
|
106
|
+
const maturity = maturities.reduce((a, b) => (0, utils_1.epochCompare)(a, b) === -1 ? b : a);
|
|
107
|
+
return {
|
|
108
|
+
tx,
|
|
109
|
+
ckbFee,
|
|
110
|
+
maturity,
|
|
111
|
+
};
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
113
|
+
}
|
|
114
|
+
catch (_) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// TODO: Allow filtering which cells are fetched
|
|
119
|
+
async getL1State(client, locks, options) {
|
|
31
120
|
const tip = await client.getTipHeader();
|
|
32
121
|
const onChain = true;
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
(0, utils_1.
|
|
39
|
-
(0, utils_1.
|
|
40
|
-
(0, utils_1.
|
|
41
|
-
this.orderManager.findOrders(client),
|
|
122
|
+
const opts = { ...options, tip, onChain };
|
|
123
|
+
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests,] = await Promise.all([
|
|
124
|
+
client.getFeeRate(),
|
|
125
|
+
(0, utils_1.collect)(this.logicManager.findDeposits(client, opts)),
|
|
126
|
+
(0, utils_1.collect)(utils_1.CapacityManager.findCapacities(client, locks, opts)),
|
|
127
|
+
(0, utils_1.collect)(this.ickbUdtManager.findUdts(client, locks, opts)),
|
|
128
|
+
(0, utils_1.collect)(this.logicManager.findReceipts(client, locks, opts)),
|
|
129
|
+
(0, utils_1.collect)(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts)),
|
|
130
|
+
(0, utils_1.collect)(this.orderManager.findOrders(client)),
|
|
131
|
+
(0, utils_1.collect)(this.daoManager.findDeposits(client, locks, opts)),
|
|
132
|
+
(0, utils_1.collect)(this.daoManager.findWithdrawalRequests(client, locks, opts)),
|
|
133
|
+
// TODO: fetch bot cells for estimating timings
|
|
42
134
|
]);
|
|
43
|
-
|
|
135
|
+
// Sort depositPool by maturity
|
|
136
|
+
depositPool.sort((a, b) => (0, utils_1.epochCompare)(a.maturity, b.maturity));
|
|
137
|
+
// Calculate cumulative value of deposits in the pool
|
|
138
|
+
let udtCumulative = 0n;
|
|
139
|
+
const cumulativeDepositPool = [];
|
|
140
|
+
for (const deposit of depositPool) {
|
|
141
|
+
udtCumulative += deposit.udtValue;
|
|
142
|
+
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
143
|
+
}
|
|
144
|
+
const userOrders = [];
|
|
145
|
+
const nonUserOrders = [];
|
|
146
|
+
for (const group of orders) {
|
|
147
|
+
if (group.isOwner(...locks)) {
|
|
148
|
+
userOrders.push(group);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
nonUserOrders.push(group.order);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const exchangeRatio = (0, core_2.ickbExchangeRatio)(tip);
|
|
155
|
+
const ckbDepositCap = (0, core_2.convert)(false, core_2.ICKB_DEPOSIT_CAP, exchangeRatio);
|
|
156
|
+
return {
|
|
157
|
+
system: {
|
|
158
|
+
feeRate,
|
|
159
|
+
tip,
|
|
160
|
+
exchangeRatio,
|
|
161
|
+
ckbDepositCap,
|
|
162
|
+
udtDepositCap: core_2.ICKB_DEPOSIT_CAP,
|
|
163
|
+
deposits: cumulativeDepositPool,
|
|
164
|
+
orders: nonUserOrders,
|
|
165
|
+
},
|
|
166
|
+
user: {
|
|
167
|
+
capacities,
|
|
168
|
+
udts,
|
|
169
|
+
receipts,
|
|
170
|
+
withdrawalGroups,
|
|
171
|
+
orders: userOrders,
|
|
172
|
+
deposits,
|
|
173
|
+
withdrawalRequests,
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
// TODO: Allow filtering which cells are accounted for
|
|
178
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }) {
|
|
179
|
+
const cells = [
|
|
180
|
+
capacities,
|
|
181
|
+
udts,
|
|
182
|
+
receipts,
|
|
183
|
+
].flat();
|
|
184
|
+
for (const { owner, owned } of withdrawalGroups) {
|
|
185
|
+
cells.push(owned, owner);
|
|
186
|
+
}
|
|
187
|
+
for (const { master, order } of orders) {
|
|
188
|
+
cells.push(master, order);
|
|
189
|
+
}
|
|
190
|
+
let ckb = core_1.ccc.Zero;
|
|
191
|
+
let udt = core_1.ccc.Zero;
|
|
192
|
+
for (const cell of cells) {
|
|
193
|
+
ckb += cell.cell.cellOutput.capacity;
|
|
194
|
+
udt += cell.udtValue ?? core_1.ccc.Zero;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
ckb,
|
|
198
|
+
udt,
|
|
199
|
+
};
|
|
44
200
|
}
|
|
45
201
|
}
|
|
46
202
|
exports.IckbController = IckbController;
|
|
203
|
+
// Estimate bot ability to fulfill orders:
|
|
204
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
205
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
206
|
+
function orderMaturityEstimate(isCkb2Udt, amount, tip) {
|
|
207
|
+
return (0, utils_1.epochAdd)(tip.epoch, [
|
|
208
|
+
0n,
|
|
209
|
+
1n + amount / core_1.ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
210
|
+
4n * 60n,
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
47
213
|
//# 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,uCAWqB;AACrB,qCAUoB;AACpB,mCAAqD;AACrD,uCAOqB;AAErB,MAAa,cAAc;IACzB,YACkB,cAA8B,EAC9B,YAA0B,EAC1B,iBAAoC,EACpC,YAA0B,EAC1B,UAAsB;QAJtB,mBAAc,GAAd,cAAc,CAAgB;QAC9B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,iBAAY,GAAZ,YAAY,CAAc;QAC1B,eAAU,GAAV,UAAU,CAAY;IACrC,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,I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;IAE1B,OAAO,IAAA,gBAAQ,EAAC,GAAG,CAAC,KAAK,EAAE;QACzB,EAAE;QACF,EAAE,GAAG,MAAM,GAAG,UAAG,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC;QAC1D,EAAE,GAAG,GAAG;KACT,CAAC,CAAC;AACL,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { type ScriptDeps } from "@ickb/utils";
|
|
3
|
-
import { LogicManager, OwnedOwnerManager, type
|
|
4
|
-
import {
|
|
1
|
+
import { ccc, type Epoch } from "@ckb-ccc/core";
|
|
2
|
+
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell } from "@ickb/utils";
|
|
3
|
+
import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroups } from "@ickb/core";
|
|
4
|
+
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
5
|
+
import { OrderManager, type OrderCell, type OrderGroup, type RatioLike } from "@ickb/order";
|
|
5
6
|
export declare class IckbController {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
readonly ickbUdtManager: IckbUdtManager;
|
|
8
|
+
readonly logicManager: LogicManager;
|
|
9
|
+
readonly ownedOwnerManager: OwnedOwnerManager;
|
|
10
|
+
readonly orderManager: OrderManager;
|
|
11
|
+
readonly daoManager: DaoManager;
|
|
12
|
+
constructor(ickbUdtManager: IckbUdtManager, logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager, daoManager: DaoManager);
|
|
10
13
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
11
14
|
xudt: ScriptDeps;
|
|
12
15
|
dao: ScriptDeps;
|
|
@@ -14,23 +17,63 @@ export declare class IckbController {
|
|
|
14
17
|
ownedOwner: ScriptDeps;
|
|
15
18
|
order: ScriptDeps;
|
|
16
19
|
}): IckbController;
|
|
17
|
-
previewConversion(isCkb2Udt: boolean, amount: ccc.FixedPoint,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
previewConversion(isCkb2Udt: boolean, amount: ccc.FixedPoint, rate: RatioLike | ccc.ClientBlockHeader, fee?: {
|
|
21
|
+
numerator: ccc.Num;
|
|
22
|
+
denominator: ccc.Num;
|
|
23
|
+
}): ccc.FixedPoint;
|
|
24
|
+
convert(options: {
|
|
25
|
+
signer: ccc.Signer;
|
|
26
|
+
isCkb2Udt: boolean;
|
|
27
|
+
amount: ccc.FixedPoint;
|
|
28
|
+
userCells: UserCells;
|
|
29
|
+
minCkbChange: ccc.FixedPoint;
|
|
30
|
+
system: SystemState;
|
|
31
|
+
balance: Balance;
|
|
22
32
|
fee?: {
|
|
23
33
|
numerator: ccc.Num;
|
|
24
34
|
denominator: ccc.Num;
|
|
25
35
|
};
|
|
26
|
-
}):
|
|
27
|
-
|
|
36
|
+
}): Promise<Conversion | undefined>;
|
|
37
|
+
private attemptConversion;
|
|
38
|
+
getL1State(client: ccc.Client, locks: ccc.Script[], options?: {
|
|
39
|
+
minLockUp?: ccc.Epoch;
|
|
40
|
+
maxLockUp?: ccc.Epoch;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
system: SystemState;
|
|
43
|
+
user: UserCells;
|
|
44
|
+
}>;
|
|
45
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
46
|
+
}
|
|
47
|
+
export interface Balance {
|
|
48
|
+
ckb: ccc.FixedPoint;
|
|
49
|
+
udt: ccc.FixedPoint;
|
|
28
50
|
}
|
|
29
|
-
export interface
|
|
51
|
+
export interface SystemState {
|
|
52
|
+
feeRate: ccc.Num;
|
|
30
53
|
tip: ccc.ClientBlockHeader;
|
|
31
|
-
|
|
54
|
+
exchangeRatio: {
|
|
55
|
+
ckbScale: ccc.Num;
|
|
56
|
+
udtScale: ccc.Num;
|
|
57
|
+
};
|
|
58
|
+
ckbDepositCap: ccc.FixedPoint;
|
|
59
|
+
udtDepositCap: ccc.FixedPoint;
|
|
60
|
+
deposits: (IckbDepositCell & {
|
|
61
|
+
udtCumulative: bigint;
|
|
62
|
+
})[];
|
|
63
|
+
orders: OrderCell[];
|
|
64
|
+
}
|
|
65
|
+
export interface UserCells {
|
|
66
|
+
capacities: CapacityCell[];
|
|
67
|
+
udts: UdtCell[];
|
|
32
68
|
receipts: ReceiptCell[];
|
|
33
69
|
withdrawalGroups: WithdrawalGroups[];
|
|
34
70
|
orders: OrderGroup[];
|
|
71
|
+
deposits: DaoCell[];
|
|
72
|
+
withdrawalRequests: DaoCell[];
|
|
73
|
+
}
|
|
74
|
+
export interface Conversion {
|
|
75
|
+
tx: SmartTransaction;
|
|
76
|
+
ckbFee: ccc.FixedPoint;
|
|
77
|
+
maturity: Epoch;
|
|
35
78
|
}
|
|
36
79
|
//# 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,EAIlB,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,cAAc,EACd,YAAY,EACZ,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAE,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AACrD,OAAO,EAEL,YAAY,EAEZ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AAC;IACxB,gBAAgB,EAAE,gBAAgB,EAAE,CAAC;IACrC,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,QAAQ,EAAE,OAAO,EAAE,CAAC;IACpB,kBAAkB,EAAE,OAAO,EAAE,CAAC;CAC/B;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,43 +1,209 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { collect, epochCompare, CapacityManager, SmartTransaction, epochAdd, asyncBinarySearch, RestrictedTransaction, } from "@ickb/utils";
|
|
3
|
+
import { convert, ICKB_DEPOSIT_CAP, ickbExchangeRatio, IckbUdtManager, 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(ickbUdtManager, logicManager, ownedOwnerManager, orderManager, daoManager) {
|
|
8
|
+
this.ickbUdtManager = ickbUdtManager;
|
|
7
9
|
this.logicManager = logicManager;
|
|
8
10
|
this.ownedOwnerManager = ownedOwnerManager;
|
|
9
11
|
this.orderManager = orderManager;
|
|
12
|
+
this.daoManager = daoManager;
|
|
10
13
|
}
|
|
11
14
|
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
12
15
|
const daoManager = DaoManager.fromDeps(dao);
|
|
13
|
-
const
|
|
14
|
-
|
|
16
|
+
const { script: { codeHash, hashType }, cellDeps, } = xudt;
|
|
17
|
+
const ickbXudt = {
|
|
18
|
+
script: new ccc.Script(codeHash, hashType, [ickbLogic.script.hash(), "00000080"].join("")),
|
|
19
|
+
cellDeps,
|
|
20
|
+
};
|
|
21
|
+
const ickbUdtManager = IckbUdtManager.fromDeps(ickbXudt, daoManager);
|
|
22
|
+
return new IckbController(ickbUdtManager, LogicManager.fromDeps(ickbLogic, daoManager, ickbUdtManager), OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbUdtManager), OrderManager.fromDeps(order, ickbUdtManager), daoManager);
|
|
15
23
|
}
|
|
16
|
-
previewConversion(isCkb2Udt, amount,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
: (amount * udtScale) / ckbScale;
|
|
21
|
-
if (options.fee) {
|
|
22
|
-
const { numerator, denominator } = options.fee;
|
|
24
|
+
previewConversion(isCkb2Udt, amount, rate, fee) {
|
|
25
|
+
let convertedAmount = convert(isCkb2Udt, amount, rate);
|
|
26
|
+
if (fee) {
|
|
27
|
+
const { numerator, denominator } = fee;
|
|
23
28
|
convertedAmount -= (convertedAmount * numerator) / denominator;
|
|
24
29
|
}
|
|
25
30
|
return convertedAmount;
|
|
26
31
|
}
|
|
27
|
-
|
|
32
|
+
// TODO streamline parameters with previewConversion
|
|
33
|
+
async convert(options) {
|
|
34
|
+
const { isCkb2Udt, amount, system, signer } = options;
|
|
35
|
+
// TODO: RatioLike and specify fee!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
36
|
+
// Or let outside specify adjusted rate...
|
|
37
|
+
const info = Info.create(isCkb2Udt, Ratio.from(system.exchangeRatio));
|
|
38
|
+
const address = await signer.getRecommendedAddressObj();
|
|
39
|
+
const N = isCkb2Udt
|
|
40
|
+
? Number(amount / system.ckbDepositCap)
|
|
41
|
+
: system.deposits.slice(0, 30).length;
|
|
42
|
+
const cache = Array(N);
|
|
43
|
+
const attempt = (n) => {
|
|
44
|
+
n = N - n;
|
|
45
|
+
return (cache[n] =
|
|
46
|
+
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
47
|
+
};
|
|
48
|
+
return attempt(await asyncBinarySearch(N, async (n) => (await attempt(n)) !== undefined));
|
|
49
|
+
}
|
|
50
|
+
async attemptConversion(n, info, address, { signer, isCkb2Udt, amount, userCells, system: { feeRate, tip, deposits, ckbDepositCap, exchangeRatio }, }) {
|
|
51
|
+
try {
|
|
52
|
+
const tx = SmartTransaction.default();
|
|
53
|
+
const maturities = [];
|
|
54
|
+
let ckbFee = ccc.Zero;
|
|
55
|
+
if (n > 0) {
|
|
56
|
+
if (isCkb2Udt) {
|
|
57
|
+
amount -= ckbDepositCap * BigInt(n);
|
|
58
|
+
if (amount < 0n) {
|
|
59
|
+
// Too many Deposits respectfully to the amount
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, address.script);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const myDeposits = deposits.slice(0, n);
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
67
|
+
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
68
|
+
if (amount < 0n) {
|
|
69
|
+
// Too many Withdrawal Requests respectfully to the amount
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, address.script);
|
|
73
|
+
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (amount > 0n) {
|
|
77
|
+
this.orderManager.mint(tx, address.script, info, isCkb2Udt ? amount : ccc.Zero, isCkb2Udt ? ccc.Zero : amount);
|
|
78
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
79
|
+
const convertedAmount = convert(isCkb2Udt, amount, ratio);
|
|
80
|
+
ckbFee = isCkb2Udt
|
|
81
|
+
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
82
|
+
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
83
|
+
maturities.push(
|
|
84
|
+
// TODO: use real world data from deposits, open orders and bot balances
|
|
85
|
+
orderMaturityEstimate(isCkb2Udt, amount, tip));
|
|
86
|
+
}
|
|
87
|
+
// Check that order provides enough fee to the bot for being matched
|
|
88
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
89
|
+
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
93
|
+
this.logicManager.completeDeposit(tx, receipts);
|
|
94
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, {
|
|
95
|
+
isReadyOnly: true,
|
|
96
|
+
});
|
|
97
|
+
this.orderManager.melt(tx, orders);
|
|
98
|
+
this.ickbUdtManager.addUdts(tx, udts);
|
|
99
|
+
CapacityManager.addCapacities(tx, capacities);
|
|
100
|
+
// Throw an error if inputs do not cover for outputs
|
|
101
|
+
await new RestrictedTransaction(tx).completeFeeBy(signer, feeRate);
|
|
102
|
+
// Find the maturity most distant in the future
|
|
103
|
+
const maturity = maturities.reduce((a, b) => epochCompare(a, b) === -1 ? b : a);
|
|
104
|
+
return {
|
|
105
|
+
tx,
|
|
106
|
+
ckbFee,
|
|
107
|
+
maturity,
|
|
108
|
+
};
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
110
|
+
}
|
|
111
|
+
catch (_) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// TODO: Allow filtering which cells are fetched
|
|
116
|
+
async getL1State(client, locks, options) {
|
|
28
117
|
const tip = await client.getTipHeader();
|
|
29
118
|
const onChain = true;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.orderManager.findOrders(client),
|
|
119
|
+
const opts = { ...options, tip, onChain };
|
|
120
|
+
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests,] = await Promise.all([
|
|
121
|
+
client.getFeeRate(),
|
|
122
|
+
collect(this.logicManager.findDeposits(client, opts)),
|
|
123
|
+
collect(CapacityManager.findCapacities(client, locks, opts)),
|
|
124
|
+
collect(this.ickbUdtManager.findUdts(client, locks, opts)),
|
|
125
|
+
collect(this.logicManager.findReceipts(client, locks, opts)),
|
|
126
|
+
collect(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts)),
|
|
127
|
+
collect(this.orderManager.findOrders(client)),
|
|
128
|
+
collect(this.daoManager.findDeposits(client, locks, opts)),
|
|
129
|
+
collect(this.daoManager.findWithdrawalRequests(client, locks, opts)),
|
|
130
|
+
// TODO: fetch bot cells for estimating timings
|
|
39
131
|
]);
|
|
40
|
-
|
|
132
|
+
// Sort depositPool by maturity
|
|
133
|
+
depositPool.sort((a, b) => epochCompare(a.maturity, b.maturity));
|
|
134
|
+
// Calculate cumulative value of deposits in the pool
|
|
135
|
+
let udtCumulative = 0n;
|
|
136
|
+
const cumulativeDepositPool = [];
|
|
137
|
+
for (const deposit of depositPool) {
|
|
138
|
+
udtCumulative += deposit.udtValue;
|
|
139
|
+
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
140
|
+
}
|
|
141
|
+
const userOrders = [];
|
|
142
|
+
const nonUserOrders = [];
|
|
143
|
+
for (const group of orders) {
|
|
144
|
+
if (group.isOwner(...locks)) {
|
|
145
|
+
userOrders.push(group);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
nonUserOrders.push(group.order);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const exchangeRatio = ickbExchangeRatio(tip);
|
|
152
|
+
const ckbDepositCap = convert(false, ICKB_DEPOSIT_CAP, exchangeRatio);
|
|
153
|
+
return {
|
|
154
|
+
system: {
|
|
155
|
+
feeRate,
|
|
156
|
+
tip,
|
|
157
|
+
exchangeRatio,
|
|
158
|
+
ckbDepositCap,
|
|
159
|
+
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
160
|
+
deposits: cumulativeDepositPool,
|
|
161
|
+
orders: nonUserOrders,
|
|
162
|
+
},
|
|
163
|
+
user: {
|
|
164
|
+
capacities,
|
|
165
|
+
udts,
|
|
166
|
+
receipts,
|
|
167
|
+
withdrawalGroups,
|
|
168
|
+
orders: userOrders,
|
|
169
|
+
deposits,
|
|
170
|
+
withdrawalRequests,
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
// TODO: Allow filtering which cells are accounted for
|
|
175
|
+
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }) {
|
|
176
|
+
const cells = [
|
|
177
|
+
capacities,
|
|
178
|
+
udts,
|
|
179
|
+
receipts,
|
|
180
|
+
].flat();
|
|
181
|
+
for (const { owner, owned } of withdrawalGroups) {
|
|
182
|
+
cells.push(owned, owner);
|
|
183
|
+
}
|
|
184
|
+
for (const { master, order } of orders) {
|
|
185
|
+
cells.push(master, order);
|
|
186
|
+
}
|
|
187
|
+
let ckb = ccc.Zero;
|
|
188
|
+
let udt = ccc.Zero;
|
|
189
|
+
for (const cell of cells) {
|
|
190
|
+
ckb += cell.cell.cellOutput.capacity;
|
|
191
|
+
udt += cell.udtValue ?? ccc.Zero;
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
ckb,
|
|
195
|
+
udt,
|
|
196
|
+
};
|
|
41
197
|
}
|
|
42
198
|
}
|
|
199
|
+
// Estimate bot ability to fulfill orders:
|
|
200
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
201
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
202
|
+
function orderMaturityEstimate(isCkb2Udt, amount, tip) {
|
|
203
|
+
return epochAdd(tip.epoch, [
|
|
204
|
+
0n,
|
|
205
|
+
1n + amount / ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
206
|
+
4n * 60n,
|
|
207
|
+
]);
|
|
208
|
+
}
|
|
43
209
|
//# 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":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAc,MAAM,eAAe,CAAC;AAChD,OAAO,EACL,OAAO,EACP,YAAY,EACZ,eAAe,EACf,gBAAgB,EAIhB,QAAQ,EACR,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,OAAO,EACP,gBAAgB,EAChB,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,iBAAiB,GAIlB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EAAgB,MAAM,WAAW,CAAC;AACrD,OAAO,EACL,IAAI,EACJ,YAAY,EACZ,KAAK,GAIN,MAAM,aAAa,CAAC;AAErB,MAAM,OAAO,cAAc;IACzB,YACkB,cAA8B,EAC9B,YAA0B,EAC1B,iBAAoC,EACpC,YAA0B,EAC1B,UAAsB;QAJtB,mBAAc,GAAd,cAAc,CAAgB;QAC9B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,iBAAY,GAAZ,YAAY,CAAc;QAC1B,eAAU,GAAV,UAAU,CAAY;IACrC,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;QAE5C,MAAM,EACJ,MAAM,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAC9B,QAAQ,GACT,GAAG,IAAI,CAAC;QACT,MAAM,QAAQ,GAAG;YACf,MAAM,EAAE,IAAI,GAAG,CAAC,MAAM,CACpB,QAA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CAAC,GAAG,CAAC;QAC1D,EAAE,GAAG,GAAG;KACT,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ickb/sdk",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1000.0.40",
|
|
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.39",
|
|
35
|
+
"@ickb/dao": "^1000.0.35",
|
|
36
|
+
"@ickb/order": "^1000.0.36",
|
|
37
|
+
"@ickb/utils": "^1000.0.40"
|
|
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,22 +1,44 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { arrayFrom, type ScriptDeps } from "@ickb/utils";
|
|
1
|
+
import { ccc, type Epoch } from "@ckb-ccc/core";
|
|
3
2
|
import {
|
|
3
|
+
collect,
|
|
4
|
+
epochCompare,
|
|
5
|
+
CapacityManager,
|
|
6
|
+
SmartTransaction,
|
|
7
|
+
type ScriptDeps,
|
|
8
|
+
type UdtCell,
|
|
9
|
+
type CapacityCell,
|
|
10
|
+
epochAdd,
|
|
11
|
+
asyncBinarySearch,
|
|
12
|
+
RestrictedTransaction,
|
|
13
|
+
} from "@ickb/utils";
|
|
14
|
+
import {
|
|
15
|
+
convert,
|
|
16
|
+
ICKB_DEPOSIT_CAP,
|
|
4
17
|
ickbExchangeRatio,
|
|
5
|
-
|
|
18
|
+
IckbUdtManager,
|
|
6
19
|
LogicManager,
|
|
7
20
|
OwnedOwnerManager,
|
|
8
|
-
type
|
|
21
|
+
type IckbDepositCell,
|
|
9
22
|
type ReceiptCell,
|
|
10
23
|
type WithdrawalGroups,
|
|
11
24
|
} from "@ickb/core";
|
|
12
|
-
import { DaoManager } from "@ickb/dao";
|
|
13
|
-
import {
|
|
25
|
+
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
26
|
+
import {
|
|
27
|
+
Info,
|
|
28
|
+
OrderManager,
|
|
29
|
+
Ratio,
|
|
30
|
+
type OrderCell,
|
|
31
|
+
type OrderGroup,
|
|
32
|
+
type RatioLike,
|
|
33
|
+
} from "@ickb/order";
|
|
14
34
|
|
|
15
35
|
export class IckbController {
|
|
16
36
|
constructor(
|
|
17
|
-
public
|
|
18
|
-
public
|
|
19
|
-
public
|
|
37
|
+
public readonly ickbUdtManager: IckbUdtManager,
|
|
38
|
+
public readonly logicManager: LogicManager,
|
|
39
|
+
public readonly ownedOwnerManager: OwnedOwnerManager,
|
|
40
|
+
public readonly orderManager: OrderManager,
|
|
41
|
+
public readonly daoManager: DaoManager,
|
|
20
42
|
) {}
|
|
21
43
|
|
|
22
44
|
static fromDeps({
|
|
@@ -33,70 +55,345 @@ export class IckbController {
|
|
|
33
55
|
order: ScriptDeps;
|
|
34
56
|
}): IckbController {
|
|
35
57
|
const daoManager = DaoManager.fromDeps(dao);
|
|
36
|
-
|
|
58
|
+
|
|
59
|
+
const {
|
|
60
|
+
script: { codeHash, hashType },
|
|
61
|
+
cellDeps,
|
|
62
|
+
} = xudt;
|
|
63
|
+
const ickbXudt = {
|
|
64
|
+
script: new ccc.Script(
|
|
65
|
+
codeHash,
|
|
66
|
+
hashType,
|
|
67
|
+
[ickbLogic.script.hash(), "00000080"].join("") as ccc.Hex,
|
|
68
|
+
),
|
|
69
|
+
cellDeps,
|
|
70
|
+
};
|
|
71
|
+
const ickbUdtManager = IckbUdtManager.fromDeps(ickbXudt, daoManager);
|
|
37
72
|
|
|
38
73
|
return new IckbController(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
74
|
+
ickbUdtManager,
|
|
75
|
+
LogicManager.fromDeps(ickbLogic, daoManager, ickbUdtManager),
|
|
76
|
+
OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbUdtManager),
|
|
77
|
+
OrderManager.fromDeps(order, ickbUdtManager),
|
|
78
|
+
daoManager,
|
|
42
79
|
);
|
|
43
80
|
}
|
|
44
81
|
|
|
45
82
|
previewConversion(
|
|
46
83
|
isCkb2Udt: boolean,
|
|
47
84
|
amount: ccc.FixedPoint,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
};
|
|
85
|
+
rate: RatioLike | ccc.ClientBlockHeader,
|
|
86
|
+
fee?: {
|
|
87
|
+
numerator: ccc.Num;
|
|
88
|
+
denominator: ccc.Num;
|
|
53
89
|
},
|
|
54
90
|
): ccc.FixedPoint {
|
|
55
|
-
|
|
56
|
-
"ratio" in options ? options.ratio : ickbExchangeRatio(options.tip);
|
|
57
|
-
|
|
58
|
-
let convertedAmount = isCkb2Udt
|
|
59
|
-
? (amount * ckbScale) / udtScale
|
|
60
|
-
: (amount * udtScale) / ckbScale;
|
|
91
|
+
let convertedAmount = convert(isCkb2Udt, amount, rate);
|
|
61
92
|
|
|
62
|
-
if (
|
|
63
|
-
const { numerator, denominator } =
|
|
93
|
+
if (fee) {
|
|
94
|
+
const { numerator, denominator } = fee;
|
|
64
95
|
convertedAmount -= (convertedAmount * numerator) / denominator;
|
|
65
96
|
}
|
|
66
97
|
|
|
67
98
|
return convertedAmount;
|
|
68
99
|
}
|
|
69
100
|
|
|
70
|
-
|
|
101
|
+
// TODO streamline parameters with previewConversion
|
|
102
|
+
async convert(options: {
|
|
103
|
+
signer: ccc.Signer;
|
|
104
|
+
isCkb2Udt: boolean;
|
|
105
|
+
amount: ccc.FixedPoint;
|
|
106
|
+
userCells: UserCells;
|
|
107
|
+
minCkbChange: ccc.FixedPoint;
|
|
108
|
+
system: SystemState;
|
|
109
|
+
balance: Balance;
|
|
110
|
+
fee?: {
|
|
111
|
+
numerator: ccc.Num;
|
|
112
|
+
denominator: ccc.Num;
|
|
113
|
+
};
|
|
114
|
+
}): Promise<Conversion | undefined> {
|
|
115
|
+
const { isCkb2Udt, amount, system, signer } = options;
|
|
116
|
+
// TODO: RatioLike and specify fee!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
117
|
+
// Or let outside specify adjusted rate...
|
|
118
|
+
const info = Info.create(isCkb2Udt, Ratio.from(system.exchangeRatio));
|
|
119
|
+
const address = await signer.getRecommendedAddressObj();
|
|
120
|
+
const N = isCkb2Udt
|
|
121
|
+
? Number(amount / system.ckbDepositCap)
|
|
122
|
+
: system.deposits.slice(0, 30).length;
|
|
123
|
+
const cache = Array<ReturnType<IckbController["convert"]>>(N);
|
|
124
|
+
const attempt = (n: number): ReturnType<IckbController["convert"]> => {
|
|
125
|
+
n = N - n;
|
|
126
|
+
return (cache[n] =
|
|
127
|
+
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
128
|
+
};
|
|
129
|
+
return attempt(
|
|
130
|
+
await asyncBinarySearch(
|
|
131
|
+
N,
|
|
132
|
+
async (n: number) => (await attempt(n)) !== undefined,
|
|
133
|
+
),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private async attemptConversion(
|
|
138
|
+
n: number,
|
|
139
|
+
info: Info,
|
|
140
|
+
address: ccc.Address,
|
|
141
|
+
{
|
|
142
|
+
signer,
|
|
143
|
+
isCkb2Udt,
|
|
144
|
+
amount,
|
|
145
|
+
userCells,
|
|
146
|
+
system: { feeRate, tip, deposits, ckbDepositCap, exchangeRatio },
|
|
147
|
+
}: Parameters<IckbController["convert"]>[0],
|
|
148
|
+
): ReturnType<IckbController["convert"]> {
|
|
149
|
+
try {
|
|
150
|
+
const tx = SmartTransaction.default();
|
|
151
|
+
const maturities: Epoch[] = [];
|
|
152
|
+
let ckbFee = ccc.Zero;
|
|
153
|
+
|
|
154
|
+
if (n > 0) {
|
|
155
|
+
if (isCkb2Udt) {
|
|
156
|
+
amount -= ckbDepositCap * BigInt(n);
|
|
157
|
+
if (amount < 0n) {
|
|
158
|
+
// Too many Deposits respectfully to the amount
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, address.script);
|
|
162
|
+
} else {
|
|
163
|
+
const myDeposits = deposits.slice(0, n);
|
|
164
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
165
|
+
amount -= myDeposits.slice(-1)[0]!.udtCumulative;
|
|
166
|
+
if (amount < 0n) {
|
|
167
|
+
// Too many Withdrawal Requests respectfully to the amount
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
this.ownedOwnerManager.requestWithdrawal(
|
|
171
|
+
tx,
|
|
172
|
+
myDeposits,
|
|
173
|
+
address.script,
|
|
174
|
+
);
|
|
175
|
+
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (amount > 0n) {
|
|
180
|
+
this.orderManager.mint(
|
|
181
|
+
tx,
|
|
182
|
+
address.script,
|
|
183
|
+
info,
|
|
184
|
+
isCkb2Udt ? amount : ccc.Zero,
|
|
185
|
+
isCkb2Udt ? ccc.Zero : amount,
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
|
|
189
|
+
const convertedAmount = convert(isCkb2Udt, amount, ratio);
|
|
190
|
+
ckbFee = isCkb2Udt
|
|
191
|
+
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
192
|
+
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
193
|
+
|
|
194
|
+
maturities.push(
|
|
195
|
+
// TODO: use real world data from deposits, open orders and bot balances
|
|
196
|
+
orderMaturityEstimate(isCkb2Udt, amount, tip),
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Check that order provides enough fee to the bot for being matched
|
|
201
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
202
|
+
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const { receipts, withdrawalGroups, orders, udts, capacities } =
|
|
207
|
+
userCells;
|
|
208
|
+
|
|
209
|
+
this.logicManager.completeDeposit(tx, receipts);
|
|
210
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, {
|
|
211
|
+
isReadyOnly: true,
|
|
212
|
+
});
|
|
213
|
+
this.orderManager.melt(tx, orders);
|
|
214
|
+
this.ickbUdtManager.addUdts(tx, udts);
|
|
215
|
+
CapacityManager.addCapacities(tx, capacities);
|
|
216
|
+
|
|
217
|
+
// Throw an error if inputs do not cover for outputs
|
|
218
|
+
await new RestrictedTransaction(tx).completeFeeBy(signer, feeRate);
|
|
219
|
+
|
|
220
|
+
// Find the maturity most distant in the future
|
|
221
|
+
const maturity = maturities.reduce((a, b) =>
|
|
222
|
+
epochCompare(a, b) === -1 ? b : a,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
tx,
|
|
227
|
+
ckbFee,
|
|
228
|
+
maturity,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
232
|
+
} catch (_) {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// TODO: Allow filtering which cells are fetched
|
|
238
|
+
async getL1State(
|
|
71
239
|
client: ccc.Client,
|
|
72
|
-
locks: ccc.
|
|
73
|
-
|
|
240
|
+
locks: ccc.Script[],
|
|
241
|
+
options?: {
|
|
242
|
+
minLockUp?: ccc.Epoch;
|
|
243
|
+
maxLockUp?: ccc.Epoch;
|
|
244
|
+
},
|
|
245
|
+
): Promise<{
|
|
246
|
+
system: SystemState;
|
|
247
|
+
user: UserCells;
|
|
248
|
+
}> {
|
|
74
249
|
const tip = await client.getTipHeader();
|
|
75
250
|
const onChain = true;
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
251
|
+
const opts = { ...options, tip, onChain };
|
|
252
|
+
const [
|
|
253
|
+
feeRate,
|
|
254
|
+
depositPool,
|
|
255
|
+
capacities,
|
|
256
|
+
udts,
|
|
257
|
+
receipts,
|
|
258
|
+
withdrawalGroups,
|
|
259
|
+
orders,
|
|
260
|
+
deposits,
|
|
261
|
+
withdrawalRequests,
|
|
262
|
+
] = await Promise.all([
|
|
263
|
+
client.getFeeRate(),
|
|
264
|
+
collect(this.logicManager.findDeposits(client, opts)),
|
|
265
|
+
collect(CapacityManager.findCapacities(client, locks, opts)),
|
|
266
|
+
collect(this.ickbUdtManager.findUdts(client, locks, opts)),
|
|
267
|
+
collect(this.logicManager.findReceipts(client, locks, opts)),
|
|
268
|
+
collect(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts)),
|
|
269
|
+
collect(this.orderManager.findOrders(client)),
|
|
270
|
+
collect(this.daoManager.findDeposits(client, locks, opts)),
|
|
271
|
+
collect(this.daoManager.findWithdrawalRequests(client, locks, opts)),
|
|
272
|
+
// TODO: fetch bot cells for estimating timings
|
|
273
|
+
]);
|
|
80
274
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
275
|
+
// Sort depositPool by maturity
|
|
276
|
+
depositPool.sort((a, b) => epochCompare(a.maturity, b.maturity));
|
|
277
|
+
// Calculate cumulative value of deposits in the pool
|
|
278
|
+
let udtCumulative = 0n;
|
|
279
|
+
const cumulativeDepositPool: SystemState["deposits"] = [];
|
|
280
|
+
for (const deposit of depositPool) {
|
|
281
|
+
udtCumulative += deposit.udtValue;
|
|
282
|
+
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const userOrders: OrderGroup[] = [];
|
|
286
|
+
const nonUserOrders: OrderCell[] = [];
|
|
287
|
+
for (const group of orders) {
|
|
288
|
+
if (group.isOwner(...locks)) {
|
|
289
|
+
userOrders.push(group);
|
|
290
|
+
} else {
|
|
291
|
+
nonUserOrders.push(group.order);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const exchangeRatio = ickbExchangeRatio(tip);
|
|
296
|
+
const ckbDepositCap = convert(false, ICKB_DEPOSIT_CAP, exchangeRatio);
|
|
297
|
+
|
|
298
|
+
return {
|
|
299
|
+
system: {
|
|
300
|
+
feeRate,
|
|
301
|
+
tip,
|
|
302
|
+
exchangeRatio,
|
|
303
|
+
ckbDepositCap,
|
|
304
|
+
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
305
|
+
deposits: cumulativeDepositPool,
|
|
306
|
+
orders: nonUserOrders,
|
|
307
|
+
},
|
|
308
|
+
user: {
|
|
309
|
+
capacities,
|
|
310
|
+
udts,
|
|
311
|
+
receipts,
|
|
312
|
+
withdrawalGroups,
|
|
313
|
+
orders: userOrders,
|
|
314
|
+
deposits,
|
|
315
|
+
withdrawalRequests,
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
}
|
|
91
319
|
|
|
92
|
-
|
|
320
|
+
// TODO: Allow filtering which cells are accounted for
|
|
321
|
+
static getBalance({
|
|
322
|
+
capacities,
|
|
323
|
+
udts,
|
|
324
|
+
receipts,
|
|
325
|
+
withdrawalGroups,
|
|
326
|
+
orders,
|
|
327
|
+
}: UserCells): Balance {
|
|
328
|
+
const cells: { cell: ccc.Cell; udtValue?: ccc.FixedPoint }[] = [
|
|
329
|
+
capacities,
|
|
330
|
+
udts,
|
|
331
|
+
receipts,
|
|
332
|
+
].flat();
|
|
333
|
+
for (const { owner, owned } of withdrawalGroups) {
|
|
334
|
+
cells.push(owned, owner);
|
|
335
|
+
}
|
|
336
|
+
for (const { master, order } of orders) {
|
|
337
|
+
cells.push(master, order);
|
|
338
|
+
}
|
|
339
|
+
let ckb = ccc.Zero;
|
|
340
|
+
let udt = ccc.Zero;
|
|
341
|
+
for (const cell of cells) {
|
|
342
|
+
ckb += cell.cell.cellOutput.capacity;
|
|
343
|
+
udt += cell.udtValue ?? ccc.Zero;
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
ckb,
|
|
347
|
+
udt,
|
|
348
|
+
};
|
|
93
349
|
}
|
|
94
350
|
}
|
|
95
351
|
|
|
96
|
-
export interface
|
|
352
|
+
export interface Balance {
|
|
353
|
+
ckb: ccc.FixedPoint;
|
|
354
|
+
udt: ccc.FixedPoint;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export interface SystemState {
|
|
358
|
+
feeRate: ccc.Num;
|
|
97
359
|
tip: ccc.ClientBlockHeader;
|
|
98
|
-
|
|
360
|
+
exchangeRatio: {
|
|
361
|
+
ckbScale: ccc.Num;
|
|
362
|
+
udtScale: ccc.Num;
|
|
363
|
+
};
|
|
364
|
+
ckbDepositCap: ccc.FixedPoint;
|
|
365
|
+
udtDepositCap: ccc.FixedPoint;
|
|
366
|
+
deposits: (IckbDepositCell & { udtCumulative: bigint })[];
|
|
367
|
+
orders: OrderCell[];
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export interface UserCells {
|
|
371
|
+
capacities: CapacityCell[];
|
|
372
|
+
udts: UdtCell[];
|
|
99
373
|
receipts: ReceiptCell[];
|
|
100
374
|
withdrawalGroups: WithdrawalGroups[];
|
|
101
375
|
orders: OrderGroup[];
|
|
376
|
+
deposits: DaoCell[];
|
|
377
|
+
withdrawalRequests: DaoCell[];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export interface Conversion {
|
|
381
|
+
tx: SmartTransaction;
|
|
382
|
+
ckbFee: ccc.FixedPoint;
|
|
383
|
+
maturity: Epoch;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Estimate bot ability to fulfill orders:
|
|
387
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
388
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
389
|
+
function orderMaturityEstimate(
|
|
390
|
+
isCkb2Udt: boolean,
|
|
391
|
+
amount: bigint,
|
|
392
|
+
tip: ccc.ClientBlockHeader,
|
|
393
|
+
): ccc.Epoch {
|
|
394
|
+
return epochAdd(tip.epoch, [
|
|
395
|
+
0n,
|
|
396
|
+
1n + amount / ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
397
|
+
4n * 60n,
|
|
398
|
+
]);
|
|
102
399
|
}
|