@ickb/sdk 1000.0.40 → 1000.0.52
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 +145 -34
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +220 -92
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.d.ts +145 -34
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +222 -94
- package/dist/esm/index.js.map +1 -1
- package/package.json +9 -9
- package/src/index.ts +339 -165
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,79 +1,190 @@
|
|
|
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
|
+
/**
|
|
7
|
+
* Controller for managing iCKB operations.
|
|
8
|
+
*/
|
|
6
9
|
export declare class IckbController {
|
|
7
10
|
readonly ickbUdtManager: IckbUdtManager;
|
|
8
11
|
readonly logicManager: LogicManager;
|
|
9
12
|
readonly ownedOwnerManager: OwnedOwnerManager;
|
|
10
13
|
readonly orderManager: OrderManager;
|
|
11
14
|
readonly daoManager: DaoManager;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
readonly bots: ccc.Script[];
|
|
16
|
+
/**
|
|
17
|
+
* Creates an instance of IckbController.
|
|
18
|
+
* @param ickbUdtManager - The manager for iCKB UDT operations.
|
|
19
|
+
* @param logicManager - The logic manager for handling business logic.
|
|
20
|
+
* @param ownedOwnerManager - The manager for owned owner operations.
|
|
21
|
+
* @param orderManager - The manager for order operations.
|
|
22
|
+
* @param daoManager - The manager for DAO operations.
|
|
23
|
+
*/
|
|
24
|
+
constructor(ickbUdtManager: IckbUdtManager, logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager, daoManager: DaoManager, bots: ccc.Script[]);
|
|
25
|
+
/**
|
|
26
|
+
* Creates an instance of IckbController from script dependencies.
|
|
27
|
+
* @param deps - The script dependencies.
|
|
28
|
+
* @param deps.udt - The script dependencies for UDT.
|
|
29
|
+
* @param deps.dao - The script dependencies for DAO.
|
|
30
|
+
* @param deps.ickbLogic - The script dependencies for iCKB logic.
|
|
31
|
+
* @param deps.ownedOwner - The script dependencies for owned owner.
|
|
32
|
+
* @param deps.order - The script dependencies for order.
|
|
33
|
+
* @returns An instance of IckbController.
|
|
34
|
+
*/
|
|
35
|
+
static fromDeps(deps: {
|
|
36
|
+
udt: ScriptDeps;
|
|
15
37
|
dao: ScriptDeps;
|
|
16
38
|
ickbLogic: ScriptDeps;
|
|
17
39
|
ownedOwner: ScriptDeps;
|
|
18
40
|
order: ScriptDeps;
|
|
19
|
-
}): IckbController;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
41
|
+
}, bots: ccc.Script[]): IckbController;
|
|
42
|
+
/**
|
|
43
|
+
* Previews the conversion between CKB and UDT.
|
|
44
|
+
* @param conversion - The conversion parameters.
|
|
45
|
+
* @param conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
|
|
46
|
+
* @param conversion.amount - The amount to convert.
|
|
47
|
+
* @param conversion.rate - The exchange rate for conversion.
|
|
48
|
+
* @param conversion.fee - The fee for the conversion.
|
|
49
|
+
* @returns The rate and converted amount.
|
|
50
|
+
*
|
|
51
|
+
* Fee meaning examples:
|
|
52
|
+
* - 100000n => 0.001%
|
|
53
|
+
* - 10000n => 0.01%
|
|
54
|
+
* - 1000n => 0.1%
|
|
55
|
+
* - 100n => 1%
|
|
56
|
+
* - 10n => 10%
|
|
57
|
+
* - 1n => 100%
|
|
58
|
+
* - less than 1n => 0%
|
|
59
|
+
*/
|
|
60
|
+
static previewConversion(conversion: {
|
|
26
61
|
isCkb2Udt: boolean;
|
|
27
62
|
amount: ccc.FixedPoint;
|
|
63
|
+
rate: ExchangeRatio | ccc.ClientBlockHeader;
|
|
64
|
+
fee: ccc.Num;
|
|
65
|
+
}): {
|
|
66
|
+
rate: ExchangeRatio;
|
|
67
|
+
convertedAmount: ccc.FixedPoint;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Converts between CKB and UDT based on the provided options.
|
|
71
|
+
* @param options - The conversion options.
|
|
72
|
+
* @param options.conversion - The conversion parameters.
|
|
73
|
+
* @param options.conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
|
|
74
|
+
* @param options.conversion.amount - The amount to convert.
|
|
75
|
+
* @param options.conversion.rate - The exchange rate for conversion.
|
|
76
|
+
* @param options.conversion.ckbMinMatchLog - Minimum CKB match log (optional).
|
|
77
|
+
* @param options.conversion.ckbMinFee - Minimum CKB fee (optional).
|
|
78
|
+
* @param options.signer - The signer for the transaction.
|
|
79
|
+
* @param options.userCells - The user's cells.
|
|
80
|
+
* @param options.system - The current system state.
|
|
81
|
+
* @param options.filters - The filters for the conversion.
|
|
82
|
+
* @param options.filters.isReadyOnly - Indicates if only ready conversions should be considered.
|
|
83
|
+
* @param options.filters.isFulfilledOnly - Indicates if only fulfilled conversions should be considered.
|
|
84
|
+
* @returns A promise that resolves to the conversion result or undefined.
|
|
85
|
+
*/
|
|
86
|
+
convert(options: {
|
|
87
|
+
conversion: Parameters<typeof IckbController.previewConversion>[0] & {
|
|
88
|
+
ckbMinMatchLog?: number;
|
|
89
|
+
ckbMinFee?: ccc.Num;
|
|
90
|
+
};
|
|
91
|
+
signer: ccc.Signer;
|
|
28
92
|
userCells: UserCells;
|
|
29
|
-
minCkbChange: ccc.FixedPoint;
|
|
30
93
|
system: SystemState;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
denominator: ccc.Num;
|
|
94
|
+
filters: {
|
|
95
|
+
isReadyOnly: boolean;
|
|
96
|
+
isFulfilledOnly: boolean;
|
|
35
97
|
};
|
|
36
98
|
}): Promise<Conversion | undefined>;
|
|
37
|
-
|
|
38
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Attempts to perform a conversion based on the provided parameters.
|
|
101
|
+
* @param n - The size of deposit to make or withdraw from.
|
|
102
|
+
* @param conversion - The conversion parameters.
|
|
103
|
+
* @param conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
|
|
104
|
+
* @param conversion.amount - The amount to convert.
|
|
105
|
+
* @param conversion.rate - The exchange rate for conversion.
|
|
106
|
+
* @param conversion.info - Additional information for the conversion.
|
|
107
|
+
* @param conversion.ckbMinFee - Minimum CKB fee.
|
|
108
|
+
* @param conversion.lock - The script lock for the transaction.
|
|
109
|
+
* @param options - Additional options for the conversion.
|
|
110
|
+
* @returns A promise that resolves to the conversion result or undefined.
|
|
111
|
+
*/
|
|
112
|
+
private attempt;
|
|
113
|
+
/**
|
|
114
|
+
* Retrieves the L1 state based on the provided parameters.
|
|
115
|
+
* @param client - The client to interact with the blockchain.
|
|
116
|
+
* @param filter - The set of filters to apply.
|
|
117
|
+
* @param locks - The script locks to filter the user cells.
|
|
118
|
+
* @param options - Optional parameters for the state retrieval.
|
|
119
|
+
* @param options.minLockUp - The minimum lock-up period (optional).
|
|
120
|
+
* @param options.maxLockUp - The maximum lock-up period (optional).
|
|
121
|
+
* @returns A promise that resolves to an object containing the system state and user cells.
|
|
122
|
+
*/
|
|
123
|
+
getL1State(client: ccc.Client, filter: Set<keyof UserCells | "depositPool" | "ckbBots">, locks: ccc.Script[], options?: {
|
|
39
124
|
minLockUp?: ccc.Epoch;
|
|
40
125
|
maxLockUp?: ccc.Epoch;
|
|
41
126
|
}): Promise<{
|
|
42
127
|
system: SystemState;
|
|
43
128
|
user: UserCells;
|
|
44
129
|
}>;
|
|
45
|
-
static getBalance({ capacities, udts, receipts, withdrawalGroups, orders, }: UserCells): Balance;
|
|
46
|
-
}
|
|
47
|
-
export interface Balance {
|
|
48
|
-
ckb: ccc.FixedPoint;
|
|
49
|
-
udt: ccc.FixedPoint;
|
|
50
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Represents the state of the system.
|
|
133
|
+
*/
|
|
51
134
|
export interface SystemState {
|
|
135
|
+
/** The fee rate for transactions. */
|
|
52
136
|
feeRate: ccc.Num;
|
|
137
|
+
/** The tip for the current block header. */
|
|
53
138
|
tip: ccc.ClientBlockHeader;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
};
|
|
139
|
+
/** The exchange ratio between CKB and UDT. */
|
|
140
|
+
exchangeRatio: ExchangeRatio;
|
|
141
|
+
/** The deposit cap expressed in CKB. */
|
|
58
142
|
ckbDepositCap: ccc.FixedPoint;
|
|
143
|
+
/** The deposit cap expressed in iCKB. */
|
|
59
144
|
udtDepositCap: ccc.FixedPoint;
|
|
60
|
-
|
|
145
|
+
/**
|
|
146
|
+
* The deposit pool containing iCKB deposit cells with cumulative iCKB amounts.
|
|
147
|
+
*/
|
|
148
|
+
depositPool: (IckbDepositCell & {
|
|
61
149
|
udtCumulative: bigint;
|
|
62
150
|
})[];
|
|
63
|
-
|
|
151
|
+
/**
|
|
152
|
+
* The order pool containing non-user order cells.
|
|
153
|
+
*/
|
|
154
|
+
orderPool: OrderCell[];
|
|
155
|
+
/**
|
|
156
|
+
* The ckb held by bots ready to match incoming iCKB to CKB conversions.
|
|
157
|
+
*/
|
|
158
|
+
ckbBots: ccc.FixedPoint;
|
|
64
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* Represents the user cells in the system.
|
|
162
|
+
*/
|
|
65
163
|
export interface UserCells {
|
|
164
|
+
/** The capacities owned by the user. */
|
|
66
165
|
capacities: CapacityCell[];
|
|
166
|
+
/** The UDTs owned by the user. */
|
|
67
167
|
udts: UdtCell[];
|
|
168
|
+
/** The receipts associated with the user's transactions. */
|
|
68
169
|
receipts: ReceiptCell[];
|
|
69
|
-
|
|
170
|
+
/** The withdrawal groups for the user. */
|
|
171
|
+
withdrawalGroups: WithdrawalGroup[];
|
|
172
|
+
/** The orders placed by the user. */
|
|
70
173
|
orders: OrderGroup[];
|
|
174
|
+
/** The classical/non-iCKB deposits made by the user. */
|
|
71
175
|
deposits: DaoCell[];
|
|
176
|
+
/** The classical/non-iCKB withdrawal requests made by the user. */
|
|
72
177
|
withdrawalRequests: DaoCell[];
|
|
73
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Represents a conversion transaction.
|
|
181
|
+
*/
|
|
74
182
|
export interface Conversion {
|
|
183
|
+
/** The smart transaction associated with the conversion. */
|
|
75
184
|
tx: SmartTransaction;
|
|
185
|
+
/** The fee in CKB for the order created by the conversion. */
|
|
76
186
|
ckbFee: ccc.FixedPoint;
|
|
77
|
-
maturity
|
|
187
|
+
/** The estimated maturity epoch for conversion. */
|
|
188
|
+
maturity: ccc.Epoch;
|
|
78
189
|
}
|
|
79
190
|
//# 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,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xC,aAAa,EAAE,GAAG,CAAC,UAAU,CAAC;IAE9B,yCAAyC;IACzC,aAAa,EAAE,GAAG,CAAC,UAAU,CAAC;IAE9B;;OAEG;IACH,WAAW,EAAE,CAAC,eAAe,GAAG;QAAE,aAAa,EAAE,MAAM,CAAA;KAAE,CAAC,EAAE,CAAC;IAE7D;;OAEG;IACH,SAAS,EAAE,SAAS,EAAE,CAAC;IAEvB;;OAEG;IACH,OAAO,EAAE,GAAG,CAAC,UAAU,CAAC;CACzB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,wCAAwC;IACxC,UAAU,EAAE,YAAY,EAAE,CAAC;IAE3B,kCAAkC;IAClC,IAAI,EAAE,OAAO,EAAE,CAAC;IAEhB,4DAA4D;IAC5D,QAAQ,EAAE,WAAW,EAAE,CAAC;IAExB,0CAA0C;IAC1C,gBAAgB,EAAE,eAAe,EAAE,CAAC;IAEpC,qCAAqC;IACrC,MAAM,EAAE,UAAU,EAAE,CAAC;IAErB,wDAAwD;IACxD,QAAQ,EAAE,OAAO,EAAE,CAAC;IAEpB,mEAAmE;IACnE,kBAAkB,EAAE,OAAO,EAAE,CAAC;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,4DAA4D;IAC5D,EAAE,EAAE,gBAAgB,CAAC;IAErB,8DAA8D;IAC9D,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC;IAEvB,mDAAmD;IACnD,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC;CACrB"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -6,98 +6,193 @@ const utils_1 = require("@ickb/utils");
|
|
|
6
6
|
const core_2 = require("@ickb/core");
|
|
7
7
|
const dao_1 = require("@ickb/dao");
|
|
8
8
|
const order_1 = require("@ickb/order");
|
|
9
|
+
/**
|
|
10
|
+
* Controller for managing iCKB operations.
|
|
11
|
+
*/
|
|
9
12
|
class IckbController {
|
|
10
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of IckbController.
|
|
15
|
+
* @param ickbUdtManager - The manager for iCKB UDT operations.
|
|
16
|
+
* @param logicManager - The logic manager for handling business logic.
|
|
17
|
+
* @param ownedOwnerManager - The manager for owned owner operations.
|
|
18
|
+
* @param orderManager - The manager for order operations.
|
|
19
|
+
* @param daoManager - The manager for DAO operations.
|
|
20
|
+
*/
|
|
21
|
+
constructor(ickbUdtManager, logicManager, ownedOwnerManager, orderManager, daoManager, bots) {
|
|
11
22
|
this.ickbUdtManager = ickbUdtManager;
|
|
12
23
|
this.logicManager = logicManager;
|
|
13
24
|
this.ownedOwnerManager = ownedOwnerManager;
|
|
14
25
|
this.orderManager = orderManager;
|
|
15
26
|
this.daoManager = daoManager;
|
|
27
|
+
this.bots = bots;
|
|
16
28
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Creates an instance of IckbController from script dependencies.
|
|
31
|
+
* @param deps - The script dependencies.
|
|
32
|
+
* @param deps.udt - The script dependencies for UDT.
|
|
33
|
+
* @param deps.dao - The script dependencies for DAO.
|
|
34
|
+
* @param deps.ickbLogic - The script dependencies for iCKB logic.
|
|
35
|
+
* @param deps.ownedOwner - The script dependencies for owned owner.
|
|
36
|
+
* @param deps.order - The script dependencies for order.
|
|
37
|
+
* @returns An instance of IckbController.
|
|
38
|
+
*/
|
|
39
|
+
static fromDeps(deps, bots) {
|
|
40
|
+
const { udt: { script: { codeHash, hashType }, cellDeps, }, ickbLogic: { script: ickbLogicScript }, } = deps;
|
|
41
|
+
const daoManager = dao_1.DaoManager.fromDeps(deps);
|
|
42
|
+
const udt = {
|
|
43
|
+
script: new core_1.ccc.Script(codeHash, hashType, [ickbLogicScript.hash(), "00000080"].join("")),
|
|
22
44
|
cellDeps,
|
|
23
45
|
};
|
|
24
|
-
const ickbUdtManager = core_2.IckbUdtManager.fromDeps(
|
|
25
|
-
return new IckbController(ickbUdtManager, core_2.LogicManager.fromDeps(
|
|
46
|
+
const ickbUdtManager = core_2.IckbUdtManager.fromDeps({ udt }, daoManager);
|
|
47
|
+
return new IckbController(ickbUdtManager, core_2.LogicManager.fromDeps(deps, daoManager, ickbUdtManager), core_2.OwnedOwnerManager.fromDeps(deps, daoManager, ickbUdtManager), order_1.OrderManager.fromDeps(deps, ickbUdtManager), daoManager, bots);
|
|
26
48
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Previews the conversion between CKB and UDT.
|
|
51
|
+
* @param conversion - The conversion parameters.
|
|
52
|
+
* @param conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
|
|
53
|
+
* @param conversion.amount - The amount to convert.
|
|
54
|
+
* @param conversion.rate - The exchange rate for conversion.
|
|
55
|
+
* @param conversion.fee - The fee for the conversion.
|
|
56
|
+
* @returns The rate and converted amount.
|
|
57
|
+
*
|
|
58
|
+
* Fee meaning examples:
|
|
59
|
+
* - 100000n => 0.001%
|
|
60
|
+
* - 10000n => 0.01%
|
|
61
|
+
* - 1000n => 0.1%
|
|
62
|
+
* - 100n => 1%
|
|
63
|
+
* - 10n => 10%
|
|
64
|
+
* - 1n => 100%
|
|
65
|
+
* - less than 1n => 0%
|
|
66
|
+
*/
|
|
67
|
+
static previewConversion(conversion) {
|
|
68
|
+
const { isCkb2Udt, amount, fee } = conversion;
|
|
69
|
+
let rate = conversion.rate;
|
|
70
|
+
rate = "dao" in rate ? (0, core_2.ickbExchangeRatio)(rate) : rate;
|
|
71
|
+
if (fee > core_1.ccc.Zero) {
|
|
72
|
+
const { ckbScale, udtScale } = rate;
|
|
73
|
+
rate = {
|
|
74
|
+
ckbScale,
|
|
75
|
+
// Worst case scenario where fee applies to the full amount
|
|
76
|
+
udtScale: udtScale + (isCkb2Udt ? 1n : -1n) * (udtScale / fee),
|
|
77
|
+
};
|
|
32
78
|
}
|
|
33
|
-
|
|
79
|
+
const convertedAmount = amount > core_1.ccc.Zero ? (0, core_2.convert)(isCkb2Udt, amount, rate) : core_1.ccc.Zero;
|
|
80
|
+
return { rate, convertedAmount };
|
|
34
81
|
}
|
|
35
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Converts between CKB and UDT based on the provided options.
|
|
84
|
+
* @param options - The conversion options.
|
|
85
|
+
* @param options.conversion - The conversion parameters.
|
|
86
|
+
* @param options.conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
|
|
87
|
+
* @param options.conversion.amount - The amount to convert.
|
|
88
|
+
* @param options.conversion.rate - The exchange rate for conversion.
|
|
89
|
+
* @param options.conversion.ckbMinMatchLog - Minimum CKB match log (optional).
|
|
90
|
+
* @param options.conversion.ckbMinFee - Minimum CKB fee (optional).
|
|
91
|
+
* @param options.signer - The signer for the transaction.
|
|
92
|
+
* @param options.userCells - The user's cells.
|
|
93
|
+
* @param options.system - The current system state.
|
|
94
|
+
* @param options.filters - The filters for the conversion.
|
|
95
|
+
* @param options.filters.isReadyOnly - Indicates if only ready conversions should be considered.
|
|
96
|
+
* @param options.filters.isFulfilledOnly - Indicates if only fulfilled conversions should be considered.
|
|
97
|
+
* @returns A promise that resolves to the conversion result or undefined.
|
|
98
|
+
*/
|
|
36
99
|
async convert(options) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const
|
|
100
|
+
// Get user lock
|
|
101
|
+
const { system, signer } = options;
|
|
102
|
+
const lock = (await signer.getRecommendedAddressObj()).script;
|
|
103
|
+
// Normalize rate
|
|
104
|
+
const { rate } = IckbController.previewConversion({
|
|
105
|
+
...options.conversion,
|
|
106
|
+
amount: core_1.ccc.Zero,
|
|
107
|
+
});
|
|
108
|
+
const { isCkb2Udt, amount, ckbMinMatchLog } = options.conversion;
|
|
109
|
+
const info = order_1.Info.create(isCkb2Udt, rate, ckbMinMatchLog);
|
|
110
|
+
// feeRate is a good approximation of the bot tx size matching the order
|
|
111
|
+
const ckbMinFee = options.conversion.ckbMinFee ?? 10n * system.feeRate;
|
|
112
|
+
const conversion = {
|
|
113
|
+
isCkb2Udt,
|
|
114
|
+
amount,
|
|
115
|
+
rate,
|
|
116
|
+
info,
|
|
117
|
+
ckbMinFee,
|
|
118
|
+
lock,
|
|
119
|
+
};
|
|
42
120
|
const N = isCkb2Udt
|
|
43
121
|
? Number(amount / system.ckbDepositCap)
|
|
44
|
-
: system.
|
|
122
|
+
: system.depositPool.slice(0, 30).length;
|
|
45
123
|
const cache = Array(N);
|
|
46
|
-
const
|
|
124
|
+
const cachedAttempt = (n) => {
|
|
47
125
|
n = N - n;
|
|
48
|
-
return (cache[n] =
|
|
49
|
-
cache[n] ?? this.attemptConversion(n, info, address, options));
|
|
126
|
+
return (cache[n] = cache[n] ?? this.attempt(n, conversion, options));
|
|
50
127
|
};
|
|
51
|
-
return
|
|
128
|
+
return cachedAttempt(await (0, utils_1.asyncBinarySearch)(N, async (n) => (await cachedAttempt(n)) !== undefined));
|
|
52
129
|
}
|
|
53
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Attempts to perform a conversion based on the provided parameters.
|
|
132
|
+
* @param n - The size of deposit to make or withdraw from.
|
|
133
|
+
* @param conversion - The conversion parameters.
|
|
134
|
+
* @param conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
|
|
135
|
+
* @param conversion.amount - The amount to convert.
|
|
136
|
+
* @param conversion.rate - The exchange rate for conversion.
|
|
137
|
+
* @param conversion.info - Additional information for the conversion.
|
|
138
|
+
* @param conversion.ckbMinFee - Minimum CKB fee.
|
|
139
|
+
* @param conversion.lock - The script lock for the transaction.
|
|
140
|
+
* @param options - Additional options for the conversion.
|
|
141
|
+
* @returns A promise that resolves to the conversion result or undefined.
|
|
142
|
+
*/
|
|
143
|
+
async attempt(n, conversion, options) {
|
|
54
144
|
try {
|
|
145
|
+
const { isCkb2Udt, rate, info, ckbMinFee, lock } = conversion;
|
|
146
|
+
const { signer, userCells, filters, system } = options;
|
|
147
|
+
const { feeRate, ckbDepositCap, exchangeRatio, depositPool } = system;
|
|
55
148
|
const tx = utils_1.SmartTransaction.default();
|
|
56
|
-
const maturities = [];
|
|
149
|
+
const maturities = [[0n, 1n, 80n]]; // 3 minutes minimum
|
|
57
150
|
let ckbFee = core_1.ccc.Zero;
|
|
151
|
+
let amount = conversion.amount;
|
|
152
|
+
let udtWithdrawed = core_1.ccc.Zero;
|
|
153
|
+
// Core protocol
|
|
58
154
|
if (n > 0) {
|
|
59
155
|
if (isCkb2Udt) {
|
|
60
156
|
amount -= ckbDepositCap * BigInt(n);
|
|
61
|
-
if (amount <
|
|
157
|
+
if (amount < core_1.ccc.Zero) {
|
|
62
158
|
// Too many Deposits respectfully to the amount
|
|
63
159
|
return undefined;
|
|
64
160
|
}
|
|
65
|
-
this.logicManager.deposit(tx, n, ckbDepositCap,
|
|
161
|
+
this.logicManager.deposit(tx, n, ckbDepositCap, lock);
|
|
66
162
|
}
|
|
67
163
|
else {
|
|
68
|
-
const myDeposits =
|
|
164
|
+
const myDeposits = depositPool.slice(0, n);
|
|
69
165
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
70
|
-
|
|
71
|
-
|
|
166
|
+
udtWithdrawed = myDeposits.slice(-1)[0].udtCumulative;
|
|
167
|
+
amount -= udtWithdrawed;
|
|
168
|
+
if (amount < core_1.ccc.Zero) {
|
|
72
169
|
// Too many Withdrawal Requests respectfully to the amount
|
|
73
170
|
return undefined;
|
|
74
171
|
}
|
|
75
|
-
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits,
|
|
172
|
+
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, lock);
|
|
76
173
|
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
77
174
|
}
|
|
78
175
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
176
|
+
// Order protocol
|
|
177
|
+
if (amount > core_1.ccc.Zero) {
|
|
178
|
+
this.orderManager.mint(tx, lock, info, {
|
|
179
|
+
ckbValue: isCkb2Udt ? amount : core_1.ccc.Zero,
|
|
180
|
+
udtValue: isCkb2Udt ? core_1.ccc.Zero : amount,
|
|
181
|
+
});
|
|
182
|
+
const convertedAmount = (0, core_2.convert)(isCkb2Udt, amount, rate);
|
|
83
183
|
ckbFee = isCkb2Udt
|
|
84
184
|
? amount - (0, core_2.convert)(false, convertedAmount, exchangeRatio)
|
|
85
185
|
: (0, core_2.convert)(true, amount, exchangeRatio) - convertedAmount;
|
|
86
|
-
maturities.push(
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// feeRate is a good approximation of the bot tx size matching the order
|
|
92
|
-
if (10n * (feeRate - ckbFee) > ckbFee) {
|
|
93
|
-
return undefined;
|
|
186
|
+
maturities.push(orderMaturityEstimate(isCkb2Udt, amount, udtWithdrawed, system));
|
|
187
|
+
// Check that order provides enough fee to the bot for being matched
|
|
188
|
+
if (ckbFee < ckbMinFee) {
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
94
191
|
}
|
|
95
192
|
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
96
193
|
this.logicManager.completeDeposit(tx, receipts);
|
|
97
|
-
this.ownedOwnerManager.withdraw(tx, withdrawalGroups,
|
|
98
|
-
|
|
99
|
-
});
|
|
100
|
-
this.orderManager.melt(tx, orders);
|
|
194
|
+
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, filters);
|
|
195
|
+
this.orderManager.melt(tx, orders, filters);
|
|
101
196
|
this.ickbUdtManager.addUdts(tx, udts);
|
|
102
197
|
utils_1.CapacityManager.addCapacities(tx, capacities);
|
|
103
198
|
// Throw an error if inputs do not cover for outputs
|
|
@@ -115,22 +210,47 @@ class IckbController {
|
|
|
115
210
|
return undefined;
|
|
116
211
|
}
|
|
117
212
|
}
|
|
118
|
-
|
|
119
|
-
|
|
213
|
+
/**
|
|
214
|
+
* Retrieves the L1 state based on the provided parameters.
|
|
215
|
+
* @param client - The client to interact with the blockchain.
|
|
216
|
+
* @param filter - The set of filters to apply.
|
|
217
|
+
* @param locks - The script locks to filter the user cells.
|
|
218
|
+
* @param options - Optional parameters for the state retrieval.
|
|
219
|
+
* @param options.minLockUp - The minimum lock-up period (optional).
|
|
220
|
+
* @param options.maxLockUp - The maximum lock-up period (optional).
|
|
221
|
+
* @returns A promise that resolves to an object containing the system state and user cells.
|
|
222
|
+
*/
|
|
223
|
+
async getL1State(client, filter, locks, options) {
|
|
120
224
|
const tip = await client.getTipHeader();
|
|
121
225
|
const onChain = true;
|
|
122
226
|
const opts = { ...options, tip, onChain };
|
|
123
|
-
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests,] = await Promise.all([
|
|
227
|
+
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests, botCapacities,] = await Promise.all([
|
|
124
228
|
client.getFeeRate(),
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
229
|
+
filter.has("depositPool")
|
|
230
|
+
? (0, utils_1.collect)(this.logicManager.findDeposits(client, opts))
|
|
231
|
+
: [],
|
|
232
|
+
filter.has("capacities")
|
|
233
|
+
? (0, utils_1.collect)(utils_1.CapacityManager.findCapacities(client, locks, opts))
|
|
234
|
+
: [],
|
|
235
|
+
filter.has("udts")
|
|
236
|
+
? (0, utils_1.collect)(this.ickbUdtManager.findUdts(client, locks, opts))
|
|
237
|
+
: [],
|
|
238
|
+
filter.has("receipts")
|
|
239
|
+
? (0, utils_1.collect)(this.logicManager.findReceipts(client, locks, opts))
|
|
240
|
+
: [],
|
|
241
|
+
filter.has("withdrawalGroups")
|
|
242
|
+
? (0, utils_1.collect)(this.ownedOwnerManager.findWithdrawalGroups(client, locks, opts))
|
|
243
|
+
: [],
|
|
244
|
+
filter.has("orders") ? (0, utils_1.collect)(this.orderManager.findOrders(client)) : [],
|
|
245
|
+
filter.has("deposits")
|
|
246
|
+
? (0, utils_1.collect)(this.daoManager.findDeposits(client, locks, opts))
|
|
247
|
+
: [],
|
|
248
|
+
filter.has("withdrawalRequests")
|
|
249
|
+
? (0, utils_1.collect)(this.daoManager.findWithdrawalRequests(client, locks, opts))
|
|
250
|
+
: [],
|
|
251
|
+
filter.has("ckbBots")
|
|
252
|
+
? (0, utils_1.collect)(utils_1.CapacityManager.findCapacities(client, locks, opts))
|
|
253
|
+
: [],
|
|
134
254
|
]);
|
|
135
255
|
// Sort depositPool by maturity
|
|
136
256
|
depositPool.sort((a, b) => (0, utils_1.epochCompare)(a.maturity, b.maturity));
|
|
@@ -153,6 +273,11 @@ class IckbController {
|
|
|
153
273
|
}
|
|
154
274
|
const exchangeRatio = (0, core_2.ickbExchangeRatio)(tip);
|
|
155
275
|
const ckbDepositCap = (0, core_2.convert)(false, core_2.ICKB_DEPOSIT_CAP, exchangeRatio);
|
|
276
|
+
// Calculate the ckb ready to match incoming transactions
|
|
277
|
+
let ckbBots = botCapacities.reduce((acc, c) => acc + c.ckbValue, core_1.ccc.Zero);
|
|
278
|
+
// Each bot keeps a reserve of CKB for operations
|
|
279
|
+
ckbBots -= core_1.ccc.numFrom(this.bots.length) * core_1.ccc.fixedPointFrom("2000");
|
|
280
|
+
ckbBots = ckbBots > core_1.ccc.Zero ? ckbBots : core_1.ccc.Zero;
|
|
156
281
|
return {
|
|
157
282
|
system: {
|
|
158
283
|
feeRate,
|
|
@@ -160,8 +285,9 @@ class IckbController {
|
|
|
160
285
|
exchangeRatio,
|
|
161
286
|
ckbDepositCap,
|
|
162
287
|
udtDepositCap: core_2.ICKB_DEPOSIT_CAP,
|
|
163
|
-
|
|
164
|
-
|
|
288
|
+
depositPool: cumulativeDepositPool,
|
|
289
|
+
orderPool: nonUserOrders,
|
|
290
|
+
ckbBots,
|
|
165
291
|
},
|
|
166
292
|
user: {
|
|
167
293
|
capacities,
|
|
@@ -174,40 +300,42 @@ class IckbController {
|
|
|
174
300
|
},
|
|
175
301
|
};
|
|
176
302
|
}
|
|
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
303
|
}
|
|
202
304
|
exports.IckbController = IckbController;
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
305
|
+
/**
|
|
306
|
+
* Estimates the maturity of an order based on the type of conversion and amount.
|
|
307
|
+
*
|
|
308
|
+
* @param isCkb2Udt - A boolean indicating if the order is from CKB to UDT.
|
|
309
|
+
* @param amount - The amount of CKB or UDT involved in the order.
|
|
310
|
+
* @param udtWithdrawed - The amount of UDT that has been withdrawn by the current conversion via core.
|
|
311
|
+
* @param system - The current system state, which includes the block header tip, order pool, deposit pool, CKB bots, and exchange ratio.
|
|
312
|
+
*
|
|
313
|
+
* @returns The estimated maturity epoch for the order.
|
|
314
|
+
*/
|
|
315
|
+
function orderMaturityEstimate(isCkb2Udt, amount, udtWithdrawed, system) {
|
|
316
|
+
const { tip, orderPool, depositPool, ckbBots, exchangeRatio } = system;
|
|
317
|
+
// Maturity estimate a priori, the estimation is based on:
|
|
318
|
+
// - CKB to iCKB orders at 100k CKB every minute
|
|
319
|
+
// - iCKB to CKB orders at 200 CKB every minute
|
|
320
|
+
let maturity = (0, utils_1.epochAdd)(tip.epoch, [
|
|
208
321
|
0n,
|
|
209
322
|
1n + amount / core_1.ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
210
323
|
4n * 60n,
|
|
211
324
|
]);
|
|
325
|
+
if (isCkb2Udt) {
|
|
326
|
+
return maturity;
|
|
327
|
+
}
|
|
328
|
+
if (depositPool.length > 0) {
|
|
329
|
+
const udtWaiting = orderPool
|
|
330
|
+
.filter((o) => o.isUdt2CkbMatchable())
|
|
331
|
+
.reduce((acc, o) => acc + o.udtValue, core_1.ccc.Zero);
|
|
332
|
+
const udtAvailable = (0, core_2.convert)(isCkb2Udt, ckbBots, exchangeRatio);
|
|
333
|
+
const udtToConvert = amount + udtWithdrawed + udtWaiting - udtAvailable;
|
|
334
|
+
const i = (0, utils_1.binarySearch)(depositPool.length,
|
|
335
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
336
|
+
(n) => depositPool[n].udtCumulative >= udtToConvert);
|
|
337
|
+
maturity = depositPool[i]?.maturity ?? maturity;
|
|
338
|
+
}
|
|
339
|
+
return maturity;
|
|
212
340
|
}
|
|
213
341
|
//# sourceMappingURL=index.js.map
|