@ickb/sdk 1000.0.52 → 1000.0.81
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/codec.d.ts +3 -0
- package/dist/codec.d.ts.map +1 -0
- package/dist/codec.js +58 -0
- package/dist/codec.js.map +1 -0
- package/dist/constants.d.ts +23 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +95 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/sdk.d.ts +51 -0
- package/dist/sdk.d.ts.map +1 -0
- package/dist/sdk.js +242 -0
- package/dist/sdk.js.map +1 -0
- package/package.json +35 -20
- package/src/codec.ts +138 -0
- package/src/constants.ts +205 -0
- package/src/index.ts +3 -573
- package/src/sdk.ts +513 -0
- package/dist/cjs/index.d.ts +0 -190
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js +0 -341
- package/dist/cjs/index.js.map +0 -1
- package/dist/esm/index.d.ts +0 -190
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js +0 -337
- package/dist/esm/index.js.map +0 -1
package/src/codec.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { ccc, mol } from "@ckb-ccc/core";
|
|
2
|
+
import { max } from "@ickb/utils";
|
|
3
|
+
|
|
4
|
+
const N = 1024;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Codec for encoding and decoding a pool snapshot.
|
|
8
|
+
*
|
|
9
|
+
* The PoolSnapshotCodec encodes an array of 1024 numbers representing event counts for bins,
|
|
10
|
+
* where the number of bits used per bin is dynamically computed as the ceiling of log2(max + 1)
|
|
11
|
+
* across all bins. The codec provides methods to encode the array into a Uint8Array and decode
|
|
12
|
+
* a Uint8Array back to the original array of numbers.
|
|
13
|
+
*
|
|
14
|
+
* @remarks The total number of bits for encoding all the bins is computed as bitsPerBin * 1024. The codec
|
|
15
|
+
* does not have a fixed byteLength since it depends on the data.
|
|
16
|
+
*/
|
|
17
|
+
export const PoolSnapshot = mol.Codec.from<number[]>({
|
|
18
|
+
/**
|
|
19
|
+
* Encodes an array of 1024 bin counts into a Uint8Array.
|
|
20
|
+
*
|
|
21
|
+
* @param bins - An array of 1024 numbers. Each number represents the event count for a bin.
|
|
22
|
+
* @returns A Uint8Array containing the packed bit representation of the bin counts.
|
|
23
|
+
* @throws Error if the input array does not contain exactly 1024 elements.
|
|
24
|
+
*/
|
|
25
|
+
encode: (bins) => {
|
|
26
|
+
if (bins.length !== N) {
|
|
27
|
+
throw new Error("Expected 1024 bins");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const bitsPerBin = computeBits(bins);
|
|
31
|
+
const buffer = new Uint8Array((bitsPerBin * N) >> 3);
|
|
32
|
+
|
|
33
|
+
let bitOffset = 0;
|
|
34
|
+
for (const count of bins) {
|
|
35
|
+
packBits(buffer, bitOffset, bitsPerBin, count);
|
|
36
|
+
bitOffset += bitsPerBin;
|
|
37
|
+
}
|
|
38
|
+
return buffer;
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Decodes a buffer into an array of 1024 bin counts.
|
|
43
|
+
*
|
|
44
|
+
* The function automatically computes the number of bits per bin from the buffer length,
|
|
45
|
+
* expecting that the total number of bits in the buffer is a multiple of 1024.
|
|
46
|
+
*
|
|
47
|
+
* @param bufferLike - The input that can be converted into a Uint8Array.
|
|
48
|
+
* @returns An array of 1024 numbers representing the decoded bin counts.
|
|
49
|
+
* @throws Error if the buffer length is invalid (i.e., its total bit count is not divisible by 1024).
|
|
50
|
+
*/
|
|
51
|
+
decode: (bufferLike) => {
|
|
52
|
+
const buffer = ccc.bytesFrom(bufferLike);
|
|
53
|
+
// Determine bitsPerBin from the fixed structure (totalBits divided by N).
|
|
54
|
+
const bitsPerBin = (buffer.byteLength * 8) / N;
|
|
55
|
+
if (!Number.isInteger(bitsPerBin)) {
|
|
56
|
+
throw new Error("Invalid buffer length for 1024 bins");
|
|
57
|
+
}
|
|
58
|
+
const bins = new Array<number>(N);
|
|
59
|
+
let bitOffset = 0;
|
|
60
|
+
for (let i = 0; i < N; i++) {
|
|
61
|
+
bins[i] = unpackBits(buffer, bitOffset, bitsPerBin);
|
|
62
|
+
bitOffset += bitsPerBin;
|
|
63
|
+
}
|
|
64
|
+
return bins;
|
|
65
|
+
},
|
|
66
|
+
// Note: The byteLength is not fixed as it depends on the data (i.e., the maximum bin count).
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Computes the minimal number of bits required to represent the maximum number in the bins.
|
|
71
|
+
*
|
|
72
|
+
* Given an array of numbers that represent counts for each bin, this function calculates
|
|
73
|
+
* the number of bits required to represent the maximum bin count. A minimum of 1 bit is
|
|
74
|
+
* always returned.
|
|
75
|
+
*
|
|
76
|
+
* @param bins - An array of numbers, each representing a bin's value.
|
|
77
|
+
* @returns The minimal number of bits required to represent the highest value among the bins.
|
|
78
|
+
*/
|
|
79
|
+
function computeBits(bins: number[]): number {
|
|
80
|
+
return Math.ceil(Math.log2(1 + max(1, ...bins)));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Packs a given numeric value into a Uint8Array (bit array) at the specified bit offset and width.
|
|
85
|
+
*
|
|
86
|
+
* The function encodes the given number into the provided buffer, using the specified number of bits (width)
|
|
87
|
+
* starting from the specified bit offset. Each bit of the value is written into the buffer.
|
|
88
|
+
*
|
|
89
|
+
* @param buffer - The Uint8Array into which the value will be packed.
|
|
90
|
+
* @param bitOffset - The starting bit index in the array where the value should be packed.
|
|
91
|
+
* @param width - The number of bits to use for packing the value.
|
|
92
|
+
* @param value - The numeric value to pack.
|
|
93
|
+
*/
|
|
94
|
+
function packBits(
|
|
95
|
+
buffer: Uint8Array,
|
|
96
|
+
bitOffset: number,
|
|
97
|
+
width: number,
|
|
98
|
+
value: number,
|
|
99
|
+
): void {
|
|
100
|
+
let offset = bitOffset;
|
|
101
|
+
for (let i = 0; i < width; i++) {
|
|
102
|
+
const byteIndex = offset >> 3;
|
|
103
|
+
const bitInByte = offset % 8;
|
|
104
|
+
const bit = (value >> i) & 1;
|
|
105
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
106
|
+
buffer[byteIndex]! |= bit << bitInByte;
|
|
107
|
+
offset++;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Unpacks a numeric value from a Uint8Array (bit array) starting at a given bit offset with a specified width.
|
|
113
|
+
*
|
|
114
|
+
* The function reads bits from the buffer starting at the bit offset and reconstructs a numeric value
|
|
115
|
+
* using the specified number of bits (width).
|
|
116
|
+
*
|
|
117
|
+
* @param buffer - The Uint8Array from which the value will be unpacked.
|
|
118
|
+
* @param bitOffset - The starting bit index in the buffer.
|
|
119
|
+
* @param width - The number of bits to read for unpacking the value.
|
|
120
|
+
* @returns The numeric value resulting from decoding the specified bits.
|
|
121
|
+
*/
|
|
122
|
+
function unpackBits(
|
|
123
|
+
buffer: Uint8Array,
|
|
124
|
+
bitOffset: number,
|
|
125
|
+
width: number,
|
|
126
|
+
): number {
|
|
127
|
+
let value = 0;
|
|
128
|
+
let offset = bitOffset;
|
|
129
|
+
for (let i = 0; i < width; i++) {
|
|
130
|
+
const byteIndex = offset >> 3;
|
|
131
|
+
const bitInByte = offset % 8;
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
133
|
+
const bit = (buffer[byteIndex]! >> bitInByte) & 1;
|
|
134
|
+
value |= bit << i;
|
|
135
|
+
offset++;
|
|
136
|
+
}
|
|
137
|
+
return value;
|
|
138
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
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, type ScriptDeps } from "@ickb/utils";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Retrieves the configuration for the given deployment environment.
|
|
9
|
+
*
|
|
10
|
+
* Accepts either a string identifier ("mainnet" or "testnet") or a custom configuration
|
|
11
|
+
* object containing script dependencies for a devnet.
|
|
12
|
+
*
|
|
13
|
+
* It sets up various managers (UDT, Logic, OwnedOwner, Order, Dao, Capacity) and also
|
|
14
|
+
* aggregates a unique list of known bot scripts.
|
|
15
|
+
*
|
|
16
|
+
* @param d - Either a network identifier ("mainnet"/"testnet") or an object containing
|
|
17
|
+
* explicit script dependencies for devnet.
|
|
18
|
+
* @param bots - An optional array of bot script-like objects to augment the list of known bots.
|
|
19
|
+
* @returns An object containing the instantiated managers and bots.
|
|
20
|
+
*/
|
|
21
|
+
export function getConfig(
|
|
22
|
+
d:
|
|
23
|
+
| "mainnet"
|
|
24
|
+
| "testnet"
|
|
25
|
+
| {
|
|
26
|
+
// For devnet configuration provide explicit script dependencies.
|
|
27
|
+
udt: ScriptDeps;
|
|
28
|
+
logic: ScriptDeps;
|
|
29
|
+
ownedOwner: ScriptDeps;
|
|
30
|
+
order: ScriptDeps;
|
|
31
|
+
dao: ScriptDeps;
|
|
32
|
+
},
|
|
33
|
+
bots: ccc.ScriptLike[] = [],
|
|
34
|
+
): {
|
|
35
|
+
managers: {
|
|
36
|
+
capacity: CapacityManager;
|
|
37
|
+
dao: DaoManager;
|
|
38
|
+
ickbUdt: IckbUdtManager;
|
|
39
|
+
logic: LogicManager;
|
|
40
|
+
ownedOwner: OwnedOwnerManager;
|
|
41
|
+
order: OrderManager;
|
|
42
|
+
};
|
|
43
|
+
bots: ccc.Script[];
|
|
44
|
+
} {
|
|
45
|
+
// If deps is provided as a network string, use the pre-defined constants.
|
|
46
|
+
if (d === "mainnet" || d === "testnet") {
|
|
47
|
+
bots = bots.concat(
|
|
48
|
+
d === "mainnet" ? MAINNET_KNOWN_BOTS : TESTNET_KNOWN_BOTS,
|
|
49
|
+
);
|
|
50
|
+
const depGroup = d === "mainnet" ? MAINNET_DEP_GROUP : TESTNET_DEP_GROUP;
|
|
51
|
+
d = {
|
|
52
|
+
udt: from(UDT, depGroup),
|
|
53
|
+
logic: from(ICKB_LOGIC, depGroup),
|
|
54
|
+
ownedOwner: from(OWNED_OWNER, depGroup),
|
|
55
|
+
order: from(ORDER, depGroup),
|
|
56
|
+
dao: from(DAO, depGroup),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const capacity = CapacityManager.withAnyData();
|
|
61
|
+
const dao = new DaoManager(d.dao.script, d.dao.cellDeps);
|
|
62
|
+
const ickbUdt = new IckbUdtManager(
|
|
63
|
+
d.udt.script,
|
|
64
|
+
d.udt.cellDeps,
|
|
65
|
+
d.logic.script,
|
|
66
|
+
dao,
|
|
67
|
+
);
|
|
68
|
+
const logic = new LogicManager(
|
|
69
|
+
d.logic.script,
|
|
70
|
+
d.logic.cellDeps,
|
|
71
|
+
dao,
|
|
72
|
+
ickbUdt,
|
|
73
|
+
);
|
|
74
|
+
const ownedOwner = new OwnedOwnerManager(
|
|
75
|
+
d.ownedOwner.script,
|
|
76
|
+
d.ownedOwner.cellDeps,
|
|
77
|
+
dao,
|
|
78
|
+
ickbUdt,
|
|
79
|
+
);
|
|
80
|
+
const order = new OrderManager(d.order.script, d.order.cellDeps, ickbUdt);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
managers: {
|
|
84
|
+
capacity,
|
|
85
|
+
dao,
|
|
86
|
+
ickbUdt,
|
|
87
|
+
logic,
|
|
88
|
+
ownedOwner,
|
|
89
|
+
order,
|
|
90
|
+
},
|
|
91
|
+
bots: [...unique(bots.map((b) => ccc.Script.from(b)))],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Wraps a script-like object into a ScriptDeps structure.
|
|
97
|
+
*
|
|
98
|
+
* @param script - The script or script-like object.
|
|
99
|
+
* @param cellDeps - Additional cell dependencies that the script may require.
|
|
100
|
+
* @returns An object containing the script and its associated cell dependencies.
|
|
101
|
+
*/
|
|
102
|
+
function from(script: ccc.ScriptLike, ...cellDeps: ccc.CellDep[]): ScriptDeps {
|
|
103
|
+
return {
|
|
104
|
+
script: ccc.Script.from(script),
|
|
105
|
+
cellDeps,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* DAO (Decentralized Autonomous Organization) lock script information.
|
|
111
|
+
*/
|
|
112
|
+
const DAO = {
|
|
113
|
+
codeHash:
|
|
114
|
+
"0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
|
|
115
|
+
hashType: "type",
|
|
116
|
+
args: "0x",
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* UDT (User Defined Token) lock script information used for onchain validation.
|
|
121
|
+
*/
|
|
122
|
+
const UDT = {
|
|
123
|
+
codeHash:
|
|
124
|
+
"0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95",
|
|
125
|
+
hashType: "data1",
|
|
126
|
+
args: "0xb73b6ab39d79390c6de90a09c96b290c331baf1798ed6f97aed02590929734e800000080",
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Logic script information for the iCKB logic contract.
|
|
131
|
+
*/
|
|
132
|
+
const ICKB_LOGIC = {
|
|
133
|
+
codeHash:
|
|
134
|
+
"0x2a8100ab5990fa055ab1b50891702e1e895c7bd1df6322cd725c1a6115873bd3",
|
|
135
|
+
hashType: "data1",
|
|
136
|
+
args: "0x",
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* OwnedOwner lock script information.
|
|
141
|
+
*/
|
|
142
|
+
const OWNED_OWNER = {
|
|
143
|
+
codeHash:
|
|
144
|
+
"0xacc79e07d107831feef4c70c9e683dac5644d5993b9cb106dca6e74baa381bd0",
|
|
145
|
+
hashType: "data1",
|
|
146
|
+
args: "0x",
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Order lock script information.
|
|
151
|
+
*/
|
|
152
|
+
const ORDER = {
|
|
153
|
+
codeHash:
|
|
154
|
+
"0x49dfb6afee5cc8ac4225aeea8cb8928b150caf3cd92fea33750683c74b13254a",
|
|
155
|
+
hashType: "data1",
|
|
156
|
+
args: "0x",
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Mainnet dependency group cell dep.
|
|
161
|
+
*/
|
|
162
|
+
const MAINNET_DEP_GROUP = ccc.CellDep.from({
|
|
163
|
+
outPoint: {
|
|
164
|
+
txHash:
|
|
165
|
+
"0x621a6f38de3b9f453016780edac3b26bfcbfa3e2ecb47c2da275471a5d3ed165",
|
|
166
|
+
index: "0x0",
|
|
167
|
+
},
|
|
168
|
+
depType: "depGroup",
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Testnet dependency group cell dep.
|
|
173
|
+
*/
|
|
174
|
+
const TESTNET_DEP_GROUP = ccc.CellDep.from({
|
|
175
|
+
outPoint: {
|
|
176
|
+
txHash:
|
|
177
|
+
"0xf7ece4fb33d8378344cab11fcd6a4c6f382fd4207ac921cf5821f30712dcd311",
|
|
178
|
+
index: "0x0",
|
|
179
|
+
},
|
|
180
|
+
depType: "depGroup",
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Array of known bot scripts on the mainnet.
|
|
185
|
+
*/
|
|
186
|
+
const MAINNET_KNOWN_BOTS = [
|
|
187
|
+
{
|
|
188
|
+
codeHash:
|
|
189
|
+
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
|
|
190
|
+
hashType: "type",
|
|
191
|
+
args: "0xd096cb29e2f68a85a46bd6bf6cbee6327959ba64",
|
|
192
|
+
},
|
|
193
|
+
];
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Array of known bot scripts on the testnet.
|
|
197
|
+
*/
|
|
198
|
+
const TESTNET_KNOWN_BOTS = [
|
|
199
|
+
{
|
|
200
|
+
codeHash:
|
|
201
|
+
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
|
|
202
|
+
hashType: "type",
|
|
203
|
+
args: "0xb4380110f7679ac31cefe4925485645d82bf619f",
|
|
204
|
+
},
|
|
205
|
+
];
|