@layerzerolabs/static-chain-info 0.0.8

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.
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ var VK5U52R7_cjs = require('./VK5U52R7.cjs');
4
+
5
+
6
+
7
+ Object.defineProperty(exports, "ChainSubType", {
8
+ enumerable: true,
9
+ get: function () { return VK5U52R7_cjs.ChainSubType; }
10
+ });
11
+ Object.defineProperty(exports, "isZkChain", {
12
+ enumerable: true,
13
+ get: function () { return VK5U52R7_cjs.isZkChain; }
14
+ });
15
+ Object.defineProperty(exports, "staticConfig", {
16
+ enumerable: true,
17
+ get: function () { return VK5U52R7_cjs.staticConfig; }
18
+ });
19
+ //# sourceMappingURL=staticChainInfo.cjs.map
20
+ //# sourceMappingURL=staticChainInfo.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"staticChainInfo.cjs"}
@@ -0,0 +1,22 @@
1
+ import { ChainName } from '@layerzerolabs/layerzero-definitions';
2
+ export declare enum ChainSubType {
3
+ ARBSTACK = 0,
4
+ OPSTACK = 1,
5
+ AVALANCHE = 2,
6
+ TRON = 3
7
+ }
8
+ export interface StaticInfoForChain {
9
+ chainSubType?: ChainSubType;
10
+ addressSizeInBytes: number;
11
+ decimals: number;
12
+ mptBaseGas?: number;
13
+ fpBaseGas: number;
14
+ symbol: string;
15
+ chainLayer?: number;
16
+ }
17
+ export interface StaticConfig {
18
+ [baseName: string]: StaticInfoForChain;
19
+ }
20
+ export declare const staticConfig: StaticConfig;
21
+ export declare const isZkChain: (chainName: ChainName) => boolean;
22
+ //# sourceMappingURL=staticChainInfo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"staticChainInfo.d.ts","sourceRoot":"","sources":["../src/staticChainInfo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AAEjE,oBAAY,YAAY;IACpB,QAAQ,IAAA;IACR,OAAO,IAAA;IACP,SAAS,IAAA;IACT,IAAI,IAAA;CACP;AAED,MAAM,WAAW,kBAAkB;IAC/B,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IACzB,CAAC,QAAQ,EAAE,MAAM,GAAG,kBAAkB,CAAC;CAC1C;AAWD,eAAO,MAAM,YAAY,EAAE,YAkiD1B,CAAC;AAEF,eAAO,MAAM,SAAS,GAAI,WAAW,SAAS,KAAG,OAWhD,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { ChainSubType, isZkChain, staticConfig } from './FJ5AKDP4.js';
2
+ //# sourceMappingURL=staticChainInfo.js.map
3
+ //# sourceMappingURL=staticChainInfo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"staticChainInfo.js"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@layerzerolabs/static-chain-info",
3
+ "version": "0.0.8",
4
+ "private": false,
5
+ "license": "MIT",
6
+ "sideEffects": false,
7
+ "type": "module",
8
+ "exports": {
9
+ "types": "./dist/index.d.ts",
10
+ "require": "./dist/index.cjs",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.cjs"
13
+ },
14
+ "main": "./dist/index.cjs",
15
+ "module": "./dist/index.js",
16
+ "types": "./dist/index.d.ts",
17
+ "dependencies": {
18
+ "@layerzerolabs/lz-definitions": "3.0.115",
19
+ "@layerzerolabs/chain-utils": "0.0.8",
20
+ "@layerzerolabs/common-chain-model": "0.0.8",
21
+ "@layerzerolabs/common-utils": "0.0.8",
22
+ "@layerzerolabs/layerzero-definitions": "0.0.8"
23
+ },
24
+ "devDependencies": {
25
+ "bs58": "^5.0.0",
26
+ "tsup": "^8.4.0",
27
+ "vitest": "^3.2.3",
28
+ "@layerzerolabs/tsup-configuration": "0.0.8",
29
+ "@layerzerolabs/typescript-configuration": "0.0.8"
30
+ },
31
+ "publishConfig": {
32
+ "access": "restricted",
33
+ "registry": "https://registry.npmjs.org/"
34
+ },
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "clean": "rm -rf ./node_modules .turbo ./dist",
38
+ "dev": "tsup --watch",
39
+ "lint": "eslint . --max-warnings 0",
40
+ "lint:fix": "eslint . --fix --max-warnings 0",
41
+ "test": "vitest --run --pass-with-no-tests"
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,185 @@
1
+ import { getAddressAsBytes32HexString } from '@layerzerolabs/chain-utils';
2
+ import type { HexAddress } from '@layerzerolabs/common-chain-model';
3
+ import { ensure0xPrefixed, hexToBase58, hexZeroPad, isBase58 } from '@layerzerolabs/common-utils';
4
+ import type { Environment } from '@layerzerolabs/layerzero-definitions';
5
+ import { ChainName } from '@layerzerolabs/layerzero-definitions';
6
+ import { EndpointVersion, UlnVersion } from '@layerzerolabs/layerzero-definitions';
7
+ import type { Chain, EndpointId, Stage } from '@layerzerolabs/lz-definitions';
8
+ import {
9
+ chainAndStageToEndpointId,
10
+ ChannelId,
11
+ EndpointV2IdBase,
12
+ getChainIdForNetwork,
13
+ getNetworkForChainId,
14
+ } from '@layerzerolabs/lz-definitions';
15
+
16
+ import { ChainSubType, staticConfig } from './staticChainInfo';
17
+
18
+ export { ChainSubType };
19
+
20
+ export * from './staticChainInfo';
21
+
22
+ export const getChainName = (chainId: string | number | bigint): ChainName => {
23
+ return getNetworkForChainId(parseInt(chainId.toString()))?.chainName! as unknown as ChainName;
24
+ };
25
+
26
+ export const isLzReadEndpointId = (endpointId: number | bigint | string) => {
27
+ return Object.values(ChannelId).includes(Number(endpointId));
28
+ };
29
+
30
+ export const getLZEndpointVersionFromChainId = (chainId: number): EndpointVersion => {
31
+ if (isLzReadEndpointId(chainId)) {
32
+ return EndpointVersion.V2;
33
+ }
34
+ return chainId < EndpointV2IdBase ? EndpointVersion.V1 : EndpointVersion.V2;
35
+ };
36
+
37
+ export const chainIdToEndpointVersion = (chainId: string | number): EndpointVersion => {
38
+ const asNumber = Number(chainId);
39
+ if (!Number.isInteger(asNumber) || asNumber < 0)
40
+ throw new Error('Argument must be positive integer!');
41
+ return getLZEndpointVersionFromChainId(asNumber);
42
+ };
43
+
44
+ export const getEnvFromChainId = (chainId: string): string => {
45
+ return getNetworkForChainId(parseInt(chainId))!['env'];
46
+ };
47
+
48
+ export const getUlnVersionFromChainId = (chainId: string): UlnVersion => {
49
+ const ulnVersionString = getNetworkForChainId(parseInt(chainId))!['ulnVersion'];
50
+ return { '2': UlnVersion.V2 }[ulnVersionString]!; // V1 will not be supported anymore
51
+ };
52
+
53
+ export const getEndpointVersionFromUlnVersion = (ulnVersion: UlnVersion): EndpointVersion => {
54
+ if (ulnVersion === UlnVersion.V1) {
55
+ throw new Error('UlnVersion.V1 is not supported');
56
+ }
57
+ switch (ulnVersion) {
58
+ case UlnVersion.V2:
59
+ case UlnVersion.V301:
60
+ return EndpointVersion.V1;
61
+ case UlnVersion.V300:
62
+ case UlnVersion.ReadV1002:
63
+ case UlnVersion.V302:
64
+ return EndpointVersion.V2;
65
+ default:
66
+ throw new Error(`Unknown UlnVersion: ${ulnVersion}`);
67
+ }
68
+ };
69
+
70
+ /**
71
+ * @deprecated Use getChainIdForEndpointVersion instead
72
+ */
73
+ export const getChainId = (
74
+ chainName: ChainName,
75
+ environment: Environment,
76
+ ulnVersion: UlnVersion,
77
+ ): string => {
78
+ const ulnVer = {
79
+ [UlnVersion.V1]: '1',
80
+ [UlnVersion.V2]: '2',
81
+ [UlnVersion.V300]: '3.0.2', // simpleMessageLib should get V302 chainIds
82
+ [UlnVersion.V301]: '3.0.1',
83
+ [UlnVersion.V302]: '3.0.2',
84
+ [UlnVersion.ReadV1002]: '10.0.2',
85
+ }[ulnVersion];
86
+
87
+ return getChainIdForNetwork(chainName, environment, ulnVer).valueOf().toString();
88
+ };
89
+
90
+ // Note: This doesn't work for UlnVersion.V1
91
+ export const getChainIdForEndpointVersion = (
92
+ chainName: ChainName,
93
+ environment: Environment,
94
+ version: EndpointVersion,
95
+ ): EndpointId => {
96
+ // FIXME-STARKNET: Remove this once we have a proper ChainId for Starknet
97
+ if (chainName === ChainName.STARKNET) {
98
+ return 80085 as EndpointId;
99
+ }
100
+
101
+ // TODO: This is essentially 'getChainIdForNetwork' but skipping the UlnVersion -> EndpointVersion step, refactor monorepo to export this function
102
+ return chainAndStageToEndpointId(
103
+ chainName as unknown as Chain,
104
+ environment as unknown as Stage,
105
+ version,
106
+ );
107
+ };
108
+
109
+ export const getVId = (chainName: ChainName, environment: Environment): string => {
110
+ // By convention the vid is alwayd the endpointV1 chainId
111
+ // TODO: Monorepo doesnt have these chains for EndpointVersion.V1
112
+ if (['solana', 'ton', 'initia', 'movement'].includes(chainName)) {
113
+ const eid = getChainIdForEndpointVersion(chainName, environment, EndpointVersion.V2);
114
+ return (parseInt(eid.toString()) % 30000).toString();
115
+ }
116
+ return getChainIdForEndpointVersion(chainName, environment, EndpointVersion.V1).toString();
117
+ };
118
+
119
+ export class StaticChainConfigs {
120
+ static getChainName(chainId: string | number): ChainName {
121
+ return getChainName(chainId);
122
+ }
123
+ static getAddressSizeInBytesFromChainId(chainId: string | number): number {
124
+ return StaticChainConfigs.getAddressSizeInBytes(StaticChainConfigs.getChainName(chainId));
125
+ }
126
+ static getAddressSizeInBytes(chainName: ChainName): number {
127
+ return staticConfig[chainName].addressSizeInBytes;
128
+ }
129
+ static getSymbol(chainName: ChainName): string {
130
+ return staticConfig[chainName].symbol;
131
+ }
132
+
133
+ static getDecimals(chainName: ChainName): number {
134
+ return staticConfig[chainName].decimals;
135
+ }
136
+
137
+ static getMptBaseFee(chainName: ChainName): number {
138
+ return staticConfig[chainName].mptBaseGas!;
139
+ }
140
+
141
+ static getChainLayer(chainName: ChainName): number {
142
+ return staticConfig[chainName].chainLayer ?? 1;
143
+ }
144
+
145
+ static getChainSubType(chainName: ChainName): ChainSubType | undefined {
146
+ return staticConfig[chainName].chainSubType;
147
+ }
148
+ }
149
+
150
+ // TODO should be replaces with AddressParserSDK once it is written https://linear.app/layerzerolabs/issue/BE-1876
151
+ export function getAddressEncodedByChain(chainName: ChainName, address: HexAddress): string {
152
+ // SOLANA addresses is base58 encoded and should not be lower cased
153
+ // or web3.js cannot convert it to PublicKey
154
+ if (chainName === ChainName.SOLANA) {
155
+ return isBase58(address) ? address : hexToBase58(address);
156
+ } else if (['aptos', 'movement', 'initia', 'ton'].includes(chainName)) {
157
+ const addressSizeInBytes = StaticChainConfigs.getAddressSizeInBytes(chainName);
158
+ // APTOS addresses is bytes32 hex encoded
159
+ return hexZeroPad(ensure0xPrefixed(address), addressSizeInBytes).toLowerCase();
160
+ }
161
+ // EVM addresses are 20 bytes
162
+ // The getAddress(...).toLowerCase() does not change the address in this case but it does some validation
163
+ return hexZeroPad(ensure0xPrefixed(address), 32).slice(-40).toLowerCase();
164
+ }
165
+
166
+ export function convertNativeAddressToHex(nativeAddress: {
167
+ nativeAddress: string;
168
+ chainName: ChainName;
169
+ }): HexAddress {
170
+ const { nativeAddress: address, chainName } = nativeAddress;
171
+ // SOLANA addresses is base58 encoded and should not be lower cased
172
+ // or web3.js cannot convert it to PublicKey
173
+ if (chainName === ChainName.SOLANA) {
174
+ return getAddressAsBytes32HexString(address);
175
+ } else if (['aptos', 'movement', 'initia', 'ton'].includes(chainName)) {
176
+ const addressSizeInBytes = StaticChainConfigs.getAddressSizeInBytes(chainName);
177
+ // APTOS addresses is bytes32 hex encoded
178
+ return ensure0xPrefixed(
179
+ hexZeroPad(address, addressSizeInBytes).toLowerCase(),
180
+ ) as HexAddress;
181
+ }
182
+ // EVM addresses are 20 bytes
183
+ // The getAddress(...).toLowerCase() does not change the address in this case but it does some validation
184
+ return ensure0xPrefixed(hexZeroPad(address, 32).slice(-40).toLowerCase()) as HexAddress;
185
+ }