@arkiv-network/sdk 0.6.8 → 0.7.0-dev.1
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/README.md +52 -16
- package/dist/consts-Bz0LZQZY.js +7 -0
- package/dist/consts-Bz0LZQZY.js.map +1 -0
- package/dist/consts-CxT1-RXU.cjs +18 -0
- package/dist/entity-BhSXaB23.cjs +81 -0
- package/dist/{query-CO6f35AE.d.cts → entity-CN41IOrq.d.cts} +125 -59
- package/dist/entity-CN41IOrq.d.cts.map +1 -0
- package/dist/entity-DGfVyB-a.js +71 -0
- package/dist/entity-DGfVyB-a.js.map +1 -0
- package/dist/{query-BhdmAzYg.d.ts → entity-NnEVlePN.d.ts} +125 -59
- package/dist/entity-NnEVlePN.d.ts.map +1 -0
- package/dist/index-ClkpS6s_.d.ts +481 -0
- package/dist/index-ClkpS6s_.d.ts.map +1 -0
- package/dist/index-w4qop0Pe.d.cts +481 -0
- package/dist/index-w4qop0Pe.d.cts.map +1 -0
- package/dist/index.cjs +285 -233
- package/dist/index.d.cts +100 -6
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +101 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +280 -230
- package/dist/index.js.map +1 -1
- package/dist/payload-BpNUPOnY.js +13 -0
- package/dist/payload-BpNUPOnY.js.map +1 -0
- package/dist/{payload-Da9gJMG5.d.ts → payload-C2KHOUoj.d.ts} +1 -1
- package/dist/{payload-Da9gJMG5.d.ts.map → payload-C2KHOUoj.d.ts.map} +1 -1
- package/dist/{payload-vwU3Rc6P.cjs → payload-D8YLKcJj.cjs} +1 -18
- package/dist/query/index.cjs +11 -28
- package/dist/query/index.d.cts +3 -3
- package/dist/query/index.d.ts +3 -3
- package/dist/query/index.js +8 -28
- package/dist/query/index.js.map +1 -1
- package/dist/{queryBuilder-DonAg1aq.js → queryBuilder-BE_XhLLa.js} +303 -152
- package/dist/queryBuilder-BE_XhLLa.js.map +1 -0
- package/dist/{queryBuilder-DM-YO1Y7.cjs → queryBuilder-J37buGCi.cjs} +372 -149
- package/dist/types/index.cjs +4 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +4 -1
- package/dist/utils/index.cjs +4 -10
- package/dist/utils/index.d.cts +0 -1
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.ts +1 -2
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -3
- package/dist/utils/index.js.map +1 -1
- package/package.json +11 -15
- package/src/actions/public/buildQuery.ts +4 -0
- package/src/actions/public/query.ts +10 -0
- package/src/actions/public/subscribeEntityEvents.ts +25 -51
- package/src/actions/wallet/changeOwnership.ts +6 -6
- package/src/actions/wallet/createEntity.ts +19 -6
- package/src/actions/wallet/deleteEntity.ts +2 -3
- package/src/actions/wallet/extendEntity.ts +9 -5
- package/src/actions/wallet/mutateEntities.ts +11 -54
- package/src/actions/wallet/updateEntity.ts +15 -6
- package/src/clients/createPublicClient.test.ts +2 -0
- package/src/clients/decorators/arkivPublic.ts +67 -2
- package/src/clients/decorators/arkivWallet.ts +23 -1
- package/src/consts.ts +2 -1
- package/src/errors.ts +46 -0
- package/src/index.ts +0 -3
- package/src/query/engine.test.ts +45 -0
- package/src/query/engine.ts +28 -14
- package/src/query/index.ts +1 -0
- package/src/query/predicate.test.ts +43 -0
- package/src/query/predicate.ts +14 -10
- package/src/query/queryBuilder.test.ts +22 -0
- package/src/query/queryBuilder.ts +221 -106
- package/src/query/queryResult.ts +13 -7
- package/src/query/selectQueryBuilder.test.ts +213 -0
- package/src/query/selection.test.ts +120 -0
- package/src/query/selection.ts +116 -0
- package/src/types/attributes.ts +25 -1
- package/src/types/entity.ts +12 -2
- package/src/types/events.ts +14 -16
- package/src/types/index.ts +1 -0
- package/src/types/rpcSchema.ts +7 -2
- package/src/utils/arkivTransactions.test.ts +260 -0
- package/src/utils/arkivTransactions.ts +255 -92
- package/src/utils/entities.ts +17 -7
- package/src/utils/index.ts +0 -1
- package/src/utils/payload.ts +4 -2
- package/src/utils/validation.test.ts +106 -0
- package/src/utils/validation.ts +80 -0
- package/dist/accounts/index.cjs +0 -9
- package/dist/accounts/index.d.cts +0 -1
- package/dist/accounts/index.d.ts +0 -1
- package/dist/accounts/index.js +0 -1
- package/dist/payload-DKBYYjeA.js +0 -18
- package/dist/payload-DKBYYjeA.js.map +0 -1
- package/dist/query-BhdmAzYg.d.ts.map +0 -1
- package/dist/query-CO6f35AE.d.cts.map +0 -1
- package/dist/queryBuilder-BvoQXa_u.d.ts +0 -328
- package/dist/queryBuilder-BvoQXa_u.d.ts.map +0 -1
- package/dist/queryBuilder-DonAg1aq.js.map +0 -1
- package/dist/queryBuilder-zTsKXctT.d.cts +0 -328
- package/dist/queryBuilder-zTsKXctT.d.cts.map +0 -1
- package/src/accounts/index.ts +0 -1
- /package/dist/{types-H-TSmRpm.js → types-Ci4BRClw.js} +0 -0
- /package/dist/{types-o3zWarRp.cjs → types-a8gm_IaQ.cjs} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
const require_chains = require('./chains-BifaY04V.cjs');
|
|
2
|
-
const require_queryBuilder = require('./queryBuilder-
|
|
3
|
-
const
|
|
4
|
-
require('./
|
|
2
|
+
const require_queryBuilder = require('./queryBuilder-J37buGCi.cjs');
|
|
3
|
+
const require_consts = require('./consts-CxT1-RXU.cjs');
|
|
4
|
+
const require_entity = require('./entity-BhSXaB23.cjs');
|
|
5
|
+
require('./types-a8gm_IaQ.cjs');
|
|
6
|
+
const require_payload = require('./payload-D8YLKcJj.cjs');
|
|
5
7
|
let viem = require("viem");
|
|
6
8
|
|
|
7
9
|
//#region src/actions/public/getBlockTiming.ts
|
|
8
|
-
const logger$
|
|
10
|
+
const logger$8 = require_queryBuilder.getLogger("actions:public:get-block-timing");
|
|
9
11
|
async function getBlockTiming(client) {
|
|
10
12
|
const blockTiming = await client.request({
|
|
11
13
|
method: "arkiv_getBlockTiming",
|
|
12
14
|
params: []
|
|
13
15
|
});
|
|
14
|
-
logger$
|
|
16
|
+
logger$8("Block timing %o", blockTiming);
|
|
15
17
|
return {
|
|
16
18
|
currentBlock: BigInt(blockTiming.current_block),
|
|
17
19
|
currentBlockTime: blockTiming.current_block_time,
|
|
@@ -97,78 +99,289 @@ async function query(client, query$1, queryOptions) {
|
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
//#endregion
|
|
100
|
-
//#region src/
|
|
101
|
-
const logger$
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
//#region src/utils/arkivTransactions.ts
|
|
103
|
+
const logger$7 = require_queryBuilder.getLogger("utils:arkiv-transactions");
|
|
104
|
+
const MIME_REGEX = /^[a-z][a-z0-9!#$&\-^_]*\/[a-z0-9][a-z0-9!#$&\-^_.+]*$/;
|
|
105
|
+
function validateContentType(contentType) {
|
|
106
|
+
if (!MIME_REGEX.test(contentType)) throw new require_queryBuilder.InvalidContentTypeError(contentType);
|
|
107
|
+
}
|
|
108
|
+
const ENTITY_EXECUTE_ABI = (0, viem.parseAbi)(["function execute((uint8 operationType, bytes32 entityKey, bytes payload, (bytes32[4] data) contentType, (bytes32 name, uint8 valueType, bytes32[4] value)[] attributes, uint32 expiresIn, address newOwner)[] ops) external"]);
|
|
109
|
+
const ENTITY_OPERATION_EVENT_ABI = (0, viem.parseAbi)(["event EntityOperation(bytes32 indexed entityKey, uint8 indexed operationType, address indexed owner, uint32 expiresAt, bytes32 entityHash)"]);
|
|
110
|
+
const ENTITY_ERRORS_ABI = (0, viem.parseAbi)([
|
|
111
|
+
"error EmptyBatch()",
|
|
112
|
+
"error AttributesNotSorted()",
|
|
113
|
+
"error InvalidValueType(bytes32 name, uint8 valueType)",
|
|
114
|
+
"error InvalidOpType(uint8 operationType)",
|
|
115
|
+
"error ExpiryInPast(uint32 expiresAt, uint32 currentBlock)",
|
|
116
|
+
"error TooManyAttributes(uint256 count, uint256 maxCount)",
|
|
117
|
+
"error EntityNotFound(bytes32 entityKey)",
|
|
118
|
+
"error NotOwner(bytes32 entityKey, address caller, address owner)",
|
|
119
|
+
"error EntityExpired(bytes32 entityKey, uint32 expiresAt)",
|
|
120
|
+
"error ExpiryNotExtended(bytes32 entityKey, uint32 newExpiresAt, uint32 currentExpiresAt)",
|
|
121
|
+
"error TransferToZeroAddress(bytes32 entityKey)",
|
|
122
|
+
"error TransferToSelf(bytes32 entityKey)",
|
|
123
|
+
"error EntityNotExpired(bytes32 entityKey, uint32 expiresAt)",
|
|
124
|
+
"error Ident32Empty()",
|
|
125
|
+
"error Ident32TooLong(uint256 length)",
|
|
126
|
+
"error Ident32InvalidByte(uint256 position, bytes1 value)",
|
|
127
|
+
"error MimeEmpty()",
|
|
128
|
+
"error MimeTooLong(uint256 length, uint256 maxLength)",
|
|
129
|
+
"error MimeInvalidByte(uint256 position, bytes1 value)",
|
|
130
|
+
"error MimeIncomplete()"
|
|
109
131
|
]);
|
|
132
|
+
const EXECUTE_ABI = [...ENTITY_EXECUTE_ABI, ...ENTITY_ERRORS_ABI];
|
|
133
|
+
const ENTITY_NONCE_ABI = (0, viem.parseAbi)(["function nonces(address owner) view returns (uint32)"]);
|
|
134
|
+
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
135
|
+
const ZERO_32 = `0x${"00".repeat(32)}`;
|
|
136
|
+
const EMPTY_BYTES128 = [
|
|
137
|
+
ZERO_32,
|
|
138
|
+
ZERO_32,
|
|
139
|
+
ZERO_32,
|
|
140
|
+
ZERO_32
|
|
141
|
+
];
|
|
142
|
+
function encodeBytes128(data) {
|
|
143
|
+
const padded = new Uint8Array(128);
|
|
144
|
+
padded.set(data.slice(0, 128));
|
|
145
|
+
return [
|
|
146
|
+
(0, viem.toHex)(padded.slice(0, 32)),
|
|
147
|
+
(0, viem.toHex)(padded.slice(32, 64)),
|
|
148
|
+
(0, viem.toHex)(padded.slice(64, 96)),
|
|
149
|
+
(0, viem.toHex)(padded.slice(96, 128))
|
|
150
|
+
];
|
|
151
|
+
}
|
|
152
|
+
function encodeMime128(contentType) {
|
|
153
|
+
return { data: contentType ? encodeBytes128((0, viem.stringToBytes)(contentType)) : EMPTY_BYTES128 };
|
|
154
|
+
}
|
|
155
|
+
function encodeAttribute(attr) {
|
|
156
|
+
require_queryBuilder.validateAttribute(attr);
|
|
157
|
+
const name = (0, viem.toHex)(attr.key, { size: 32 });
|
|
158
|
+
if (typeof attr.value === "string") {
|
|
159
|
+
if (require_queryBuilder.isEntityKey(attr.value)) return {
|
|
160
|
+
name,
|
|
161
|
+
valueType: require_queryBuilder.RpcAttributeValueType.EntityKey,
|
|
162
|
+
value: [
|
|
163
|
+
(0, viem.toHex)((0, viem.toBytes)(attr.value), { size: 32 }),
|
|
164
|
+
ZERO_32,
|
|
165
|
+
ZERO_32,
|
|
166
|
+
ZERO_32
|
|
167
|
+
]
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
name,
|
|
171
|
+
valueType: require_queryBuilder.RpcAttributeValueType.String,
|
|
172
|
+
value: encodeBytes128((0, viem.stringToBytes)(attr.value))
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
name,
|
|
177
|
+
valueType: require_queryBuilder.RpcAttributeValueType.Uint,
|
|
178
|
+
value: [
|
|
179
|
+
(0, viem.toHex)(BigInt(attr.value), { size: 32 }),
|
|
180
|
+
ZERO_32,
|
|
181
|
+
ZERO_32,
|
|
182
|
+
ZERO_32
|
|
183
|
+
]
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function encodeAttributes(attrs) {
|
|
187
|
+
const encoded = attrs.map((attr) => ({
|
|
188
|
+
key: attr.key,
|
|
189
|
+
...encodeAttribute(attr)
|
|
190
|
+
})).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
191
|
+
for (let i = 1; i < encoded.length; i++) if (encoded[i].name === encoded[i - 1].name) throw new require_queryBuilder.DuplicateAttributeError(encoded[i].key);
|
|
192
|
+
return encoded.map(({ key: _,...attr }) => attr);
|
|
193
|
+
}
|
|
194
|
+
function toBTL(expiresIn) {
|
|
195
|
+
require_queryBuilder.validateExpiresIn(expiresIn);
|
|
196
|
+
return Math.ceil(expiresIn / require_consts.BLOCK_TIME);
|
|
197
|
+
}
|
|
198
|
+
function deriveEntityKey(chainId, owner, nonce) {
|
|
199
|
+
return (0, viem.keccak256)((0, viem.encodePacked)([
|
|
200
|
+
"uint256",
|
|
201
|
+
"address",
|
|
202
|
+
"address",
|
|
203
|
+
"uint32"
|
|
204
|
+
], [
|
|
205
|
+
BigInt(chainId),
|
|
206
|
+
require_consts.ARKIV_ADDRESS,
|
|
207
|
+
owner,
|
|
208
|
+
Number(nonce)
|
|
209
|
+
]));
|
|
210
|
+
}
|
|
211
|
+
async function sendArkivTransaction(client, ops, txParams) {
|
|
212
|
+
if (!client.account) throw new Error("Account required");
|
|
213
|
+
if (!client.chain) throw new Error("Chain required");
|
|
214
|
+
const walletClient = client;
|
|
215
|
+
const { creates, updates, deletes, extensions, ownershipChanges } = ops;
|
|
216
|
+
const owner = client.account.address;
|
|
217
|
+
const ownerNonce = creates?.length ? BigInt(await walletClient.readContract({
|
|
218
|
+
address: require_consts.ARKIV_ADDRESS,
|
|
219
|
+
abi: ENTITY_NONCE_ABI,
|
|
220
|
+
functionName: "nonces",
|
|
221
|
+
args: [owner]
|
|
222
|
+
})) : 0n;
|
|
223
|
+
const createdEntityKeys = (creates ?? []).map((_, i) => deriveEntityKey(client.chain.id, owner, ownerNonce + BigInt(i)));
|
|
224
|
+
for (const item of creates ?? []) validateContentType(item.contentType);
|
|
225
|
+
for (const item of updates ?? []) validateContentType(item.contentType);
|
|
226
|
+
const operations = [
|
|
227
|
+
...(creates ?? []).map((item, i) => ({
|
|
228
|
+
operationType: require_entity.EntityOperationType.Create,
|
|
229
|
+
entityKey: createdEntityKeys[i],
|
|
230
|
+
payload: (0, viem.toHex)(item.payload),
|
|
231
|
+
contentType: encodeMime128(item.contentType),
|
|
232
|
+
attributes: encodeAttributes(item.attributes),
|
|
233
|
+
expiresIn: toBTL(item.expiresIn),
|
|
234
|
+
newOwner: ZERO_ADDRESS
|
|
235
|
+
})),
|
|
236
|
+
...(updates ?? []).map((item) => ({
|
|
237
|
+
operationType: require_entity.EntityOperationType.Update,
|
|
238
|
+
entityKey: item.entityKey,
|
|
239
|
+
payload: (0, viem.toHex)(item.payload),
|
|
240
|
+
contentType: encodeMime128(item.contentType),
|
|
241
|
+
attributes: encodeAttributes(item.attributes),
|
|
242
|
+
expiresIn: 0,
|
|
243
|
+
newOwner: ZERO_ADDRESS
|
|
244
|
+
})),
|
|
245
|
+
...(deletes ?? []).map((item) => ({
|
|
246
|
+
operationType: require_entity.EntityOperationType.Delete,
|
|
247
|
+
entityKey: item.entityKey,
|
|
248
|
+
payload: "0x",
|
|
249
|
+
contentType: encodeMime128(""),
|
|
250
|
+
attributes: [],
|
|
251
|
+
expiresIn: 0,
|
|
252
|
+
newOwner: ZERO_ADDRESS
|
|
253
|
+
})),
|
|
254
|
+
...(extensions ?? []).map((item) => ({
|
|
255
|
+
operationType: require_entity.EntityOperationType.Extend,
|
|
256
|
+
entityKey: item.entityKey,
|
|
257
|
+
payload: "0x",
|
|
258
|
+
contentType: encodeMime128(""),
|
|
259
|
+
attributes: [],
|
|
260
|
+
expiresIn: toBTL(item.expiresIn),
|
|
261
|
+
newOwner: ZERO_ADDRESS
|
|
262
|
+
})),
|
|
263
|
+
...(ownershipChanges ?? []).map((item) => ({
|
|
264
|
+
operationType: require_entity.EntityOperationType.Transfer,
|
|
265
|
+
entityKey: item.entityKey,
|
|
266
|
+
payload: "0x",
|
|
267
|
+
contentType: encodeMime128(""),
|
|
268
|
+
attributes: [],
|
|
269
|
+
expiresIn: 0,
|
|
270
|
+
newOwner: item.newOwner
|
|
271
|
+
}))
|
|
272
|
+
];
|
|
273
|
+
logger$7("Sending execute with %d operations %s", operations.length, JSON.stringify(operations));
|
|
274
|
+
try {
|
|
275
|
+
const txHash = await walletClient.writeContract({
|
|
276
|
+
address: require_consts.ARKIV_ADDRESS,
|
|
277
|
+
abi: EXECUTE_ABI,
|
|
278
|
+
functionName: "execute",
|
|
279
|
+
args: [operations],
|
|
280
|
+
account: client.account,
|
|
281
|
+
chain: client.chain,
|
|
282
|
+
...txParams
|
|
283
|
+
});
|
|
284
|
+
const receipt = await walletClient.waitForTransactionReceipt({ hash: txHash });
|
|
285
|
+
logger$7("Tx receipt %o", receipt);
|
|
286
|
+
if (receipt.status === "reverted") {
|
|
287
|
+
try {
|
|
288
|
+
await walletClient.simulateContract({
|
|
289
|
+
address: require_consts.ARKIV_ADDRESS,
|
|
290
|
+
abi: EXECUTE_ABI,
|
|
291
|
+
functionName: "execute",
|
|
292
|
+
args: [operations],
|
|
293
|
+
account: client.account,
|
|
294
|
+
chain: client.chain
|
|
295
|
+
});
|
|
296
|
+
} catch (err) {
|
|
297
|
+
const error = err;
|
|
298
|
+
const reason = error.shortMessage ?? error.cause?.details ?? "No reason provided by backend.";
|
|
299
|
+
throw new require_queryBuilder.EntityMutationError(`Transaction ${receipt.transactionHash} reverted. Reason: ${reason}`);
|
|
300
|
+
}
|
|
301
|
+
throw new require_queryBuilder.EntityMutationError(`Transaction ${receipt.transactionHash} reverted. No reason provided by backend.`);
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
receipt,
|
|
305
|
+
createdEntityKeys
|
|
306
|
+
};
|
|
307
|
+
} catch (error) {
|
|
308
|
+
let message = "Transaction failed";
|
|
309
|
+
if (error instanceof viem.TransactionExecutionError) message += `: ${error.details}`;
|
|
310
|
+
else if (error instanceof viem.ContractFunctionExecutionError) {
|
|
311
|
+
logger$7("Contract function execution error data:", error.shortMessage);
|
|
312
|
+
if (error.cause instanceof viem.ContractFunctionRevertedError) message += `: ${error.cause.message}`;
|
|
313
|
+
else message += ": Execution error without revert data";
|
|
314
|
+
} else if (error instanceof require_queryBuilder.EntityMutationError) throw error;
|
|
315
|
+
throw new require_queryBuilder.EntityMutationError(message);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
//#endregion
|
|
320
|
+
//#region src/actions/public/subscribeEntityEvents.ts
|
|
321
|
+
const logger$6 = require_queryBuilder.getLogger("actions:public:subscribe-entity-events");
|
|
322
|
+
const arkivABI = ENTITY_OPERATION_EVENT_ABI;
|
|
110
323
|
async function subscribeEntityEvents(client, { onError, onEntityCreated, onEntityUpdated, onEntityDeleted, onEntityExpired, onEntityExpiresInExtended, onEntityOwnerChanged }, pollingInterval, fromBlock) {
|
|
111
324
|
return client.watchEvent({
|
|
112
325
|
pollingInterval: pollingInterval ?? 1e3,
|
|
113
326
|
fromBlock,
|
|
114
327
|
events: arkivABI,
|
|
115
328
|
onLogs: (logs) => {
|
|
116
|
-
logger$
|
|
329
|
+
logger$6("logs from subscribeEntityEvents %o", logs);
|
|
117
330
|
for (const log of logs) {
|
|
118
331
|
const event = (0, viem.decodeEventLog)({
|
|
119
332
|
abi: arkivABI,
|
|
120
333
|
topics: log.topics,
|
|
121
334
|
data: log.data
|
|
122
335
|
});
|
|
123
|
-
logger$
|
|
124
|
-
|
|
125
|
-
|
|
336
|
+
logger$6("event from subscribeEntityEvents %o", event);
|
|
337
|
+
if (event.eventName !== "EntityOperation") continue;
|
|
338
|
+
const entityKey = event.args.entityKey;
|
|
339
|
+
const owner = event.args.owner;
|
|
340
|
+
const expiresAt = event.args.expiresAt;
|
|
341
|
+
const entityHash = event.args.entityHash;
|
|
342
|
+
switch (Number(event.args.operationType)) {
|
|
343
|
+
case require_entity.EntityOperationType.Create:
|
|
126
344
|
onEntityCreated?.({
|
|
127
|
-
entityKey
|
|
128
|
-
owner
|
|
129
|
-
|
|
130
|
-
|
|
345
|
+
entityKey,
|
|
346
|
+
owner,
|
|
347
|
+
expiresAt,
|
|
348
|
+
entityHash
|
|
131
349
|
});
|
|
132
350
|
break;
|
|
133
|
-
case
|
|
351
|
+
case require_entity.EntityOperationType.Update:
|
|
134
352
|
onEntityUpdated?.({
|
|
135
|
-
entityKey
|
|
136
|
-
owner
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
cost: event.args.cost
|
|
140
|
-
});
|
|
141
|
-
break;
|
|
142
|
-
case "ArkivEntityDeleted":
|
|
143
|
-
onEntityDeleted?.({
|
|
144
|
-
entityKey: (0, viem.toHex)(event.args.entityKey, { size: 32 }),
|
|
145
|
-
owner: event.args.ownerAddress
|
|
353
|
+
entityKey,
|
|
354
|
+
owner,
|
|
355
|
+
expiresAt,
|
|
356
|
+
entityHash
|
|
146
357
|
});
|
|
147
358
|
break;
|
|
148
|
-
case
|
|
359
|
+
case require_entity.EntityOperationType.Extend:
|
|
149
360
|
onEntityExpiresInExtended?.({
|
|
150
|
-
entityKey
|
|
151
|
-
owner
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
cost: event.args.cost
|
|
361
|
+
entityKey,
|
|
362
|
+
owner,
|
|
363
|
+
expiresAt,
|
|
364
|
+
entityHash
|
|
155
365
|
});
|
|
156
366
|
break;
|
|
157
|
-
case
|
|
158
|
-
|
|
159
|
-
entityKey
|
|
160
|
-
owner
|
|
367
|
+
case require_entity.EntityOperationType.Transfer:
|
|
368
|
+
onEntityOwnerChanged?.({
|
|
369
|
+
entityKey,
|
|
370
|
+
owner,
|
|
371
|
+
entityHash
|
|
161
372
|
});
|
|
162
373
|
break;
|
|
163
|
-
case
|
|
164
|
-
|
|
165
|
-
entityKey
|
|
166
|
-
|
|
167
|
-
newOwner: event.args.newOwnerAddress
|
|
374
|
+
case require_entity.EntityOperationType.Delete:
|
|
375
|
+
onEntityDeleted?.({
|
|
376
|
+
entityKey,
|
|
377
|
+
owner
|
|
168
378
|
});
|
|
169
379
|
break;
|
|
170
380
|
default:
|
|
171
|
-
|
|
381
|
+
onEntityExpired?.({
|
|
382
|
+
entityKey,
|
|
383
|
+
owner
|
|
384
|
+
});
|
|
172
385
|
break;
|
|
173
386
|
}
|
|
174
387
|
}
|
|
@@ -187,6 +400,7 @@ function publicArkivActions(client) {
|
|
|
187
400
|
getEntity: (key) => getEntity(client, key),
|
|
188
401
|
query: (rawQuery, queryOptions) => query(client, rawQuery, queryOptions),
|
|
189
402
|
buildQuery: () => new require_queryBuilder.QueryBuilder(client),
|
|
403
|
+
select: (selection) => new require_queryBuilder.SelectQueryBuilder(client, selection),
|
|
190
404
|
getBlockTiming: () => getBlockTiming(client),
|
|
191
405
|
getEntityCount: () => getEntityCount(client),
|
|
192
406
|
subscribeEntityEvents: ({ onError, onEntityCreated, onEntityUpdated, onEntityDeleted, onEntityExpired, onEntityExpiresInExtended }, pollingInterval, fromBlock) => subscribeEntityEvents(client, {
|
|
@@ -230,141 +444,12 @@ function createPublicClient(parameters) {
|
|
|
230
444
|
}).extend(viem.publicActions).extend(publicArkivActions);
|
|
231
445
|
}
|
|
232
446
|
|
|
233
|
-
//#endregion
|
|
234
|
-
//#region src/utils/compression.ts
|
|
235
|
-
const logger$7 = require_queryBuilder.getLogger("utils:compression");
|
|
236
|
-
const isNode = typeof process !== "undefined" && process.versions?.node != null;
|
|
237
|
-
let zlib = null;
|
|
238
|
-
let brotli = null;
|
|
239
|
-
async function getZlib() {
|
|
240
|
-
if (!isNode) return null;
|
|
241
|
-
if (!zlib) try {
|
|
242
|
-
zlib = await import("zlib");
|
|
243
|
-
} catch (error) {
|
|
244
|
-
console.error("Error importing zlib", error);
|
|
245
|
-
return null;
|
|
246
|
-
}
|
|
247
|
-
return zlib;
|
|
248
|
-
}
|
|
249
|
-
async function getBrotli() {
|
|
250
|
-
if (brotli) return brotli;
|
|
251
|
-
try {
|
|
252
|
-
const brotliModule = await import("brotli-wasm");
|
|
253
|
-
brotli = brotliModule.default ? await brotliModule.default : brotliModule;
|
|
254
|
-
} catch (error) {
|
|
255
|
-
console.error("Error importing brotli-wasm", error);
|
|
256
|
-
return null;
|
|
257
|
-
}
|
|
258
|
-
return brotli;
|
|
259
|
-
}
|
|
260
|
-
async function compress(data) {
|
|
261
|
-
logger$7("Compressing data size %d", data.length);
|
|
262
|
-
if (isNode) {
|
|
263
|
-
const zlibModule = await getZlib();
|
|
264
|
-
if (zlibModule) try {
|
|
265
|
-
const buffer = Buffer.from(data);
|
|
266
|
-
const compressed = zlibModule.brotliCompressSync(buffer);
|
|
267
|
-
const result = new Uint8Array(compressed);
|
|
268
|
-
logger$7("Brotli compressed with zlib size %d", result.length);
|
|
269
|
-
return result;
|
|
270
|
-
} catch (error) {
|
|
271
|
-
console.warn("zlib compression failed, falling back to brotli-wasm", error);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
const brotliInstance = await getBrotli();
|
|
275
|
-
if (!brotliInstance) {
|
|
276
|
-
console.warn("Brotli instance not found, not compressing data");
|
|
277
|
-
return data;
|
|
278
|
-
}
|
|
279
|
-
const brotliCompressed = brotliInstance.compress(data);
|
|
280
|
-
logger$7("Brotli compressed with wasm size %d", brotliCompressed.length);
|
|
281
|
-
return brotliCompressed;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
//#endregion
|
|
285
|
-
//#region src/utils/arkivTransactions.ts
|
|
286
|
-
const logger$6 = require_queryBuilder.getLogger("utils:arkiv-transactions");
|
|
287
|
-
function opsToTxData({ creates, updates, deletes, extensions, ownershipChanges }) {
|
|
288
|
-
function formatAttributes(attribute) {
|
|
289
|
-
return [(0, viem.toHex)(attribute.key), (0, viem.toHex)(typeof attribute.value === "number" && attribute.value === 0 ? "" : attribute.value)];
|
|
290
|
-
}
|
|
291
|
-
const payload = [
|
|
292
|
-
(creates ?? []).map((item) => [
|
|
293
|
-
(0, viem.toHex)(Math.ceil(item.expiresIn / require_payload.BLOCK_TIME)),
|
|
294
|
-
(0, viem.toHex)(item.contentType),
|
|
295
|
-
(0, viem.toHex)(item.payload),
|
|
296
|
-
item.attributes.filter((attribute) => typeof attribute.value === "string").map(formatAttributes),
|
|
297
|
-
item.attributes.filter((attribute) => typeof attribute.value === "number").map(formatAttributes)
|
|
298
|
-
]),
|
|
299
|
-
(updates ?? []).map((item) => [
|
|
300
|
-
item.entityKey,
|
|
301
|
-
(0, viem.toHex)(item.contentType),
|
|
302
|
-
(0, viem.toHex)(Math.ceil(item.expiresIn / require_payload.BLOCK_TIME)),
|
|
303
|
-
(0, viem.toHex)(item.payload),
|
|
304
|
-
item.attributes.filter((attribute) => typeof attribute.value === "string").map(formatAttributes),
|
|
305
|
-
item.attributes.filter((attribute) => typeof attribute.value === "number").map(formatAttributes)
|
|
306
|
-
]),
|
|
307
|
-
(deletes ?? []).map((item) => item.entityKey),
|
|
308
|
-
(extensions ?? []).map((item) => [item.entityKey, (0, viem.toHex)(Math.ceil(item.expiresIn / require_payload.BLOCK_TIME))]),
|
|
309
|
-
(ownershipChanges ?? []).map((item) => [item.entityKey, item.newOwner])
|
|
310
|
-
];
|
|
311
|
-
logger$6("txData to send as RLP %o length %d", payload, payload.length);
|
|
312
|
-
return (0, viem.toRlp)(payload);
|
|
313
|
-
}
|
|
314
|
-
async function sendArkivTransaction(client, data, txParams) {
|
|
315
|
-
if (!client.account) throw new Error("Account required");
|
|
316
|
-
const walletClient = client;
|
|
317
|
-
logger$6("Sending transaction %o", {
|
|
318
|
-
account: client.account,
|
|
319
|
-
chain: client.chain,
|
|
320
|
-
to: require_payload.ARKIV_ADDRESS,
|
|
321
|
-
value: 0n,
|
|
322
|
-
data,
|
|
323
|
-
...txParams
|
|
324
|
-
});
|
|
325
|
-
try {
|
|
326
|
-
const compressed = await compress((0, viem.toBytes)(data));
|
|
327
|
-
const txHash = await walletClient.sendTransaction({
|
|
328
|
-
account: client.account,
|
|
329
|
-
chain: client.chain,
|
|
330
|
-
to: require_payload.ARKIV_ADDRESS,
|
|
331
|
-
value: 0n,
|
|
332
|
-
data: (0, viem.toHex)(compressed),
|
|
333
|
-
...txParams
|
|
334
|
-
});
|
|
335
|
-
const receipt = await walletClient.waitForTransactionReceipt({ hash: txHash });
|
|
336
|
-
logger$6("Tx receipt %o", receipt);
|
|
337
|
-
if (receipt.status === "reverted") try {
|
|
338
|
-
const callResult = await walletClient.call({
|
|
339
|
-
account: client.account,
|
|
340
|
-
to: require_payload.ARKIV_ADDRESS,
|
|
341
|
-
value: 0n,
|
|
342
|
-
data: (0, viem.toHex)(compressed),
|
|
343
|
-
...txParams
|
|
344
|
-
});
|
|
345
|
-
throw new require_queryBuilder.EntityMutationError(`Transaction ${receipt.transactionHash} reverted. Please make sure the data for entities doesn't contain invalid characters or invalid data types.
|
|
346
|
-
Reason: ${callResult.data ? (0, viem.hexToString)(callResult.data) : "No reason provided by backend."}`);
|
|
347
|
-
} catch (err) {
|
|
348
|
-
const reason = err.cause?.details ?? "No reason provided by backend.";
|
|
349
|
-
logger$6("Error while calling to get more details about reverted transaction. Reason: %s", reason);
|
|
350
|
-
throw new require_queryBuilder.EntityMutationError(`Transaction ${receipt.transactionHash} reverted. Reason: ${reason}`);
|
|
351
|
-
}
|
|
352
|
-
return receipt;
|
|
353
|
-
} catch (error) {
|
|
354
|
-
let message = "Transaction failed";
|
|
355
|
-
if (error instanceof viem.TransactionExecutionError) message += `: ${error.details}`;
|
|
356
|
-
else if (error instanceof require_queryBuilder.EntityMutationError) throw error;
|
|
357
|
-
logger$6("%s Detailed error stack: %o", message, error);
|
|
358
|
-
throw new require_queryBuilder.EntityMutationError(message);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
447
|
//#endregion
|
|
363
448
|
//#region src/actions/wallet/changeOwnership.ts
|
|
364
449
|
const logger$5 = require_queryBuilder.getLogger("actions:wallet:change-ownership");
|
|
365
450
|
async function changeOwnership(client, data, txParams) {
|
|
366
451
|
logger$5("changeOwnership %o", data);
|
|
367
|
-
const receipt = await sendArkivTransaction(client,
|
|
452
|
+
const { receipt } = await sendArkivTransaction(client, { ownershipChanges: [data] }, txParams);
|
|
368
453
|
logger$5("Receipt from changeOwnership %o", receipt);
|
|
369
454
|
return {
|
|
370
455
|
txHash: receipt.transactionHash,
|
|
@@ -377,11 +462,11 @@ async function changeOwnership(client, data, txParams) {
|
|
|
377
462
|
const logger$4 = require_queryBuilder.getLogger("actions:wallet:create-entity");
|
|
378
463
|
async function createEntity(client, data, txParams) {
|
|
379
464
|
logger$4("createEntity %o", data);
|
|
380
|
-
const receipt = await sendArkivTransaction(client,
|
|
465
|
+
const { receipt, createdEntityKeys } = await sendArkivTransaction(client, { creates: [data] }, txParams);
|
|
381
466
|
logger$4("Receipt from createEntity %o", receipt);
|
|
382
467
|
return {
|
|
383
468
|
txHash: receipt.transactionHash,
|
|
384
|
-
entityKey:
|
|
469
|
+
entityKey: createdEntityKeys[0]
|
|
385
470
|
};
|
|
386
471
|
}
|
|
387
472
|
|
|
@@ -390,7 +475,7 @@ async function createEntity(client, data, txParams) {
|
|
|
390
475
|
const logger$3 = require_queryBuilder.getLogger("actions:wallet:delete-entity");
|
|
391
476
|
async function deleteEntity(client, data, txParams) {
|
|
392
477
|
logger$3("deleteEntity %o", data);
|
|
393
|
-
const receipt = await sendArkivTransaction(client,
|
|
478
|
+
const { receipt } = await sendArkivTransaction(client, { deletes: [data] }, txParams);
|
|
394
479
|
logger$3("Receipt from deleteEntity %o", receipt);
|
|
395
480
|
return {
|
|
396
481
|
txHash: receipt.transactionHash,
|
|
@@ -403,61 +488,28 @@ async function deleteEntity(client, data, txParams) {
|
|
|
403
488
|
const logger$2 = require_queryBuilder.getLogger("actions:wallet:extend-entity");
|
|
404
489
|
async function extendEntity(client, data, txParams) {
|
|
405
490
|
logger$2("extendEntity %o", data);
|
|
406
|
-
const receipt = await sendArkivTransaction(client,
|
|
491
|
+
const { receipt } = await sendArkivTransaction(client, { extensions: [data] }, txParams);
|
|
407
492
|
logger$2("Receipt from extendEntity %o", receipt);
|
|
408
493
|
return {
|
|
409
494
|
txHash: receipt.transactionHash,
|
|
410
|
-
entityKey:
|
|
495
|
+
entityKey: data.entityKey
|
|
411
496
|
};
|
|
412
497
|
}
|
|
413
498
|
|
|
414
499
|
//#endregion
|
|
415
500
|
//#region src/actions/wallet/mutateEntities.ts
|
|
416
501
|
const logger$1 = require_queryBuilder.getLogger("actions:wallet:mutate-entities");
|
|
417
|
-
function parseReceipt(receipt, params) {
|
|
418
|
-
const createdEntities = [];
|
|
419
|
-
const updatedEntities = [];
|
|
420
|
-
const deletedEntities = [];
|
|
421
|
-
const extendedEntities = [];
|
|
422
|
-
const ownershipChanges = [];
|
|
423
|
-
const totalCreates = params.creates?.length ?? 0;
|
|
424
|
-
const totalUpdates = params.updates?.length ?? 0;
|
|
425
|
-
const totalDeletes = params.deletes?.length ?? 0;
|
|
426
|
-
const totalExtensions = params.extensions?.length ?? 0;
|
|
427
|
-
for (let index = 0; index < receipt.logs.length; index++) {
|
|
428
|
-
const log = receipt.logs[index];
|
|
429
|
-
if (index < totalCreates) createdEntities.push(log.topics[1]);
|
|
430
|
-
else if (index < totalCreates + totalDeletes) deletedEntities.push(log.topics[1]);
|
|
431
|
-
else if (index < totalCreates + totalUpdates + totalDeletes) updatedEntities.push(log.topics[1]);
|
|
432
|
-
else if (index < totalCreates + totalUpdates + totalDeletes + totalExtensions) extendedEntities.push(log.topics[1]);
|
|
433
|
-
else ownershipChanges.push(log.topics[1]);
|
|
434
|
-
}
|
|
435
|
-
return {
|
|
436
|
-
createdEntities,
|
|
437
|
-
updatedEntities,
|
|
438
|
-
deletedEntities,
|
|
439
|
-
extendedEntities,
|
|
440
|
-
ownershipChanges
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
502
|
async function mutateEntities(client, data, txParams) {
|
|
444
503
|
if (!data.creates && !data.updates && !data.deletes && !data.extensions) throw new Error("No operations to perform");
|
|
445
|
-
const receipt = await sendArkivTransaction(client,
|
|
446
|
-
creates: data.creates ?? [],
|
|
447
|
-
updates: data.updates ?? [],
|
|
448
|
-
deletes: data.deletes ?? [],
|
|
449
|
-
extensions: data.extensions ?? [],
|
|
450
|
-
ownershipChanges: data.ownershipChanges ?? []
|
|
451
|
-
}), txParams);
|
|
504
|
+
const { receipt, createdEntityKeys } = await sendArkivTransaction(client, data, txParams);
|
|
452
505
|
logger$1("Receipt from mutateEntities %o", receipt);
|
|
453
|
-
const { createdEntities, updatedEntities, deletedEntities, extendedEntities, ownershipChanges } = parseReceipt(receipt, data);
|
|
454
506
|
return {
|
|
455
507
|
txHash: receipt.transactionHash,
|
|
456
|
-
createdEntities,
|
|
457
|
-
updatedEntities,
|
|
458
|
-
deletedEntities,
|
|
459
|
-
extendedEntities,
|
|
460
|
-
ownershipChanges
|
|
508
|
+
createdEntities: createdEntityKeys,
|
|
509
|
+
updatedEntities: (data.updates ?? []).map((u) => u.entityKey),
|
|
510
|
+
deletedEntities: (data.deletes ?? []).map((d) => d.entityKey),
|
|
511
|
+
extendedEntities: (data.extensions ?? []).map((e) => e.entityKey),
|
|
512
|
+
ownershipChanges: (data.ownershipChanges ?? []).map((o) => o.entityKey)
|
|
461
513
|
};
|
|
462
514
|
}
|
|
463
515
|
|
|
@@ -466,11 +518,11 @@ async function mutateEntities(client, data, txParams) {
|
|
|
466
518
|
const logger = require_queryBuilder.getLogger("actions:wallet:update-entity");
|
|
467
519
|
async function updateEntity(client, data, txParams) {
|
|
468
520
|
logger("updateEntity %o", data);
|
|
469
|
-
const receipt = await sendArkivTransaction(client,
|
|
521
|
+
const { receipt } = await sendArkivTransaction(client, { updates: [data] }, txParams);
|
|
470
522
|
logger("Receipt from updateEntity %o", receipt);
|
|
471
523
|
return {
|
|
472
524
|
txHash: receipt.transactionHash,
|
|
473
|
-
entityKey:
|
|
525
|
+
entityKey: data.entityKey
|
|
474
526
|
};
|
|
475
527
|
}
|
|
476
528
|
|
|
@@ -533,7 +585,13 @@ function chainFromName(name) {
|
|
|
533
585
|
}
|
|
534
586
|
|
|
535
587
|
//#endregion
|
|
588
|
+
exports.DuplicateAttributeError = require_queryBuilder.DuplicateAttributeError;
|
|
536
589
|
exports.EntityMutationError = require_queryBuilder.EntityMutationError;
|
|
590
|
+
exports.EntityOperationType = require_entity.EntityOperationType;
|
|
591
|
+
exports.InvalidAttributeError = require_queryBuilder.InvalidAttributeError;
|
|
592
|
+
exports.InvalidAttributeKeyError = require_queryBuilder.InvalidAttributeKeyError;
|
|
593
|
+
exports.InvalidContentTypeError = require_queryBuilder.InvalidContentTypeError;
|
|
594
|
+
exports.InvalidExpirationError = require_queryBuilder.InvalidExpirationError;
|
|
537
595
|
exports.NoCursorOrLimitError = require_queryBuilder.NoCursorOrLimitError;
|
|
538
596
|
exports.NoEntityFoundError = require_queryBuilder.NoEntityFoundError;
|
|
539
597
|
exports.NoMoreResultsError = require_queryBuilder.NoMoreResultsError;
|
|
@@ -541,10 +599,4 @@ exports.chainFromName = chainFromName;
|
|
|
541
599
|
exports.createPublicClient = createPublicClient;
|
|
542
600
|
exports.createWalletClient = createWalletClient;
|
|
543
601
|
exports.jsonToPayload = require_payload.jsonToPayload;
|
|
544
|
-
exports.stringToPayload = require_payload.stringToPayload;
|
|
545
|
-
Object.keys(viem).forEach(function (k) {
|
|
546
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
547
|
-
enumerable: true,
|
|
548
|
-
get: function () { return viem[k]; }
|
|
549
|
-
});
|
|
550
|
-
});
|
|
602
|
+
exports.stringToPayload = require_payload.stringToPayload;
|