From edd723d361a77260df8e30c5beda23350cf5f6be Mon Sep 17 00:00:00 2001 From: Akash Date: Mon, 1 Jul 2024 15:08:18 +0530 Subject: [PATCH 1/4] fix : outbound load test script --- .../helpers/send-msg/outbound-load-test.ts | 144 ++++++++---------- 1 file changed, 61 insertions(+), 83 deletions(-) diff --git a/scripts/deploy/helpers/send-msg/outbound-load-test.ts b/scripts/deploy/helpers/send-msg/outbound-load-test.ts index 9bc3dcb2..cefbd310 100644 --- a/scripts/deploy/helpers/send-msg/outbound-load-test.ts +++ b/scripts/deploy/helpers/send-msg/outbound-load-test.ts @@ -11,14 +11,21 @@ import { getProviderFromChainSlug } from "../../../constants"; import SocketABI from "../../../../out/Socket.sol/Socket.json"; import path from "path"; -import { mode } from "../../config/config"; +import { mode, overrides } from "../../config/config"; import { CORE_CONTRACTS, + DeploymentMode, HardhatChainName, hardhatChainNameToSlug, } from "../../../../src"; import { sleep } from "@socket.tech/dl-common"; import { formatEther } from "ethers/lib/utils"; +import { ChainSlug } from "@socket.tech/dl-core"; + +const API_BASE_URL = + mode == DeploymentMode.DEV + ? process.env.DL_API_DEV_URL + : process.env.DL_API_PROD_URL; const deployedAddressPath = path.join( __dirname, @@ -31,10 +38,14 @@ const helperContractAddress = { 11155111: "0x91C27Cad374246314E756f8Aa2f62F433d6F102C", 80001: "0x7d96De5fa59F61457da325649bcF2B4e500055Ad", 421613: "0x60c3A0bCEa43F5aaf8743a41351C0a7b982aE01E", + [ChainSlug.ARBITRUM_SEPOLIA]: "0x2c3E3Ff54d82cA96BBB2F4529bee114eB200e3F0", + [ChainSlug.OPTIMISM_SEPOLIA]: "0x4B882c8A1009c0a4fd80151FEb6d1a3656C49C9a", 420: "0xD21e53E568FD904c2599E41aFC2434ea11b38A2e", 5: "0x28f26c101e3F694f1d03D477b4f34F8835141611", }; +const payload = + "0xbad314e77c9e165fb6cdad2b69ae15ea10f47a976480a84ea0ef9a8b8817b997000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000"; const helperABI = [ { inputs: [ @@ -58,6 +69,11 @@ const helperABI = [ name: "totalMsgs_", type: "uint256", }, + { + internalType: "bytes", + name: "payload", + type: "bytes", + }, ], name: "remoteAddOperationBatch", outputs: [], @@ -66,15 +82,15 @@ const helperABI = [ }, ]; -const WAIT_FOR_TX = false; +const WAIT_FOR_TX = true; const totalIterations = 10; // usage: -// npx ts-node scripts/deploy/scripts/outbound-load-test.ts --chain optimism-goerli --remoteChain arbitrum-goerli --numOfRequests 10 --waitTime 6 +// npx ts-node scripts/deploy/helpers/send-msg/outbound-load-test.ts --chain arbitrum_sepolia --remoteChain optimism_sepolia --numOfRequests 10 --waitTime 6 export const main = async () => { const amount = 100; - const msgGasLimit = "100000"; + const msgGasLimit = "0"; let remoteChainSlug; try { @@ -110,17 +126,25 @@ export const main = async () => { const chain = argv.chain as HardhatChainName; const chainSlug = hardhatChainNameToSlug[chain]; - const providerInstance = getProviderFromChainSlug(chainSlug); + if (!process.env.LOAD_TEST_PRIVATE_KEY) { + console.error("LOAD_TEST_PRIVATE_KEY not found in env"); + return; + } const signer = new ethers.Wallet( - process.env.SOCKET_SIGNER_KEY as string, + process.env.LOAD_TEST_PRIVATE_KEY as string, providerInstance ); - + console.log("signer address : ", signer.address); + console.log( + "signer balance on chain ", + chainSlug, + " is ", + formatEther(await signer.getBalance()) + ); const remoteChain = argv.remoteChain as HardhatChainName; remoteChainSlug = hardhatChainNameToSlug[remoteChain]; - const numOfRequests = argv.numOfRequests as number; const waitTime = argv.waitTime as number; @@ -129,7 +153,10 @@ export const main = async () => { ); const counterAddress = config[chainSlug]["Counter"]; - + if (!helperContractAddress[chainSlug]) { + console.log("helperContractAddress not found for ", chainSlug); + return; + } const helper: Contract = new ethers.Contract( helperContractAddress[chainSlug], helperABI, @@ -151,31 +178,19 @@ export const main = async () => { counterAddress ); console.log("fees : ", value.toString(), formatEther(value)); - if (WAIT_FOR_TX) { - await confirmAndWait( - signer, - helper, - remoteChainSlug, - amount, - msgGasLimit, - numOfRequests, - value, - waitTime, - chainSlug - ); - } else { - await sendAndWait( - signer, - helper, - remoteChainSlug, - amount, - msgGasLimit, - numOfRequests, - value, - waitTime, - chainSlug - ); - } + + await sendTx( + signer, + helper, + remoteChainSlug, + amount, + msgGasLimit, + numOfRequests, + value, + waitTime, + chainSlug, + WAIT_FOR_TX + ); } catch (error) { console.log( `Error while sending remoteAddOperation with ${amount} amount and ${msgGasLimit} gas limit to counter at ${remoteChainSlug}` @@ -185,7 +200,7 @@ export const main = async () => { } }; -const sendAndWait = async ( +const sendTx = async ( signer, helper, remoteChainSlug, @@ -194,9 +209,14 @@ const sendAndWait = async ( numOfRequests, value, waitTime, - chainSlug + chainSlug, + waitForConfirmation: boolean ) => { const nonce = await signer.getTransactionCount(); + console.log( + "total value: ", + formatEther(BigNumber.from(value).mul(numOfRequests)) + ); for (let index = 0; index < totalIterations; index++) { const tx = await helper @@ -206,6 +226,7 @@ const sendAndWait = async ( amount, msgGasLimit, numOfRequests, + payload, { value: BigNumber.from(value).mul(numOfRequests), nonce: nonce + index, @@ -220,56 +241,13 @@ const sendAndWait = async ( }` ); console.log( - `Track here: https://6il289myzb.execute-api.us-east-1.amazonaws.com/dev/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${tx.hash - .toString() - .toLowerCase()}` + `Track here: ${API_BASE_URL}/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${tx?.hash}` ); - if (waitTime && waitTime > 0) { - await sleep(waitTime); + if (waitForConfirmation) { + await tx.wait(); + console.log(`remoteAddOperation-tx with hash: ${tx.hash} confirmed`); } - } -}; - -const confirmAndWait = async ( - signer, - helper, - remoteChainSlug, - amount, - msgGasLimit, - numOfRequests, - value, - waitTime, - chainSlug -) => { - for (let i = 0; i < totalIterations; i++) { - const tx = await helper - .connect(signer) - .remoteAddOperationBatch( - remoteChainSlug, - amount, - msgGasLimit, - numOfRequests, - { - value: BigNumber.from(value).mul(numOfRequests), - ...overrides(chainSlug), - } - ); - - await tx.wait(); - console.log( - `remoteAddOperation-tx with hash: ${ - tx.hash - } was sent with ${amount} amount and ${msgGasLimit} gas limit to counter at ${remoteChainSlug}, value: ${ - value * numOfRequests - }` - ); - console.log( - `Track here: https://6il289myzb.execute-api.us-east-1.amazonaws.com/dev/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${tx.hash - .toString() - .toLowerCase()}` - ); - if (waitTime && waitTime > 0) { await sleep(waitTime); } From a569d0b97266cb8f066fa07f668640db0333d3b4 Mon Sep 17 00:00:00 2001 From: Akash Date: Mon, 1 Jul 2024 16:09:06 +0530 Subject: [PATCH 2/4] chore: refactor allPathTest and outbound-load-test --- .env.example | 4 + .../deploy/helpers/send-msg/allPathTest.ts | 205 +++--------------- .../helpers/send-msg/outbound-load-test.ts | 88 ++------ scripts/deploy/helpers/send-msg/outbound.ts | 61 ------ scripts/deploy/helpers/send-msg/utils.ts | 148 +++++++++++++ scripts/deploy/utils/index.ts | 1 + scripts/deploy/utils/relayer.ts | 44 ++++ scripts/deploy/utils/utils.ts | 9 + 8 files changed, 253 insertions(+), 307 deletions(-) delete mode 100644 scripts/deploy/helpers/send-msg/outbound.ts create mode 100644 scripts/deploy/helpers/send-msg/utils.ts create mode 100644 scripts/deploy/utils/relayer.ts diff --git a/.env.example b/.env.example index 15a1492d..4ba406c0 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,10 @@ RELAYER_URL_DEV="" RELAYER_URL_SURGE="" RELAYER_URL_PROD="" +# dl api +DL_API_DEV_URL="" +DL_API_PROD_URL="" + # etherscan verification ETHERSCAN_API_KEY=xxx POLYGONSCAN_API_KEY=xxx diff --git a/scripts/deploy/helpers/send-msg/allPathTest.ts b/scripts/deploy/helpers/send-msg/allPathTest.ts index 2df29fc6..d78a34a1 100644 --- a/scripts/deploy/helpers/send-msg/allPathTest.ts +++ b/scripts/deploy/helpers/send-msg/allPathTest.ts @@ -1,41 +1,17 @@ import { config as dotenvConfig } from "dotenv"; -import axios from "axios"; - -dotenvConfig(); import { ChainSlug, - TestnetIds, MainnetIds, - isTestnet, + TestnetIds, isMainnet, - CORE_CONTRACTS, - DeploymentMode, + isTestnet, } 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 { getProviderFromChainSlug } from "../../../constants/networks"; -import { formatEther } from "ethers/lib/utils"; -import { chains, mode, overrides } from "../../config/config"; - -interface RequestObj { - to: string; - data: string; - chainSlug: number; - value?: string | BigNumber; - gasPrice?: string | BigNumber | undefined; - gasLimit: string | number | undefined; -} +dotenvConfig(); -const API_BASE_URL = - mode == DeploymentMode.DEV - ? "https://raf5spoep4.execute-api.us-east-1.amazonaws.com/dev/v1" - : "https://prod.dlapi.socket.tech"; +import { chains } from "../../config/config"; +import { sendCounterBridgeMsg } from "./utils"; const getSiblingSlugs = (chainSlug: ChainSlug): ChainSlug[] => { - console.log(chainSlug, isMainnet(chainSlug)); if (isTestnet(chainSlug)) return TestnetIds.filter( (siblingChainSlug) => chainSlug !== siblingChainSlug @@ -47,68 +23,21 @@ const getSiblingSlugs = (chainSlug: ChainSlug): ChainSlug[] => { return []; }; -const axiosPost = async (url: string, data: object, config = {}) => { - try { - let response = await axios.post(url, data, config); - // console.log("txStatus : ", response.status, response.data); - return { success: true, ...response?.data }; - } catch (error) { - //@ts-ignore - console.log("status : ", error?.response?.status); - console.log( - "error occurred, url : ", - url, - "data : ", - data, - config, - "\n error : ", - //@ts-ignore - error?.message, - //@ts-ignore - error?.response.data - ); - //@ts-ignore - return { success: false, ...error?.response?.data }; - } -}; - -const relayTx = async (params: RequestObj, provider: any) => { - try { - let { to, data, chainSlug, gasPrice, value, gasLimit } = params; - let url = await getRelayUrl(mode); - let config = { - headers: { - "x-api-key": getRelayAPIKEY(mode), - }, - }; - // console.log({url}) - let body = { - to, - data, - value, - chainId: (await provider.getNetwork()).chainId, - gasLimit, - gasPrice, - sequential: false, - source: "LoadTester", - }; - let response = await axiosPost(url!, body, config); - if (response?.success) return response?.data; - else return { hash: "" }; - } catch (error) { - console.log("uncaught error"); - } +const config = { + msgGasLimit: "200000", + payloadSize: 100, // for counter add operation + executionParams: + "0x0000000000000000000000000000000000000000000000000000000000000000", + transmissionParams: + "0x0000000000000000000000000000000000000000000000000000000000000000", }; export const sendMessagesToAllPaths = async (params: { senderChains: ChainSlug[]; receiverChains: ChainSlug[]; - count: number; }) => { - const amount = 100; - const msgGasLimit = "200000"; // update this when add fee logic for dst gas limit try { - let { senderChains, receiverChains, count } = params; + let { senderChains, receiverChains } = params; console.log("================= checking for : ", params); let activeChainSlugs = @@ -118,36 +47,6 @@ export const sendMessagesToAllPaths = async (params: { await Promise.all( activeChainSlugs.map(async (chainSlug) => { const siblingSlugs = getSiblingSlugs(chainSlug); - const addresses = await getAddresses(chainSlug, mode); - - console.log({ chainSlug, siblingSlugs }); - - if (!addresses) { - console.log("addresses not found for ", chainSlug, addresses); - return; - } - - // console.log(" 2 "); - - const counterAddress = addresses["Counter"]; - if (!counterAddress) { - console.log(chainSlug, "counter address not present: ", chainSlug); - return; - } - // console.log(" 3 "); - - const provider = await getProviderFromChainSlug(chainSlug); - const socket: Contract = new ethers.Contract( - addresses[CORE_CONTRACTS.Socket], - Socket.abi, - provider - ); - - const counter: Contract = new ethers.Contract( - counterAddress, - Counter.abi - ); - await Promise.all( siblingSlugs.map(async (siblingSlug) => { if ( @@ -155,68 +54,25 @@ export const sendMessagesToAllPaths = async (params: { !receiverChains.includes(siblingSlug) ) return; - - // value = 100 - let executionParams = - "0x0000000000000000000000000000000000000000000000000000000000000000"; - let transmissionParams = - "0x0000000000000000000000000000000000000000000000000000000000000000"; - let data = counter.interface.encodeFunctionData( - "remoteAddOperation", - [ - siblingSlug, - amount, - msgGasLimit, - // executionParams, - ethers.constants.HashZero, - ethers.constants.HashZero, - ] + console.log( + "sending message from ", + chainSlug, + " to ", + siblingSlug ); - let to = counter.address; - let value = await socket.getMinFees( + const { + payloadSize, msgGasLimit, - Math.ceil(data.length / 2), // payload size executionParams, transmissionParams, + } = config; + await sendCounterBridgeMsg( + chainSlug, siblingSlug, - to - ); - - console.log( - `fees for path ${chainSlug}-${siblingSlug} is ${formatEther( - value - )}` - ); - - const gasLimit: number | string | undefined = - chainSlug === ChainSlug.ARBITRUM || - chainSlug === ChainSlug.ARBITRUM_SEPOLIA - ? 200000 - : overrides(chainSlug)?.gasLimit - ? overrides(chainSlug).gasLimit.toString() - : undefined; - - let tempArray = new Array(count).fill(1); - await Promise.all( - tempArray.map(async (c) => { - // console.log(c) - console.log(to, data, value); - let response = await relayTx( - { - to, - data, - value, - gasLimit, - gasPrice: - overrides(chainSlug)?.gasPrice?.toString() || undefined, - chainSlug, - }, - provider - ); - console.log( - `Track message here: ${API_BASE_URL}/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${response?.hash}` - ); - }) + msgGasLimit, + payloadSize, + executionParams, + transmissionParams ); }) ); @@ -228,10 +84,9 @@ export const sendMessagesToAllPaths = async (params: { }; const main = async () => { - let senderChains = chains; - let receiverChains = chains; - let count = 1; - await sendMessagesToAllPaths({ senderChains, receiverChains, count }); + let senderChains = [ChainSlug.OPTIMISM_SEPOLIA]; + let receiverChains = [ChainSlug.ARBITRUM_SEPOLIA]; + await sendMessagesToAllPaths({ senderChains, receiverChains }); }; main() diff --git a/scripts/deploy/helpers/send-msg/outbound-load-test.ts b/scripts/deploy/helpers/send-msg/outbound-load-test.ts index cefbd310..82acd48c 100644 --- a/scripts/deploy/helpers/send-msg/outbound-load-test.ts +++ b/scripts/deploy/helpers/send-msg/outbound-load-test.ts @@ -1,31 +1,21 @@ import { config as dotenvConfig } from "dotenv"; -dotenvConfig(); import { BigNumber, constants } from "ethers"; +dotenvConfig(); +import { Contract, ethers } from "ethers"; import fs from "fs"; -import { ethers } from "ethers"; -import { Contract } from "ethers"; -require("dotenv").config(); import yargs from "yargs"; import { getProviderFromChainSlug } from "../../../constants"; -import SocketABI from "../../../../out/Socket.sol/Socket.json"; +require("dotenv").config(); -import path from "path"; -import { mode, overrides } from "../../config/config"; -import { - CORE_CONTRACTS, - DeploymentMode, - HardhatChainName, - hardhatChainNameToSlug, -} from "../../../../src"; import { sleep } from "@socket.tech/dl-common"; -import { formatEther } from "ethers/lib/utils"; import { ChainSlug } from "@socket.tech/dl-core"; - -const API_BASE_URL = - mode == DeploymentMode.DEV - ? process.env.DL_API_DEV_URL - : process.env.DL_API_PROD_URL; +import { formatEther } from "ethers/lib/utils"; +import path from "path"; +import { HardhatChainName, hardhatChainNameToSlug } from "../../../../src"; +import { mode } from "../../config/config"; +import { getAPIBaseURL } from "../../utils"; +import { LoadTestHelperABI, getSocketFees } from "./utils"; const deployedAddressPath = path.join( __dirname, @@ -36,51 +26,12 @@ const deployedAddressPath = path.join( const helperContractAddress = { 11155112: "0xF76E77186Ae85Fa0D5fce51D03e59b964fe7717A", 11155111: "0x91C27Cad374246314E756f8Aa2f62F433d6F102C", - 80001: "0x7d96De5fa59F61457da325649bcF2B4e500055Ad", - 421613: "0x60c3A0bCEa43F5aaf8743a41351C0a7b982aE01E", [ChainSlug.ARBITRUM_SEPOLIA]: "0x2c3E3Ff54d82cA96BBB2F4529bee114eB200e3F0", [ChainSlug.OPTIMISM_SEPOLIA]: "0x4B882c8A1009c0a4fd80151FEb6d1a3656C49C9a", - 420: "0xD21e53E568FD904c2599E41aFC2434ea11b38A2e", - 5: "0x28f26c101e3F694f1d03D477b4f34F8835141611", }; const payload = "0xbad314e77c9e165fb6cdad2b69ae15ea10f47a976480a84ea0ef9a8b8817b997000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000"; -const helperABI = [ - { - inputs: [ - { - internalType: "uint32", - name: "chainSlug_", - type: "uint32", - }, - { - internalType: "uint256", - name: "amount_", - type: "uint256", - }, - { - internalType: "uint256", - name: "msgGasLimit_", - type: "uint256", - }, - { - internalType: "uint256", - name: "totalMsgs_", - type: "uint256", - }, - { - internalType: "bytes", - name: "payload", - type: "bytes", - }, - ], - name: "remoteAddOperationBatch", - outputs: [], - stateMutability: "payable", - type: "function", - }, -]; const WAIT_FOR_TX = true; const totalIterations = 10; @@ -159,26 +110,19 @@ export const main = async () => { } const helper: Contract = new ethers.Contract( helperContractAddress[chainSlug], - helperABI, - signer - ); - - const socket: Contract = new ethers.Contract( - config[chainSlug][CORE_CONTRACTS.Socket], - SocketABI.abi, + LoadTestHelperABI, signer ); - const value = await socket.getMinFees( + const value = await getSocketFees( + chainSlug, + remoteChainSlug, msgGasLimit, - 100, // payload size + payload.length, constants.HashZero, constants.HashZero, - remoteChainSlug, counterAddress ); - console.log("fees : ", value.toString(), formatEther(value)); - await sendTx( signer, helper, @@ -241,7 +185,9 @@ const sendTx = async ( }` ); console.log( - `Track here: ${API_BASE_URL}/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${tx?.hash}` + `Track here: ${getAPIBaseURL( + mode + )}/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${tx?.hash}` ); if (waitForConfirmation) { diff --git a/scripts/deploy/helpers/send-msg/outbound.ts b/scripts/deploy/helpers/send-msg/outbound.ts deleted file mode 100644 index fdd98b79..00000000 --- a/scripts/deploy/helpers/send-msg/outbound.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { config as dotenvConfig } from "dotenv"; -dotenvConfig(); - -import fs from "fs"; -import { ethers } from "hardhat"; -import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; -import { utils, constants } from "ethers"; -import { getInstance, getChainSlug, deployedAddressPath } from "../../utils"; -import { Contract } from "ethers"; -import { mode } from "../../config/config"; - -const remoteChainSlug = ""; - -export const main = async () => { - try { - const chainSlug = await getChainSlug(); - const amount = 100; - const msgGasLimit = "19000000"; - const gasLimit = "200485"; - const fees = "2000000000000"; - - const config: any = JSON.parse( - fs.readFileSync(deployedAddressPath(mode), "utf-8") - ); - - const socketSigners: SignerWithAddress = await ethers.getSigners(); - const signer = socketSigners[0]; - - const counter: Contract = await getInstance( - "Counter", - config[chainSlug]["Counter"] - ); - const tx = await counter - .connect(signer) - .remoteAddOperation( - remoteChainSlug, - amount, - msgGasLimit, - constants.HashZero, - constants.HashZero, - { - value: fees, - } - ); - - await tx.wait(); - console.log( - `Sent remoteAddOperation with ${amount} amount and ${msgGasLimit} gas limit to counter at ${remoteChainSlug}, tx hash: ${tx.hash.toLowerCase()}` - ); - } catch (error) { - console.log("Error while sending transaction", error); - throw error; - } -}; - -main() - .then(() => process.exit(0)) - .catch((error: Error) => { - console.error(error); - process.exit(1); - }); diff --git a/scripts/deploy/helpers/send-msg/utils.ts b/scripts/deploy/helpers/send-msg/utils.ts new file mode 100644 index 00000000..8a6d8cff --- /dev/null +++ b/scripts/deploy/helpers/send-msg/utils.ts @@ -0,0 +1,148 @@ +import { config as dotenvConfig } from "dotenv"; +import { ethers } from "ethers"; +import Counter from "../../../../out/Counter.sol/Counter.json"; +import Socket from "../../../../out/Socket.sol/Socket.json"; +import { ChainSlug } from "../../../../src"; +import { getAPIBaseURL, getAddresses, relayTx } from "../../utils"; +dotenvConfig(); + +import { formatEther } from "ethers/lib/utils"; +import { getProviderFromChainSlug } from "../../../constants/networks"; +import { mode, overrides } from "../../config/config"; + +const counterAddAmount = 100; + +export const LoadTestHelperABI = [ + { + inputs: [ + { + internalType: "uint32", + name: "chainSlug_", + type: "uint32", + }, + { + internalType: "uint256", + name: "amount_", + type: "uint256", + }, + { + internalType: "uint256", + name: "msgGasLimit_", + type: "uint256", + }, + { + internalType: "uint256", + name: "totalMsgs_", + type: "uint256", + }, + { + internalType: "bytes", + name: "payload", + type: "bytes", + }, + ], + name: "remoteAddOperationBatch", + outputs: [], + stateMutability: "payable", + type: "function", + }, +]; +export const getCounterContract = async (chainSlug: ChainSlug) => { + const chainAddresses = await getAddresses(chainSlug, mode); + if (!chainAddresses) { + throw new Error(`addresses not found for ${chainSlug}, ${chainAddresses}`); + } + const counterAddress = chainAddresses["Counter"]; + if (!counterAddress) { + throw new Error(` counter address not found for ${chainSlug}`); + } + return new ethers.Contract(counterAddress, Counter.abi); +}; + +export const getSocketContract = async (chainSlug: ChainSlug) => { + const chainAddresses = await getAddresses(chainSlug, mode); + if (!chainAddresses) { + throw new Error(`addresses not found for ${chainSlug}, ${chainAddresses}`); + } + const socketAddress = chainAddresses.Socket; + if (!socketAddress) { + throw new Error(`socket address not found for ${chainSlug}`); + } + const provider = getProviderFromChainSlug(chainSlug); + return new ethers.Contract(socketAddress, Socket.abi, provider); +}; + +export const getSocketFees = async ( + chainSlug: ChainSlug, + siblingChainSlug: ChainSlug, + msgGasLimit: string, + payloadSize: number, + executionParams: string, + transmissionParams: string, + plugAddress: string +) => { + const socket = await getSocketContract(chainSlug); + const value = await socket.getMinFees( + msgGasLimit, + payloadSize, + executionParams, + transmissionParams, + siblingChainSlug, + plugAddress + ); + return value; +}; + +export const sendCounterBridgeMsg = async ( + chainSlug: ChainSlug, + siblingSlug: ChainSlug, + msgGasLimit: string, + payloadSize: number, + executionParams: string, + transmissionParams: string +) => { + const counter = await getCounterContract(chainSlug); + let data = counter.interface.encodeFunctionData("remoteAddOperation", [ + siblingSlug, + counterAddAmount, + msgGasLimit, + executionParams, + transmissionParams, + ]); + let to = counter.address; + const value = await getSocketFees( + chainSlug, + siblingSlug, + msgGasLimit, + payloadSize, + executionParams, + transmissionParams, + to + ); + + console.log( + `fees for path ${chainSlug}-${siblingSlug} is ${formatEther(value)} ETH` + ); + + const gasLimit: number | string | undefined = + chainSlug === ChainSlug.ARBITRUM || chainSlug === ChainSlug.ARBITRUM_SEPOLIA + ? 200000 + : overrides(chainSlug)?.gasLimit + ? overrides(chainSlug).gasLimit.toString() + : undefined; + + // console.log({to, data, value, gasLimit}); + let response = await relayTx({ + to, + data, + value, + gasLimit, + gasPrice: overrides(chainSlug)?.gasPrice?.toString() || undefined, + chainSlug, + }); + console.log( + `Track message here: ${getAPIBaseURL( + mode + )}/messages-from-tx?srcChainSlug=${chainSlug}&srcTxHash=${response?.hash}` + ); +}; diff --git a/scripts/deploy/utils/index.ts b/scripts/deploy/utils/index.ts index e9b2ec3e..ee5db7e7 100644 --- a/scripts/deploy/utils/index.ts +++ b/scripts/deploy/utils/index.ts @@ -1,2 +1,3 @@ export * from "./address"; export * from "./utils"; +export * from "./relayer"; diff --git a/scripts/deploy/utils/relayer.ts b/scripts/deploy/utils/relayer.ts new file mode 100644 index 00000000..5fadebba --- /dev/null +++ b/scripts/deploy/utils/relayer.ts @@ -0,0 +1,44 @@ +import { BigNumber } from "ethers"; +import { getRelayAPIKEY, getRelayUrl } from "./utils"; +import { axiosPost } from "@socket.tech/dl-common"; +import { mode } from "../config/config"; +import { ChainSlugToId } from "@socket.tech/dl-core"; + +interface RequestObj { + to: string; + data: string; + chainSlug: number; + value?: string | BigNumber; + gasPrice?: string | BigNumber | undefined; + gasLimit: string | number | undefined; +} + +export const relayTx = async (params: RequestObj) => { + try { + let { to, data, chainSlug, gasPrice, value, gasLimit } = params; + let url = await getRelayUrl(mode); + let config = { + headers: { + "x-api-key": getRelayAPIKEY(mode), + }, + }; + let body = { + to, + data, + value, + chainId: ChainSlugToId[chainSlug], + gasLimit, + gasPrice, + sequential: false, + source: "LoadTester", + }; + let response = await axiosPost(url!, body, config); + if (response?.success) return response?.data; + else { + console.log("error in relaying tx", response); + return { hash: "" }; + } + } catch (error) { + console.log("uncaught error", error); + } +}; diff --git a/scripts/deploy/utils/utils.ts b/scripts/deploy/utils/utils.ts index 577d59e4..f79af3c5 100644 --- a/scripts/deploy/utils/utils.ts +++ b/scripts/deploy/utils/utils.ts @@ -274,6 +274,15 @@ export const getRelayAPIKEY = (mode: DeploymentMode) => { } }; +export const getAPIBaseURL = (mode: DeploymentMode) => { + switch (mode) { + case DeploymentMode.PROD: + return process.env.DL_API_PROD_URL; + default: + return process.env.DL_API_DEV_URL; + } +}; + export const getAddresses = async ( chainSlug: ChainSlug, mode = DeploymentMode.DEV From 5c33ada9f2abd998c69249b60bb39f7cad9ff705 Mon Sep 17 00:00:00 2001 From: Akash Date: Mon, 1 Jul 2024 17:05:44 +0530 Subject: [PATCH 3/4] feat : added transmissionParams test script --- .../send-msg/transmissionParamsTestScript.ts | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 scripts/deploy/helpers/send-msg/transmissionParamsTestScript.ts diff --git a/scripts/deploy/helpers/send-msg/transmissionParamsTestScript.ts b/scripts/deploy/helpers/send-msg/transmissionParamsTestScript.ts new file mode 100644 index 00000000..ccd0f4b2 --- /dev/null +++ b/scripts/deploy/helpers/send-msg/transmissionParamsTestScript.ts @@ -0,0 +1,58 @@ +import { sendCounterBridgeMsg } from "./utils"; +import { ChainSlug } from "../../../../src"; + +const config = { + chainSlug: ChainSlug.OPTIMISM_SEPOLIA, + siblingSlug: ChainSlug.ARBITRUM_SEPOLIA, + msgGasLimit: "200000", + payloadSize: 100, // for counter add operation + executionParams: + "0x0000000000000000000000000000000000000000000000000000000000000000", +}; + +const transmissionParams = [ + "0x0101000000010000000000000000000000000000000000000000000000000000", + "0x0101000000020000000000000000000000000000000000000000000000000000", + "0x0101000000030000000000000000000000000000000000000000000000000000", +]; +const sendMsgsWithVaryingParams = async () => { + for (const param of transmissionParams) { + try { + const { + msgGasLimit, + payloadSize, + executionParams, + chainSlug, + siblingSlug, + } = config; + console.log( + `\n\n Sending message with transmission params: ${JSON.stringify( + param + )}` + ); + await sendCounterBridgeMsg( + chainSlug, + siblingSlug, + msgGasLimit, + payloadSize, + executionParams, + param + ); + console.log( + `Message sent with transmission params: ${JSON.stringify(param)}` + ); + } catch (error) { + console.error( + `Failed to send message with transmission params: ${JSON.stringify( + param + )}` + ); + console.error(error); + } + } +}; + +sendMsgsWithVaryingParams(); + +// usage: +// npx ts-node scripts/deploy/helpers/send-msg/transmissionParamsTestScript.ts From 7303c86b3b79b7175480aa111cb8680e585dd3f7 Mon Sep 17 00:00:00 2001 From: Akash Date: Mon, 1 Jul 2024 17:54:28 +0530 Subject: [PATCH 4/4] fix : add type in relayTx params --- scripts/deploy/helpers/send-msg/utils.ts | 11 +++-------- scripts/deploy/utils/relayer.ts | 10 ++++++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/scripts/deploy/helpers/send-msg/utils.ts b/scripts/deploy/helpers/send-msg/utils.ts index 8a6d8cff..628f7030 100644 --- a/scripts/deploy/helpers/send-msg/utils.ts +++ b/scripts/deploy/helpers/send-msg/utils.ts @@ -124,20 +124,15 @@ export const sendCounterBridgeMsg = async ( `fees for path ${chainSlug}-${siblingSlug} is ${formatEther(value)} ETH` ); - const gasLimit: number | string | undefined = - chainSlug === ChainSlug.ARBITRUM || chainSlug === ChainSlug.ARBITRUM_SEPOLIA - ? 200000 - : overrides(chainSlug)?.gasLimit - ? overrides(chainSlug).gasLimit.toString() - : undefined; - + const { gasLimit, gasPrice, type } = overrides(chainSlug); // console.log({to, data, value, gasLimit}); let response = await relayTx({ to, data, value, gasLimit, - gasPrice: overrides(chainSlug)?.gasPrice?.toString() || undefined, + gasPrice, + type, chainSlug, }); console.log( diff --git a/scripts/deploy/utils/relayer.ts b/scripts/deploy/utils/relayer.ts index 5fadebba..16e3aa42 100644 --- a/scripts/deploy/utils/relayer.ts +++ b/scripts/deploy/utils/relayer.ts @@ -1,4 +1,4 @@ -import { BigNumber } from "ethers"; +import { BigNumber, BigNumberish } from "ethers"; import { getRelayAPIKEY, getRelayUrl } from "./utils"; import { axiosPost } from "@socket.tech/dl-common"; import { mode } from "../config/config"; @@ -9,13 +9,14 @@ interface RequestObj { data: string; chainSlug: number; value?: string | BigNumber; - gasPrice?: string | BigNumber | undefined; - gasLimit: string | number | undefined; + gasPrice?: BigNumberish; + gasLimit?: BigNumberish; + type?: number; } export const relayTx = async (params: RequestObj) => { try { - let { to, data, chainSlug, gasPrice, value, gasLimit } = params; + let { to, data, chainSlug, gasPrice, value, type, gasLimit } = params; let url = await getRelayUrl(mode); let config = { headers: { @@ -29,6 +30,7 @@ export const relayTx = async (params: RequestObj) => { chainId: ChainSlugToId[chainSlug], gasLimit, gasPrice, + type, sequential: false, source: "LoadTester", };