@orbinum/sdk 0.7.4 → 0.7.6
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.js +26 -29
- package/dist/index.mjs +26 -29
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -777,14 +777,7 @@ var EvmExplorer = class _EvmExplorer {
|
|
|
777
777
|
async getLatestBlocks(count = 10) {
|
|
778
778
|
const latest = await this.evm.getBlockNumber();
|
|
779
779
|
const nums = Array.from({ length: Math.min(count, latest + 1) }, (_, i) => latest - i);
|
|
780
|
-
const results = await
|
|
781
|
-
nums.map(
|
|
782
|
-
(n) => this.evm.request("eth_getBlockByNumber", [
|
|
783
|
-
`0x${n.toString(16)}`,
|
|
784
|
-
false
|
|
785
|
-
]).catch(() => null)
|
|
786
|
-
)
|
|
787
|
-
);
|
|
780
|
+
const results = await this.evm.batchRequest(nums.map((n) => ({ method: "eth_getBlockByNumber", params: [`0x${n.toString(16)}`, false] }))).catch(() => nums.map(() => null));
|
|
788
781
|
return results.filter((b) => b !== null && !!b.hash).map((b) => this.parseBlock(b));
|
|
789
782
|
}
|
|
790
783
|
/** Returns a single block by number or hash, or `null` if not found. */
|
|
@@ -827,14 +820,18 @@ var EvmExplorer = class _EvmExplorer {
|
|
|
827
820
|
const latest = await this.evm.getBlockNumber();
|
|
828
821
|
const from = Math.max(0, latest - maxBlocks + 1);
|
|
829
822
|
const blockNums = Array.from({ length: latest - from + 1 }, (_, i) => latest - i);
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
823
|
+
const CHUNK = 50;
|
|
824
|
+
const blocks = [];
|
|
825
|
+
for (let i = 0; i < blockNums.length; i += CHUNK) {
|
|
826
|
+
const slice = blockNums.slice(i, i + CHUNK);
|
|
827
|
+
const part = await this.evm.batchRequest(
|
|
828
|
+
slice.map((n) => ({
|
|
829
|
+
method: "eth_getBlockByNumber",
|
|
830
|
+
params: [`0x${n.toString(16)}`, true]
|
|
831
|
+
}))
|
|
832
|
+
).catch(() => slice.map(() => null));
|
|
833
|
+
blocks.push(...part);
|
|
834
|
+
}
|
|
838
835
|
const matchingTxs = [];
|
|
839
836
|
for (const block of blocks) {
|
|
840
837
|
if (!block?.transactions) continue;
|
|
@@ -1699,10 +1696,7 @@ function isEvmAddress(addr) {
|
|
|
1699
1696
|
return /^0x[0-9a-fA-F]{40}$/.test(addr);
|
|
1700
1697
|
}
|
|
1701
1698
|
function evmAddressToAccountId(evmAddr) {
|
|
1702
|
-
const clean =
|
|
1703
|
-
if (clean.length !== 40) {
|
|
1704
|
-
throw new Error(`Expected 20-byte EVM address, got: ${evmAddr}`);
|
|
1705
|
-
}
|
|
1699
|
+
const clean = cleanEvmAddress(evmAddr);
|
|
1706
1700
|
const bytes = new Uint8Array(32);
|
|
1707
1701
|
for (let i = 0; i < 20; i++) {
|
|
1708
1702
|
bytes[i + 12] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
@@ -1710,10 +1704,7 @@ function evmAddressToAccountId(evmAddr) {
|
|
|
1710
1704
|
return bytes;
|
|
1711
1705
|
}
|
|
1712
1706
|
function evmToImplicitSubstrate(evmAddr) {
|
|
1713
|
-
const clean =
|
|
1714
|
-
if (clean.length !== 40) {
|
|
1715
|
-
throw new Error(`Expected 20-byte EVM address, got: ${evmAddr}`);
|
|
1716
|
-
}
|
|
1707
|
+
const clean = cleanEvmAddress(evmAddr);
|
|
1717
1708
|
return "0x" + clean.toLowerCase() + "0".repeat(24);
|
|
1718
1709
|
}
|
|
1719
1710
|
function evmToMappedAccountHex(address) {
|
|
@@ -1734,6 +1725,14 @@ function implicitSubstrateToEvm(accountHex) {
|
|
|
1734
1725
|
}
|
|
1735
1726
|
var ACCOUNT_ID_BYTES = 32;
|
|
1736
1727
|
var EVM_BYTES = 20;
|
|
1728
|
+
var EVM_HEX_RE = /^[0-9a-fA-F]{40}$/;
|
|
1729
|
+
function cleanEvmAddress(addr) {
|
|
1730
|
+
const clean = addr.startsWith("0x") ? addr.slice(2) : addr;
|
|
1731
|
+
if (!EVM_HEX_RE.test(clean)) {
|
|
1732
|
+
throw new Error(`Expected 20-byte EVM address, got: ${addr}`);
|
|
1733
|
+
}
|
|
1734
|
+
return clean;
|
|
1735
|
+
}
|
|
1737
1736
|
function isSubstrateAddress(addr) {
|
|
1738
1737
|
if (!addr || typeof addr !== "string" || isEvmAddress(addr)) return false;
|
|
1739
1738
|
if (addr.length < 40 || addr.length > 60) return false;
|
|
@@ -1768,10 +1767,8 @@ function substrateToEvm(addr) {
|
|
|
1768
1767
|
}
|
|
1769
1768
|
}
|
|
1770
1769
|
function evmToSubstrate(addr) {
|
|
1771
|
-
const
|
|
1772
|
-
if (!
|
|
1773
|
-
const hex = normalized.slice(2);
|
|
1774
|
-
if (hex.length !== 40) return null;
|
|
1770
|
+
const hex = addr.startsWith("0x") ? addr.slice(2) : addr;
|
|
1771
|
+
if (!EVM_HEX_RE.test(hex)) return null;
|
|
1775
1772
|
const mapped = new Uint8Array(ACCOUNT_ID_BYTES);
|
|
1776
1773
|
for (let i = 0; i < EVM_BYTES; i++) {
|
|
1777
1774
|
mapped[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
@@ -1809,7 +1806,7 @@ function substrateSs58ToAccountIdHex(addr) {
|
|
|
1809
1806
|
function addressToAccountIdHex(addr) {
|
|
1810
1807
|
if (!addr) return null;
|
|
1811
1808
|
if (isEvmAddress(addr)) {
|
|
1812
|
-
const hex =
|
|
1809
|
+
const hex = cleanEvmAddress(addr);
|
|
1813
1810
|
const mapped = new Uint8Array(ACCOUNT_ID_BYTES);
|
|
1814
1811
|
for (let i = 0; i < EVM_BYTES; i++) {
|
|
1815
1812
|
mapped[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
package/dist/index.mjs
CHANGED
|
@@ -650,14 +650,7 @@ var EvmExplorer = class _EvmExplorer {
|
|
|
650
650
|
async getLatestBlocks(count = 10) {
|
|
651
651
|
const latest = await this.evm.getBlockNumber();
|
|
652
652
|
const nums = Array.from({ length: Math.min(count, latest + 1) }, (_, i) => latest - i);
|
|
653
|
-
const results = await
|
|
654
|
-
nums.map(
|
|
655
|
-
(n) => this.evm.request("eth_getBlockByNumber", [
|
|
656
|
-
`0x${n.toString(16)}`,
|
|
657
|
-
false
|
|
658
|
-
]).catch(() => null)
|
|
659
|
-
)
|
|
660
|
-
);
|
|
653
|
+
const results = await this.evm.batchRequest(nums.map((n) => ({ method: "eth_getBlockByNumber", params: [`0x${n.toString(16)}`, false] }))).catch(() => nums.map(() => null));
|
|
661
654
|
return results.filter((b) => b !== null && !!b.hash).map((b) => this.parseBlock(b));
|
|
662
655
|
}
|
|
663
656
|
/** Returns a single block by number or hash, or `null` if not found. */
|
|
@@ -700,14 +693,18 @@ var EvmExplorer = class _EvmExplorer {
|
|
|
700
693
|
const latest = await this.evm.getBlockNumber();
|
|
701
694
|
const from = Math.max(0, latest - maxBlocks + 1);
|
|
702
695
|
const blockNums = Array.from({ length: latest - from + 1 }, (_, i) => latest - i);
|
|
703
|
-
const
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
696
|
+
const CHUNK = 50;
|
|
697
|
+
const blocks = [];
|
|
698
|
+
for (let i = 0; i < blockNums.length; i += CHUNK) {
|
|
699
|
+
const slice = blockNums.slice(i, i + CHUNK);
|
|
700
|
+
const part = await this.evm.batchRequest(
|
|
701
|
+
slice.map((n) => ({
|
|
702
|
+
method: "eth_getBlockByNumber",
|
|
703
|
+
params: [`0x${n.toString(16)}`, true]
|
|
704
|
+
}))
|
|
705
|
+
).catch(() => slice.map(() => null));
|
|
706
|
+
blocks.push(...part);
|
|
707
|
+
}
|
|
711
708
|
const matchingTxs = [];
|
|
712
709
|
for (const block of blocks) {
|
|
713
710
|
if (!block?.transactions) continue;
|
|
@@ -1572,10 +1569,7 @@ function isEvmAddress(addr) {
|
|
|
1572
1569
|
return /^0x[0-9a-fA-F]{40}$/.test(addr);
|
|
1573
1570
|
}
|
|
1574
1571
|
function evmAddressToAccountId(evmAddr) {
|
|
1575
|
-
const clean =
|
|
1576
|
-
if (clean.length !== 40) {
|
|
1577
|
-
throw new Error(`Expected 20-byte EVM address, got: ${evmAddr}`);
|
|
1578
|
-
}
|
|
1572
|
+
const clean = cleanEvmAddress(evmAddr);
|
|
1579
1573
|
const bytes = new Uint8Array(32);
|
|
1580
1574
|
for (let i = 0; i < 20; i++) {
|
|
1581
1575
|
bytes[i + 12] = parseInt(clean.slice(i * 2, i * 2 + 2), 16);
|
|
@@ -1583,10 +1577,7 @@ function evmAddressToAccountId(evmAddr) {
|
|
|
1583
1577
|
return bytes;
|
|
1584
1578
|
}
|
|
1585
1579
|
function evmToImplicitSubstrate(evmAddr) {
|
|
1586
|
-
const clean =
|
|
1587
|
-
if (clean.length !== 40) {
|
|
1588
|
-
throw new Error(`Expected 20-byte EVM address, got: ${evmAddr}`);
|
|
1589
|
-
}
|
|
1580
|
+
const clean = cleanEvmAddress(evmAddr);
|
|
1590
1581
|
return "0x" + clean.toLowerCase() + "0".repeat(24);
|
|
1591
1582
|
}
|
|
1592
1583
|
function evmToMappedAccountHex(address) {
|
|
@@ -1607,6 +1598,14 @@ function implicitSubstrateToEvm(accountHex) {
|
|
|
1607
1598
|
}
|
|
1608
1599
|
var ACCOUNT_ID_BYTES = 32;
|
|
1609
1600
|
var EVM_BYTES = 20;
|
|
1601
|
+
var EVM_HEX_RE = /^[0-9a-fA-F]{40}$/;
|
|
1602
|
+
function cleanEvmAddress(addr) {
|
|
1603
|
+
const clean = addr.startsWith("0x") ? addr.slice(2) : addr;
|
|
1604
|
+
if (!EVM_HEX_RE.test(clean)) {
|
|
1605
|
+
throw new Error(`Expected 20-byte EVM address, got: ${addr}`);
|
|
1606
|
+
}
|
|
1607
|
+
return clean;
|
|
1608
|
+
}
|
|
1610
1609
|
function isSubstrateAddress(addr) {
|
|
1611
1610
|
if (!addr || typeof addr !== "string" || isEvmAddress(addr)) return false;
|
|
1612
1611
|
if (addr.length < 40 || addr.length > 60) return false;
|
|
@@ -1641,10 +1640,8 @@ function substrateToEvm(addr) {
|
|
|
1641
1640
|
}
|
|
1642
1641
|
}
|
|
1643
1642
|
function evmToSubstrate(addr) {
|
|
1644
|
-
const
|
|
1645
|
-
if (!
|
|
1646
|
-
const hex = normalized.slice(2);
|
|
1647
|
-
if (hex.length !== 40) return null;
|
|
1643
|
+
const hex = addr.startsWith("0x") ? addr.slice(2) : addr;
|
|
1644
|
+
if (!EVM_HEX_RE.test(hex)) return null;
|
|
1648
1645
|
const mapped = new Uint8Array(ACCOUNT_ID_BYTES);
|
|
1649
1646
|
for (let i = 0; i < EVM_BYTES; i++) {
|
|
1650
1647
|
mapped[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
@@ -1682,7 +1679,7 @@ function substrateSs58ToAccountIdHex(addr) {
|
|
|
1682
1679
|
function addressToAccountIdHex(addr) {
|
|
1683
1680
|
if (!addr) return null;
|
|
1684
1681
|
if (isEvmAddress(addr)) {
|
|
1685
|
-
const hex =
|
|
1682
|
+
const hex = cleanEvmAddress(addr);
|
|
1686
1683
|
const mapped = new Uint8Array(ACCOUNT_ID_BYTES);
|
|
1687
1684
|
for (let i = 0; i < EVM_BYTES; i++) {
|
|
1688
1685
|
mapped[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|