@ickb/sdk 1000.0.40 → 1000.0.43
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 +26 -26
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +92 -70
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +26 -26
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +93 -71
- package/dist/esm/index.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +134 -126
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ccc
|
|
2
|
-
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell } from "@ickb/utils";
|
|
3
|
-
import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell, type ExchangeRatio } from "@ickb/utils";
|
|
3
|
+
import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroup } from "@ickb/core";
|
|
4
4
|
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
5
|
-
import { OrderManager, type OrderCell, type OrderGroup
|
|
5
|
+
import { OrderManager, type OrderCell, type OrderGroup } from "@ickb/order";
|
|
6
6
|
export declare class IckbController {
|
|
7
7
|
readonly ickbUdtManager: IckbUdtManager;
|
|
8
8
|
readonly logicManager: LogicManager;
|
|
@@ -17,36 +17,36 @@ export declare class IckbController {
|
|
|
17
17
|
ownedOwner: ScriptDeps;
|
|
18
18
|
order: ScriptDeps;
|
|
19
19
|
}): IckbController;
|
|
20
|
-
previewConversion(
|
|
21
|
-
numerator: ccc.Num;
|
|
22
|
-
denominator: ccc.Num;
|
|
23
|
-
}): ccc.FixedPoint;
|
|
24
|
-
convert(options: {
|
|
25
|
-
signer: ccc.Signer;
|
|
20
|
+
static previewConversion(conversion: {
|
|
26
21
|
isCkb2Udt: boolean;
|
|
27
22
|
amount: ccc.FixedPoint;
|
|
23
|
+
rate: ExchangeRatio | ccc.ClientBlockHeader;
|
|
24
|
+
fee: ccc.Num;
|
|
25
|
+
}): {
|
|
26
|
+
rate: ExchangeRatio;
|
|
27
|
+
convertedAmount: ccc.FixedPoint;
|
|
28
|
+
};
|
|
29
|
+
convert(options: {
|
|
30
|
+
conversion: Parameters<typeof IckbController.previewConversion>[0] & {
|
|
31
|
+
ckbMinMatchLog?: number;
|
|
32
|
+
ckbMinFee?: ccc.Num;
|
|
33
|
+
};
|
|
34
|
+
signer: ccc.Signer;
|
|
28
35
|
userCells: UserCells;
|
|
29
|
-
minCkbChange: ccc.FixedPoint;
|
|
30
36
|
system: SystemState;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
denominator: ccc.Num;
|
|
37
|
+
filters: {
|
|
38
|
+
isReadyOnly: boolean;
|
|
39
|
+
isFulfilledOnly: boolean;
|
|
35
40
|
};
|
|
36
41
|
}): Promise<Conversion | undefined>;
|
|
37
|
-
private
|
|
38
|
-
getL1State(client: ccc.Client, locks: ccc.Script[], options?: {
|
|
42
|
+
private attempt;
|
|
43
|
+
getL1State(client: ccc.Client, filter: Set<keyof UserCells | "depositPool">, locks: ccc.Script[], options?: {
|
|
39
44
|
minLockUp?: ccc.Epoch;
|
|
40
45
|
maxLockUp?: ccc.Epoch;
|
|
41
46
|
}): Promise<{
|
|
42
47
|
system: SystemState;
|
|
43
48
|
user: UserCells;
|
|
44
49
|
}>;
|
|
45
|
-
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
46
|
-
}
|
|
47
|
-
export interface Balance {
|
|
48
|
-
ckb: ccc.FixedPoint;
|
|
49
|
-
udt: ccc.FixedPoint;
|
|
50
50
|
}
|
|
51
51
|
export interface SystemState {
|
|
52
52
|
feeRate: ccc.Num;
|
|
@@ -57,16 +57,16 @@ export interface SystemState {
|
|
|
57
57
|
};
|
|
58
58
|
ckbDepositCap: ccc.FixedPoint;
|
|
59
59
|
udtDepositCap: ccc.FixedPoint;
|
|
60
|
-
|
|
60
|
+
depositPool: (IckbDepositCell & {
|
|
61
61
|
udtCumulative: bigint;
|
|
62
62
|
})[];
|
|
63
|
-
|
|
63
|
+
orderPool: OrderCell[];
|
|
64
64
|
}
|
|
65
65
|
export interface UserCells {
|
|
66
66
|
capacities: CapacityCell[];
|
|
67
67
|
udts: UdtCell[];
|
|
68
68
|
receipts: ReceiptCell[];
|
|
69
|
-
withdrawalGroups:
|
|
69
|
+
withdrawalGroups: WithdrawalGroup[];
|
|
70
70
|
orders: OrderGroup[];
|
|
71
71
|
deposits: DaoCell[];
|
|
72
72
|
withdrawalRequests: DaoCell[];
|
|
@@ -74,6 +74,6 @@ export interface UserCells {
|
|
|
74
74
|
export interface Conversion {
|
|
75
75
|
tx: SmartTransaction;
|
|
76
76
|
ckbFee: ccc.FixedPoint;
|
|
77
|
-
maturity: Epoch;
|
|
77
|
+
maturity: ccc.Epoch;
|
|
78
78
|
}
|
|
79
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,EAAE,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAIL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,YAAY,EAIjB,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,cAAc,EACd,YAAY,EACZ,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,WAA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;IACxB,gBAAgB,EAAE,eAAe,EAAE,CAAC;IACpC,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,GAAG,CAAC,KAAK,CAAC;CACrB"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -24,80 +24,113 @@ class IckbController {
|
|
|
24
24
|
const ickbUdtManager = core_2.IckbUdtManager.fromDeps(ickbXudt, daoManager);
|
|
25
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);
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
// fee examples:
|
|
28
|
+
// 100000n => 0.001%
|
|
29
|
+
// 10000n => 0.01%
|
|
30
|
+
// 1000n => 0.1%
|
|
31
|
+
// 100n => 1%
|
|
32
|
+
// 10n => 10%
|
|
33
|
+
// 1n => 100%
|
|
34
|
+
// less than 1n => 0%
|
|
35
|
+
static previewConversion(conversion) {
|
|
36
|
+
const { isCkb2Udt, amount, fee } = conversion;
|
|
37
|
+
let rate = conversion.rate;
|
|
38
|
+
rate = "dao" in rate ? (0, core_2.ickbExchangeRatio)(rate) : rate;
|
|
39
|
+
if (fee > core_1.ccc.Zero) {
|
|
40
|
+
const { ckbScale, udtScale } = rate;
|
|
41
|
+
rate = {
|
|
42
|
+
ckbScale,
|
|
43
|
+
// Worst case scenario where fee applies to the full amount
|
|
44
|
+
udtScale: udtScale + (isCkb2Udt ? 1n : -1n) * (udtScale / fee),
|
|
45
|
+
};
|
|
32
46
|
}
|
|
33
|
-
|
|
47
|
+
const convertedAmount = amount > core_1.ccc.Zero ? (0, core_2.convert)(isCkb2Udt, amount, rate) : core_1.ccc.Zero;
|
|
48
|
+
return { rate, convertedAmount };
|
|
34
49
|
}
|
|
35
|
-
// TODO streamline parameters with previewConversion
|
|
36
50
|
async convert(options) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
51
|
+
// Get user lock
|
|
52
|
+
const { system, signer } = options;
|
|
53
|
+
const lock = (await signer.getRecommendedAddressObj()).script;
|
|
54
|
+
// Normalize rate
|
|
55
|
+
const { rate } = IckbController.previewConversion({
|
|
56
|
+
...options.conversion,
|
|
57
|
+
amount: core_1.ccc.Zero,
|
|
58
|
+
});
|
|
59
|
+
const { isCkb2Udt, amount, ckbMinMatchLog } = options.conversion;
|
|
60
|
+
const info = order_1.Info.create(isCkb2Udt, rate, ckbMinMatchLog);
|
|
61
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
62
|
+
const ckbMinFee = options.conversion.ckbMinFee ?? 10n * system.feeRate;
|
|
63
|
+
const conversion = {
|
|
64
|
+
isCkb2Udt,
|
|
65
|
+
amount,
|
|
66
|
+
rate,
|
|
67
|
+
info,
|
|
68
|
+
ckbMinFee,
|
|
69
|
+
lock,
|
|
70
|
+
};
|
|
42
71
|
const N = isCkb2Udt
|
|
43
72
|
? Number(amount / system.ckbDepositCap)
|
|
44
|
-
: system.
|
|
73
|
+
: system.depositPool.slice(0, 30).length;
|
|
45
74
|
const cache = Array(N);
|
|
46
|
-
const
|
|
75
|
+
const cachedAttempt = (n) => {
|
|
47
76
|
n = N - n;
|
|
48
|
-
return (cache[n] =
|
|
49
|
-
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
77
|
+
return (cache[n] = cache[n] ?? this.attempt(n, conversion, options));
|
|
50
78
|
};
|
|
51
|
-
return
|
|
79
|
+
return cachedAttempt(await (0, utils_1.asyncBinarySearch)(N, async (n) => (await cachedAttempt(n)) !== undefined));
|
|
52
80
|
}
|
|
53
|
-
async
|
|
81
|
+
async attempt(n, conversion, options) {
|
|
54
82
|
try {
|
|
83
|
+
const { isCkb2Udt, rate, info, ckbMinFee, lock } = conversion;
|
|
84
|
+
const { signer, userCells, system, filters } = options;
|
|
85
|
+
const { feeRate, depositPool, ckbDepositCap, exchangeRatio } = system;
|
|
55
86
|
const tx = utils_1.SmartTransaction.default();
|
|
56
87
|
const maturities = [];
|
|
57
88
|
let ckbFee = core_1.ccc.Zero;
|
|
89
|
+
let amount = conversion.amount;
|
|
90
|
+
// Core protocol
|
|
58
91
|
if (n > 0) {
|
|
59
92
|
if (isCkb2Udt) {
|
|
60
93
|
amount -= ckbDepositCap * BigInt(n);
|
|
61
|
-
if (amount <
|
|
94
|
+
if (amount < core_1.ccc.Zero) {
|
|
62
95
|
// Too many Deposits respectfully to the amount
|
|
63
96
|
return undefined;
|
|
64
97
|
}
|
|
65
|
-
this.logicManager.deposit(tx, n, ckbDepositCap,
|
|
98
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, lock);
|
|
66
99
|
}
|
|
67
100
|
else {
|
|
68
|
-
const myDeposits =
|
|
101
|
+
const myDeposits = depositPool.slice(0, n);
|
|
69
102
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
70
103
|
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
71
|
-
if (amount <
|
|
104
|
+
if (amount < core_1.ccc.Zero) {
|
|
72
105
|
// Too many Withdrawal Requests respectfully to the amount
|
|
73
106
|
return undefined;
|
|
74
107
|
}
|
|
75
|
-
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits,
|
|
108
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, lock);
|
|
76
109
|
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
77
110
|
}
|
|
78
111
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
112
|
+
// Order protocol
|
|
113
|
+
if (amount > core_1.ccc.Zero) {
|
|
114
|
+
this.orderManager.mint(tx, lock, info, {
|
|
115
|
+
ckbValue: isCkb2Udt ? amount : core_1.ccc.Zero,
|
|
116
|
+
udtValue: isCkb2Udt ? core_1.ccc.Zero : amount,
|
|
117
|
+
});
|
|
118
|
+
const convertedAmount = (0, core_2.convert)(isCkb2Udt, amount, rate);
|
|
83
119
|
ckbFee = isCkb2Udt
|
|
84
120
|
? amount - (0, core_2.convert)(false, convertedAmount, exchangeRatio)
|
|
85
121
|
: (0, core_2.convert)(true, amount, exchangeRatio) - convertedAmount;
|
|
86
122
|
maturities.push(
|
|
87
123
|
// TODO: use real world data from deposits, open orders and bot balances
|
|
88
|
-
orderMaturityEstimate(isCkb2Udt, amount, tip));
|
|
124
|
+
orderMaturityEstimate(isCkb2Udt, amount, system.tip));
|
|
89
125
|
}
|
|
90
126
|
// Check that order provides enough fee to the bot for being matched
|
|
91
|
-
|
|
92
|
-
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
127
|
+
if (ckbFee < ckbMinFee) {
|
|
93
128
|
return undefined;
|
|
94
129
|
}
|
|
95
130
|
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
96
131
|
this.logicManager.completeDeposit(tx, receipts);
|
|
97
|
-
this.ownedOwnerManager.withdraw(tx, withdrawalGroups,
|
|
98
|
-
|
|
99
|
-
});
|
|
100
|
-
this.orderManager.melt(tx, orders);
|
|
132
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, filters);
|
|
133
|
+
this.orderManager.melt(tx, orders, filters);
|
|
101
134
|
this.ickbUdtManager.addUdts(tx, udts);
|
|
102
135
|
utils_1.CapacityManager.addCapacities(tx, capacities);
|
|
103
136
|
// Throw an error if inputs do not cover for outputs
|
|
@@ -115,21 +148,34 @@ class IckbController {
|
|
|
115
148
|
return undefined;
|
|
116
149
|
}
|
|
117
150
|
}
|
|
118
|
-
|
|
119
|
-
async getL1State(client, locks, options) {
|
|
151
|
+
async getL1State(client, filter, locks, options) {
|
|
120
152
|
const tip = await client.getTipHeader();
|
|
121
153
|
const onChain = true;
|
|
122
154
|
const opts = { ...options, tip, onChain };
|
|
123
155
|
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests,] = await Promise.all([
|
|
124
156
|
client.getFeeRate(),
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
157
|
+
filter.has("depositPool")
|
|
158
|
+
? (0, utils_1.collect)(this.logicManager.findDeposits(client, opts))
|
|
159
|
+
: [],
|
|
160
|
+
filter.has("capacities")
|
|
161
|
+
? (0, utils_1.collect)(utils_1.CapacityManager.findCapacities(client, locks, opts))
|
|
162
|
+
: [],
|
|
163
|
+
filter.has("udts")
|
|
164
|
+
? (0, utils_1.collect)(this.ickbUdtManager.findUdts(client, locks, opts))
|
|
165
|
+
: [],
|
|
166
|
+
filter.has("receipts")
|
|
167
|
+
? (0, utils_1.collect)(this.logicManager.findReceipts(client, locks, opts))
|
|
168
|
+
: [],
|
|
169
|
+
filter.has("withdrawalGroups")
|
|
170
|
+
? (0, utils_1.collect)(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts))
|
|
171
|
+
: [],
|
|
172
|
+
filter.has("orders") ? (0, utils_1.collect)(this.orderManager.findOrders(client)) : [],
|
|
173
|
+
filter.has("deposits")
|
|
174
|
+
? (0, utils_1.collect)(this.daoManager.findDeposits(client, locks, opts))
|
|
175
|
+
: [],
|
|
176
|
+
filter.has("withdrawalRequests")
|
|
177
|
+
? (0, utils_1.collect)(this.daoManager.findWithdrawalRequests(client, locks, opts))
|
|
178
|
+
: [],
|
|
133
179
|
// TODO: fetch bot cells for estimating timings
|
|
134
180
|
]);
|
|
135
181
|
// Sort depositPool by maturity
|
|
@@ -160,8 +206,8 @@ class IckbController {
|
|
|
160
206
|
exchangeRatio,
|
|
161
207
|
ckbDepositCap,
|
|
162
208
|
udtDepositCap: core_2.ICKB_DEPOSIT_CAP,
|
|
163
|
-
|
|
164
|
-
|
|
209
|
+
depositPool: cumulativeDepositPool,
|
|
210
|
+
orderPool: nonUserOrders,
|
|
165
211
|
},
|
|
166
212
|
user: {
|
|
167
213
|
capacities,
|
|
@@ -174,30 +220,6 @@ class IckbController {
|
|
|
174
220
|
},
|
|
175
221
|
};
|
|
176
222
|
}
|
|
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
|
-
};
|
|
200
|
-
}
|
|
201
223
|
}
|
|
202
224
|
exports.IckbController = IckbController;
|
|
203
225
|
// Estimate bot ability to fulfill orders:
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,wCAAoC;AACpC,uCAYqB;AACrB,qCAUoB;AACpB,mCAAqD;AACrD,uCAKqB;AAErB,MAAa,cAAc;IACzB,YACkB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;gBAClB,QAAQ;gBACR,kBAAkB;aACnB;SACF,CAAC;IACJ,CAAC;CACF;AAzUD,wCAyUC;AA+BD,0CAA0C;AAC1C,gDAAgD;AAChD,+CAA+C;AAC/C,SAAS,qBAAqB,CAC5B,SAAkB,EAClB,MAAc,EACd,GAA0B;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,8 +1,8 @@
|
|
|
1
|
-
import { ccc
|
|
2
|
-
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell } from "@ickb/utils";
|
|
3
|
-
import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell, type ExchangeRatio } from "@ickb/utils";
|
|
3
|
+
import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroup } from "@ickb/core";
|
|
4
4
|
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
5
|
-
import { OrderManager, type OrderCell, type OrderGroup
|
|
5
|
+
import { OrderManager, type OrderCell, type OrderGroup } from "@ickb/order";
|
|
6
6
|
export declare class IckbController {
|
|
7
7
|
readonly ickbUdtManager: IckbUdtManager;
|
|
8
8
|
readonly logicManager: LogicManager;
|
|
@@ -17,36 +17,36 @@ export declare class IckbController {
|
|
|
17
17
|
ownedOwner: ScriptDeps;
|
|
18
18
|
order: ScriptDeps;
|
|
19
19
|
}): IckbController;
|
|
20
|
-
previewConversion(
|
|
21
|
-
numerator: ccc.Num;
|
|
22
|
-
denominator: ccc.Num;
|
|
23
|
-
}): ccc.FixedPoint;
|
|
24
|
-
convert(options: {
|
|
25
|
-
signer: ccc.Signer;
|
|
20
|
+
static previewConversion(conversion: {
|
|
26
21
|
isCkb2Udt: boolean;
|
|
27
22
|
amount: ccc.FixedPoint;
|
|
23
|
+
rate: ExchangeRatio | ccc.ClientBlockHeader;
|
|
24
|
+
fee: ccc.Num;
|
|
25
|
+
}): {
|
|
26
|
+
rate: ExchangeRatio;
|
|
27
|
+
convertedAmount: ccc.FixedPoint;
|
|
28
|
+
};
|
|
29
|
+
convert(options: {
|
|
30
|
+
conversion: Parameters<typeof IckbController.previewConversion>[0] & {
|
|
31
|
+
ckbMinMatchLog?: number;
|
|
32
|
+
ckbMinFee?: ccc.Num;
|
|
33
|
+
};
|
|
34
|
+
signer: ccc.Signer;
|
|
28
35
|
userCells: UserCells;
|
|
29
|
-
minCkbChange: ccc.FixedPoint;
|
|
30
36
|
system: SystemState;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
denominator: ccc.Num;
|
|
37
|
+
filters: {
|
|
38
|
+
isReadyOnly: boolean;
|
|
39
|
+
isFulfilledOnly: boolean;
|
|
35
40
|
};
|
|
36
41
|
}): Promise<Conversion | undefined>;
|
|
37
|
-
private
|
|
38
|
-
getL1State(client: ccc.Client, locks: ccc.Script[], options?: {
|
|
42
|
+
private attempt;
|
|
43
|
+
getL1State(client: ccc.Client, filter: Set<keyof UserCells | "depositPool">, locks: ccc.Script[], options?: {
|
|
39
44
|
minLockUp?: ccc.Epoch;
|
|
40
45
|
maxLockUp?: ccc.Epoch;
|
|
41
46
|
}): Promise<{
|
|
42
47
|
system: SystemState;
|
|
43
48
|
user: UserCells;
|
|
44
49
|
}>;
|
|
45
|
-
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
46
|
-
}
|
|
47
|
-
export interface Balance {
|
|
48
|
-
ckb: ccc.FixedPoint;
|
|
49
|
-
udt: ccc.FixedPoint;
|
|
50
50
|
}
|
|
51
51
|
export interface SystemState {
|
|
52
52
|
feeRate: ccc.Num;
|
|
@@ -57,16 +57,16 @@ export interface SystemState {
|
|
|
57
57
|
};
|
|
58
58
|
ckbDepositCap: ccc.FixedPoint;
|
|
59
59
|
udtDepositCap: ccc.FixedPoint;
|
|
60
|
-
|
|
60
|
+
depositPool: (IckbDepositCell & {
|
|
61
61
|
udtCumulative: bigint;
|
|
62
62
|
})[];
|
|
63
|
-
|
|
63
|
+
orderPool: OrderCell[];
|
|
64
64
|
}
|
|
65
65
|
export interface UserCells {
|
|
66
66
|
capacities: CapacityCell[];
|
|
67
67
|
udts: UdtCell[];
|
|
68
68
|
receipts: ReceiptCell[];
|
|
69
|
-
withdrawalGroups:
|
|
69
|
+
withdrawalGroups: WithdrawalGroup[];
|
|
70
70
|
orders: OrderGroup[];
|
|
71
71
|
deposits: DaoCell[];
|
|
72
72
|
withdrawalRequests: DaoCell[];
|
|
@@ -74,6 +74,6 @@ export interface UserCells {
|
|
|
74
74
|
export interface Conversion {
|
|
75
75
|
tx: SmartTransaction;
|
|
76
76
|
ckbFee: ccc.FixedPoint;
|
|
77
|
-
maturity: Epoch;
|
|
77
|
+
maturity: ccc.Epoch;
|
|
78
78
|
}
|
|
79
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,EAAE,GAAG,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAIL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,KAAK,YAAY,EAIjB,KAAK,aAAa,EACnB,MAAM,aAAa,CAAC;AACrB,O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C;IAC3B,IAAI,EAAE,OAAO,EAAE,CAAC;IAChB,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,gBAAgB,EAAE,eAAe,EAAE,CAAC;IACpC,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,GAAG,CAAC,KAAK,CAAC;CACrB"}
|
package/dist/esm/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { ccc } from "@ckb-ccc/core";
|
|
|
2
2
|
import { collect, epochCompare, CapacityManager, SmartTransaction, epochAdd, asyncBinarySearch, RestrictedTransaction, } from "@ickb/utils";
|
|
3
3
|
import { convert, ICKB_DEPOSIT_CAP, ickbExchangeRatio, IckbUdtManager, LogicManager, OwnedOwnerManager, } from "@ickb/core";
|
|
4
4
|
import { DaoManager } from "@ickb/dao";
|
|
5
|
-
import { Info, OrderManager,
|
|
5
|
+
import { Info, OrderManager, } from "@ickb/order";
|
|
6
6
|
export class IckbController {
|
|
7
7
|
constructor(ickbUdtManager, logicManager, ownedOwnerManager, orderManager, daoManager) {
|
|
8
8
|
this.ickbUdtManager = ickbUdtManager;
|
|
@@ -21,80 +21,113 @@ export class IckbController {
|
|
|
21
21
|
const ickbUdtManager = IckbUdtManager.fromDeps(ickbXudt, daoManager);
|
|
22
22
|
return new IckbController(ickbUdtManager, LogicManager.fromDeps(ickbLogic, daoManager, ickbUdtManager), OwnedOwnerManager.fromDeps(ownedOwner, daoManager, ickbUdtManager), OrderManager.fromDeps(order, ickbUdtManager), daoManager);
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
// fee examples:
|
|
25
|
+
// 100000n => 0.001%
|
|
26
|
+
// 10000n => 0.01%
|
|
27
|
+
// 1000n => 0.1%
|
|
28
|
+
// 100n => 1%
|
|
29
|
+
// 10n => 10%
|
|
30
|
+
// 1n => 100%
|
|
31
|
+
// less than 1n => 0%
|
|
32
|
+
static previewConversion(conversion) {
|
|
33
|
+
const { isCkb2Udt, amount, fee } = conversion;
|
|
34
|
+
let rate = conversion.rate;
|
|
35
|
+
rate = "dao" in rate ? ickbExchangeRatio(rate) : rate;
|
|
36
|
+
if (fee > ccc.Zero) {
|
|
37
|
+
const { ckbScale, udtScale } = rate;
|
|
38
|
+
rate = {
|
|
39
|
+
ckbScale,
|
|
40
|
+
// Worst case scenario where fee applies to the full amount
|
|
41
|
+
udtScale: udtScale + (isCkb2Udt ? 1n : -1n) * (udtScale / fee),
|
|
42
|
+
};
|
|
29
43
|
}
|
|
30
|
-
|
|
44
|
+
const convertedAmount = amount > ccc.Zero ? convert(isCkb2Udt, amount, rate) : ccc.Zero;
|
|
45
|
+
return { rate, convertedAmount };
|
|
31
46
|
}
|
|
32
|
-
// TODO streamline parameters with previewConversion
|
|
33
47
|
async convert(options) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
48
|
+
// Get user lock
|
|
49
|
+
const { system, signer } = options;
|
|
50
|
+
const lock = (await signer.getRecommendedAddressObj()).script;
|
|
51
|
+
// Normalize rate
|
|
52
|
+
const { rate } = IckbController.previewConversion({
|
|
53
|
+
...options.conversion,
|
|
54
|
+
amount: ccc.Zero,
|
|
55
|
+
});
|
|
56
|
+
const { isCkb2Udt, amount, ckbMinMatchLog } = options.conversion;
|
|
57
|
+
const info = Info.create(isCkb2Udt, rate, ckbMinMatchLog);
|
|
58
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
59
|
+
const ckbMinFee = options.conversion.ckbMinFee ?? 10n * system.feeRate;
|
|
60
|
+
const conversion = {
|
|
61
|
+
isCkb2Udt,
|
|
62
|
+
amount,
|
|
63
|
+
rate,
|
|
64
|
+
info,
|
|
65
|
+
ckbMinFee,
|
|
66
|
+
lock,
|
|
67
|
+
};
|
|
39
68
|
const N = isCkb2Udt
|
|
40
69
|
? Number(amount / system.ckbDepositCap)
|
|
41
|
-
: system.
|
|
70
|
+
: system.depositPool.slice(0, 30).length;
|
|
42
71
|
const cache = Array(N);
|
|
43
|
-
const
|
|
72
|
+
const cachedAttempt = (n) => {
|
|
44
73
|
n = N - n;
|
|
45
|
-
return (cache[n] =
|
|
46
|
-
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
74
|
+
return (cache[n] = cache[n] ?? this.attempt(n, conversion, options));
|
|
47
75
|
};
|
|
48
|
-
return
|
|
76
|
+
return cachedAttempt(await asyncBinarySearch(N, async (n) => (await cachedAttempt(n)) !== undefined));
|
|
49
77
|
}
|
|
50
|
-
async
|
|
78
|
+
async attempt(n, conversion, options) {
|
|
51
79
|
try {
|
|
80
|
+
const { isCkb2Udt, rate, info, ckbMinFee, lock } = conversion;
|
|
81
|
+
const { signer, userCells, system, filters } = options;
|
|
82
|
+
const { feeRate, depositPool, ckbDepositCap, exchangeRatio } = system;
|
|
52
83
|
const tx = SmartTransaction.default();
|
|
53
84
|
const maturities = [];
|
|
54
85
|
let ckbFee = ccc.Zero;
|
|
86
|
+
let amount = conversion.amount;
|
|
87
|
+
// Core protocol
|
|
55
88
|
if (n > 0) {
|
|
56
89
|
if (isCkb2Udt) {
|
|
57
90
|
amount -= ckbDepositCap * BigInt(n);
|
|
58
|
-
if (amount <
|
|
91
|
+
if (amount < ccc.Zero) {
|
|
59
92
|
// Too many Deposits respectfully to the amount
|
|
60
93
|
return undefined;
|
|
61
94
|
}
|
|
62
|
-
this.logicManager.deposit(tx, n, ckbDepositCap,
|
|
95
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, lock);
|
|
63
96
|
}
|
|
64
97
|
else {
|
|
65
|
-
const myDeposits =
|
|
98
|
+
const myDeposits = depositPool.slice(0, n);
|
|
66
99
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
67
100
|
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
68
|
-
if (amount <
|
|
101
|
+
if (amount < ccc.Zero) {
|
|
69
102
|
// Too many Withdrawal Requests respectfully to the amount
|
|
70
103
|
return undefined;
|
|
71
104
|
}
|
|
72
|
-
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits,
|
|
105
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, lock);
|
|
73
106
|
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
74
107
|
}
|
|
75
108
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
109
|
+
// Order protocol
|
|
110
|
+
if (amount > ccc.Zero) {
|
|
111
|
+
this.orderManager.mint(tx, lock, info, {
|
|
112
|
+
ckbValue: isCkb2Udt ? amount : ccc.Zero,
|
|
113
|
+
udtValue: isCkb2Udt ? ccc.Zero : amount,
|
|
114
|
+
});
|
|
115
|
+
const convertedAmount = convert(isCkb2Udt, amount, rate);
|
|
80
116
|
ckbFee = isCkb2Udt
|
|
81
117
|
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
82
118
|
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
83
119
|
maturities.push(
|
|
84
120
|
// TODO: use real world data from deposits, open orders and bot balances
|
|
85
|
-
orderMaturityEstimate(isCkb2Udt, amount, tip));
|
|
121
|
+
orderMaturityEstimate(isCkb2Udt, amount, system.tip));
|
|
86
122
|
}
|
|
87
123
|
// Check that order provides enough fee to the bot for being matched
|
|
88
|
-
|
|
89
|
-
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
124
|
+
if (ckbFee < ckbMinFee) {
|
|
90
125
|
return undefined;
|
|
91
126
|
}
|
|
92
127
|
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
93
128
|
this.logicManager.completeDeposit(tx, receipts);
|
|
94
|
-
this.ownedOwnerManager.withdraw(tx, withdrawalGroups,
|
|
95
|
-
|
|
96
|
-
});
|
|
97
|
-
this.orderManager.melt(tx, orders);
|
|
129
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, filters);
|
|
130
|
+
this.orderManager.melt(tx, orders, filters);
|
|
98
131
|
this.ickbUdtManager.addUdts(tx, udts);
|
|
99
132
|
CapacityManager.addCapacities(tx, capacities);
|
|
100
133
|
// Throw an error if inputs do not cover for outputs
|
|
@@ -112,21 +145,34 @@ export class IckbController {
|
|
|
112
145
|
return undefined;
|
|
113
146
|
}
|
|
114
147
|
}
|
|
115
|
-
|
|
116
|
-
async getL1State(client, locks, options) {
|
|
148
|
+
async getL1State(client, filter, locks, options) {
|
|
117
149
|
const tip = await client.getTipHeader();
|
|
118
150
|
const onChain = true;
|
|
119
151
|
const opts = { ...options, tip, onChain };
|
|
120
152
|
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests,] = await Promise.all([
|
|
121
153
|
client.getFeeRate(),
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
154
|
+
filter.has("depositPool")
|
|
155
|
+
? collect(this.logicManager.findDeposits(client, opts))
|
|
156
|
+
: [],
|
|
157
|
+
filter.has("capacities")
|
|
158
|
+
? collect(CapacityManager.findCapacities(client, locks, opts))
|
|
159
|
+
: [],
|
|
160
|
+
filter.has("udts")
|
|
161
|
+
? collect(this.ickbUdtManager.findUdts(client, locks, opts))
|
|
162
|
+
: [],
|
|
163
|
+
filter.has("receipts")
|
|
164
|
+
? collect(this.logicManager.findReceipts(client, locks, opts))
|
|
165
|
+
: [],
|
|
166
|
+
filter.has("withdrawalGroups")
|
|
167
|
+
? collect(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts))
|
|
168
|
+
: [],
|
|
169
|
+
filter.has("orders") ? collect(this.orderManager.findOrders(client)) : [],
|
|
170
|
+
filter.has("deposits")
|
|
171
|
+
? collect(this.daoManager.findDeposits(client, locks, opts))
|
|
172
|
+
: [],
|
|
173
|
+
filter.has("withdrawalRequests")
|
|
174
|
+
? collect(this.daoManager.findWithdrawalRequests(client, locks, opts))
|
|
175
|
+
: [],
|
|
130
176
|
// TODO: fetch bot cells for estimating timings
|
|
131
177
|
]);
|
|
132
178
|
// Sort depositPool by maturity
|
|
@@ -157,8 +203,8 @@ export class IckbController {
|
|
|
157
203
|
exchangeRatio,
|
|
158
204
|
ckbDepositCap,
|
|
159
205
|
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
160
|
-
|
|
161
|
-
|
|
206
|
+
depositPool: cumulativeDepositPool,
|
|
207
|
+
orderPool: nonUserOrders,
|
|
162
208
|
},
|
|
163
209
|
user: {
|
|
164
210
|
capacities,
|
|
@@ -171,30 +217,6 @@ export class IckbController {
|
|
|
171
217
|
},
|
|
172
218
|
};
|
|
173
219
|
}
|
|
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
|
-
};
|
|
197
|
-
}
|
|
198
220
|
}
|
|
199
221
|
// Estimate bot ability to fulfill orders:
|
|
200
222
|
// - CKB to iCKB orders at 100k CKB every minute
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EACL,OAAO,EACP,YAAY,EACZ,eAAe,EACf,gBAAgB,EAIhB,QAAQ,EACR,iBAAiB,EACjB,qBAAqB,GAEtB,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,GAGb,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,QAAQ,EACR,QAAQ,EAC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C,CAAC;YACzB,CAAC;iBAAM,CAAC;gBACN,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;QAED,MAAM,aAAa,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC;QAC7C,MAAM,aAAa,GAAG,OAAO,CAAC,KAAK,EAAE,gBAAgB,EAAE,aAAa,CAAC,CAAC;QAEtE,OAAO;YACL,MAAM,EAAE;gBACN,OAAO;gBACP,GAAG;gBACH,aAAa;gBACb,aAAa;gBACb,aAAa,EAAE,gBAAgB;gBAC/B,WAAW,EAAE,qBAAqB;gBAClC,SAAS,EAAE,aAAa;aACzB;YACD,IAAI,EAAE;gBACJ,UAAU;gBACV,IAAI;gBACJ,QAAQ;gBACR,gBAAgB;gBAChB,MAAM,EAAE,UAAU;gBAClB,QAAQ;gBACR,kBAAkB;aACnB;SACF,CAAC;IACJ,CAAC;CACF;AA+BD,0CAA0C;AAC1C,gDAAgD;AAChD,+CAA+C;AAC/C,SAAS,qBAAqB,CAC5B,SAAkB,EAClB,MAAc,EACd,GAA0B;IAE1B,OAAO,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE;QACzB,EAAE;QACF,EAAE,GAAG,MAAM,GAAG,GAAG,CAAC,cAAc,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,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": "1000.0.
|
|
3
|
+
"version": "1000.0.43",
|
|
4
4
|
"description": "iCKB SDK built on top of CCC",
|
|
5
5
|
"author": "phroi",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,10 +31,10 @@
|
|
|
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.43",
|
|
35
|
+
"@ickb/dao": "^1000.0.41",
|
|
36
|
+
"@ickb/order": "^1000.0.43",
|
|
37
|
+
"@ickb/utils": "^1000.0.43"
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import { ccc
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
2
|
import {
|
|
3
3
|
collect,
|
|
4
4
|
epochCompare,
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
epochAdd,
|
|
11
11
|
asyncBinarySearch,
|
|
12
12
|
RestrictedTransaction,
|
|
13
|
+
type ExchangeRatio,
|
|
13
14
|
} from "@ickb/utils";
|
|
14
15
|
import {
|
|
15
16
|
convert,
|
|
@@ -20,16 +21,14 @@ import {
|
|
|
20
21
|
OwnedOwnerManager,
|
|
21
22
|
type IckbDepositCell,
|
|
22
23
|
type ReceiptCell,
|
|
23
|
-
type
|
|
24
|
+
type WithdrawalGroup,
|
|
24
25
|
} from "@ickb/core";
|
|
25
26
|
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
26
27
|
import {
|
|
27
28
|
Info,
|
|
28
29
|
OrderManager,
|
|
29
|
-
Ratio,
|
|
30
30
|
type OrderCell,
|
|
31
31
|
type OrderGroup,
|
|
32
|
-
type RatioLike,
|
|
33
32
|
} from "@ickb/order";
|
|
34
33
|
|
|
35
34
|
export class IckbController {
|
|
@@ -79,127 +78,158 @@ export class IckbController {
|
|
|
79
78
|
);
|
|
80
79
|
}
|
|
81
80
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
81
|
+
// fee examples:
|
|
82
|
+
// 100000n => 0.001%
|
|
83
|
+
// 10000n => 0.01%
|
|
84
|
+
// 1000n => 0.1%
|
|
85
|
+
// 100n => 1%
|
|
86
|
+
// 10n => 10%
|
|
87
|
+
// 1n => 100%
|
|
88
|
+
// less than 1n => 0%
|
|
89
|
+
static previewConversion(conversion: {
|
|
90
|
+
isCkb2Udt: boolean;
|
|
91
|
+
amount: ccc.FixedPoint;
|
|
92
|
+
rate: ExchangeRatio | ccc.ClientBlockHeader;
|
|
93
|
+
fee: ccc.Num;
|
|
94
|
+
}): { rate: ExchangeRatio; convertedAmount: ccc.FixedPoint } {
|
|
95
|
+
const { isCkb2Udt, amount, fee } = conversion;
|
|
96
|
+
let rate = conversion.rate;
|
|
97
|
+
rate = "dao" in rate ? ickbExchangeRatio(rate) : rate;
|
|
92
98
|
|
|
93
|
-
if (fee) {
|
|
94
|
-
const {
|
|
95
|
-
|
|
99
|
+
if (fee > ccc.Zero) {
|
|
100
|
+
const { ckbScale, udtScale } = rate;
|
|
101
|
+
rate = {
|
|
102
|
+
ckbScale,
|
|
103
|
+
// Worst case scenario where fee applies to the full amount
|
|
104
|
+
udtScale: udtScale + (isCkb2Udt ? 1n : -1n) * (udtScale / fee),
|
|
105
|
+
};
|
|
96
106
|
}
|
|
97
107
|
|
|
98
|
-
|
|
108
|
+
const convertedAmount =
|
|
109
|
+
amount > ccc.Zero ? convert(isCkb2Udt, amount, rate) : ccc.Zero;
|
|
110
|
+
|
|
111
|
+
return { rate, convertedAmount };
|
|
99
112
|
}
|
|
100
113
|
|
|
101
|
-
// TODO streamline parameters with previewConversion
|
|
102
114
|
async convert(options: {
|
|
115
|
+
conversion: Parameters<typeof IckbController.previewConversion>[0] & {
|
|
116
|
+
ckbMinMatchLog?: number;
|
|
117
|
+
ckbMinFee?: ccc.Num;
|
|
118
|
+
};
|
|
103
119
|
signer: ccc.Signer;
|
|
104
|
-
isCkb2Udt: boolean;
|
|
105
|
-
amount: ccc.FixedPoint;
|
|
106
120
|
userCells: UserCells;
|
|
107
|
-
minCkbChange: ccc.FixedPoint;
|
|
108
121
|
system: SystemState;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
denominator: ccc.Num;
|
|
122
|
+
filters: {
|
|
123
|
+
isReadyOnly: boolean;
|
|
124
|
+
isFulfilledOnly: boolean;
|
|
113
125
|
};
|
|
114
126
|
}): Promise<Conversion | undefined> {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
127
|
+
// Get user lock
|
|
128
|
+
const { system, signer } = options;
|
|
129
|
+
const lock = (await signer.getRecommendedAddressObj()).script;
|
|
130
|
+
|
|
131
|
+
// Normalize rate
|
|
132
|
+
const { rate } = IckbController.previewConversion({
|
|
133
|
+
...options.conversion,
|
|
134
|
+
amount: ccc.Zero,
|
|
135
|
+
});
|
|
136
|
+
const { isCkb2Udt, amount, ckbMinMatchLog } = options.conversion;
|
|
137
|
+
const info = Info.create(isCkb2Udt, rate, ckbMinMatchLog);
|
|
138
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
139
|
+
const ckbMinFee = options.conversion.ckbMinFee ?? 10n * system.feeRate;
|
|
140
|
+
const conversion = {
|
|
141
|
+
isCkb2Udt,
|
|
142
|
+
amount,
|
|
143
|
+
rate,
|
|
144
|
+
info,
|
|
145
|
+
ckbMinFee,
|
|
146
|
+
lock,
|
|
147
|
+
};
|
|
148
|
+
|
|
120
149
|
const N = isCkb2Udt
|
|
121
150
|
? Number(amount / system.ckbDepositCap)
|
|
122
|
-
: system.
|
|
151
|
+
: system.depositPool.slice(0, 30).length;
|
|
152
|
+
|
|
123
153
|
const cache = Array<ReturnType<IckbController["convert"]>>(N);
|
|
124
|
-
const
|
|
154
|
+
const cachedAttempt = (
|
|
155
|
+
n: number,
|
|
156
|
+
): ReturnType<IckbController["convert"]> => {
|
|
125
157
|
n = N - n;
|
|
126
|
-
return (cache[n] =
|
|
127
|
-
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
158
|
+
return (cache[n] = cache[n] ?? this.attempt(n, conversion, options));
|
|
128
159
|
};
|
|
129
|
-
|
|
160
|
+
|
|
161
|
+
return cachedAttempt(
|
|
130
162
|
await asyncBinarySearch(
|
|
131
163
|
N,
|
|
132
|
-
async (n: number) => (await
|
|
164
|
+
async (n: number) => (await cachedAttempt(n)) !== undefined,
|
|
133
165
|
),
|
|
134
166
|
);
|
|
135
167
|
}
|
|
136
168
|
|
|
137
|
-
private async
|
|
169
|
+
private async attempt(
|
|
138
170
|
n: number,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
171
|
+
conversion: {
|
|
172
|
+
isCkb2Udt: boolean;
|
|
173
|
+
amount: bigint;
|
|
174
|
+
rate: ExchangeRatio;
|
|
175
|
+
info: Info;
|
|
176
|
+
ckbMinFee: ccc.Num;
|
|
177
|
+
lock: ccc.Script;
|
|
178
|
+
},
|
|
179
|
+
options: Omit<Parameters<IckbController["convert"]>[0], "conversion">,
|
|
148
180
|
): ReturnType<IckbController["convert"]> {
|
|
149
181
|
try {
|
|
182
|
+
const { isCkb2Udt, rate, info, ckbMinFee, lock } = conversion;
|
|
183
|
+
const { signer, userCells, system, filters } = options;
|
|
184
|
+
const { feeRate, depositPool, ckbDepositCap, exchangeRatio } = system;
|
|
185
|
+
|
|
150
186
|
const tx = SmartTransaction.default();
|
|
151
|
-
const maturities: Epoch[] = [];
|
|
187
|
+
const maturities: ccc.Epoch[] = [];
|
|
152
188
|
let ckbFee = ccc.Zero;
|
|
189
|
+
let amount = conversion.amount;
|
|
153
190
|
|
|
191
|
+
// Core protocol
|
|
154
192
|
if (n > 0) {
|
|
155
193
|
if (isCkb2Udt) {
|
|
156
194
|
amount -= ckbDepositCap * BigInt(n);
|
|
157
|
-
if (amount <
|
|
195
|
+
if (amount < ccc.Zero) {
|
|
158
196
|
// Too many Deposits respectfully to the amount
|
|
159
197
|
return undefined;
|
|
160
198
|
}
|
|
161
|
-
this.logicManager.deposit(tx, n, ckbDepositCap,
|
|
199
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, lock);
|
|
162
200
|
} else {
|
|
163
|
-
const myDeposits =
|
|
201
|
+
const myDeposits = depositPool.slice(0, n);
|
|
164
202
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
165
203
|
amount -= myDeposits.slice(-1)[0]!.udtCumulative;
|
|
166
|
-
if (amount <
|
|
204
|
+
if (amount < ccc.Zero) {
|
|
167
205
|
// Too many Withdrawal Requests respectfully to the amount
|
|
168
206
|
return undefined;
|
|
169
207
|
}
|
|
170
|
-
this.ownedOwnerManager.requestWithdrawal(
|
|
171
|
-
tx,
|
|
172
|
-
myDeposits,
|
|
173
|
-
address.script,
|
|
174
|
-
);
|
|
208
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, lock);
|
|
175
209
|
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
176
210
|
}
|
|
177
211
|
}
|
|
178
212
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
isCkb2Udt ? ccc.Zero : amount,
|
|
186
|
-
);
|
|
213
|
+
// Order protocol
|
|
214
|
+
if (amount > ccc.Zero) {
|
|
215
|
+
this.orderManager.mint(tx, lock, info, {
|
|
216
|
+
ckbValue: isCkb2Udt ? amount : ccc.Zero,
|
|
217
|
+
udtValue: isCkb2Udt ? ccc.Zero : amount,
|
|
218
|
+
});
|
|
187
219
|
|
|
188
|
-
const
|
|
189
|
-
const convertedAmount = convert(isCkb2Udt, amount, ratio);
|
|
220
|
+
const convertedAmount = convert(isCkb2Udt, amount, rate);
|
|
190
221
|
ckbFee = isCkb2Udt
|
|
191
222
|
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
192
223
|
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
193
224
|
|
|
194
225
|
maturities.push(
|
|
195
226
|
// TODO: use real world data from deposits, open orders and bot balances
|
|
196
|
-
orderMaturityEstimate(isCkb2Udt, amount, tip),
|
|
227
|
+
orderMaturityEstimate(isCkb2Udt, amount, system.tip),
|
|
197
228
|
);
|
|
198
229
|
}
|
|
199
230
|
|
|
200
231
|
// Check that order provides enough fee to the bot for being matched
|
|
201
|
-
|
|
202
|
-
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
232
|
+
if (ckbFee < ckbMinFee) {
|
|
203
233
|
return undefined;
|
|
204
234
|
}
|
|
205
235
|
|
|
@@ -207,10 +237,8 @@ export class IckbController {
|
|
|
207
237
|
userCells;
|
|
208
238
|
|
|
209
239
|
this.logicManager.completeDeposit(tx, receipts);
|
|
210
|
-
this.ownedOwnerManager.withdraw(tx, withdrawalGroups,
|
|
211
|
-
|
|
212
|
-
});
|
|
213
|
-
this.orderManager.melt(tx, orders);
|
|
240
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, filters);
|
|
241
|
+
this.orderManager.melt(tx, orders, filters);
|
|
214
242
|
this.ickbUdtManager.addUdts(tx, udts);
|
|
215
243
|
CapacityManager.addCapacities(tx, capacities);
|
|
216
244
|
|
|
@@ -234,9 +262,9 @@ export class IckbController {
|
|
|
234
262
|
}
|
|
235
263
|
}
|
|
236
264
|
|
|
237
|
-
// TODO: Allow filtering which cells are fetched
|
|
238
265
|
async getL1State(
|
|
239
266
|
client: ccc.Client,
|
|
267
|
+
filter: Set<keyof UserCells | "depositPool">,
|
|
240
268
|
locks: ccc.Script[],
|
|
241
269
|
options?: {
|
|
242
270
|
minLockUp?: ccc.Epoch;
|
|
@@ -261,14 +289,30 @@ export class IckbController {
|
|
|
261
289
|
withdrawalRequests,
|
|
262
290
|
] = await Promise.all([
|
|
263
291
|
client.getFeeRate(),
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
292
|
+
filter.has("depositPool")
|
|
293
|
+
? collect(this.logicManager.findDeposits(client, opts))
|
|
294
|
+
: [],
|
|
295
|
+
filter.has("capacities")
|
|
296
|
+
? collect(CapacityManager.findCapacities(client, locks, opts))
|
|
297
|
+
: [],
|
|
298
|
+
filter.has("udts")
|
|
299
|
+
? collect(this.ickbUdtManager.findUdts(client, locks, opts))
|
|
300
|
+
: [],
|
|
301
|
+
filter.has("receipts")
|
|
302
|
+
? collect(this.logicManager.findReceipts(client, locks, opts))
|
|
303
|
+
: [],
|
|
304
|
+
filter.has("withdrawalGroups")
|
|
305
|
+
? collect(
|
|
306
|
+
this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts),
|
|
307
|
+
)
|
|
308
|
+
: [],
|
|
309
|
+
filter.has("orders") ? collect(this.orderManager.findOrders(client)) : [],
|
|
310
|
+
filter.has("deposits")
|
|
311
|
+
? collect(this.daoManager.findDeposits(client, locks, opts))
|
|
312
|
+
: [],
|
|
313
|
+
filter.has("withdrawalRequests")
|
|
314
|
+
? collect(this.daoManager.findWithdrawalRequests(client, locks, opts))
|
|
315
|
+
: [],
|
|
272
316
|
// TODO: fetch bot cells for estimating timings
|
|
273
317
|
]);
|
|
274
318
|
|
|
@@ -276,7 +320,7 @@ export class IckbController {
|
|
|
276
320
|
depositPool.sort((a, b) => epochCompare(a.maturity, b.maturity));
|
|
277
321
|
// Calculate cumulative value of deposits in the pool
|
|
278
322
|
let udtCumulative = 0n;
|
|
279
|
-
const cumulativeDepositPool: SystemState["
|
|
323
|
+
const cumulativeDepositPool: SystemState["depositPool"] = [];
|
|
280
324
|
for (const deposit of depositPool) {
|
|
281
325
|
udtCumulative += deposit.udtValue;
|
|
282
326
|
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
@@ -302,8 +346,8 @@ export class IckbController {
|
|
|
302
346
|
exchangeRatio,
|
|
303
347
|
ckbDepositCap,
|
|
304
348
|
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
305
|
-
|
|
306
|
-
|
|
349
|
+
depositPool: cumulativeDepositPool,
|
|
350
|
+
orderPool: nonUserOrders,
|
|
307
351
|
},
|
|
308
352
|
user: {
|
|
309
353
|
capacities,
|
|
@@ -316,42 +360,6 @@ export class IckbController {
|
|
|
316
360
|
},
|
|
317
361
|
};
|
|
318
362
|
}
|
|
319
|
-
|
|
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
|
-
};
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
export interface Balance {
|
|
353
|
-
ckb: ccc.FixedPoint;
|
|
354
|
-
udt: ccc.FixedPoint;
|
|
355
363
|
}
|
|
356
364
|
|
|
357
365
|
export interface SystemState {
|
|
@@ -363,15 +371,15 @@ export interface SystemState {
|
|
|
363
371
|
};
|
|
364
372
|
ckbDepositCap: ccc.FixedPoint;
|
|
365
373
|
udtDepositCap: ccc.FixedPoint;
|
|
366
|
-
|
|
367
|
-
|
|
374
|
+
depositPool: (IckbDepositCell & { udtCumulative: bigint })[];
|
|
375
|
+
orderPool: OrderCell[];
|
|
368
376
|
}
|
|
369
377
|
|
|
370
378
|
export interface UserCells {
|
|
371
379
|
capacities: CapacityCell[];
|
|
372
380
|
udts: UdtCell[];
|
|
373
381
|
receipts: ReceiptCell[];
|
|
374
|
-
withdrawalGroups:
|
|
382
|
+
withdrawalGroups: WithdrawalGroup[];
|
|
375
383
|
orders: OrderGroup[];
|
|
376
384
|
deposits: DaoCell[];
|
|
377
385
|
withdrawalRequests: DaoCell[];
|
|
@@ -380,7 +388,7 @@ export interface UserCells {
|
|
|
380
388
|
export interface Conversion {
|
|
381
389
|
tx: SmartTransaction;
|
|
382
390
|
ckbFee: ccc.FixedPoint;
|
|
383
|
-
maturity: Epoch;
|
|
391
|
+
maturity: ccc.Epoch;
|
|
384
392
|
}
|
|
385
393
|
|
|
386
394
|
// Estimate bot ability to fulfill orders:
|