diff --git a/package.json b/package.json index eb0bb08e..f347019c 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "scripts": { "lint": "prettier \"./**\" --write", "build": "hardhat export-abi && tsc --project lib.tsconfig.json", + "tsc": "tsc --project lib.tsconfig.json", "abi": "hardhat export-abi", "compile": "forge build", "test": "prettier --write $(git diff --name-only --diff-filter d | xargs) && forge test", diff --git a/scripts/deploy/helpers/send-msg/allPathTest.ts b/scripts/deploy/helpers/send-msg/allPathTest.ts index bd16f732..e5b4e4db 100644 --- a/scripts/deploy/helpers/send-msg/allPathTest.ts +++ b/scripts/deploy/helpers/send-msg/allPathTest.ts @@ -9,15 +9,16 @@ import { isTestnet, isMainnet, CORE_CONTRACTS, + DeploymentMode, } from "../../../../src"; import { getAddresses, getRelayUrl, getRelayAPIKEY } from "../../utils"; import { BigNumber, Contract, ethers } from "ethers"; import Counter from "../../../../out/Counter.sol/Counter.json"; import Socket from "../../../../out/Socket.sol/Socket.json"; -import { chains, mode, overrides } from "../../config"; import { getProviderFromChainSlug } from "../../../constants/networks"; import { formatEther } from "ethers/lib/utils"; +import { chains, mode, overrides } from "../../config/config"; interface RequestObj { to: string; @@ -28,6 +29,10 @@ interface RequestObj { gasLimit: string | number | undefined; } +const API_BASE_URL = + mode == DeploymentMode.DEV + ? "https://raf5spoep4.execute-api.us-east-1.amazonaws.com/dev/v1" + : "https://prod.dlapi.socket.tech"; const getSiblingSlugs = (chainSlug: ChainSlug): ChainSlug[] => { console.log(chainSlug, isMainnet(chainSlug)); if (isTestnet(chainSlug)) @@ -208,7 +213,7 @@ export const sendMessagesToAllPaths = async (params: { provider ); console.log( - `Track message here: https://prod.dlapi.socket.tech/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${response?.hash}` + `Track message here: ${API_BASE_URL}/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${response?.hash}` ); }) ); diff --git a/scripts/rpcConfig/constants/confirmations.ts b/scripts/rpcConfig/constants/confirmations.ts deleted file mode 100644 index ebe625d6..00000000 --- a/scripts/rpcConfig/constants/confirmations.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { ChainSlug } from "../../../src"; - -export const confirmations = { - [ChainSlug.AEVO]: 2, - [ChainSlug.ARBITRUM]: 1, - [ChainSlug.LYRA]: 2, - [ChainSlug.OPTIMISM]: 15, - [ChainSlug.BSC]: 1, - [ChainSlug.POLYGON_MAINNET]: 256, - [ChainSlug.MAINNET]: 18, - [ChainSlug.BASE]: 1, - [ChainSlug.MODE]: 2, - [ChainSlug.ARBITRUM_GOERLI]: 1, - [ChainSlug.AEVO_TESTNET]: 1, - [ChainSlug.LYRA_TESTNET]: 1, - [ChainSlug.OPTIMISM_GOERLI]: 1, - [ChainSlug.GOERLI]: 1, - [ChainSlug.XAI_TESTNET]: 1, - [ChainSlug.SX_NETWORK_TESTNET]: 1, - [ChainSlug.SX_NETWORK]: 1, - [ChainSlug.MODE_TESTNET]: 1, - [ChainSlug.VICTION_TESTNET]: 1, - [ChainSlug.CDK_TESTNET]: 1, - [ChainSlug.ARBITRUM_SEPOLIA]: 1, - [ChainSlug.OPTIMISM_SEPOLIA]: 1, - [ChainSlug.SEPOLIA]: 1, - [ChainSlug.ANCIENT8_TESTNET]: 1, - [ChainSlug.ANCIENT8_TESTNET2]: 1, - [ChainSlug.HOOK_TESTNET]: 1, - [ChainSlug.HOOK]: 1, - [ChainSlug.PARALLEL]: 1, - [ChainSlug.MANTLE]: 1, - [ChainSlug.REYA_CRONOS]: 1, - [ChainSlug.REYA]: 0, - [ChainSlug.SYNDR_SEPOLIA_L3]: 1, - [ChainSlug.POLYNOMIAL_TESTNET]: 1, - [ChainSlug.BOB]: 0, - [ChainSlug.KINTO]: 0, - [ChainSlug.KINTO_DEVNET]: 0, - [ChainSlug.SIPHER_FUNKI_TESTNET]: 0, - [ChainSlug.WINR]: 0, - [ChainSlug.BLAST]: 0, - [ChainSlug.BSC_TESTNET]: 1, - [ChainSlug.POLYNOMIAL]: 1, -}; diff --git a/scripts/rpcConfig/constants/defaultFinalityBucket.ts b/scripts/rpcConfig/constants/defaultFinalityBucket.ts new file mode 100644 index 00000000..0e07a4e0 --- /dev/null +++ b/scripts/rpcConfig/constants/defaultFinalityBucket.ts @@ -0,0 +1,8 @@ +import { ChainSlug } from "../../../src/enums/chainSlug"; + +import { FinalityBucket } from "../../../src/socket-types"; + +export const defaultFinalityBucket = { + [ChainSlug.POLYGON_MAINNET]: FinalityBucket.medium, + [ChainSlug.MAINNET]: FinalityBucket.medium, +}; diff --git a/scripts/rpcConfig/constants/finality.ts b/scripts/rpcConfig/constants/finality.ts new file mode 100644 index 00000000..75a1a162 --- /dev/null +++ b/scripts/rpcConfig/constants/finality.ts @@ -0,0 +1,19 @@ +import { ChainSlug } from "../../../src/enums/chainSlug"; + +import { ChainFinalityInfo, FinalityBucket } from "../../../src/socket-types"; + +export const getFinality = ( + chainSlug: ChainSlug +): ChainFinalityInfo | undefined => { + return finalityOverrides[chainSlug]; +}; + +export const finalityOverrides: { + [chainSlug in ChainSlug]?: ChainFinalityInfo; +} = { + [ChainSlug.POLYGON_MAINNET]: { + [FinalityBucket.fast]: 64, + [FinalityBucket.medium]: 256, + [FinalityBucket.slow]: 1000, + }, +}; diff --git a/scripts/rpcConfig/constants/index.ts b/scripts/rpcConfig/constants/index.ts index 21a613be..701451b6 100644 --- a/scripts/rpcConfig/constants/index.ts +++ b/scripts/rpcConfig/constants/index.ts @@ -1,6 +1,7 @@ export * from "./batcherSupportedChainSlug"; -export * from "./confirmations"; +export * from "./reSyncInterval"; export * from "./explorers"; export * from "./icons"; export * from "./rpc"; export * from "./version"; +export * from "./finality"; diff --git a/scripts/rpcConfig/constants/reSyncInterval.ts b/scripts/rpcConfig/constants/reSyncInterval.ts new file mode 100644 index 00000000..91ecbcd9 --- /dev/null +++ b/scripts/rpcConfig/constants/reSyncInterval.ts @@ -0,0 +1,6 @@ +import { ChainSlug } from "../../../src"; + +export const reSyncInterval = { + [ChainSlug.POLYGON_MAINNET]: 256, + [ChainSlug.MAINNET]: 6, +}; diff --git a/scripts/rpcConfig/constants/version.ts b/scripts/rpcConfig/constants/version.ts index a4803bc7..bc2b55bb 100644 --- a/scripts/rpcConfig/constants/version.ts +++ b/scripts/rpcConfig/constants/version.ts @@ -1,6 +1,6 @@ import { DeploymentMode } from "../../../src"; export const version = { - [DeploymentMode.DEV]: "1.0.1", + [DeploymentMode.DEV]: "1.0.2", [DeploymentMode.PROD]: "1.0.20", }; diff --git a/scripts/rpcConfig/rpcConfig.ts b/scripts/rpcConfig/rpcConfig.ts index 85e3b4c8..cdc8d210 100644 --- a/scripts/rpcConfig/rpcConfig.ts +++ b/scripts/rpcConfig/rpcConfig.ts @@ -17,16 +17,19 @@ import { arbL3Chains, polygonCDKChains, S3ChainConfig, + FinalityBucket, } from "../../src"; import { - confirmations, + reSyncInterval, explorers, icons, batcherSupportedChainSlugs, prodFeesUpdaterSupportedChainSlugs, rpcs, version, + getFinality, } from "./constants"; +import { defaultFinalityBucket } from "./constants/defaultFinalityBucket"; import { getChainTxData } from "./txdata-builder/generate-calldata"; import dotenv from "dotenv"; @@ -91,7 +94,9 @@ const getChainData = async ( chainTxData: await getChainTxData(chainSlug, txData), nativeToken: getCurrency(chainSlug), chainType: getChainType(chainSlug), - confirmations: confirmations[chainSlug], + reSyncInterval: getReSyncInterval(chainSlug), + finalityInfo: getFinality(chainSlug), + defaultFinalityBucket: getDefaultFinalityBucket(chainSlug), icon: icons[chainSlug], }; }; @@ -158,3 +163,13 @@ export const generateProdConfig = async (txData: TxData): Promise => { chainSlugToId: ChainSlugToId, }; }; + +export const getDefaultFinalityBucket = ( + chainSlug: ChainSlug +): FinalityBucket => { + return defaultFinalityBucket[chainSlug] ?? FinalityBucket.fast; +}; + +export const getReSyncInterval = (chainSlug: ChainSlug) => { + return reSyncInterval[chainSlug] ?? 0; +}; diff --git a/src/index.ts b/src/index.ts index 3934760f..aa1241ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,10 @@ import { DeploymentMode } from "./socket-types"; - +export { getFinality } from "../scripts/rpcConfig/constants/finality"; export * from "./socket-types"; export * from "./enums"; export * from "./addresses"; export * from "./currency-util"; +export * from "./transmission-utils"; export const version = { [DeploymentMode.DEV]: "IMLI", diff --git a/src/socket-types.ts b/src/socket-types.ts index b8163433..afb2b056 100644 --- a/src/socket-types.ts +++ b/src/socket-types.ts @@ -206,6 +206,22 @@ export enum ChainType { default = "default", } +export enum FinalityBucket { + fast = 1, // latest + medium = 2, // safe + slow = 3, // finalized +} + +export enum TransmissionParamFinalityType { + bucket = 1, +} + +export type ChainFinalityInfo = { + [FinalityBucket.fast]: number; + [FinalityBucket.medium]: number; + [FinalityBucket.slow]: number; +}; + export type TxData = { [chainSlug in ChainSlug]?: ChainTxData; }; @@ -220,7 +236,7 @@ export interface ChainTxData { export interface S3ChainConfig { rpc: string; blockNumber: number; - confirmations: number; + reSyncInterval: number; siblings: ChainSlug[]; chainName: string; eventBlockRange?: number; @@ -229,6 +245,8 @@ export interface S3ChainConfig { chainTxData?: ChainTxData; explorer?: string; icon?: string; + finalityInfo?: ChainFinalityInfo; + defaultFinalityBucket?: FinalityBucket; } export type S3Config = { diff --git a/src/transmission-utils.ts b/src/transmission-utils.ts new file mode 100644 index 00000000..7467d96d --- /dev/null +++ b/src/transmission-utils.ts @@ -0,0 +1,57 @@ +/* +Transmission param format: +- first byte - version - current version is 1 +- 2nd byte - finalityType (1: bucket) +- next 4 bytes - value - bucket (1:fast, 2:medium, 3:slow) + +eg : want to transmit message with slow speed - +transmissionParam = 0x0101000000030000000000000000000000000000000000000000000000000000 + +eg : want to transmit message with fast speed - +transmissionParam = 0x0101000000010000000000000000000000000000000000000000000000000000 + +if version is 0, or invalid finality type, or invalid value is mentioned, it will use the default Bucket for that chain +*/ + +export const decodeTransmissionParams = (transmissionParam: string) => { + if (transmissionParam.length !== 64 && transmissionParam.length !== 66) { + throw new Error("Invalid transmission param length"); + } + transmissionParam = transmissionParam.replace("0x", ""); + const version = parseInt("0x" + transmissionParam.slice(0, 2)); + const finalityType = parseInt("0x" + transmissionParam.slice(2, 4)); + const value = parseInt("0x" + transmissionParam.slice(4, 12)); + + return { version, finalityType, value }; +}; + +export const encodeTransmissionParams = ( + version: number, + finalityType: number, + value: number +) => { + let transmissionParam = "0x"; + transmissionParam += version.toString(16).padStart(2, "0"); + transmissionParam += finalityType.toString(16).padStart(2, "0"); + transmissionParam += value.toString(16).padStart(8, "0"); + transmissionParam = transmissionParam.padEnd(66, "0"); + return transmissionParam; +}; + +export const isTxFinalized = ( + currentBlock: number, + eventBlock: number, + finalityBlockDiff: number +) => { + if ( + currentBlock == null || + currentBlock == undefined || + eventBlock == null || + eventBlock == undefined || + finalityBlockDiff == null || + finalityBlockDiff == undefined + ) { + throw new Error("Invalid data for block finality check"); + } + return currentBlock - eventBlock >= finalityBlockDiff; +}; diff --git a/yarn.lock b/yarn.lock index e24b4dbd..c4e1fc17 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11343,6 +11343,11 @@ semver@^7.3.8: dependencies: lru-cache "^6.0.0" +semver@^7.5.4: + version "7.6.2" + resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.2.tgz#1e3b34759f896e8f14d6134732ce798aeb0c6e13" + integrity sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w== + semver@^7.6.0: version "7.6.0" resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.0.tgz#1a46a4db4bffcccd97b743b5005c8325f23d4e2d"