@ickb/sdk 0.0.0
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/LICENSE +19 -0
- package/README.md +35 -0
- package/dist/cjs/index.d.ts +36 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +47 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.d.ts +36 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +43 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +46 -0
- package/src/index.ts +102 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# iCKB/SDK
|
|
2
|
+
|
|
3
|
+
iCKB SDK built on top of CCC
|
|
4
|
+
|
|
5
|
+
## Dependencies
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD;
|
|
9
|
+
A["@ickb/utils"] --> B["@ckb-ccc/core"];
|
|
10
|
+
C["@ickb/dao"] --> A;
|
|
11
|
+
C --> B;
|
|
12
|
+
D["@ickb/core"] --> A;
|
|
13
|
+
D --> C;
|
|
14
|
+
E["@ickb/order"] --> A;
|
|
15
|
+
E --> B;
|
|
16
|
+
F["@ickb/sdk"] --> A;
|
|
17
|
+
F --> C;
|
|
18
|
+
F --> D;
|
|
19
|
+
F --> E;
|
|
20
|
+
|
|
21
|
+
click A "https://github.com/ickb/utils" "Go to @ickb/utils"
|
|
22
|
+
click B "https://github.com/ckb-devrel/ccc/tree/master/packages/core" "Go to @ckb-ccc/core"
|
|
23
|
+
click C "https://github.com/ickb/dao" "Go to @ickb/dao"
|
|
24
|
+
click D "https://github.com/ickb/core" "Go to @ickb/core"
|
|
25
|
+
click E "https://github.com/ickb/order" "Go to @ickb/order"
|
|
26
|
+
click F "https://github.com/ickb/sdk" "Go to @ickb/sdk"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Epoch Semantic Versioning
|
|
30
|
+
|
|
31
|
+
This repository follows [Epoch Semantic Versioning](https://antfu.me/posts/epoch-semver). In short ESV aims to provide a more nuanced and effective way to communicate software changes, allowing for better user understanding and smoother upgrades.
|
|
32
|
+
|
|
33
|
+
## Licensing
|
|
34
|
+
|
|
35
|
+
This source code, crafted with care by [Phroi](https://phroi.com/), is freely available on [GitHub](https://github.com/ickb/sdk) and it is released under the [MIT License](./LICENSE).
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { type ScriptDeps } from "@ickb/utils";
|
|
3
|
+
import { LogicManager, OwnedOwnerManager, type iCKBDepositCell, type ReceiptCell, type WithdrawalGroups } from "@ickb/core";
|
|
4
|
+
import { OrderManager, Ratio, type OrderGroup } from "@ickb/order";
|
|
5
|
+
export declare class IckbController {
|
|
6
|
+
logicManager: LogicManager;
|
|
7
|
+
ownedOwnerManager: OwnedOwnerManager;
|
|
8
|
+
orderManager: OrderManager;
|
|
9
|
+
constructor(logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager);
|
|
10
|
+
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
11
|
+
xudt: ScriptDeps;
|
|
12
|
+
dao: ScriptDeps;
|
|
13
|
+
ickbLogic: ScriptDeps;
|
|
14
|
+
ownedOwner: ScriptDeps;
|
|
15
|
+
order: ScriptDeps;
|
|
16
|
+
}): IckbController;
|
|
17
|
+
previewConversion(isCkb2Udt: boolean, amount: ccc.FixedPoint, options: ({
|
|
18
|
+
ratio: Ratio;
|
|
19
|
+
} | {
|
|
20
|
+
tip: ccc.ClientBlockHeader;
|
|
21
|
+
}) & {
|
|
22
|
+
fee?: {
|
|
23
|
+
numerator: ccc.Num;
|
|
24
|
+
denominator: ccc.Num;
|
|
25
|
+
};
|
|
26
|
+
}): ccc.FixedPoint;
|
|
27
|
+
fetchL1State(client: ccc.Client, locks: ccc.ScriptLike[]): Promise<L1State>;
|
|
28
|
+
}
|
|
29
|
+
export interface L1State {
|
|
30
|
+
tip: ccc.ClientBlockHeader;
|
|
31
|
+
depositPool: iCKBDepositCell[];
|
|
32
|
+
receipts: ReceiptCell[];
|
|
33
|
+
withdrawalGroups: WithdrawalGroups[];
|
|
34
|
+
orders: OrderGroup[];
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;A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,iBAAiB,CAAC;IAC3B,WAAW,EAAE,eAAe,EAAE,CAAC;IAC/B,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,gBAAgB,EAAE,gBAAgB,EAAE,CAAC;IACrC,MAAM,EAAE,UAAU,EAAE,CAAC;CACtB"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IckbController = void 0;
|
|
4
|
+
const utils_1 = require("@ickb/utils");
|
|
5
|
+
const core_1 = require("@ickb/core");
|
|
6
|
+
const dao_1 = require("@ickb/dao");
|
|
7
|
+
const order_1 = require("@ickb/order");
|
|
8
|
+
class IckbController {
|
|
9
|
+
constructor(logicManager, ownedOwnerManager, orderManager) {
|
|
10
|
+
this.logicManager = logicManager;
|
|
11
|
+
this.ownedOwnerManager = ownedOwnerManager;
|
|
12
|
+
this.orderManager = orderManager;
|
|
13
|
+
}
|
|
14
|
+
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
15
|
+
const daoManager = dao_1.DaoManager.fromDeps(dao);
|
|
16
|
+
const udtHandler = core_1.iCKBUdtHandler.fromDeps(xudt, daoManager);
|
|
17
|
+
return new IckbController(core_1.LogicManager.fromDeps(ickbLogic, daoManager, udtHandler), core_1.OwnedOwnerManager.fromDeps(ownedOwner, daoManager, udtHandler), order_1.OrderManager.fromDeps(order, udtHandler));
|
|
18
|
+
}
|
|
19
|
+
previewConversion(isCkb2Udt, amount, options) {
|
|
20
|
+
const { ckbScale, udtScale } = "ratio" in options ? options.ratio : (0, core_1.ickbExchangeRatio)(options.tip);
|
|
21
|
+
let convertedAmount = isCkb2Udt
|
|
22
|
+
? (amount * ckbScale) / udtScale
|
|
23
|
+
: (amount * udtScale) / ckbScale;
|
|
24
|
+
if (options.fee) {
|
|
25
|
+
const { numerator, denominator } = options.fee;
|
|
26
|
+
convertedAmount -= (convertedAmount * numerator) / denominator;
|
|
27
|
+
}
|
|
28
|
+
return convertedAmount;
|
|
29
|
+
}
|
|
30
|
+
async fetchL1State(client, locks) {
|
|
31
|
+
const tip = await client.getTipHeader();
|
|
32
|
+
const onChain = true;
|
|
33
|
+
const options = {
|
|
34
|
+
tip,
|
|
35
|
+
onChain,
|
|
36
|
+
};
|
|
37
|
+
const [depositPool, receipts, withdrawalGroups, orders] = await Promise.all([
|
|
38
|
+
(0, utils_1.arrayFrom)(this.logicManager.findDeposits(client, options)),
|
|
39
|
+
(0, utils_1.arrayFrom)(this.logicManager.findReceipts(client, locks, options)),
|
|
40
|
+
(0, utils_1.arrayFrom)(this.ownedOwnerManager.findWithdrawalGroups(client, locks, options)),
|
|
41
|
+
this.orderManager.findOrders(client),
|
|
42
|
+
]);
|
|
43
|
+
return { tip, depositPool, receipts, withdrawalGroups, orders };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.IckbController = IckbController;
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AACA,uCAAyD;AACzD,qCAQoB;AACpB,mCAAuC;AACvC,uCAAmE;AAEnE,MAAa,cAAc;IACzB,YACS,YAA0B,EAC1B,iBAAoC,EACpC,YAA0B;QAF1B,iBAAY,GAAZ,YAAY,CAAc;QAC1B,sBAAiB,GAAjB,iBAAiB,CAAmB;QACpC,iBAAY,GAAZ,YAAY,CAAc;IAChC,CAAC;IAEJ,MAAM,CAAC,QAAQ,CAAC,EACd,IAAI,EACJ,GAAG,EACH,SAAS,EACT,UAAU,EACV,KAAK,GAON;QACC,MAAM,UAAU,GAAG,gBAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC5C,MAAM,UAAU,GAAG,qBAAc,CAAC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;QAE7D,OAAO,IAAI,cAAc,CACvB,mBAAY,CAAC,QAAQ,CAAC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IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;YACjE,IAAA,iBAAS,EACP,IAAI,CAAC,iBAAiB,CAAC,oBAAoB,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CACpE;YACD,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,MAAM,CAAC;SACrC,CACF,CAAC;QAEF,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAClE,CAAC;CACF;AA/ED,wCA+EC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { type ScriptDeps } from "@ickb/utils";
|
|
3
|
+
import { LogicManager, OwnedOwnerManager, type iCKBDepositCell, type ReceiptCell, type WithdrawalGroups } from "@ickb/core";
|
|
4
|
+
import { OrderManager, Ratio, type OrderGroup } from "@ickb/order";
|
|
5
|
+
export declare class IckbController {
|
|
6
|
+
logicManager: LogicManager;
|
|
7
|
+
ownedOwnerManager: OwnedOwnerManager;
|
|
8
|
+
orderManager: OrderManager;
|
|
9
|
+
constructor(logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager);
|
|
10
|
+
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }: {
|
|
11
|
+
xudt: ScriptDeps;
|
|
12
|
+
dao: ScriptDeps;
|
|
13
|
+
ickbLogic: ScriptDeps;
|
|
14
|
+
ownedOwner: ScriptDeps;
|
|
15
|
+
order: ScriptDeps;
|
|
16
|
+
}): IckbController;
|
|
17
|
+
previewConversion(isCkb2Udt: boolean, amount: ccc.FixedPoint, options: ({
|
|
18
|
+
ratio: Ratio;
|
|
19
|
+
} | {
|
|
20
|
+
tip: ccc.ClientBlockHeader;
|
|
21
|
+
}) & {
|
|
22
|
+
fee?: {
|
|
23
|
+
numerator: ccc.Num;
|
|
24
|
+
denominator: ccc.Num;
|
|
25
|
+
};
|
|
26
|
+
}): ccc.FixedPoint;
|
|
27
|
+
fetchL1State(client: ccc.Client, locks: ccc.ScriptLike[]): Promise<L1State>;
|
|
28
|
+
}
|
|
29
|
+
export interface L1State {
|
|
30
|
+
tip: ccc.ClientBlockHeader;
|
|
31
|
+
depositPool: iCKBDepositCell[];
|
|
32
|
+
receipts: ReceiptCell[];
|
|
33
|
+
withdrawalGroups: WithdrawalGroups[];
|
|
34
|
+
orders: OrderGroup[];
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAa,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,EAGL,YAAY,EACZ,iBAAiB,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACtB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAEnE,qBAAa,cAAc;IAEhB,YAAY,EAAE,YAAY;IAC1B,iBAAiB,EAAE,iBAAiB;IACpC,YAAY,EAAE,YAAY;gBAF1B,YAAY,EAAE,YAAY,EAC1B,iBAAiB,EAAE,iBAAiB,EACpC,YAAY,EAAE,YAAY;IAGnC,MAAM,CAAC,QAAQ,CAAC,EACd,IAAI,EACJ,GAAG,EACH,SAAS,EACT,UAAU,EACV,KAAK,GACN,EAAE;QACD,IAAI,EAAE,UAAU,CAAC;QACjB,GAAG,EAAE,UAAU,CAAC;QAChB,SAAS,EAAE,UAAU,CAAC;QACtB,UAAU,EAAE,UAAU,CAAC;QACvB,KAAK,EAAE,UAAU,CAAC;KACnB,GAAG,cAAc;IAWlB,iBAAiB,CACf,SAAS,EAAE,OAAO,EAClB,MAAM,EAAE,GAAG,CAAC,UAAU,EACtB,OAAO,EAAE,CAAC;QAAE,KAAK,EAAE,KAAK,CAAA;KAAE,GAAG;QAAE,GAAG,EAAE,GAAG,CAAC,iBAAiB,CAAA;KAAE,CAAC,GAAG;QAC7D,GAAG,CAAC,EAAE;YACJ,SAAS,EAAE,GAAG,CAAC,GAAG,CAAC;YACnB,WAAW,EAAE,GAAG,CAAC,GAAG,CAAC;SACtB,CAAC;KACH,GACA,GAAG,CAAC,UAAU;IAgBX,YAAY,CAChB,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,KAAK,EAAE,GAAG,CAAC,UAAU,EAAE,GACtB,OAAO,CAAC,OAAO,CAAC;CAqBpB;AAED,MAAM,WAAW,OAAO;IACtB,GAAG,EAAE,GAAG,CAAC,iBAAiB,CAAC;IAC3B,WAAW,EAAE,eAAe,EAAE,CAAC;IAC/B,QAAQ,EAAE,WAAW,EAAE,CAAC;IACxB,gBAAgB,EAAE,gBAAgB,EAAE,CAAC;IACrC,MAAM,EAAE,UAAU,EAAE,CAAC;CACtB"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { arrayFrom } from "@ickb/utils";
|
|
2
|
+
import { ickbExchangeRatio, iCKBUdtHandler, LogicManager, OwnedOwnerManager, } from "@ickb/core";
|
|
3
|
+
import { DaoManager } from "@ickb/dao";
|
|
4
|
+
import { OrderManager } from "@ickb/order";
|
|
5
|
+
export class IckbController {
|
|
6
|
+
constructor(logicManager, ownedOwnerManager, orderManager) {
|
|
7
|
+
this.logicManager = logicManager;
|
|
8
|
+
this.ownedOwnerManager = ownedOwnerManager;
|
|
9
|
+
this.orderManager = orderManager;
|
|
10
|
+
}
|
|
11
|
+
static fromDeps({ xudt, dao, ickbLogic, ownedOwner, order, }) {
|
|
12
|
+
const daoManager = DaoManager.fromDeps(dao);
|
|
13
|
+
const udtHandler = iCKBUdtHandler.fromDeps(xudt, daoManager);
|
|
14
|
+
return new IckbController(LogicManager.fromDeps(ickbLogic, daoManager, udtHandler), OwnedOwnerManager.fromDeps(ownedOwner, daoManager, udtHandler), OrderManager.fromDeps(order, udtHandler));
|
|
15
|
+
}
|
|
16
|
+
previewConversion(isCkb2Udt, amount, options) {
|
|
17
|
+
const { ckbScale, udtScale } = "ratio" in options ? options.ratio : ickbExchangeRatio(options.tip);
|
|
18
|
+
let convertedAmount = isCkb2Udt
|
|
19
|
+
? (amount * ckbScale) / udtScale
|
|
20
|
+
: (amount * udtScale) / ckbScale;
|
|
21
|
+
if (options.fee) {
|
|
22
|
+
const { numerator, denominator } = options.fee;
|
|
23
|
+
convertedAmount -= (convertedAmount * numerator) / denominator;
|
|
24
|
+
}
|
|
25
|
+
return convertedAmount;
|
|
26
|
+
}
|
|
27
|
+
async fetchL1State(client, locks) {
|
|
28
|
+
const tip = await client.getTipHeader();
|
|
29
|
+
const onChain = true;
|
|
30
|
+
const options = {
|
|
31
|
+
tip,
|
|
32
|
+
onChain,
|
|
33
|
+
};
|
|
34
|
+
const [depositPool, receipts, withdrawalGroups, orders] = await Promise.all([
|
|
35
|
+
arrayFrom(this.logicManager.findDeposits(client, options)),
|
|
36
|
+
arrayFrom(this.logicManager.findReceipts(client, locks, options)),
|
|
37
|
+
arrayFrom(this.ownedOwnerManager.findWithdrawalGroups(client, locks, options)),
|
|
38
|
+
this.orderManager.findOrders(client),
|
|
39
|
+
]);
|
|
40
|
+
return { tip, depositPool, receipts, withdrawalGroups, orders };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAmB,MAAM,aAAa,CAAC;AACzD,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,iBAAiB,GAIlB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,UAAU,EA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,CAAC;YAC/C,eAAe,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC,GAAG,WAAW,CAAC;QACjE,CAAC;QAED,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,MAAkB,EAClB,KAAuB;QAEvB,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,YAAY,EAAE,CAAC;QACxC,MAAM,OAAO,GAAG,IAAI,CAAC;QACrB,MAAM,OAAO,GAAG;YACd,GAAG;YACH,OAAO;SACR,CAAC;QAEF,MAAM,CAAC,WAAW,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CACzE;YACE,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YAC1D,SAAS,CAAC,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;YACjE,SAAS,CACP,IAAI,CAAC,iBAAiB,CAAC,oBAAoB,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,CACpE;YACD,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,MAAM,CAAC;SACrC,CACF,CAAC;QAEF,OAAO,EAAE,GAAG,EAAE,WAAW,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAClE,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ickb/sdk",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "iCKB SDK built on top of CCC",
|
|
5
|
+
"author": "phroi",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"private": false,
|
|
8
|
+
"homepage": "https://github.com/ickb/sdk",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git://github.com/ickb/sdk.git"
|
|
12
|
+
},
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"main": "dist/cjs/index.js",
|
|
15
|
+
"module": "dist/esm/index.js",
|
|
16
|
+
"types": "dist/esm/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"/dist",
|
|
19
|
+
"/src"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"provenance": true
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@eslint/js": "^9.25.1",
|
|
27
|
+
"eslint": "^9.25.1",
|
|
28
|
+
"prettier": "^3.5.3",
|
|
29
|
+
"typescript": "^5.8.3",
|
|
30
|
+
"typescript-eslint": "^8.31.0"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@ckb-ccc/core": "^1.8.0",
|
|
34
|
+
"@ickb/core": "^1000.0.19",
|
|
35
|
+
"@ickb/dao": "^1000.0.19",
|
|
36
|
+
"@ickb/order": "^1000.0.17",
|
|
37
|
+
"@ickb/utils": "^1000.0.15"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "pnpm run build:esm && pnpm run build:cjs && node dist/cjs/index.js",
|
|
41
|
+
"build:esm": "tsc",
|
|
42
|
+
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
43
|
+
"lint": "eslint ./src",
|
|
44
|
+
"up2lint": "rm pnpm-lock.yaml && pnpm up && pnpm build && pnpm lint"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ccc } from "@ckb-ccc/core";
|
|
2
|
+
import { arrayFrom, type ScriptDeps } from "@ickb/utils";
|
|
3
|
+
import {
|
|
4
|
+
ickbExchangeRatio,
|
|
5
|
+
iCKBUdtHandler,
|
|
6
|
+
LogicManager,
|
|
7
|
+
OwnedOwnerManager,
|
|
8
|
+
type iCKBDepositCell,
|
|
9
|
+
type ReceiptCell,
|
|
10
|
+
type WithdrawalGroups,
|
|
11
|
+
} from "@ickb/core";
|
|
12
|
+
import { DaoManager } from "@ickb/dao";
|
|
13
|
+
import { OrderManager, Ratio, type OrderGroup } from "@ickb/order";
|
|
14
|
+
|
|
15
|
+
export class IckbController {
|
|
16
|
+
constructor(
|
|
17
|
+
public logicManager: LogicManager,
|
|
18
|
+
public ownedOwnerManager: OwnedOwnerManager,
|
|
19
|
+
public orderManager: OrderManager,
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
static fromDeps({
|
|
23
|
+
xudt,
|
|
24
|
+
dao,
|
|
25
|
+
ickbLogic,
|
|
26
|
+
ownedOwner,
|
|
27
|
+
order,
|
|
28
|
+
}: {
|
|
29
|
+
xudt: ScriptDeps;
|
|
30
|
+
dao: ScriptDeps;
|
|
31
|
+
ickbLogic: ScriptDeps;
|
|
32
|
+
ownedOwner: ScriptDeps;
|
|
33
|
+
order: ScriptDeps;
|
|
34
|
+
}): IckbController {
|
|
35
|
+
const daoManager = DaoManager.fromDeps(dao);
|
|
36
|
+
const udtHandler = iCKBUdtHandler.fromDeps(xudt, daoManager);
|
|
37
|
+
|
|
38
|
+
return new IckbController(
|
|
39
|
+
LogicManager.fromDeps(ickbLogic, daoManager, udtHandler),
|
|
40
|
+
OwnedOwnerManager.fromDeps(ownedOwner, daoManager, udtHandler),
|
|
41
|
+
OrderManager.fromDeps(order, udtHandler),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
previewConversion(
|
|
46
|
+
isCkb2Udt: boolean,
|
|
47
|
+
amount: ccc.FixedPoint,
|
|
48
|
+
options: ({ ratio: Ratio } | { tip: ccc.ClientBlockHeader }) & {
|
|
49
|
+
fee?: {
|
|
50
|
+
numerator: ccc.Num;
|
|
51
|
+
denominator: ccc.Num;
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
): ccc.FixedPoint {
|
|
55
|
+
const { ckbScale, udtScale } =
|
|
56
|
+
"ratio" in options ? options.ratio : ickbExchangeRatio(options.tip);
|
|
57
|
+
|
|
58
|
+
let convertedAmount = isCkb2Udt
|
|
59
|
+
? (amount * ckbScale) / udtScale
|
|
60
|
+
: (amount * udtScale) / ckbScale;
|
|
61
|
+
|
|
62
|
+
if (options.fee) {
|
|
63
|
+
const { numerator, denominator } = options.fee;
|
|
64
|
+
convertedAmount -= (convertedAmount * numerator) / denominator;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return convertedAmount;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async fetchL1State(
|
|
71
|
+
client: ccc.Client,
|
|
72
|
+
locks: ccc.ScriptLike[],
|
|
73
|
+
): Promise<L1State> {
|
|
74
|
+
const tip = await client.getTipHeader();
|
|
75
|
+
const onChain = true;
|
|
76
|
+
const options = {
|
|
77
|
+
tip,
|
|
78
|
+
onChain,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const [depositPool, receipts, withdrawalGroups, orders] = await Promise.all(
|
|
82
|
+
[
|
|
83
|
+
arrayFrom(this.logicManager.findDeposits(client, options)),
|
|
84
|
+
arrayFrom(this.logicManager.findReceipts(client, locks, options)),
|
|
85
|
+
arrayFrom(
|
|
86
|
+
this.ownedOwnerManager.findWithdrawalGroups(client, locks, options),
|
|
87
|
+
),
|
|
88
|
+
this.orderManager.findOrders(client),
|
|
89
|
+
],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return { tip, depositPool, receipts, withdrawalGroups, orders };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface L1State {
|
|
97
|
+
tip: ccc.ClientBlockHeader;
|
|
98
|
+
depositPool: iCKBDepositCell[];
|
|
99
|
+
receipts: ReceiptCell[];
|
|
100
|
+
withdrawalGroups: WithdrawalGroups[];
|
|
101
|
+
orders: OrderGroup[];
|
|
102
|
+
}
|