@stableflow/utils-evm 0.0.0-experimental-5a8afd7389-20260703
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 +21 -0
- package/README.md +27 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +99 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +92 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 StableFlow AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @stableflow/utils-evm
|
|
2
|
+
|
|
3
|
+
EVM utilities for the StableFlow SDK: RPC fallback providers and LayerZero compose helpers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @stableflow/utils-evm @stableflow/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This package is primarily used internally by `@stableflow/bridges` and `@stableflow/wallet-evm`. Application code rarely imports it directly.
|
|
12
|
+
|
|
13
|
+
## Main exports
|
|
14
|
+
|
|
15
|
+
| Export | Description |
|
|
16
|
+
|--------|-------------|
|
|
17
|
+
| `evmRpcFallbackProvider` | EVM JSON-RPC provider with fallback URLs |
|
|
18
|
+
| `buildEndpointV2LzComposePayload` | LayerZero v2 compose payload builder |
|
|
19
|
+
| `encodeUint`, `normalizeHex`, `toBytes32` | Encoding helpers |
|
|
20
|
+
| `NATIVE_MSG_FEE_BUFFER` | Native message fee buffer constant |
|
|
21
|
+
|
|
22
|
+
See `dist/index.d.ts` after `pnpm build`.
|
|
23
|
+
|
|
24
|
+
## See also
|
|
25
|
+
|
|
26
|
+
- [Developer Guide](../../DEVELOPER_GUIDE.md)
|
|
27
|
+
- [@stableflow/wallet-evm](../wallet-evm/README.md)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { TokenConfig } from '@stableflow/core';
|
|
3
|
+
|
|
4
|
+
declare class SequentialFallbackProvider extends ethers.AbstractProvider {
|
|
5
|
+
private providers;
|
|
6
|
+
constructor(providers: ethers.JsonRpcProvider[], chainId: number);
|
|
7
|
+
_detectNetwork(): Promise<ethers.Network>;
|
|
8
|
+
_perform(req: ethers.PerformActionRequest): Promise<any>;
|
|
9
|
+
}
|
|
10
|
+
declare function evmRpcFallbackProvider(chain: TokenConfig): ethers.AbstractProvider | SequentialFallbackProvider | ethers.JsonRpcProvider;
|
|
11
|
+
|
|
12
|
+
declare function normalizeHex(hex: string): string;
|
|
13
|
+
declare function toBytes32(addrOrBytes32: string): string;
|
|
14
|
+
declare function encodeUint(value: string, sizeBytes: number): string;
|
|
15
|
+
declare function buildEndpointV2LzComposePayload(params: {
|
|
16
|
+
nonce: string;
|
|
17
|
+
srcEid: string;
|
|
18
|
+
amountLD: string;
|
|
19
|
+
composeFrom: string;
|
|
20
|
+
composeMsg: string;
|
|
21
|
+
}): string;
|
|
22
|
+
declare const NATIVE_MSG_FEE_BUFFER: bigint;
|
|
23
|
+
|
|
24
|
+
export { NATIVE_MSG_FEE_BUFFER, buildEndpointV2LzComposePayload, encodeUint, evmRpcFallbackProvider, normalizeHex, toBytes32 };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { TokenConfig } from '@stableflow/core';
|
|
3
|
+
|
|
4
|
+
declare class SequentialFallbackProvider extends ethers.AbstractProvider {
|
|
5
|
+
private providers;
|
|
6
|
+
constructor(providers: ethers.JsonRpcProvider[], chainId: number);
|
|
7
|
+
_detectNetwork(): Promise<ethers.Network>;
|
|
8
|
+
_perform(req: ethers.PerformActionRequest): Promise<any>;
|
|
9
|
+
}
|
|
10
|
+
declare function evmRpcFallbackProvider(chain: TokenConfig): ethers.AbstractProvider | SequentialFallbackProvider | ethers.JsonRpcProvider;
|
|
11
|
+
|
|
12
|
+
declare function normalizeHex(hex: string): string;
|
|
13
|
+
declare function toBytes32(addrOrBytes32: string): string;
|
|
14
|
+
declare function encodeUint(value: string, sizeBytes: number): string;
|
|
15
|
+
declare function buildEndpointV2LzComposePayload(params: {
|
|
16
|
+
nonce: string;
|
|
17
|
+
srcEid: string;
|
|
18
|
+
amountLD: string;
|
|
19
|
+
composeFrom: string;
|
|
20
|
+
composeMsg: string;
|
|
21
|
+
}): string;
|
|
22
|
+
declare const NATIVE_MSG_FEE_BUFFER: bigint;
|
|
23
|
+
|
|
24
|
+
export { NATIVE_MSG_FEE_BUFFER, buildEndpointV2LzComposePayload, encodeUint, evmRpcFallbackProvider, normalizeHex, toBytes32 };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ethers = require('ethers');
|
|
4
|
+
|
|
5
|
+
// src/evm-rpc-providers.ts
|
|
6
|
+
var providerCache = /* @__PURE__ */ new Map();
|
|
7
|
+
var SequentialFallbackProvider = class extends ethers.ethers.AbstractProvider {
|
|
8
|
+
constructor(providers, chainId) {
|
|
9
|
+
super(chainId);
|
|
10
|
+
this.providers = providers;
|
|
11
|
+
}
|
|
12
|
+
async _detectNetwork() {
|
|
13
|
+
return this.providers[0]._detectNetwork();
|
|
14
|
+
}
|
|
15
|
+
async _perform(req) {
|
|
16
|
+
let lastError;
|
|
17
|
+
for (const provider of this.providers) {
|
|
18
|
+
try {
|
|
19
|
+
return await provider._perform(req);
|
|
20
|
+
} catch (err) {
|
|
21
|
+
if (ethers.ethers.isError(err, "CALL_EXCEPTION")) throw err;
|
|
22
|
+
lastError = err;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
throw lastError;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
function evmRpcFallbackProvider(chain) {
|
|
29
|
+
const { rpcUrls, chainId } = chain;
|
|
30
|
+
if (providerCache.has(chainId)) {
|
|
31
|
+
return providerCache.get(chainId);
|
|
32
|
+
}
|
|
33
|
+
const providers = rpcUrls.map(
|
|
34
|
+
(rpc) => {
|
|
35
|
+
return new ethers.ethers.JsonRpcProvider(rpc, chainId, { staticNetwork: true });
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
const provider = providers.length === 1 ? providers[0] : new SequentialFallbackProvider(providers, chainId);
|
|
39
|
+
providerCache.set(chainId, provider);
|
|
40
|
+
return provider;
|
|
41
|
+
}
|
|
42
|
+
function normalizeHex(hex) {
|
|
43
|
+
if (!hex) return "0x";
|
|
44
|
+
const s = String(hex);
|
|
45
|
+
const indices = [];
|
|
46
|
+
const re = /0x/gi;
|
|
47
|
+
let m;
|
|
48
|
+
while ((m = re.exec(s)) !== null) {
|
|
49
|
+
indices.push(m.index);
|
|
50
|
+
}
|
|
51
|
+
let best = "";
|
|
52
|
+
if (indices.length === 0) {
|
|
53
|
+
best = s.replace(/[^0-9a-fA-F]/g, "");
|
|
54
|
+
} else {
|
|
55
|
+
for (let i = 0; i < indices.length; i++) {
|
|
56
|
+
const start = indices[i] + 2;
|
|
57
|
+
const end = i + 1 < indices.length ? indices[i + 1] : s.length;
|
|
58
|
+
const candidate = s.slice(start, end).replace(/[^0-9a-fA-F]/g, "");
|
|
59
|
+
if (candidate.length > best.length) best = candidate;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!best) return "0x";
|
|
63
|
+
if (best.length % 2) throw new Error("hex length must be even");
|
|
64
|
+
return "0x" + best.toLowerCase();
|
|
65
|
+
}
|
|
66
|
+
function toBytes32(addrOrBytes32) {
|
|
67
|
+
const h = normalizeHex(addrOrBytes32);
|
|
68
|
+
if (h.length === 42) return ethers.ethers.zeroPadValue(h, 32);
|
|
69
|
+
if (h.length === 66) return h;
|
|
70
|
+
throw new Error("expected address(20B) or bytes32(32B)");
|
|
71
|
+
}
|
|
72
|
+
function encodeUint(value, sizeBytes) {
|
|
73
|
+
const bn = BigInt(value);
|
|
74
|
+
const max = (1n << BigInt(sizeBytes * 8)) - 1n;
|
|
75
|
+
if (bn < 0n || bn > max) {
|
|
76
|
+
throw new Error(`value does not fit uint${sizeBytes * 8}`);
|
|
77
|
+
}
|
|
78
|
+
return ethers.ethers.zeroPadValue(ethers.ethers.toBeHex(bn), sizeBytes);
|
|
79
|
+
}
|
|
80
|
+
function buildEndpointV2LzComposePayload(params) {
|
|
81
|
+
const { nonce, srcEid, amountLD, composeFrom, composeMsg } = params;
|
|
82
|
+
return ethers.ethers.concat([
|
|
83
|
+
encodeUint(nonce, 8),
|
|
84
|
+
encodeUint(srcEid, 4),
|
|
85
|
+
encodeUint(amountLD, 32),
|
|
86
|
+
toBytes32(composeFrom),
|
|
87
|
+
normalizeHex(composeMsg)
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
var NATIVE_MSG_FEE_BUFFER = 105n;
|
|
91
|
+
|
|
92
|
+
exports.NATIVE_MSG_FEE_BUFFER = NATIVE_MSG_FEE_BUFFER;
|
|
93
|
+
exports.buildEndpointV2LzComposePayload = buildEndpointV2LzComposePayload;
|
|
94
|
+
exports.encodeUint = encodeUint;
|
|
95
|
+
exports.evmRpcFallbackProvider = evmRpcFallbackProvider;
|
|
96
|
+
exports.normalizeHex = normalizeHex;
|
|
97
|
+
exports.toBytes32 = toBytes32;
|
|
98
|
+
//# sourceMappingURL=index.js.map
|
|
99
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/evm-rpc-providers.ts","../src/layerzero-compose.ts"],"names":["ethers"],"mappings":";;;;;AAGA,IAAM,aAAA,uBAAoB,GAAA,EAAqC;AAE/D,IAAM,0BAAA,GAAN,cAAyCA,aAAA,CAAO,gBAAA,CAAiB;AAAA,EAG/D,WAAA,CAAY,WAAqC,OAAA,EAAiB;AAChE,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AAAA,EACnB;AAAA,EAEA,MAAM,cAAA,GAA0C;AAC9C,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,CAAC,CAAA,CAAE,cAAA,EAAe;AAAA,EAC1C;AAAA,EAEA,MAAM,SAAS,GAAA,EAAgD;AAC7D,IAAA,IAAI,SAAA;AACJ,IAAA,KAAA,MAAW,QAAA,IAAY,KAAK,SAAA,EAAW;AACrC,MAAA,IAAI;AACF,QAAA,OAAO,MAAM,QAAA,CAAS,QAAA,CAAS,GAAG,CAAA;AAAA,MACpC,SAAS,GAAA,EAAK;AACZ,QAAA,IAAIA,aAAA,CAAO,OAAA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SAAA,GAAY,CAAC,CAAA,IAAK,EAAA;AAE5C,EAAA,IAAI,EAAA,GAAK,EAAA,IAAM,EAAA,GAAK,GAAA,EAAK;AACvB,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EAA0B,SAAA,GAAY,CAAC,CAAA,CAAE,CAAA;AAAA,EAC3D;AAEA,EAAA,OAAOA,cAAO,YAAA,CAAaA,aAAAA,CAAO,OAAA,CAAQ,EAAE,GAAG,SAAS,CAAA;AAC1D;AAEO,SAAS,gCAAgC,MAAA,EAM7C;AACD,EAAA,MAAM,EAAE,KAAA,EAAO,MAAA,EAAQ,QAAA,EAAU,WAAA,EAAa,YAAW,GAAI,MAAA;AAC7D,EAAA,OAAOA,cAAO,MAAA,CAAO;AAAA,IACnB,UAAA,CAAW,OAAO,CAAC,CAAA;AAAA,IACnB,UAAA,CAAW,QAAQ,CAAC,CAAA;AAAA,IACpB,UAAA,CAAW,UAAU,EAAE,CAAA;AAAA,IACvB,UAAU,WAAW,CAAA;AAAA,IACrB,aAAa,UAAU;AAAA,GACxB,CAAA;AACH;AAEO,IAAM,qBAAA,GAAgC","file":"index.js","sourcesContent":["import { ethers } from \"ethers\";\nimport type { TokenConfig } from \"@stableflow/core\";\n\nconst providerCache = new Map<number, ethers.AbstractProvider>();\n\nclass SequentialFallbackProvider extends ethers.AbstractProvider {\n private providers: ethers.JsonRpcProvider[];\n\n constructor(providers: ethers.JsonRpcProvider[], chainId: number) {\n super(chainId);\n this.providers = providers;\n }\n\n async _detectNetwork(): Promise<ethers.Network> {\n return this.providers[0]._detectNetwork();\n }\n\n async _perform(req: ethers.PerformActionRequest): Promise<any> {\n let lastError: unknown;\n for (const provider of this.providers) {\n try {\n return await provider._perform(req);\n } catch (err) {\n if (ethers.isError(err, \"CALL_EXCEPTION\")) throw err;\n lastError = err;\n }\n }\n throw lastError;\n }\n}\n\nexport function evmRpcFallbackProvider(chain: TokenConfig) {\n const { rpcUrls, chainId } = chain;\n\n if (providerCache.has(chainId!)) {\n return providerCache.get(chainId!)!;\n }\n\n const providers = rpcUrls.map(\n (rpc: string) => {\n return new ethers.JsonRpcProvider(rpc, chainId, { staticNetwork: true });\n }\n );\n\n const provider =\n providers.length === 1\n ? providers[0]\n : new SequentialFallbackProvider(providers, chainId!);\n\n providerCache.set(chainId!, provider);\n return provider;\n}\n","import { ethers } from 'ethers';\n\nexport function normalizeHex(hex: string) {\n if (!hex) return '0x';\n const s = String(hex);\n\n const indices = [];\n const re = /0x/gi;\n let m;\n while ((m = re.exec(s)) !== null) {\n indices.push(m.index);\n }\n\n let best = '';\n if (indices.length === 0) {\n best = s.replace(/[^0-9a-fA-F]/g, '');\n } else {\n for (let i = 0; i < indices.length; i++) {\n const start = indices[i] + 2;\n const end = i + 1 < indices.length ? indices[i + 1] : s.length;\n const candidate = s.slice(start, end).replace(/[^0-9a-fA-F]/g, '');\n if (candidate.length > best.length) best = candidate;\n }\n }\n\n if (!best) return '0x';\n if (best.length % 2) throw new Error('hex length must be even');\n return '0x' + best.toLowerCase();\n}\n\nexport function toBytes32(addrOrBytes32: string) {\n const h = normalizeHex(addrOrBytes32);\n if (h.length === 42) return ethers.zeroPadValue(h, 32);\n if (h.length === 66) return h;\n throw new Error('expected address(20B) or bytes32(32B)');\n}\n\nexport function encodeUint(value: string, sizeBytes: number) {\n const bn = BigInt(value);\n const max = (1n << BigInt(sizeBytes * 8)) - 1n;\n\n if (bn < 0n || bn > max) {\n throw new Error(`value does not fit uint${sizeBytes * 8}`);\n }\n\n return ethers.zeroPadValue(ethers.toBeHex(bn), sizeBytes);\n}\n\nexport function buildEndpointV2LzComposePayload(params: {\n nonce: string;\n srcEid: string;\n amountLD: string;\n composeFrom: string;\n composeMsg: string;\n}) {\n const { nonce, srcEid, amountLD, composeFrom, composeMsg } = params;\n return ethers.concat([\n encodeUint(nonce, 8),\n encodeUint(srcEid, 4),\n encodeUint(amountLD, 32),\n toBytes32(composeFrom),\n normalizeHex(composeMsg),\n ]);\n}\n\nexport const NATIVE_MSG_FEE_BUFFER: bigint = 105n;\n"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
|
|
3
|
+
// src/evm-rpc-providers.ts
|
|
4
|
+
var providerCache = /* @__PURE__ */ new Map();
|
|
5
|
+
var SequentialFallbackProvider = class extends ethers.AbstractProvider {
|
|
6
|
+
constructor(providers, chainId) {
|
|
7
|
+
super(chainId);
|
|
8
|
+
this.providers = providers;
|
|
9
|
+
}
|
|
10
|
+
async _detectNetwork() {
|
|
11
|
+
return this.providers[0]._detectNetwork();
|
|
12
|
+
}
|
|
13
|
+
async _perform(req) {
|
|
14
|
+
let lastError;
|
|
15
|
+
for (const provider of this.providers) {
|
|
16
|
+
try {
|
|
17
|
+
return await provider._perform(req);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
if (ethers.isError(err, "CALL_EXCEPTION")) throw err;
|
|
20
|
+
lastError = err;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw lastError;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
function evmRpcFallbackProvider(chain) {
|
|
27
|
+
const { rpcUrls, chainId } = chain;
|
|
28
|
+
if (providerCache.has(chainId)) {
|
|
29
|
+
return providerCache.get(chainId);
|
|
30
|
+
}
|
|
31
|
+
const providers = rpcUrls.map(
|
|
32
|
+
(rpc) => {
|
|
33
|
+
return new ethers.JsonRpcProvider(rpc, chainId, { staticNetwork: true });
|
|
34
|
+
}
|
|
35
|
+
);
|
|
36
|
+
const provider = providers.length === 1 ? providers[0] : new SequentialFallbackProvider(providers, chainId);
|
|
37
|
+
providerCache.set(chainId, provider);
|
|
38
|
+
return provider;
|
|
39
|
+
}
|
|
40
|
+
function normalizeHex(hex) {
|
|
41
|
+
if (!hex) return "0x";
|
|
42
|
+
const s = String(hex);
|
|
43
|
+
const indices = [];
|
|
44
|
+
const re = /0x/gi;
|
|
45
|
+
let m;
|
|
46
|
+
while ((m = re.exec(s)) !== null) {
|
|
47
|
+
indices.push(m.index);
|
|
48
|
+
}
|
|
49
|
+
let best = "";
|
|
50
|
+
if (indices.length === 0) {
|
|
51
|
+
best = s.replace(/[^0-9a-fA-F]/g, "");
|
|
52
|
+
} else {
|
|
53
|
+
for (let i = 0; i < indices.length; i++) {
|
|
54
|
+
const start = indices[i] + 2;
|
|
55
|
+
const end = i + 1 < indices.length ? indices[i + 1] : s.length;
|
|
56
|
+
const candidate = s.slice(start, end).replace(/[^0-9a-fA-F]/g, "");
|
|
57
|
+
if (candidate.length > best.length) best = candidate;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (!best) return "0x";
|
|
61
|
+
if (best.length % 2) throw new Error("hex length must be even");
|
|
62
|
+
return "0x" + best.toLowerCase();
|
|
63
|
+
}
|
|
64
|
+
function toBytes32(addrOrBytes32) {
|
|
65
|
+
const h = normalizeHex(addrOrBytes32);
|
|
66
|
+
if (h.length === 42) return ethers.zeroPadValue(h, 32);
|
|
67
|
+
if (h.length === 66) return h;
|
|
68
|
+
throw new Error("expected address(20B) or bytes32(32B)");
|
|
69
|
+
}
|
|
70
|
+
function encodeUint(value, sizeBytes) {
|
|
71
|
+
const bn = BigInt(value);
|
|
72
|
+
const max = (1n << BigInt(sizeBytes * 8)) - 1n;
|
|
73
|
+
if (bn < 0n || bn > max) {
|
|
74
|
+
throw new Error(`value does not fit uint${sizeBytes * 8}`);
|
|
75
|
+
}
|
|
76
|
+
return ethers.zeroPadValue(ethers.toBeHex(bn), sizeBytes);
|
|
77
|
+
}
|
|
78
|
+
function buildEndpointV2LzComposePayload(params) {
|
|
79
|
+
const { nonce, srcEid, amountLD, composeFrom, composeMsg } = params;
|
|
80
|
+
return ethers.concat([
|
|
81
|
+
encodeUint(nonce, 8),
|
|
82
|
+
encodeUint(srcEid, 4),
|
|
83
|
+
encodeUint(amountLD, 32),
|
|
84
|
+
toBytes32(composeFrom),
|
|
85
|
+
normalizeHex(composeMsg)
|
|
86
|
+
]);
|
|
87
|
+
}
|
|
88
|
+
var NATIVE_MSG_FEE_BUFFER = 105n;
|
|
89
|
+
|
|
90
|
+
export { NATIVE_MSG_FEE_BUFFER, buildEndpointV2LzComposePayload, encodeUint, evmRpcFallbackProvider, normalizeHex, toBytes32 };
|
|
91
|
+
//# sourceMappingURL=index.mjs.map
|
|
92
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/evm-rpc-providers.ts","../src/layerzero-compose.ts"],"names":["ethers"],"mappings":";;;AAGA,IAAM,aAAA,uBAAoB,GAAA,EAAqC;AAE/D,IAAM,0BAAA,GAAN,cAAyC,MAAA,CAAO,gBAAA,CAAiB;AAAA,EAG/D,WAAA,CAAY,WAAqC,OAAA,EAAiB;AAChE,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AAAA,EACnB;AAAA,EAEA,MAAM,cAAA,GAA0C;AAC9C,IAAA,OAAO,IAAA,CAAK,SAAA,CAAU,CAAC,CAAA,CAAE,cAAA,EAAe;AAAA,EAC1C;AAAA,EAEA,MAAM,SAAS,GAAA,EAAgD;AAC7D,IAAA,IAAI,SAAA;AACJ,IAAA,KAAA,MAAW,QAAA,IAAY,KAAK,SAAA,EAAW;AACrC,MAAA,IAAI;AACF,QAAA,OAAO,MAAM,QAAA,CAAS,QAAA,CAAS,GAAG,CAAA;AAAA,MACpC,SAAS,GAAA,EAAK;AACZ,QAAA,IAAI,MAAA,CAAO,OAAA,CAAQ,GAAA,EAAK,gBAAgB,GAAG,MAAM,GAAA;AACjD,QAAA,SAAA,GAAY,GAAA;AAAA,MACd;AAAA,IACF;AACA,IAAA,MAAM,SAAA;AAAA,EACR;AACF,CAAA;AAEO,SAAS,uBAAuB,KAAA,EAAoB;AACzD,EAAA,MAAM,EAAE,OAAA,EAAS,OAAA,EAAQ,GAAI,KAAA;AAE7B,EAAA,IAAI,aAAA,C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,CAAQ,EAAE,GAAG,SAAS,CAAA;AAC1D;AAEO,SAAS,gCAAgC,MAAA,EAM7C;AACD,EAAA,MAAM,EAAE,KAAA,EAAO,MAAA,EAAQ,QAAA,EAAU,WAAA,EAAa,YAAW,GAAI,MAAA;AAC7D,EAAA,OAAOA,OAAO,MAAA,CAAO;AAAA,IACnB,UAAA,CAAW,OAAO,CAAC,CAAA;AAAA,IACnB,UAAA,CAAW,QAAQ,CAAC,CAAA;AAAA,IACpB,UAAA,CAAW,UAAU,EAAE,CAAA;AAAA,IACvB,UAAU,WAAW,CAAA;AAAA,IACrB,aAAa,UAAU;AAAA,GACxB,CAAA;AACH;AAEO,IAAM,qBAAA,GAAgC","file":"index.mjs","sourcesContent":["import { ethers } from \"ethers\";\nimport type { TokenConfig } from \"@stableflow/core\";\n\nconst providerCache = new Map<number, ethers.AbstractProvider>();\n\nclass SequentialFallbackProvider extends ethers.AbstractProvider {\n private providers: ethers.JsonRpcProvider[];\n\n constructor(providers: ethers.JsonRpcProvider[], chainId: number) {\n super(chainId);\n this.providers = providers;\n }\n\n async _detectNetwork(): Promise<ethers.Network> {\n return this.providers[0]._detectNetwork();\n }\n\n async _perform(req: ethers.PerformActionRequest): Promise<any> {\n let lastError: unknown;\n for (const provider of this.providers) {\n try {\n return await provider._perform(req);\n } catch (err) {\n if (ethers.isError(err, \"CALL_EXCEPTION\")) throw err;\n lastError = err;\n }\n }\n throw lastError;\n }\n}\n\nexport function evmRpcFallbackProvider(chain: TokenConfig) {\n const { rpcUrls, chainId } = chain;\n\n if (providerCache.has(chainId!)) {\n return providerCache.get(chainId!)!;\n }\n\n const providers = rpcUrls.map(\n (rpc: string) => {\n return new ethers.JsonRpcProvider(rpc, chainId, { staticNetwork: true });\n }\n );\n\n const provider =\n providers.length === 1\n ? providers[0]\n : new SequentialFallbackProvider(providers, chainId!);\n\n providerCache.set(chainId!, provider);\n return provider;\n}\n","import { ethers } from 'ethers';\n\nexport function normalizeHex(hex: string) {\n if (!hex) return '0x';\n const s = String(hex);\n\n const indices = [];\n const re = /0x/gi;\n let m;\n while ((m = re.exec(s)) !== null) {\n indices.push(m.index);\n }\n\n let best = '';\n if (indices.length === 0) {\n best = s.replace(/[^0-9a-fA-F]/g, '');\n } else {\n for (let i = 0; i < indices.length; i++) {\n const start = indices[i] + 2;\n const end = i + 1 < indices.length ? indices[i + 1] : s.length;\n const candidate = s.slice(start, end).replace(/[^0-9a-fA-F]/g, '');\n if (candidate.length > best.length) best = candidate;\n }\n }\n\n if (!best) return '0x';\n if (best.length % 2) throw new Error('hex length must be even');\n return '0x' + best.toLowerCase();\n}\n\nexport function toBytes32(addrOrBytes32: string) {\n const h = normalizeHex(addrOrBytes32);\n if (h.length === 42) return ethers.zeroPadValue(h, 32);\n if (h.length === 66) return h;\n throw new Error('expected address(20B) or bytes32(32B)');\n}\n\nexport function encodeUint(value: string, sizeBytes: number) {\n const bn = BigInt(value);\n const max = (1n << BigInt(sizeBytes * 8)) - 1n;\n\n if (bn < 0n || bn > max) {\n throw new Error(`value does not fit uint${sizeBytes * 8}`);\n }\n\n return ethers.zeroPadValue(ethers.toBeHex(bn), sizeBytes);\n}\n\nexport function buildEndpointV2LzComposePayload(params: {\n nonce: string;\n srcEid: string;\n amountLD: string;\n composeFrom: string;\n composeMsg: string;\n}) {\n const { nonce, srcEid, amountLD, composeFrom, composeMsg } = params;\n return ethers.concat([\n encodeUint(nonce, 8),\n encodeUint(srcEid, 4),\n encodeUint(amountLD, 32),\n toBytes32(composeFrom),\n normalizeHex(composeMsg),\n ]);\n}\n\nexport const NATIVE_MSG_FEE_BUFFER: bigint = 105n;\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stableflow/utils-evm",
|
|
3
|
+
"version": "0.0.0-experimental-5a8afd7389-20260703",
|
|
4
|
+
"description": "EVM utilities for StableFlow SDK (RPC fallback, LayerZero compose)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/stableflow-ai/stableflow-sdk.git",
|
|
9
|
+
"directory": "packages/utils-evm"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.mjs",
|
|
18
|
+
"require": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"ethers": "^6.16.0",
|
|
27
|
+
"@stableflow/core": "0.0.0-experimental-5a8afd7389-20260703"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"tsup": "^8.5.1",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"clean": "rm -rf dist"
|
|
36
|
+
}
|
|
37
|
+
}
|