@ickb/sdk 1000.0.43 → 1000.0.71
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/codec.d.ts +14 -0
- package/dist/cjs/codec.d.ts.map +1 -0
- package/dist/cjs/codec.js +125 -0
- package/dist/cjs/codec.js.map +1 -0
- package/dist/cjs/constants.d.ts +49 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +156 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -78
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +17 -232
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/sdk.d.ts +178 -0
- package/dist/cjs/sdk.d.ts.map +1 -0
- package/dist/cjs/sdk.js +362 -0
- package/dist/cjs/sdk.js.map +1 -0
- package/dist/esm/codec.d.ts +14 -0
- package/dist/esm/codec.d.ts.map +1 -0
- package/dist/esm/codec.js +122 -0
- package/dist/esm/codec.js.map +1 -0
- package/dist/esm/constants.d.ts +49 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +152 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/index.d.ts +3 -78
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +3 -230
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/sdk.d.ts +178 -0
- package/dist/esm/sdk.d.ts.map +1 -0
- package/dist/esm/sdk.js +358 -0
- package/dist/esm/sdk.js.map +1 -0
- package/package.json +9 -9
- package/src/codec.ts +138 -0
- package/src/constants.ts +200 -0
- package/src/index.ts +3 -407
- package/src/sdk.ts +513 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { IckbUdtManager, LogicManager, OwnedOwnerManager } from "@ickb/core";
|
|
3
|
+
import { DaoManager } from "@ickb/dao";
|
|
4
|
+
import { OrderManager } from "@ickb/order";
|
|
5
|
+
import { CapacityManager, unique } from "@ickb/utils";
|
|
6
|
+
/**
|
|
7
|
+
* The iCKB UDT name.
|
|
8
|
+
*/
|
|
9
|
+
export const UDT_NAME = "iCKB";
|
|
10
|
+
/**
|
|
11
|
+
* The iCKB UDT symbol.
|
|
12
|
+
*/
|
|
13
|
+
export const UDT_SYMBOL = "iCKB";
|
|
14
|
+
/**
|
|
15
|
+
* The iCKB UDT number of decimals.
|
|
16
|
+
*/
|
|
17
|
+
export const UDT_DECIMALS = 8;
|
|
18
|
+
/**
|
|
19
|
+
* Retrieves the configuration for the given deployment environment.
|
|
20
|
+
*
|
|
21
|
+
* Accepts either a string identifier ("mainnet" or "testnet") or a custom configuration
|
|
22
|
+
* object containing script dependencies for a devnet.
|
|
23
|
+
*
|
|
24
|
+
* It sets up various managers (UDT, Logic, OwnedOwner, Order, Dao, Capacity) and also
|
|
25
|
+
* aggregates a unique list of known bot scripts.
|
|
26
|
+
*
|
|
27
|
+
* @param deps - Either a network identifier ("mainnet"/"testnet") or an object containing
|
|
28
|
+
* explicit script dependencies for devnet.
|
|
29
|
+
* @param bots - An optional array of bot script-like objects to augment the list of known bots.
|
|
30
|
+
* @returns An object containing the instantiated managers and bots.
|
|
31
|
+
*/
|
|
32
|
+
export function getConfig(deps, bots = []) {
|
|
33
|
+
// If deps is provided as a network string, use the pre-defined constants.
|
|
34
|
+
if (deps === "mainnet" || deps === "testnet") {
|
|
35
|
+
bots = bots.concat(deps === "mainnet" ? MAINNET_KNOWN_BOTS : TESTNET_KNOWN_BOTS);
|
|
36
|
+
const depGroup = deps === "mainnet" ? MAINNET_DEP_GROUP : TESTNET_DEP_GROUP;
|
|
37
|
+
deps = {
|
|
38
|
+
udt: from(UDT, depGroup),
|
|
39
|
+
ickbLogic: from(ICKB_LOGIC, depGroup),
|
|
40
|
+
ownedOwner: from(OWNED_OWNER, depGroup),
|
|
41
|
+
order: from(ORDER, depGroup),
|
|
42
|
+
dao: from(DAO, depGroup),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const daoManager = DaoManager.fromDeps(deps);
|
|
46
|
+
const ickbUdtManager = IckbUdtManager.fromDeps(deps, daoManager);
|
|
47
|
+
return {
|
|
48
|
+
managers: {
|
|
49
|
+
udt: ickbUdtManager,
|
|
50
|
+
ickbLogic: LogicManager.fromDeps(deps, daoManager, ickbUdtManager),
|
|
51
|
+
ownedOwner: OwnedOwnerManager.fromDeps(deps, daoManager, ickbUdtManager),
|
|
52
|
+
order: OrderManager.fromDeps(deps, ickbUdtManager),
|
|
53
|
+
dao: daoManager,
|
|
54
|
+
capacity: CapacityManager.withAnyData(),
|
|
55
|
+
},
|
|
56
|
+
bots: [...unique(bots.map((b) => ccc.Script.from(b)))],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Wraps a script-like object into a ScriptDeps structure.
|
|
61
|
+
*
|
|
62
|
+
* @param script - The script or script-like object.
|
|
63
|
+
* @param cellDeps - Additional cell dependencies that the script may require.
|
|
64
|
+
* @returns An object containing the script and its associated cell dependencies.
|
|
65
|
+
*/
|
|
66
|
+
function from(script, ...cellDeps) {
|
|
67
|
+
return {
|
|
68
|
+
script: ccc.Script.from(script),
|
|
69
|
+
cellDeps,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* UDT (User Defined Token) lock script information used for onchain validation.
|
|
74
|
+
*/
|
|
75
|
+
const UDT = {
|
|
76
|
+
codeHash: "0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95",
|
|
77
|
+
hashType: "data1",
|
|
78
|
+
args: "0xb73b6ab39d79390c6de90a09c96b290c331baf1798ed6f97aed02590929734e800000080",
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* DAO (Decentralized Autonomous Organization) lock script information.
|
|
82
|
+
*/
|
|
83
|
+
const DAO = {
|
|
84
|
+
codeHash: "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
|
|
85
|
+
hashType: "type",
|
|
86
|
+
args: "0x",
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Logic script information for the iCKB logic contract.
|
|
90
|
+
*/
|
|
91
|
+
const ICKB_LOGIC = {
|
|
92
|
+
codeHash: "0x2a8100ab5990fa055ab1b50891702e1e895c7bd1df6322cd725c1a6115873bd3",
|
|
93
|
+
hashType: "data1",
|
|
94
|
+
args: "0x",
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* OwnedOwner lock script information.
|
|
98
|
+
*/
|
|
99
|
+
const OWNED_OWNER = {
|
|
100
|
+
codeHash: "0xacc79e07d107831feef4c70c9e683dac5644d5993b9cb106dca6e74baa381bd0",
|
|
101
|
+
hashType: "data1",
|
|
102
|
+
args: "0x",
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Order lock script information.
|
|
106
|
+
*/
|
|
107
|
+
const ORDER = {
|
|
108
|
+
codeHash: "0x49dfb6afee5cc8ac4225aeea8cb8928b150caf3cd92fea33750683c74b13254a",
|
|
109
|
+
hashType: "data1",
|
|
110
|
+
args: "0x",
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Mainnet dependency group cell dep.
|
|
114
|
+
*/
|
|
115
|
+
const MAINNET_DEP_GROUP = ccc.CellDep.from({
|
|
116
|
+
outPoint: {
|
|
117
|
+
txHash: "0x621a6f38de3b9f453016780edac3b26bfcbfa3e2ecb47c2da275471a5d3ed165",
|
|
118
|
+
index: "0x0",
|
|
119
|
+
},
|
|
120
|
+
depType: "depGroup",
|
|
121
|
+
});
|
|
122
|
+
/**
|
|
123
|
+
* Testnet dependency group cell dep.
|
|
124
|
+
*/
|
|
125
|
+
const TESTNET_DEP_GROUP = ccc.CellDep.from({
|
|
126
|
+
outPoint: {
|
|
127
|
+
txHash: "0xf7ece4fb33d8378344cab11fcd6a4c6f382fd4207ac921cf5821f30712dcd311",
|
|
128
|
+
index: "0x0",
|
|
129
|
+
},
|
|
130
|
+
depType: "depGroup",
|
|
131
|
+
});
|
|
132
|
+
/**
|
|
133
|
+
* Array of known bot scripts on the mainnet.
|
|
134
|
+
*/
|
|
135
|
+
const MAINNET_KNOWN_BOTS = [
|
|
136
|
+
{
|
|
137
|
+
codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
|
|
138
|
+
hashType: "type",
|
|
139
|
+
args: "0xd096cb29e2f68a85a46bd6bf6cbee6327959ba64",
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
/**
|
|
143
|
+
* Array of known bot scripts on the testnet.
|
|
144
|
+
*/
|
|
145
|
+
const TESTNET_KNOWN_BOTS = [
|
|
146
|
+
{
|
|
147
|
+
codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
|
|
148
|
+
hashType: "type",
|
|
149
|
+
args: "0xb4380110f7679ac31cefe4925485645d82bf619f",
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,EAAmB,MAAM,aAAa,CAAC;AAEvE;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,MAAM,CAAC;AAE/B;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC;AAEjC;;GAEG;AACH,MAAM,CAAC,MA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O,CAAC,IAAI,CAAC;IACzC,QAAQ,EAAE;QACR,MAAM,EACJ,oEAAoE;QACtE,KAAK,EAAE,KAAK;KACb;IACD,OAAO,EAAE,UAAU;CACpB,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,kBAAkB,GAAG;IACzB;QACE,QAAQ,EACN,oEAAoE;QACtE,QAAQ,EAAE,MAAM;QAChB,IAAI,EAAE,4CAA4C;KACnD;CACF,CAAC;AAEF;;GAEG;AACH,MAAM,kBAAkB,GAAG;IACzB;QACE,QAAQ,EACN,oEAAoE;QACtE,QAAQ,EAAE,MAAM;QAChB,IAAI,EAAE,4CAA4C;KACnD;CACF,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,79 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { DaoManager, type DaoCell } from "@ickb/dao";
|
|
5
|
-
import { OrderManager, type OrderCell, type OrderGroup } from "@ickb/order";
|
|
6
|
-
export declare class IckbController {
|
|
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);
|
|
13
|
-
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
14
|
-
xudt: ScriptDeps;
|
|
15
|
-
dao: ScriptDeps;
|
|
16
|
-
ickbLogic: ScriptDeps;
|
|
17
|
-
ownedOwner: ScriptDeps;
|
|
18
|
-
order: ScriptDeps;
|
|
19
|
-
}): IckbController;
|
|
20
|
-
static previewConversion(conversion: {
|
|
21
|
-
isCkb2Udt: boolean;
|
|
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;
|
|
35
|
-
userCells: UserCells;
|
|
36
|
-
system: SystemState;
|
|
37
|
-
filters: {
|
|
38
|
-
isReadyOnly: boolean;
|
|
39
|
-
isFulfilledOnly: boolean;
|
|
40
|
-
};
|
|
41
|
-
}): Promise<Conversion | undefined>;
|
|
42
|
-
private attempt;
|
|
43
|
-
getL1State(client: ccc.Client, filter: Set<keyof UserCells | "depositPool">, locks: ccc.Script[], options?: {
|
|
44
|
-
minLockUp?: ccc.Epoch;
|
|
45
|
-
maxLockUp?: ccc.Epoch;
|
|
46
|
-
}): Promise<{
|
|
47
|
-
system: SystemState;
|
|
48
|
-
user: UserCells;
|
|
49
|
-
}>;
|
|
50
|
-
}
|
|
51
|
-
export interface SystemState {
|
|
52
|
-
feeRate: ccc.Num;
|
|
53
|
-
tip: ccc.ClientBlockHeader;
|
|
54
|
-
exchangeRatio: {
|
|
55
|
-
ckbScale: ccc.Num;
|
|
56
|
-
udtScale: ccc.Num;
|
|
57
|
-
};
|
|
58
|
-
ckbDepositCap: ccc.FixedPoint;
|
|
59
|
-
udtDepositCap: ccc.FixedPoint;
|
|
60
|
-
depositPool: (IckbDepositCell & {
|
|
61
|
-
udtCumulative: bigint;
|
|
62
|
-
})[];
|
|
63
|
-
orderPool: OrderCell[];
|
|
64
|
-
}
|
|
65
|
-
export interface UserCells {
|
|
66
|
-
capacities: CapacityCell[];
|
|
67
|
-
udts: UdtCell[];
|
|
68
|
-
receipts: ReceiptCell[];
|
|
69
|
-
withdrawalGroups: WithdrawalGroup[];
|
|
70
|
-
orders: OrderGroup[];
|
|
71
|
-
deposits: DaoCell[];
|
|
72
|
-
withdrawalRequests: DaoCell[];
|
|
73
|
-
}
|
|
74
|
-
export interface Conversion {
|
|
75
|
-
tx: SmartTransaction;
|
|
76
|
-
ckbFee: ccc.FixedPoint;
|
|
77
|
-
maturity: ccc.Epoch;
|
|
78
|
-
}
|
|
1
|
+
export * from "./sdk.js";
|
|
2
|
+
export * from "./codec.js";
|
|
3
|
+
export * from "./constants.js";
|
|
79
4
|
//# 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,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,231 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { DaoManager } from "@ickb/dao";
|
|
5
|
-
import { Info, OrderManager, } from "@ickb/order";
|
|
6
|
-
export class IckbController {
|
|
7
|
-
constructor(ickbUdtManager, logicManager, ownedOwnerManager, orderManager, daoManager) {
|
|
8
|
-
this.ickbUdtManager = ickbUdtManager;
|
|
9
|
-
this.logicManager = logicManager;
|
|
10
|
-
this.ownedOwnerManager = ownedOwnerManager;
|
|
11
|
-
this.orderManager = orderManager;
|
|
12
|
-
this.daoManager = daoManager;
|
|
13
|
-
}
|
|
14
|
-
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
15
|
-
const daoManager = DaoManager.fromDeps(dao);
|
|
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);
|
|
23
|
-
}
|
|
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
|
-
};
|
|
43
|
-
}
|
|
44
|
-
const convertedAmount = amount > ccc.Zero ? convert(isCkb2Udt, amount, rate) : ccc.Zero;
|
|
45
|
-
return { rate, convertedAmount };
|
|
46
|
-
}
|
|
47
|
-
async convert(options) {
|
|
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
|
-
};
|
|
68
|
-
const N = isCkb2Udt
|
|
69
|
-
? Number(amount / system.ckbDepositCap)
|
|
70
|
-
: system.depositPool.slice(0, 30).length;
|
|
71
|
-
const cache = Array(N);
|
|
72
|
-
const cachedAttempt = (n) => {
|
|
73
|
-
n = N - n;
|
|
74
|
-
return (cache[n] = cache[n] ?? this.attempt(n, conversion, options));
|
|
75
|
-
};
|
|
76
|
-
return cachedAttempt(await asyncBinarySearch(N, async (n) => (await cachedAttempt(n)) !== undefined));
|
|
77
|
-
}
|
|
78
|
-
async attempt(n, conversion, options) {
|
|
79
|
-
try {
|
|
80
|
-
const { isCkb2Udt, rate, info, ckbMinFee, lock } = conversion;
|
|
81
|
-
const { signer, userCells, system, filters } = options;
|
|
82
|
-
const { feeRate, depositPool, ckbDepositCap, exchangeRatio } = system;
|
|
83
|
-
const tx = SmartTransaction.default();
|
|
84
|
-
const maturities = [];
|
|
85
|
-
let ckbFee = ccc.Zero;
|
|
86
|
-
let amount = conversion.amount;
|
|
87
|
-
// Core protocol
|
|
88
|
-
if (n > 0) {
|
|
89
|
-
if (isCkb2Udt) {
|
|
90
|
-
amount -= ckbDepositCap * BigInt(n);
|
|
91
|
-
if (amount < ccc.Zero) {
|
|
92
|
-
// Too many Deposits respectfully to the amount
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
this.logicManager.deposit(tx, n, ckbDepositCap, lock);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
const myDeposits = depositPool.slice(0, n);
|
|
99
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
100
|
-
amount -= myDeposits.slice(-1)[0].udtCumulative;
|
|
101
|
-
if (amount < ccc.Zero) {
|
|
102
|
-
// Too many Withdrawal Requests respectfully to the amount
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
this.ownedOwnerManager.requestWithdrawal(tx, myDeposits, lock);
|
|
106
|
-
maturities.push(...myDeposits.map((d) => d.maturity));
|
|
107
|
-
}
|
|
108
|
-
}
|
|
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);
|
|
116
|
-
ckbFee = isCkb2Udt
|
|
117
|
-
? amount - convert(false, convertedAmount, exchangeRatio)
|
|
118
|
-
: convert(true, amount, exchangeRatio) - convertedAmount;
|
|
119
|
-
maturities.push(
|
|
120
|
-
// TODO: use real world data from deposits, open orders and bot balances
|
|
121
|
-
orderMaturityEstimate(isCkb2Udt, amount, system.tip));
|
|
122
|
-
}
|
|
123
|
-
// Check that order provides enough fee to the bot for being matched
|
|
124
|
-
if (ckbFee < ckbMinFee) {
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
const { receipts, withdrawalGroups, orders, udts, capacities } = userCells;
|
|
128
|
-
this.logicManager.completeDeposit(tx, receipts);
|
|
129
|
-
this.ownedOwnerManager.withdraw(tx, withdrawalGroups, filters);
|
|
130
|
-
this.orderManager.melt(tx, orders, filters);
|
|
131
|
-
this.ickbUdtManager.addUdts(tx, udts);
|
|
132
|
-
CapacityManager.addCapacities(tx, capacities);
|
|
133
|
-
// Throw an error if inputs do not cover for outputs
|
|
134
|
-
await new RestrictedTransaction(tx).completeFeeBy(signer, feeRate);
|
|
135
|
-
// Find the maturity most distant in the future
|
|
136
|
-
const maturity = maturities.reduce((a, b) => epochCompare(a, b) === -1 ? b : a);
|
|
137
|
-
return {
|
|
138
|
-
tx,
|
|
139
|
-
ckbFee,
|
|
140
|
-
maturity,
|
|
141
|
-
};
|
|
142
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
143
|
-
}
|
|
144
|
-
catch (_) {
|
|
145
|
-
return undefined;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
async getL1State(client, filter, locks, options) {
|
|
149
|
-
const tip = await client.getTipHeader();
|
|
150
|
-
const onChain = true;
|
|
151
|
-
const opts = { ...options, tip, onChain };
|
|
152
|
-
const [feeRate, depositPool, capacities, udts, receipts, withdrawalGroups, orders, deposits, withdrawalRequests,] = await Promise.all([
|
|
153
|
-
client.getFeeRate(),
|
|
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
|
-
: [],
|
|
176
|
-
// TODO: fetch bot cells for estimating timings
|
|
177
|
-
]);
|
|
178
|
-
// Sort depositPool by maturity
|
|
179
|
-
depositPool.sort((a, b) => epochCompare(a.maturity, b.maturity));
|
|
180
|
-
// Calculate cumulative value of deposits in the pool
|
|
181
|
-
let udtCumulative = 0n;
|
|
182
|
-
const cumulativeDepositPool = [];
|
|
183
|
-
for (const deposit of depositPool) {
|
|
184
|
-
udtCumulative += deposit.udtValue;
|
|
185
|
-
cumulativeDepositPool.push({ ...deposit, udtCumulative });
|
|
186
|
-
}
|
|
187
|
-
const userOrders = [];
|
|
188
|
-
const nonUserOrders = [];
|
|
189
|
-
for (const group of orders) {
|
|
190
|
-
if (group.isOwner(...locks)) {
|
|
191
|
-
userOrders.push(group);
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
nonUserOrders.push(group.order);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
const exchangeRatio = ickbExchangeRatio(tip);
|
|
198
|
-
const ckbDepositCap = convert(false, ICKB_DEPOSIT_CAP, exchangeRatio);
|
|
199
|
-
return {
|
|
200
|
-
system: {
|
|
201
|
-
feeRate,
|
|
202
|
-
tip,
|
|
203
|
-
exchangeRatio,
|
|
204
|
-
ckbDepositCap,
|
|
205
|
-
udtDepositCap: ICKB_DEPOSIT_CAP,
|
|
206
|
-
depositPool: cumulativeDepositPool,
|
|
207
|
-
orderPool: nonUserOrders,
|
|
208
|
-
},
|
|
209
|
-
user: {
|
|
210
|
-
capacities,
|
|
211
|
-
udts,
|
|
212
|
-
receipts,
|
|
213
|
-
withdrawalGroups,
|
|
214
|
-
orders: userOrders,
|
|
215
|
-
deposits,
|
|
216
|
-
withdrawalRequests,
|
|
217
|
-
},
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
// Estimate bot ability to fulfill orders:
|
|
222
|
-
// - CKB to iCKB orders at 100k CKB every minute
|
|
223
|
-
// - iCKB to CKB orders at 200 CKB every minute
|
|
224
|
-
function orderMaturityEstimate(isCkb2Udt, amount, tip) {
|
|
225
|
-
return epochAdd(tip.epoch, [
|
|
226
|
-
0n,
|
|
227
|
-
1n + amount / ccc.fixedPointFrom(isCkb2Udt ? 100000 : 200),
|
|
228
|
-
4n * 60n,
|
|
229
|
-
]);
|
|
230
|
-
}
|
|
1
|
+
export * from "./sdk.js";
|
|
2
|
+
export * from "./codec.js";
|
|
3
|
+
export * from "./constants.js";
|
|
231
4
|
//# 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":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,gBAAgB,CAAC"}
|