@apibara/starknet 2.0.0-beta.30 → 2.0.0-beta.31
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/index.cjs +164 -2
- package/dist/index.d.cts +151 -131
- package/dist/index.d.mts +151 -131
- package/dist/index.d.ts +151 -131
- package/dist/index.mjs +159 -3
- package/dist/parser.cjs +133 -0
- package/dist/parser.d.cts +72 -0
- package/dist/parser.d.mts +72 -0
- package/dist/parser.d.ts +72 -0
- package/dist/parser.mjs +109 -0
- package/dist/shared/starknet.2b19268a.d.cts +32 -0
- package/dist/shared/starknet.2b19268a.d.mts +32 -0
- package/dist/shared/starknet.2b19268a.d.ts +32 -0
- package/package.json +10 -2
- package/src/abi.ts +79 -0
- package/src/access.ts +6 -2
- package/src/common.ts +2 -0
- package/src/event.ts +204 -0
- package/src/index.ts +11 -0
- package/src/parser.test.ts +169 -0
- package/src/parser.ts +170 -0
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const protocol = require('@apibara/protocol');
|
|
4
|
+
const abiWanKanabi = require('abi-wan-kanabi');
|
|
4
5
|
const schema = require('@effect/schema');
|
|
5
6
|
const Long = require('long');
|
|
6
7
|
const _m0 = require('protobufjs/minimal.js');
|
|
8
|
+
const starknet = require('@scure/starknet');
|
|
9
|
+
const parser = require('./parser.cjs');
|
|
7
10
|
|
|
8
11
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
9
12
|
|
|
@@ -7403,14 +7406,16 @@ function mergeHeaderFilter(a, b) {
|
|
|
7403
7406
|
return "on_data";
|
|
7404
7407
|
}
|
|
7405
7408
|
|
|
7406
|
-
function getReceipt(transactionIndex,
|
|
7409
|
+
function getReceipt(transactionIndex, params) {
|
|
7410
|
+
const receipts = "receipts" in params ? params.receipts : params;
|
|
7407
7411
|
return binarySearch(
|
|
7408
7412
|
transactionIndex,
|
|
7409
7413
|
receipts,
|
|
7410
7414
|
(receipt) => receipt.meta?.transactionIndex ?? 0
|
|
7411
7415
|
);
|
|
7412
7416
|
}
|
|
7413
|
-
function getTransaction(transactionIndex,
|
|
7417
|
+
function getTransaction(transactionIndex, params) {
|
|
7418
|
+
const transactions = "transactions" in params ? params.transactions : params;
|
|
7414
7419
|
return binarySearch(
|
|
7415
7420
|
transactionIndex,
|
|
7416
7421
|
transactions,
|
|
@@ -7435,12 +7440,164 @@ function binarySearch(index, arr, getIndex) {
|
|
|
7435
7440
|
}
|
|
7436
7441
|
}
|
|
7437
7442
|
|
|
7443
|
+
function getBigIntSelector(name) {
|
|
7444
|
+
const asBytes = new TextEncoder().encode(name);
|
|
7445
|
+
return starknet.keccak(asBytes);
|
|
7446
|
+
}
|
|
7447
|
+
function getSelector(name) {
|
|
7448
|
+
const bn = getBigIntSelector(name);
|
|
7449
|
+
return `0x${bn.toString(16).padStart(64, "0")}`;
|
|
7450
|
+
}
|
|
7451
|
+
function getEventSelector(name) {
|
|
7452
|
+
const parts = name.split("::");
|
|
7453
|
+
return getSelector(parts[parts.length - 1]);
|
|
7454
|
+
}
|
|
7455
|
+
const PrimitiveTypeParsers = {
|
|
7456
|
+
"core::bool": parser.parseBool,
|
|
7457
|
+
"core::felt252": parser.parseFelt252,
|
|
7458
|
+
"core::integer::u8": parser.parseU8,
|
|
7459
|
+
"core::integer::u16": parser.parseU16,
|
|
7460
|
+
"core::integer::u32": parser.parseU32,
|
|
7461
|
+
"core::integer::u64": parser.parseU64,
|
|
7462
|
+
"core::integer::u128": parser.parseU128,
|
|
7463
|
+
"core::integer::u256": parser.parseU256,
|
|
7464
|
+
"core::starknet::contract_address::ContractAddress": parser.parseContractAddress
|
|
7465
|
+
};
|
|
7466
|
+
function isPrimitiveType(type) {
|
|
7467
|
+
return type in PrimitiveTypeParsers;
|
|
7468
|
+
}
|
|
7469
|
+
function isArrayType(type) {
|
|
7470
|
+
return type.startsWith("core::array::Array::<") && type.endsWith(">");
|
|
7471
|
+
}
|
|
7472
|
+
function getArrayElementType(type) {
|
|
7473
|
+
return type.slice("core::array::Array::<".length, -1);
|
|
7474
|
+
}
|
|
7475
|
+
function isSpanType(type) {
|
|
7476
|
+
return type.startsWith("core::array::Span::<") && type.endsWith(">");
|
|
7477
|
+
}
|
|
7478
|
+
function getSpanType(type) {
|
|
7479
|
+
return type.slice("core::array::Span::<".length, -1);
|
|
7480
|
+
}
|
|
7481
|
+
function isOptionType(type) {
|
|
7482
|
+
return type.startsWith("core::option::Option::<") && type.endsWith(">");
|
|
7483
|
+
}
|
|
7484
|
+
function getOptionType(type) {
|
|
7485
|
+
return type.slice("core::option::Option::<".length, -1);
|
|
7486
|
+
}
|
|
7487
|
+
function isEmptyType(type) {
|
|
7488
|
+
return type === "()";
|
|
7489
|
+
}
|
|
7490
|
+
|
|
7491
|
+
class DecodeEventError extends Error {
|
|
7492
|
+
constructor(message) {
|
|
7493
|
+
super(message);
|
|
7494
|
+
this.name = "DecodeEventError";
|
|
7495
|
+
}
|
|
7496
|
+
}
|
|
7497
|
+
function decodeEvent(args) {
|
|
7498
|
+
const { abi, event, eventName, strict = true } = args;
|
|
7499
|
+
const eventAbi = abi.find(
|
|
7500
|
+
(item) => item.name === eventName && item.type === "event"
|
|
7501
|
+
);
|
|
7502
|
+
if (!eventAbi || eventAbi.type !== "event") {
|
|
7503
|
+
if (strict) {
|
|
7504
|
+
throw new DecodeEventError(`Event ${eventName} not found in ABI`);
|
|
7505
|
+
}
|
|
7506
|
+
return null;
|
|
7507
|
+
}
|
|
7508
|
+
if (eventAbi.kind === "enum") {
|
|
7509
|
+
throw new DecodeEventError("enum: not implemented");
|
|
7510
|
+
}
|
|
7511
|
+
const selector = BigInt(getEventSelector(eventName));
|
|
7512
|
+
if (event.keys && selector !== BigInt(event.keys[0]) || !event.keys) {
|
|
7513
|
+
if (strict) {
|
|
7514
|
+
throw new DecodeEventError(
|
|
7515
|
+
`Selector mismatch. Expected ${selector}, got ${event.keys?.[0]}`
|
|
7516
|
+
);
|
|
7517
|
+
}
|
|
7518
|
+
return null;
|
|
7519
|
+
}
|
|
7520
|
+
const keysAbi = eventAbi.members.filter((m) => m.kind === "key");
|
|
7521
|
+
const dataAbi = eventAbi.members.filter((m) => m.kind === "data");
|
|
7522
|
+
try {
|
|
7523
|
+
const keysParser = compileEventMembers(abi, keysAbi);
|
|
7524
|
+
const dataParser = compileEventMembers(abi, dataAbi);
|
|
7525
|
+
const keysWithoutSelector = event.keys?.slice(1) ?? [];
|
|
7526
|
+
const { out: decodedKeys } = keysParser(keysWithoutSelector, 0);
|
|
7527
|
+
const { out: decodedData } = dataParser(event.data ?? [], 0);
|
|
7528
|
+
const decoded = {
|
|
7529
|
+
...decodedKeys,
|
|
7530
|
+
...decodedData
|
|
7531
|
+
};
|
|
7532
|
+
return {
|
|
7533
|
+
...event,
|
|
7534
|
+
eventName,
|
|
7535
|
+
args: decoded
|
|
7536
|
+
};
|
|
7537
|
+
} catch (error) {
|
|
7538
|
+
if (error instanceof DecodeEventError && !strict) {
|
|
7539
|
+
return null;
|
|
7540
|
+
}
|
|
7541
|
+
if (error instanceof parser.ParseError && !strict) {
|
|
7542
|
+
return null;
|
|
7543
|
+
}
|
|
7544
|
+
throw error;
|
|
7545
|
+
}
|
|
7546
|
+
}
|
|
7547
|
+
function compileEventMembers(abi, members) {
|
|
7548
|
+
return compileStructParser(abi, members);
|
|
7549
|
+
}
|
|
7550
|
+
function compileTypeParser(abi, type) {
|
|
7551
|
+
if (isPrimitiveType(type)) {
|
|
7552
|
+
return PrimitiveTypeParsers[type];
|
|
7553
|
+
}
|
|
7554
|
+
if (isArrayType(type)) {
|
|
7555
|
+
const elementType = getArrayElementType(type);
|
|
7556
|
+
return parser.parseArray(compileTypeParser(abi, elementType));
|
|
7557
|
+
}
|
|
7558
|
+
if (isSpanType(type)) {
|
|
7559
|
+
const elementType = getSpanType(type);
|
|
7560
|
+
return parser.parseSpan(compileTypeParser(abi, elementType));
|
|
7561
|
+
}
|
|
7562
|
+
if (isOptionType(type)) {
|
|
7563
|
+
const elementType = getOptionType(type);
|
|
7564
|
+
return parser.parseOption(compileTypeParser(abi, elementType));
|
|
7565
|
+
}
|
|
7566
|
+
if (isEmptyType(type)) {
|
|
7567
|
+
return parser.parseEmpty;
|
|
7568
|
+
}
|
|
7569
|
+
const typeAbi = abi.find((item) => item.name === type);
|
|
7570
|
+
if (!typeAbi) {
|
|
7571
|
+
throw new DecodeEventError(`Type ${type} not found in ABI`);
|
|
7572
|
+
}
|
|
7573
|
+
switch (typeAbi.type) {
|
|
7574
|
+
case "struct": {
|
|
7575
|
+
return compileStructParser(abi, typeAbi.members);
|
|
7576
|
+
}
|
|
7577
|
+
case "enum":
|
|
7578
|
+
throw new DecodeEventError("enum: not implemented");
|
|
7579
|
+
default:
|
|
7580
|
+
throw new DecodeEventError(`Invalid type ${typeAbi.type}`);
|
|
7581
|
+
}
|
|
7582
|
+
}
|
|
7583
|
+
function compileStructParser(abi, members) {
|
|
7584
|
+
const parsers = {};
|
|
7585
|
+
for (const [index, member] of members.entries()) {
|
|
7586
|
+
parsers[member.name] = {
|
|
7587
|
+
index,
|
|
7588
|
+
parser: compileTypeParser(abi, member.type)
|
|
7589
|
+
};
|
|
7590
|
+
}
|
|
7591
|
+
return parser.parseStruct(parsers);
|
|
7592
|
+
}
|
|
7593
|
+
|
|
7438
7594
|
const StarknetStream = new protocol.StreamConfig(
|
|
7439
7595
|
FilterFromBytes,
|
|
7440
7596
|
BlockFromBytes,
|
|
7441
7597
|
mergeFilter
|
|
7442
7598
|
);
|
|
7443
7599
|
|
|
7600
|
+
exports.Abi = abiWanKanabi.Abi;
|
|
7444
7601
|
exports.Block = Block;
|
|
7445
7602
|
exports.BlockFromBytes = BlockFromBytes;
|
|
7446
7603
|
exports.BlockHeader = BlockHeader;
|
|
@@ -7460,6 +7617,7 @@ exports.DeclareV2TransactionFilter = DeclareV2TransactionFilter;
|
|
|
7460
7617
|
exports.DeclareV3TransactionFilter = DeclareV3TransactionFilter;
|
|
7461
7618
|
exports.DeclaredClass = DeclaredClass;
|
|
7462
7619
|
exports.DeclaredClassFilter = DeclaredClassFilter;
|
|
7620
|
+
exports.DecodeEventError = DecodeEventError;
|
|
7463
7621
|
exports.DeployAccountTransactionReceipt = DeployAccountTransactionReceipt;
|
|
7464
7622
|
exports.DeployAccountTransactionV1 = DeployAccountTransactionV1;
|
|
7465
7623
|
exports.DeployAccountTransactionV3 = DeployAccountTransactionV3;
|
|
@@ -7515,13 +7673,17 @@ exports.TransactionReceiptMeta = TransactionReceiptMeta;
|
|
|
7515
7673
|
exports.TransactionStatus = TransactionStatus;
|
|
7516
7674
|
exports.TransactionStatusFilter = TransactionStatusFilter;
|
|
7517
7675
|
exports.U128 = U128;
|
|
7676
|
+
exports.decodeEvent = decodeEvent;
|
|
7518
7677
|
exports.feltFromProto = feltFromProto;
|
|
7519
7678
|
exports.feltToProto = feltToProto;
|
|
7520
7679
|
exports.filterFromBytes = filterFromBytes;
|
|
7521
7680
|
exports.filterFromProto = filterFromProto;
|
|
7522
7681
|
exports.filterToBytes = filterToBytes;
|
|
7523
7682
|
exports.filterToProto = filterToProto;
|
|
7683
|
+
exports.getBigIntSelector = getBigIntSelector;
|
|
7684
|
+
exports.getEventSelector = getEventSelector;
|
|
7524
7685
|
exports.getReceipt = getReceipt;
|
|
7686
|
+
exports.getSelector = getSelector;
|
|
7525
7687
|
exports.getTransaction = getTransaction;
|
|
7526
7688
|
exports.mergeFilter = mergeFilter;
|
|
7527
7689
|
exports.proto = index;
|