From f304a51ea2181d4ffeda9c7b68f050600ee1e09a Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Fri, 22 Nov 2024 16:16:05 +0400 Subject: [PATCH 01/17] rename ENVs to match subnet deployment --- cicd/.env.example | 4 ++-- cicd/README.md | 8 ++++---- cicd/endpointandregisterchain.js | 18 +++++++++--------- cicd/zeroandsubswap.js | 0 4 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 cicd/zeroandsubswap.js diff --git a/cicd/.env.example b/cicd/.env.example index 50c814f..b1f5f9a 100644 --- a/cicd/.env.example +++ b/cicd/.env.example @@ -1,8 +1,8 @@ SUBNET_PK=0x1111111111111111111111111111111111111111111111111111111111111111 PARENTNET_PK=0x2222222222222222222222222222222222222222222222222222222222222222 -CSC=0x3333333333333333333333333333333333333333 -REVERSE_CSC=0x4444444444444444444444444444444444444444 +CHECKPOINT_CONTRACT=0x3333333333333333333333333333333333333333 +REVERSE_CHECKPOINT_CONTRACT=0x4444444444444444444444444444444444444444 # PARENTNET_URL: full url or devnet,testnet # devnet : https://devnetstats.apothem.network/devnet/ diff --git a/cicd/README.md b/cicd/README.md index 116ab6c..0313662 100644 --- a/cicd/README.md +++ b/cicd/README.md @@ -11,8 +11,8 @@ Based on the provided `.env.example`, create your own `.env` file with the follo - **`PARENTNET_URL`**: RPC URL for the parentnet endpoint. - **`SUBNET_URL`**: RPC URL for the subnet. - **`SUBNET_PK`**: Private key for the deploy subnet XDC Zero wallet. -- **`CSC`**: Checkpoint smart contract address within the subnet chain (deployed to parentnet). -- **`REVERSE_CSC`**: Checkpoint smart contract address within the parentnet chain (deployed to subnet). +- **`CHECKPOINT_CONTRACT`**: Checkpoint smart contract address within the subnet chain (deployed to parentnet). +- **`REVERSE_CHECKPOINT_CONTRACT`**: Checkpoint smart contract address within the parentnet chain (deployed to subnet). #### Step 2: Deploy Endpoints and Register Chain @@ -78,8 +78,8 @@ Based on the provided `.env.example`, create your own `.env` file with the follo - **`PARENTNET_URL`**: RPC URL for the parentnet endpoint. - **`SUBNET_URL`**: RPC URL for the subnet. - **`SUBNET_PK`**: Private key for the deploy subnet XDC Zero wallet. -- **`CSC`**: Checkpoint smart contract address within the subnet chain (deployed to parentnet). -- **`REVERSE_CSC`**: Checkpoint smart contract address within the parentnet chain (deployed to subnet). +- **`CHECKPOINT_CONTRACT`**: Checkpoint smart contract address within the subnet chain (deployed to parentnet). +- **`REVERSE_CHECKPOINT_CONTRACT`**: Checkpoint smart contract address within the parentnet chain (deployed to subnet). ### Step 3: Deploy Endpoint and Register Chain diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index fe51c89..5c4972b 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -38,13 +38,13 @@ function initEndpointDeploy() { "SUBNET_PK", "PARENTNET_PK", "SUBNET_URL", - "CSC", - "REVERSE_CSC", + "CHECKPOINT_CONTRACT", + "REVERSE_CHECKPOINT_CONTRACT", ]; const isEnabled = reqENV.every((envVar) => envVar in process.env); if (!isEnabled) { throw Error( - "incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_URL, CSC, REVERSE_CSC" + "incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_URL, CHECKPOINT_CONTRACT, REVERSE_CHECKPOINT_CONTRACT" ); } subnetPK = process.env.SUBNET_PK.startsWith("0x") @@ -53,12 +53,12 @@ function initEndpointDeploy() { parentnetPK = process.env.PARENTNET_PK.startsWith("0x") ? process.env.PARENTNET_PK : `0x${process.env.PARENTNET_PK}`; - csc = process.env.CSC.startsWith("0x") - ? process.env.CSC - : `0x${process.env.CSC}`; - reverseCSC = process.env.REVERSE_CSC.startsWith("0x") - ? process.env.REVERSE_CSC - : `0x${process.env.REVERSE_CSC}`; + csc = process.env.CHECKPOINT_CONTRACT.startsWith("0x") + ? process.env.CHECKPOINT_CONTRACT + : `0x${process.env.CHECKPOINT_CONTRACT}`; + reverseCSC = process.env.REVERSE_CHECKPOINT_CONTRACT.startsWith("0x") + ? process.env.REVERSE_CHECKPOINT_CONTRACT + : `0x${process.env.REVERSE_CHECKPOINT_CONTRACT}`; subnetURL = process.env.SUBNET_URL; // return subnetURL, parentnetURL, subnetPK, parentnetPK, csc, reverseCSC diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js new file mode 100644 index 0000000..e69de29 From ac54984ee74a209eac8795696ee297b48a2e935d Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Thu, 13 Feb 2025 15:31:08 +0400 Subject: [PATCH 02/17] add framework to replace values in .env files add complete subswap deploy flow --- .../subswap/contract/deploy.config.json | 7 +- .../subswap/contract/network.config.json | 6 +- cicd/applicationregister.js | 19 +++--- cicd/endpointandregisterchain.js | 33 ++++++--- cicd/subswap.js | 34 +++++++--- cicd/util.js | 67 ++++++++++++++++++- cicd/zeroandsubswap.js | 26 +++++++ endpoint/endpointconfig.json | 34 +++++----- endpoint/network.config.json | 6 +- 9 files changed, 174 insertions(+), 58 deletions(-) diff --git a/applications/subswap/contract/deploy.config.json b/applications/subswap/contract/deploy.config.json index 260714a..7da76e2 100644 --- a/applications/subswap/contract/deploy.config.json +++ b/applications/subswap/contract/deploy.config.json @@ -1,5 +1,4 @@ { - "parentnetendpoint": "0x89D933dFBd879DA78643530Bf413c81F94A73c31", - "subnetendpoint": "0x0B1795ccA8E4eC4df02346a082df54D437F8D9aF", - "subnettoken": { "name": "test", "symbol": "test", "initSupply": "1000" } -} + "subnetendpoint": "0x62cb3a3c4a200664beB036863Bf0FDeaB2c992C2", + "parentnetendpoint": "0x854B92bd73081b2a80eA553407D99e6d8C902B93" +} \ No newline at end of file diff --git a/applications/subswap/contract/network.config.json b/applications/subswap/contract/network.config.json index 0dee208..2131bfc 100644 --- a/applications/subswap/contract/network.config.json +++ b/applications/subswap/contract/network.config.json @@ -1,4 +1,4 @@ { - "xdcparentnet": "https://devnetstats.apothem.network/devnet", - "xdcsubnet": "https://devnetstats.apothem.network/subnet" -} + "xdcsubnet": "http://localhost:8545", + "xdcparentnet": "https://erpc.apothem.network/" +} \ No newline at end of file diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 22fe08c..04ac06a 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -1,18 +1,15 @@ process.chdir(__dirname); -const { execSync } = require("child_process"); const fs = require("node:fs"); -const env = require("dotenv").config({ path: "mount/.env" }); -const config = { - relativePath: "../endpoint/", -}; +const config = {relativePath: "../endpoint/"}; const endpointConfig = {}; - -const { ethers } = require("ethers"); const u = require("./util.js"); +u.loadContractENV() -main(); +if (require.main === module) { + applicationRegister(); +} -async function main() { +async function applicationRegister() { console.log("start application register"); importEndpointJson(); initApplicationRegister(); @@ -192,3 +189,7 @@ function exportEndpointJson() { console.log("SUCCESS register application, endpointconfig:"); console.log(ep); } + +module.exports = { + applicationRegister, +}; diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index 5c4972b..98370e1 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -1,25 +1,28 @@ process.chdir(__dirname); -const { execSync } = require("child_process"); const fs = require("node:fs"); -const env = require("dotenv").config({ path: "mount/.env" }); -const config = { - relativePath: "../endpoint/", -}; +const config = {relativePath: "../endpoint/"}; const endpointConfig = {}; - -const { ethers } = require("ethers"); const u = require("./util.js"); +u.loadContractENV() -main(); +if (require.main === module) { + const newENV = await e.endpointAndRegisterChain() + for (const [key, value] of Object.entries(newENV)) { + u.replaceOrAddENV('./mount/contract_deploy.env', key, value) + u.replaceOrAddENV('./mount/common.env', key, value) + } + u.loadContractENV() +} -async function main() { +async function endpointAndRegisterChain() { console.log("start endpoint deploy and register chain"); initEndpointDeploy(); await u.getNetworkID(config); deployEndpoint(); configureEndpointJson(); registerEndpoint(); - exportEndpointJson(); + const newENV = exportEndpointJson(); + return newENV } function initEndpointDeploy() { @@ -119,6 +122,11 @@ function exportEndpointJson() { console.log("SUCCESS deploy endpoint and register chain, env:"); console.log("SUBNET_ZERO_CONTRACT=" + config.subnetEndpoint); console.log("PARENTNET_ZERO_CONTRACT=" + config.parentnetEndpoint); + + return { + 'SUBNET_ZERO_CONTRACT': config.subnetEndpoint, + 'PARENTNET_ZERO_CONTRACT': config.parentnetEndpoint + } } function deployEndpoint() { @@ -181,3 +189,8 @@ function parseEndpointOutput(outString) { throw Error("invalid output string: " + outString); } } + + +module.exports = { + endpointAndRegisterChain, +}; diff --git a/cicd/subswap.js b/cicd/subswap.js index 5cd3552..aaa372c 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -1,23 +1,25 @@ process.chdir(__dirname); -const { execSync } = require("child_process"); const fs = require("node:fs"); -const env = require("dotenv").config({ path: "mount/.env" }); -const config = { - relativePath: "../applications/subswap/contract/", -}; -const endpointConfig = {}; - -const { ethers } = require("ethers"); +const config = {relativePath: "../applications/subswap/contract/"}; const u = require("./util.js"); +u.loadContractENV() -main(); +if (require.main === module) { + const newENV = await s.subswap() + for (const [key, value] of Object.entries(newENV)) { + u.replaceOrAddENV('./mount/contract_deploy.env', key, value) + u.replaceOrAddENV('./mount/common.env', key, value) + } + u.loadContractENV() +} -async function main() { +async function subswap() { console.log("start subswap deploy"); checkEndpointConfig(); initSubswapDeploy(); deploySubswap(); - exportSubswap(); + const newENV = exportSubswap(); + return newENV } function checkEndpointConfig() { @@ -111,7 +113,13 @@ function exportSubswap() { ); console.log(finalSubnet); console.log(finalParentnet); + + return { + 'SUBNET_APP': config.subnetSubswap, + 'PARENTNET_APP': config.parentnetSubswap + } } + function writeSubswapDeployJson() { deployJson = { subnetendpoint: config.subnetEndpoint, @@ -144,3 +152,7 @@ function parseEndpointOutput(outString) { throw Error("invalid output string: " + outString); } } + +module.exports = { + subswap, +}; diff --git a/cicd/util.js b/cicd/util.js index d89f934..a10c401 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -1,7 +1,7 @@ process.chdir(__dirname); const { execSync } = require("child_process"); const fs = require("node:fs"); -const env = require("dotenv").config({ path: "mount/.env" }); +const dotenv = require("dotenv") const { ethers } = require("ethers"); @@ -76,9 +76,74 @@ async function getNetworkID(config) { config["parentnetID"] = parentID; } +function loadContractENV(){ + dotenv.config({ path: "mount/contract_deploy.env" }); +} +function loadCommonENV(){ + dotenv.config({ path: "mount/common.env" }); +} + +function replaceENV(filepath, replaceENV, replaceValue){ + //check files mounted + if (!fs.existsSync(filepath)) { + throw Error(`could not modify ${filepath}, file not mounted`) + } + const envFileContent = fs.readFileSync(filepath, 'utf8'); + const regex = new RegExp(`^${replaceENV}=.*`, 'gm'); + let matches = envFileContent.match(regex); + matches = (matches === null) ? [] : matches + + if (matches.length > 1){ + console.log('Warning: found more than one instance of', replaceENV, 'in', filepath) + console.log(matches) + } + let matchesCount = 0 + const updatedContent = envFileContent.replace(regex, (match) => { + let replaceString= +`# Commented old value by deployer +# ${matches[matchesCount]}` + + if (matchesCount == matches.length-1) { + replaceString+=`\n${replaceENV}=${replaceValue}` + } + matchesCount++ + console.log(`Updated ${filepath} file: \n${replaceString}`); + return replaceString + }); + + fs.writeFileSync(filepath, updatedContent); + return (updatedContent !== envFileContent) +} + +function addENV(filepath, envName, envValue){ + //check files mounted + if (!fs.existsSync(filepath)) { + throw Error(`could not modify ${filepath}, file not mounted`) + } + const envFileContent = fs.readFileSync(filepath, 'utf8'); + const appendString = `${envName}=${envValue}` + const updatedContent = envFileContent+'\n'+appendString + + fs.writeFileSync(filepath, updatedContent); +} + +function replaceOrAddENV(filepath, envKey, envValue){ + replaced = replaceENV(filepath, envKey, envValue) + !replaced && addENV(filepath, envKey, envValue) +} + +function transferTokens(url, fromAddress, toAddress, amount){ + +} + module.exports = { getNetworkID, callExec, writeEnv, writeNetworkJson, + loadContractENV, + loadCommonENV, + replaceENV, + addENV, + replaceOrAddENV, }; diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index e69de29..6220609 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -0,0 +1,26 @@ +process.chdir(__dirname); +const u = require("./util.js"); +u.loadContractENV() +const e = require("./endpointandregisterchain.js") +const s = require("./subswap.js") +const a = require("./applicationregister.js") + +main(); + +async function main(){ + const step1ENV = await e.endpointAndRegisterChain() + for (const [key, value] of Object.entries(step1ENV)) { + u.replaceOrAddENV('./mount/contract_deploy.env', key, value) + u.replaceOrAddENV('./mount/common.env', key, value) + } + u.loadContractENV() + + const step2ENV = await s.subswap() + for (const [key, value] of Object.entries(step2ENV)) { + u.replaceOrAddENV('./mount/contract_deploy.env', key, value) + u.replaceOrAddENV('./mount/common.env', key, value) + } + u.loadContractENV() + + a.applicationRegister() +} diff --git a/endpoint/endpointconfig.json b/endpoint/endpointconfig.json index ea6f507..3371698 100644 --- a/endpoint/endpointconfig.json +++ b/endpoint/endpointconfig.json @@ -1,36 +1,36 @@ { - "xdcparentnet": { - "endpoint": "0xB575c682301e20935B8C5846313a01E6f0Bd9f4B", + "xdcsubnet": { + "endpoint": "0x62cb3a3c4a200664beB036863Bf0FDeaB2c992C2", "registers": [ { - "csc": "0x3C714ffDB5A13d8d7EF0bE5f41F12bd840DA9ef1", - "endpoint": "0x0bb5a292b13C7983BFDCd62538e0e81603793342", - "chainId": "953" + "csc": "0x172cf9c1d0F263Bd338bc64aa9FE2CF62716b3Dd", + "endpoint": "0x854B92bd73081b2a80eA553407D99e6d8C902B93", + "chainId": "51" } ], "applications": [ { - "rid": "953", - "rua": "0xdfc2cD2b6AA7fD236e3A4Efa255A9b81c94B3fF4", - "sua": "0x40DC79697399686cd4003Ab9B7B87115bA945397" + "rid": "51", + "rua": "0x847a1318489C85c35d4DF06B83cCaAd17e933090", + "sua": "0xB3A4857fe7Bb80E5D584e4E85fF0a79ad5EFE902" } ] }, - "xdcsubnet": { - "endpoint": "0x0bb5a292b13C7983BFDCd62538e0e81603793342", + "xdcparentnet": { + "endpoint": "0x854B92bd73081b2a80eA553407D99e6d8C902B93", "registers": [ { - "csc": "0x720A33E9c54dDf240D42f822E8b20D9C70226AAC", - "endpoint": "0xB575c682301e20935B8C5846313a01E6f0Bd9f4B", - "chainId": "551" + "csc": "0xF48C8f2C44B99F1c9F54DB8d5781CB47d5449A05", + "endpoint": "0x62cb3a3c4a200664beB036863Bf0FDeaB2c992C2", + "chainId": "48651" } ], "applications": [ { - "rid": "551", - "rua": "0x40DC79697399686cd4003Ab9B7B87115bA945397", - "sua": "0xdfc2cD2b6AA7fD236e3A4Efa255A9b81c94B3fF4" + "rid": "48651", + "rua": "0xB3A4857fe7Bb80E5D584e4E85fF0a79ad5EFE902", + "sua": "0x847a1318489C85c35d4DF06B83cCaAd17e933090" } ] } -} +} \ No newline at end of file diff --git a/endpoint/network.config.json b/endpoint/network.config.json index 0dee208..2131bfc 100644 --- a/endpoint/network.config.json +++ b/endpoint/network.config.json @@ -1,4 +1,4 @@ { - "xdcparentnet": "https://devnetstats.apothem.network/devnet", - "xdcsubnet": "https://devnetstats.apothem.network/subnet" -} + "xdcsubnet": "http://localhost:8545", + "xdcparentnet": "https://erpc.apothem.network/" +} \ No newline at end of file From 5234362290e9086fb6ee87485ca35af2e3643cb4 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Thu, 13 Feb 2025 16:49:03 +0400 Subject: [PATCH 03/17] add auto transfer of subnet tokens to relayer addresses --- .../subswap/contract/deploy.config.json | 4 +- cicd/applicationregister.js | 6 ++- cicd/endpointandregisterchain.js | 15 +++--- cicd/subswap.js | 6 ++- cicd/util.js | 54 ++++++++++++++++++- cicd/zeroandsubswap.js | 28 +++++++++- endpoint/endpointconfig.json | 16 +++--- 7 files changed, 109 insertions(+), 20 deletions(-) diff --git a/applications/subswap/contract/deploy.config.json b/applications/subswap/contract/deploy.config.json index 7da76e2..6a7ee87 100644 --- a/applications/subswap/contract/deploy.config.json +++ b/applications/subswap/contract/deploy.config.json @@ -1,4 +1,4 @@ { - "subnetendpoint": "0x62cb3a3c4a200664beB036863Bf0FDeaB2c992C2", - "parentnetendpoint": "0x854B92bd73081b2a80eA553407D99e6d8C902B93" + "subnetendpoint": "0xD7466ba78c4a585DFEEC3515c72412524E8b50d4", + "parentnetendpoint": "0xD76fEc90732A681D6d02e0D222156009b37600Fc" } \ No newline at end of file diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 04ac06a..488b9e2 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -6,7 +6,11 @@ const u = require("./util.js"); u.loadContractENV() if (require.main === module) { - applicationRegister(); + main() +} + +async function main(){ + await applicationRegister(); } async function applicationRegister() { diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index 98370e1..37c446e 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -6,12 +6,15 @@ const u = require("./util.js"); u.loadContractENV() if (require.main === module) { - const newENV = await e.endpointAndRegisterChain() - for (const [key, value] of Object.entries(newENV)) { - u.replaceOrAddENV('./mount/contract_deploy.env', key, value) - u.replaceOrAddENV('./mount/common.env', key, value) - } - u.loadContractENV() + main() +} +async function main(){ + const newENV = await endpointAndRegisterChain() + for (const [key, value] of Object.entries(newENV)) { + u.replaceOrAddENV('./mount/contract_deploy.env', key, value) + u.replaceOrAddENV('./mount/common.env', key, value) + } + u.loadContractENV() } async function endpointAndRegisterChain() { diff --git a/cicd/subswap.js b/cicd/subswap.js index aaa372c..2457f53 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -5,7 +5,11 @@ const u = require("./util.js"); u.loadContractENV() if (require.main === module) { - const newENV = await s.subswap() + main() +} + +async function main(){ + const newENV = await subswap() for (const [key, value] of Object.entries(newENV)) { u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) diff --git a/cicd/util.js b/cicd/util.js index a10c401..6b966cb 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -4,6 +4,7 @@ const fs = require("node:fs"); const dotenv = require("dotenv") const { ethers } = require("ethers"); +const { stringify } = require("node:querystring"); function writeEnv(key, path) { content = "PRIVATE_KEY=" + key; @@ -132,8 +133,58 @@ function replaceOrAddENV(filepath, envKey, envValue){ !replaced && addENV(filepath, envKey, envValue) } -function transferTokens(url, fromAddress, toAddress, amount){ +async function transferTokens(url, fromPK, toPK, amount) { + console.log(url) + const provider = new ethers.providers.JsonRpcProvider(url); + const fromWallet = new ethers.Wallet(fromPK, provider); + const toWallet = new ethers.Wallet(toPK, provider); + let tx = { + to: toWallet.address, + value: ethers.utils.parseEther(amount.toString()), + }; + + try{ + await provider.detectNetwork(); + } catch (error){ + throw Error("Cannot connect to RPC") + } + + let sendPromise = fromWallet.sendTransaction(tx); + txHash = await sendPromise.then((tx) => { + return tx.hash; + }); + console.log("TX submitted, confirming TX execution, txhash:", txHash); + + let receipt; + let count = 0; + while (!receipt) { + count++; + // console.log("tx receipt check loop", count); + if (count > 60) { + throw Error("Timeout: transaction did not execute after 60 seconds"); + } + await sleep(1000); + let receipt = await provider.getTransactionReceipt(txHash); + if (receipt && receipt.status == 1) { + console.log("Successfully transferred", amount, "subnet token"); + let fromBalance = await provider.getBalance(fromWallet.address); + fromBalance = ethers.utils.formatEther(fromBalance); + let toBalance = await provider.getBalance(toWallet.address); + toBalance = ethers.utils.formatEther(toBalance); + console.log("Current balance"); + console.log(`${fromWallet.address}: ${fromBalance}`); + console.log(`${toWallet.address}: ${toBalance}`); + return { + fromBalance: fromBalance, + toBalance: toBalance, + txHash: txHash + } + } + } +} +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); } module.exports = { @@ -146,4 +197,5 @@ module.exports = { replaceENV, addENV, replaceOrAddENV, + transferTokens, }; diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 6220609..7e30d1a 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -1,4 +1,6 @@ process.chdir(__dirname); +const { ethers } = require("ethers"); +const fs = require("node:fs"); const u = require("./util.js"); u.loadContractENV() const e = require("./endpointandregisterchain.js") @@ -21,6 +23,30 @@ async function main(){ u.replaceOrAddENV('./mount/common.env', key, value) } u.loadContractENV() + + await a.applicationRegister() - a.applicationRegister() + await setupSubnetWallets() +} + +async function setupSubnetWallets(){ + u.loadCommonENV() + if (!fs.existsSync('./mount/keys.json')) { + throw Error(`could not modify ${filepath}, file not mounted`) + } + const grandmasterPK = JSON.parse(fs.readFileSync('./mount/keys.json', 'utf8')).Grandmaster.PrivateKey + + try{ + new ethers.Wallet(process.env.SUBNET_WALLET_PK) + new ethers.Wallet(process.env.SUBNET_ZERO_WALLET_PK) + new ethers.Wallet(grandmasterPK) + }catch(error){ + console.log(error) + console.log('failed to setup wallets, invalid PK') + process.exit() + } + + await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_WALLET_PK, 10000) + await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_ZERO_WALLET_PK, 10000) + } diff --git a/endpoint/endpointconfig.json b/endpoint/endpointconfig.json index 3371698..056401d 100644 --- a/endpoint/endpointconfig.json +++ b/endpoint/endpointconfig.json @@ -1,35 +1,35 @@ { "xdcsubnet": { - "endpoint": "0x62cb3a3c4a200664beB036863Bf0FDeaB2c992C2", + "endpoint": "0xD7466ba78c4a585DFEEC3515c72412524E8b50d4", "registers": [ { "csc": "0x172cf9c1d0F263Bd338bc64aa9FE2CF62716b3Dd", - "endpoint": "0x854B92bd73081b2a80eA553407D99e6d8C902B93", + "endpoint": "0xD76fEc90732A681D6d02e0D222156009b37600Fc", "chainId": "51" } ], "applications": [ { "rid": "51", - "rua": "0x847a1318489C85c35d4DF06B83cCaAd17e933090", - "sua": "0xB3A4857fe7Bb80E5D584e4E85fF0a79ad5EFE902" + "rua": "0x52431ea516961a5a46d8CA8c345201CedC8F335b", + "sua": "0x913D7dbEF1aA21d1E93a7f6cbD3740fA703B74f1" } ] }, "xdcparentnet": { - "endpoint": "0x854B92bd73081b2a80eA553407D99e6d8C902B93", + "endpoint": "0xD76fEc90732A681D6d02e0D222156009b37600Fc", "registers": [ { "csc": "0xF48C8f2C44B99F1c9F54DB8d5781CB47d5449A05", - "endpoint": "0x62cb3a3c4a200664beB036863Bf0FDeaB2c992C2", + "endpoint": "0xD7466ba78c4a585DFEEC3515c72412524E8b50d4", "chainId": "48651" } ], "applications": [ { "rid": "48651", - "rua": "0xB3A4857fe7Bb80E5D584e4E85fF0a79ad5EFE902", - "sua": "0x847a1318489C85c35d4DF06B83cCaAd17e933090" + "rua": "0x913D7dbEF1aA21d1E93a7f6cbD3740fA703B74f1", + "sua": "0x52431ea516961a5a46d8CA8c345201CedC8F335b" } ] } From 392726cad43d21eb5ce847b38bd2dda6cb53a4d7 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 17 Feb 2025 14:07:28 +0400 Subject: [PATCH 04/17] add auto creation of subswap-frontend config json --- .../subswap/contract/deploy.config.json | 4 +- cicd/subswap.js | 63 ++++++++++++++----- endpoint/endpointconfig.json | 16 ++--- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/applications/subswap/contract/deploy.config.json b/applications/subswap/contract/deploy.config.json index 6a7ee87..eb23eb2 100644 --- a/applications/subswap/contract/deploy.config.json +++ b/applications/subswap/contract/deploy.config.json @@ -1,4 +1,4 @@ { - "subnetendpoint": "0xD7466ba78c4a585DFEEC3515c72412524E8b50d4", - "parentnetendpoint": "0xD76fEc90732A681D6d02e0D222156009b37600Fc" + "subnetendpoint": "0xBd0D59f41c7e80012E30cd35Af2375f9A15940A3", + "parentnetendpoint": "0x4381e4c455B35c2894CCDb90164225A5A2c08bbB" } \ No newline at end of file diff --git a/cicd/subswap.js b/cicd/subswap.js index 2457f53..52a7dee 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -1,29 +1,30 @@ process.chdir(__dirname); const fs = require("node:fs"); -const config = {relativePath: "../applications/subswap/contract/"}; +const config = { relativePath: "../applications/subswap/contract/" }; const u = require("./util.js"); -u.loadContractENV() +u.loadContractENV(); if (require.main === module) { - main() + main(); } -async function main(){ - const newENV = await subswap() +async function main() { + const newENV = await subswap(); for (const [key, value] of Object.entries(newENV)) { - u.replaceOrAddENV('./mount/contract_deploy.env', key, value) - u.replaceOrAddENV('./mount/common.env', key, value) + u.replaceOrAddENV("./mount/contract_deploy.env", key, value); + u.replaceOrAddENV("./mount/common.env", key, value); } - u.loadContractENV() + u.loadContractENV(); } async function subswap() { console.log("start subswap deploy"); checkEndpointConfig(); - initSubswapDeploy(); + await initSubswapDeploy(); deploySubswap(); + writeSubswapConfigJson(); const newENV = exportSubswap(); - return newENV + return newENV; } function checkEndpointConfig() { @@ -52,7 +53,7 @@ function checkEndpointConfig() { throw Error("endpoints not found in mount/endpointconfig.json"); } -function initSubswapDeploy() { +async function initSubswapDeploy() { if (process.env.PARENTNET_URL) { parentnetURL = process.env.PARENTNET_URL; if (parentnetURL == "devnet") @@ -80,6 +81,7 @@ function initSubswapDeploy() { config["parentnetPK"] = parentnetPK; config["subnetURL"] = subnetURL; config["parentnetURL"] = parentnetURL; + await u.getNetworkID(config); } function deploySubswap() { @@ -119,9 +121,9 @@ function exportSubswap() { console.log(finalParentnet); return { - 'SUBNET_APP': config.subnetSubswap, - 'PARENTNET_APP': config.parentnetSubswap - } + SUBNET_APP: config.subnetSubswap, + PARENTNET_APP: config.parentnetSubswap, + }; } function writeSubswapDeployJson() { @@ -157,6 +159,39 @@ function parseEndpointOutput(outString) { } } +function writeSubswapConfigJson() { + // { + // "parentnetUrl":"https://erpc.apothem.network/", + // "parentnetChainId":"51", + // "subnetUrl":"http://localhost:8545", + // "subnetChainId":"999", + // "subnetApp": "0x9777050a8402ac5958aA87631B15e98e26610EB5", + // "parentnetApp": "0xC355520747171Bd75f505E8cd12f935944bD783b", + // "tokens" : [ + // { + // "name": "Token A", + // "address": "0x103BAA273da5C2FEF2d1B8f839044A9bd07Bc1A1" + // } + // ] + // } + const obj = { + parentnetUrl: config["parentnetURL"], + parentnetChainId: config["parentnetID"], + parentnetApp: config["parentnetSubswap"], + subnetUrl: config["subnetURL"], + subnetChainId: config["subnetID"], + subnetApp: config["subnetSubswap"], + tokens: [ + { + name: "Example Token", + address: "0x1111111111111111111111111111111111111111", + }, + ], + }; + + fs.writeFileSync("./mount/subswap-frontend.config.json", JSON.stringify(obj, null, 2)) +} + module.exports = { subswap, }; diff --git a/endpoint/endpointconfig.json b/endpoint/endpointconfig.json index 056401d..89eeebd 100644 --- a/endpoint/endpointconfig.json +++ b/endpoint/endpointconfig.json @@ -1,35 +1,35 @@ { "xdcsubnet": { - "endpoint": "0xD7466ba78c4a585DFEEC3515c72412524E8b50d4", + "endpoint": "0xBd0D59f41c7e80012E30cd35Af2375f9A15940A3", "registers": [ { "csc": "0x172cf9c1d0F263Bd338bc64aa9FE2CF62716b3Dd", - "endpoint": "0xD76fEc90732A681D6d02e0D222156009b37600Fc", + "endpoint": "0x4381e4c455B35c2894CCDb90164225A5A2c08bbB", "chainId": "51" } ], "applications": [ { "rid": "51", - "rua": "0x52431ea516961a5a46d8CA8c345201CedC8F335b", - "sua": "0x913D7dbEF1aA21d1E93a7f6cbD3740fA703B74f1" + "rua": "0x14937E491F30B563f93f3aAa37F298330faBFb01", + "sua": "0x1a38735Ad20503c857d9EcFB7585E8d75F13Ec6f" } ] }, "xdcparentnet": { - "endpoint": "0xD76fEc90732A681D6d02e0D222156009b37600Fc", + "endpoint": "0x4381e4c455B35c2894CCDb90164225A5A2c08bbB", "registers": [ { "csc": "0xF48C8f2C44B99F1c9F54DB8d5781CB47d5449A05", - "endpoint": "0xD7466ba78c4a585DFEEC3515c72412524E8b50d4", + "endpoint": "0xBd0D59f41c7e80012E30cd35Af2375f9A15940A3", "chainId": "48651" } ], "applications": [ { "rid": "48651", - "rua": "0x913D7dbEF1aA21d1E93a7f6cbD3740fA703B74f1", - "sua": "0x52431ea516961a5a46d8CA8c345201CedC8F335b" + "rua": "0x1a38735Ad20503c857d9EcFB7585E8d75F13Ec6f", + "sua": "0x14937E491F30B563f93f3aAa37F298330faBFb01" } ] } From 82c2ecc515517e375e4b8bc85dfbf71bfae2fe1f Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Fri, 21 Feb 2025 02:33:51 +0400 Subject: [PATCH 05/17] inc transfer amount --- cicd/util.js | 2 -- cicd/zeroandsubswap.js | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/cicd/util.js b/cicd/util.js index 6b966cb..fc4f857 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -2,9 +2,7 @@ process.chdir(__dirname); const { execSync } = require("child_process"); const fs = require("node:fs"); const dotenv = require("dotenv") - const { ethers } = require("ethers"); -const { stringify } = require("node:querystring"); function writeEnv(key, path) { content = "PRIVATE_KEY=" + key; diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 7e30d1a..9c76a3c 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -46,7 +46,7 @@ async function setupSubnetWallets(){ process.exit() } - await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_WALLET_PK, 10000) + await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_WALLET_PK, 1000000) await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_ZERO_WALLET_PK, 10000) } From bdd0b82c1b69772fdf4df76547b3d563ed7c90c3 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 14:40:30 +0400 Subject: [PATCH 06/17] include sample token deploy give more subnet funds --- cicd/subswap.js | 23 +++++++++++++++++++++-- cicd/zeroandsubswap.js | 4 ++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cicd/subswap.js b/cicd/subswap.js index 52a7dee..37b25c8 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -22,6 +22,7 @@ async function subswap() { checkEndpointConfig(); await initSubswapDeploy(); deploySubswap(); + deploySampleToken(); writeSubswapConfigJson(); const newENV = exportSubswap(); return newENV; @@ -126,10 +127,28 @@ function exportSubswap() { }; } +function deploySampleToken() { + console.log("writing deploy.config.json"); + writeSubswapDeployJson(); + console.log("configuring PK"); + u.writeEnv(config.subnetPK, config.relativePath); + console.log("deploying subnet sample token on subnet"); + tokenDeployOut = u.callExec( + "cd ../applications/subswap/contract; npx hardhat run scripts/simpletokendeploy.js --network xdcsubnet" + ); + tokenAddr = parseEndpointOutput(tokenDeployOut); + config["subnetSampleTokenAddress"] = tokenAddr +} + function writeSubswapDeployJson() { deployJson = { subnetendpoint: config.subnetEndpoint, parentnetendpoint: config.parentnetEndpoint, + "subnettoken": { + "name": "Subnet Sample Token", + "symbol": "SST", + "initSupply": 1_000_000 + } }; fs.writeFileSync( "../applications/subswap/contract/deploy.config.json", @@ -183,8 +202,8 @@ function writeSubswapConfigJson() { subnetApp: config["subnetSubswap"], tokens: [ { - name: "Example Token", - address: "0x1111111111111111111111111111111111111111", + name: "Subnet Sample Token", + address: config["subnetSampleTokenAddress"] || "0x1111111111111111111111111111111111111111", }, ], }; diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 9c76a3c..2a08048 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -46,7 +46,7 @@ async function setupSubnetWallets(){ process.exit() } - await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_WALLET_PK, 1000000) - await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_ZERO_WALLET_PK, 10000) + await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_WALLET_PK, 10_000_000) + await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_ZERO_WALLET_PK, 1_000_000) } From 16ef60d127d0cfb302db548f2c8182ac84776fa6 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 16:06:58 +0400 Subject: [PATCH 07/17] use delay to fix wrong env read --- cicd/applicationregister.js | 2 +- cicd/endpointandregisterchain.js | 4 ++-- cicd/subswap.js | 12 ++++++------ cicd/util.js | 9 +++++++-- cicd/zeroandsubswap.js | 9 +++++---- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 488b9e2..b91a313 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -3,7 +3,7 @@ const fs = require("node:fs"); const config = {relativePath: "../endpoint/"}; const endpointConfig = {}; const u = require("./util.js"); -u.loadContractENV() +await u.loadContractENV() if (require.main === module) { main() diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index 37c446e..a0b392a 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -3,7 +3,7 @@ const fs = require("node:fs"); const config = {relativePath: "../endpoint/"}; const endpointConfig = {}; const u = require("./util.js"); -u.loadContractENV() +await u.loadContractENV() if (require.main === module) { main() @@ -14,7 +14,7 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - u.loadContractENV() + await u.loadContractENV() } async function endpointAndRegisterChain() { diff --git a/cicd/subswap.js b/cicd/subswap.js index 37b25c8..2ee3650 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -2,7 +2,7 @@ process.chdir(__dirname); const fs = require("node:fs"); const config = { relativePath: "../applications/subswap/contract/" }; const u = require("./util.js"); -u.loadContractENV(); +await u.loadContractENV(); if (require.main === module) { main(); @@ -14,15 +14,15 @@ async function main() { u.replaceOrAddENV("./mount/contract_deploy.env", key, value); u.replaceOrAddENV("./mount/common.env", key, value); } - u.loadContractENV(); + await u.loadContractENV(); } async function subswap() { console.log("start subswap deploy"); checkEndpointConfig(); await initSubswapDeploy(); - deploySubswap(); - deploySampleToken(); + await deploySubswap(); + await deploySampleToken(); writeSubswapConfigJson(); const newENV = exportSubswap(); return newENV; @@ -85,7 +85,7 @@ async function initSubswapDeploy() { await u.getNetworkID(config); } -function deploySubswap() { +async function deploySubswap() { console.log("writing network config"); u.writeNetworkJson(config); console.log("writing deploy.config.json"); @@ -127,7 +127,7 @@ function exportSubswap() { }; } -function deploySampleToken() { +async function deploySampleToken() { console.log("writing deploy.config.json"); writeSubswapDeployJson(); console.log("configuring PK"); diff --git a/cicd/util.js b/cicd/util.js index fc4f857..ff7758c 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -75,12 +75,17 @@ async function getNetworkID(config) { config["parentnetID"] = parentID; } -function loadContractENV(){ +async function loadContractENV(){ + await delay(2000); dotenv.config({ path: "mount/contract_deploy.env" }); } -function loadCommonENV(){ +async function loadCommonENV(){ + await delay(2000); dotenv.config({ path: "mount/common.env" }); } +function delay(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} function replaceENV(filepath, replaceENV, replaceValue){ //check files mounted diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 2a08048..c563b8b 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -2,7 +2,7 @@ process.chdir(__dirname); const { ethers } = require("ethers"); const fs = require("node:fs"); const u = require("./util.js"); -u.loadContractENV() +await u.loadContractENV() const e = require("./endpointandregisterchain.js") const s = require("./subswap.js") const a = require("./applicationregister.js") @@ -15,14 +15,15 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - u.loadContractENV() + await u.loadContractENV() const step2ENV = await s.subswap() for (const [key, value] of Object.entries(step2ENV)) { u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - u.loadContractENV() + + await u.loadContractENV() await a.applicationRegister() @@ -30,7 +31,7 @@ async function main(){ } async function setupSubnetWallets(){ - u.loadCommonENV() + await u.loadCommonENV() if (!fs.existsSync('./mount/keys.json')) { throw Error(`could not modify ${filepath}, file not mounted`) } From a7dfe3c81aac9f5a9d554a019a9406633f2a0e25 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 16:13:52 +0400 Subject: [PATCH 08/17] Revert "use delay to fix wrong env read" This reverts commit 16ef60d127d0cfb302db548f2c8182ac84776fa6. --- cicd/applicationregister.js | 2 +- cicd/endpointandregisterchain.js | 4 ++-- cicd/subswap.js | 12 ++++++------ cicd/util.js | 9 ++------- cicd/zeroandsubswap.js | 9 ++++----- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index b91a313..488b9e2 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -3,7 +3,7 @@ const fs = require("node:fs"); const config = {relativePath: "../endpoint/"}; const endpointConfig = {}; const u = require("./util.js"); -await u.loadContractENV() +u.loadContractENV() if (require.main === module) { main() diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index a0b392a..37c446e 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -3,7 +3,7 @@ const fs = require("node:fs"); const config = {relativePath: "../endpoint/"}; const endpointConfig = {}; const u = require("./util.js"); -await u.loadContractENV() +u.loadContractENV() if (require.main === module) { main() @@ -14,7 +14,7 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - await u.loadContractENV() + u.loadContractENV() } async function endpointAndRegisterChain() { diff --git a/cicd/subswap.js b/cicd/subswap.js index 2ee3650..37b25c8 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -2,7 +2,7 @@ process.chdir(__dirname); const fs = require("node:fs"); const config = { relativePath: "../applications/subswap/contract/" }; const u = require("./util.js"); -await u.loadContractENV(); +u.loadContractENV(); if (require.main === module) { main(); @@ -14,15 +14,15 @@ async function main() { u.replaceOrAddENV("./mount/contract_deploy.env", key, value); u.replaceOrAddENV("./mount/common.env", key, value); } - await u.loadContractENV(); + u.loadContractENV(); } async function subswap() { console.log("start subswap deploy"); checkEndpointConfig(); await initSubswapDeploy(); - await deploySubswap(); - await deploySampleToken(); + deploySubswap(); + deploySampleToken(); writeSubswapConfigJson(); const newENV = exportSubswap(); return newENV; @@ -85,7 +85,7 @@ async function initSubswapDeploy() { await u.getNetworkID(config); } -async function deploySubswap() { +function deploySubswap() { console.log("writing network config"); u.writeNetworkJson(config); console.log("writing deploy.config.json"); @@ -127,7 +127,7 @@ function exportSubswap() { }; } -async function deploySampleToken() { +function deploySampleToken() { console.log("writing deploy.config.json"); writeSubswapDeployJson(); console.log("configuring PK"); diff --git a/cicd/util.js b/cicd/util.js index ff7758c..fc4f857 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -75,17 +75,12 @@ async function getNetworkID(config) { config["parentnetID"] = parentID; } -async function loadContractENV(){ - await delay(2000); +function loadContractENV(){ dotenv.config({ path: "mount/contract_deploy.env" }); } -async function loadCommonENV(){ - await delay(2000); +function loadCommonENV(){ dotenv.config({ path: "mount/common.env" }); } -function delay(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} function replaceENV(filepath, replaceENV, replaceValue){ //check files mounted diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index c563b8b..2a08048 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -2,7 +2,7 @@ process.chdir(__dirname); const { ethers } = require("ethers"); const fs = require("node:fs"); const u = require("./util.js"); -await u.loadContractENV() +u.loadContractENV() const e = require("./endpointandregisterchain.js") const s = require("./subswap.js") const a = require("./applicationregister.js") @@ -15,15 +15,14 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - await u.loadContractENV() + u.loadContractENV() const step2ENV = await s.subswap() for (const [key, value] of Object.entries(step2ENV)) { u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - - await u.loadContractENV() + u.loadContractENV() await a.applicationRegister() @@ -31,7 +30,7 @@ async function main(){ } async function setupSubnetWallets(){ - await u.loadCommonENV() + u.loadCommonENV() if (!fs.existsSync('./mount/keys.json')) { throw Error(`could not modify ${filepath}, file not mounted`) } From 8573641c8a9a1ea5e0ce773d994e29563d40ec8b Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 16:16:09 +0400 Subject: [PATCH 09/17] try another delay --- cicd/zeroandsubswap.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 2a08048..77e2c8c 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -15,6 +15,7 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } + await delay(2000) u.loadContractENV() const step2ENV = await s.subswap() @@ -22,6 +23,7 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } + await delay(2000) u.loadContractENV() await a.applicationRegister() @@ -29,6 +31,10 @@ async function main(){ await setupSubnetWallets() } +function delay(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + async function setupSubnetWallets(){ u.loadCommonENV() if (!fs.existsSync('./mount/keys.json')) { From 30c6eb10bc75b4ec8ec6df08f1f7981d1e795f1f Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 16:55:31 +0400 Subject: [PATCH 10/17] change place to load env --- cicd/applicationregister.js | 1 + cicd/subswap.js | 1 + cicd/zeroandsubswap.js | 7 ------- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 488b9e2..d3505c1 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -15,6 +15,7 @@ async function main(){ async function applicationRegister() { console.log("start application register"); + u.loadContractENV() importEndpointJson(); initApplicationRegister(); await u.getNetworkID(config); diff --git a/cicd/subswap.js b/cicd/subswap.js index 37b25c8..0c8bdc3 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -19,6 +19,7 @@ async function main() { async function subswap() { console.log("start subswap deploy"); + u.loadContractENV() checkEndpointConfig(); await initSubswapDeploy(); deploySubswap(); diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 77e2c8c..0ce6d4c 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -15,25 +15,18 @@ async function main(){ u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - await delay(2000) - u.loadContractENV() const step2ENV = await s.subswap() for (const [key, value] of Object.entries(step2ENV)) { u.replaceOrAddENV('./mount/contract_deploy.env', key, value) u.replaceOrAddENV('./mount/common.env', key, value) } - await delay(2000) - u.loadContractENV() await a.applicationRegister() await setupSubnetWallets() } -function delay(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} async function setupSubnetWallets(){ u.loadCommonENV() From f1162eb39f9c0e13ed38641d12e459f5f768783e Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 17:41:31 +0400 Subject: [PATCH 11/17] debug --- cicd/util.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cicd/util.js b/cicd/util.js index fc4f857..72964dc 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -76,6 +76,9 @@ async function getNetworkID(config) { } function loadContractENV(){ + console.log('debug show contract_deploy.env contents') + const envFileContent = fs.readFileSync("mount/contract_deploy.env", 'utf8'); + console.log(envFileContent) dotenv.config({ path: "mount/contract_deploy.env" }); } function loadCommonENV(){ From eb37ff400edd9ef717fea4f5b5eb4c761da1b2be Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 17:58:46 +0400 Subject: [PATCH 12/17] add override to env load --- cicd/applicationregister.js | 3 +++ cicd/util.js | 7 ++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index d3505c1..3f29c90 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -83,6 +83,9 @@ function initApplicationRegister() { "incomplete ENVs, require SUBNET_PK, PARENTNET_PK, SUBNET_APP, PARENTNET_APP, SUBNET_URL" ); } + + + subnetPK = process.env.SUBNET_PK.startsWith("0x") ? process.env.SUBNET_PK : `0x${process.env.SUBNET_PK}`; diff --git a/cicd/util.js b/cicd/util.js index 72964dc..e02895b 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -76,13 +76,10 @@ async function getNetworkID(config) { } function loadContractENV(){ - console.log('debug show contract_deploy.env contents') - const envFileContent = fs.readFileSync("mount/contract_deploy.env", 'utf8'); - console.log(envFileContent) - dotenv.config({ path: "mount/contract_deploy.env" }); + dotenv.config({ path: "mount/contract_deploy.env", override: true}); } function loadCommonENV(){ - dotenv.config({ path: "mount/common.env" }); + dotenv.config({ path: "mount/common.env", override: true}); } function replaceENV(filepath, replaceENV, replaceValue){ From f9523efafd5bdc7c9bcab390879fc0e48893430c Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Mon, 21 Apr 2025 18:28:18 +0400 Subject: [PATCH 13/17] add deployer log --- applications/subswap/contract/scripts/simpletokendeploy.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/applications/subswap/contract/scripts/simpletokendeploy.js b/applications/subswap/contract/scripts/simpletokendeploy.js index 0f59723..619ff48 100644 --- a/applications/subswap/contract/scripts/simpletokendeploy.js +++ b/applications/subswap/contract/scripts/simpletokendeploy.js @@ -23,8 +23,9 @@ async function main() { token.initSupply ); + const [deployer] = await hre.ethers.getSigners(); + console.log("SimpleToken deploy start, deployer address:", deployer.address) await simpleToken.deployed(); - console.log("ERC20 " + token.name + " deploy to ", simpleToken.address); } From 59572e37b500974faeede70d4164f4c655f3d71d Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Thu, 24 Apr 2025 12:57:15 +0400 Subject: [PATCH 14/17] format files --- cicd/Dockerfile | 2 - cicd/applicationregister.js | 12 ++--- cicd/endpointandregisterchain.js | 26 +++++----- cicd/subswap.js | 23 +++++---- cicd/util.js | 86 +++++++++++++++++--------------- cicd/zeroandsubswap.js | 75 ++++++++++++++++------------ 6 files changed, 118 insertions(+), 106 deletions(-) diff --git a/cicd/Dockerfile b/cicd/Dockerfile index 4ddb818..41f4124 100644 --- a/cicd/Dockerfile +++ b/cicd/Dockerfile @@ -1,14 +1,12 @@ FROM node:20-alpine COPY . /app - WORKDIR /app/cicd RUN yarn WORKDIR /app/endpoint RUN yarn WORKDIR /app/applications/subswap/contract RUN yarn - WORKDIR /app/cicd ENTRYPOINT ["node"] \ No newline at end of file diff --git a/cicd/applicationregister.js b/cicd/applicationregister.js index 3f29c90..4740dfc 100755 --- a/cicd/applicationregister.js +++ b/cicd/applicationregister.js @@ -1,21 +1,21 @@ process.chdir(__dirname); const fs = require("node:fs"); -const config = {relativePath: "../endpoint/"}; +const config = { relativePath: "../endpoint/" }; const endpointConfig = {}; const u = require("./util.js"); -u.loadContractENV() +u.loadContractENV(); if (require.main === module) { - main() + main(); } -async function main(){ +async function main() { await applicationRegister(); } async function applicationRegister() { console.log("start application register"); - u.loadContractENV() + u.loadContractENV(); importEndpointJson(); initApplicationRegister(); await u.getNetworkID(config); @@ -84,8 +84,6 @@ function initApplicationRegister() { ); } - - subnetPK = process.env.SUBNET_PK.startsWith("0x") ? process.env.SUBNET_PK : `0x${process.env.SUBNET_PK}`; diff --git a/cicd/endpointandregisterchain.js b/cicd/endpointandregisterchain.js index 37c446e..4c98a30 100644 --- a/cicd/endpointandregisterchain.js +++ b/cicd/endpointandregisterchain.js @@ -1,20 +1,20 @@ process.chdir(__dirname); const fs = require("node:fs"); -const config = {relativePath: "../endpoint/"}; +const config = { relativePath: "../endpoint/" }; const endpointConfig = {}; const u = require("./util.js"); -u.loadContractENV() +u.loadContractENV(); if (require.main === module) { - main() + main(); } -async function main(){ - const newENV = await endpointAndRegisterChain() +async function main() { + const newENV = await endpointAndRegisterChain(); for (const [key, value] of Object.entries(newENV)) { - u.replaceOrAddENV('./mount/contract_deploy.env', key, value) - u.replaceOrAddENV('./mount/common.env', key, value) + u.replaceOrAddENV("./mount/contract_deploy.env", key, value); + u.replaceOrAddENV("./mount/common.env", key, value); } - u.loadContractENV() + u.loadContractENV(); } async function endpointAndRegisterChain() { @@ -25,7 +25,7 @@ async function endpointAndRegisterChain() { configureEndpointJson(); registerEndpoint(); const newENV = exportEndpointJson(); - return newENV + return newENV; } function initEndpointDeploy() { @@ -39,7 +39,6 @@ function initEndpointDeploy() { throw Error("PARENTNET_URL not found"); } - const reqENV = [ "SUBNET_PK", "PARENTNET_PK", @@ -127,9 +126,9 @@ function exportEndpointJson() { console.log("PARENTNET_ZERO_CONTRACT=" + config.parentnetEndpoint); return { - 'SUBNET_ZERO_CONTRACT': config.subnetEndpoint, - 'PARENTNET_ZERO_CONTRACT': config.parentnetEndpoint - } + SUBNET_ZERO_CONTRACT: config.subnetEndpoint, + PARENTNET_ZERO_CONTRACT: config.parentnetEndpoint, + }; } function deployEndpoint() { @@ -193,7 +192,6 @@ function parseEndpointOutput(outString) { } } - module.exports = { endpointAndRegisterChain, }; diff --git a/cicd/subswap.js b/cicd/subswap.js index 0c8bdc3..d344b85 100644 --- a/cicd/subswap.js +++ b/cicd/subswap.js @@ -19,7 +19,7 @@ async function main() { async function subswap() { console.log("start subswap deploy"); - u.loadContractENV() + u.loadContractENV(); checkEndpointConfig(); await initSubswapDeploy(); deploySubswap(); @@ -138,18 +138,18 @@ function deploySampleToken() { "cd ../applications/subswap/contract; npx hardhat run scripts/simpletokendeploy.js --network xdcsubnet" ); tokenAddr = parseEndpointOutput(tokenDeployOut); - config["subnetSampleTokenAddress"] = tokenAddr + config["subnetSampleTokenAddress"] = tokenAddr; } function writeSubswapDeployJson() { deployJson = { subnetendpoint: config.subnetEndpoint, parentnetendpoint: config.parentnetEndpoint, - "subnettoken": { - "name": "Subnet Sample Token", - "symbol": "SST", - "initSupply": 1_000_000 - } + subnettoken: { + name: "Subnet Sample Token", + symbol: "SST", + initSupply: 1_000_000, + }, }; fs.writeFileSync( "../applications/subswap/contract/deploy.config.json", @@ -204,12 +204,17 @@ function writeSubswapConfigJson() { tokens: [ { name: "Subnet Sample Token", - address: config["subnetSampleTokenAddress"] || "0x1111111111111111111111111111111111111111", + address: + config["subnetSampleTokenAddress"] || + "0x1111111111111111111111111111111111111111", }, ], }; - fs.writeFileSync("./mount/subswap-frontend.config.json", JSON.stringify(obj, null, 2)) + fs.writeFileSync( + "./mount/subswap-frontend.config.json", + JSON.stringify(obj, null, 2) + ); } module.exports = { diff --git a/cicd/util.js b/cicd/util.js index e02895b..f70328f 100644 --- a/cicd/util.js +++ b/cicd/util.js @@ -1,7 +1,7 @@ process.chdir(__dirname); const { execSync } = require("child_process"); const fs = require("node:fs"); -const dotenv = require("dotenv") +const dotenv = require("dotenv"); const { ethers } = require("ethers"); function writeEnv(key, path) { @@ -75,64 +75,68 @@ async function getNetworkID(config) { config["parentnetID"] = parentID; } -function loadContractENV(){ - dotenv.config({ path: "mount/contract_deploy.env", override: true}); +function loadContractENV() { + dotenv.config({ path: "mount/contract_deploy.env", override: true }); } -function loadCommonENV(){ - dotenv.config({ path: "mount/common.env", override: true}); +function loadCommonENV() { + dotenv.config({ path: "mount/common.env", override: true }); } -function replaceENV(filepath, replaceENV, replaceValue){ +function replaceENV(filepath, replaceENV, replaceValue) { //check files mounted if (!fs.existsSync(filepath)) { - throw Error(`could not modify ${filepath}, file not mounted`) - } - const envFileContent = fs.readFileSync(filepath, 'utf8'); - const regex = new RegExp(`^${replaceENV}=.*`, 'gm'); + throw Error(`could not modify ${filepath}, file not mounted`); + } + const envFileContent = fs.readFileSync(filepath, "utf8"); + const regex = new RegExp(`^${replaceENV}=.*`, "gm"); let matches = envFileContent.match(regex); - matches = (matches === null) ? [] : matches - - if (matches.length > 1){ - console.log('Warning: found more than one instance of', replaceENV, 'in', filepath) - console.log(matches) + matches = matches === null ? [] : matches; + + if (matches.length > 1) { + console.log( + "Warning: found more than one instance of", + replaceENV, + "in", + filepath + ); + console.log(matches); } - let matchesCount = 0 + let matchesCount = 0; const updatedContent = envFileContent.replace(regex, (match) => { - let replaceString= -`# Commented old value by deployer -# ${matches[matchesCount]}` + let replaceString = `# Commented old value by deployer +# ${matches[matchesCount]}`; - if (matchesCount == matches.length-1) { - replaceString+=`\n${replaceENV}=${replaceValue}` + if (matchesCount == matches.length - 1) { + replaceString += `\n${replaceENV}=${replaceValue}`; } - matchesCount++ + matchesCount++; console.log(`Updated ${filepath} file: \n${replaceString}`); - return replaceString - }); + return replaceString; + }); fs.writeFileSync(filepath, updatedContent); - return (updatedContent !== envFileContent) + return updatedContent !== envFileContent; } -function addENV(filepath, envName, envValue){ +function addENV(filepath, envName, envValue) { //check files mounted if (!fs.existsSync(filepath)) { - throw Error(`could not modify ${filepath}, file not mounted`) - } - const envFileContent = fs.readFileSync(filepath, 'utf8'); - const appendString = `${envName}=${envValue}` - const updatedContent = envFileContent+'\n'+appendString - + throw Error(`could not modify ${filepath}, file not mounted`); + } + const envFileContent = fs.readFileSync(filepath, "utf8"); + const appendString = `${envName}=${envValue}`; + const updatedContent = envFileContent + "\n" + appendString; + fs.writeFileSync(filepath, updatedContent); } -function replaceOrAddENV(filepath, envKey, envValue){ - replaced = replaceENV(filepath, envKey, envValue) - !replaced && addENV(filepath, envKey, envValue) +function replaceOrAddENV(filepath, envKey, envValue) { + replaced = replaceENV(filepath, envKey, envValue); + !replaced && addENV(filepath, envKey, envValue); } async function transferTokens(url, fromPK, toPK, amount) { - console.log(url) + console.log(url); const provider = new ethers.providers.JsonRpcProvider(url); const fromWallet = new ethers.Wallet(fromPK, provider); const toWallet = new ethers.Wallet(toPK, provider); @@ -141,10 +145,10 @@ async function transferTokens(url, fromPK, toPK, amount) { value: ethers.utils.parseEther(amount.toString()), }; - try{ + try { await provider.detectNetwork(); - } catch (error){ - throw Error("Cannot connect to RPC") + } catch (error) { + throw Error("Cannot connect to RPC"); } let sendPromise = fromWallet.sendTransaction(tx); @@ -175,8 +179,8 @@ async function transferTokens(url, fromPK, toPK, amount) { return { fromBalance: fromBalance, toBalance: toBalance, - txHash: txHash - } + txHash: txHash, + }; } } } diff --git a/cicd/zeroandsubswap.js b/cicd/zeroandsubswap.js index 0ce6d4c..e19412a 100644 --- a/cicd/zeroandsubswap.js +++ b/cicd/zeroandsubswap.js @@ -2,50 +2,59 @@ process.chdir(__dirname); const { ethers } = require("ethers"); const fs = require("node:fs"); const u = require("./util.js"); -u.loadContractENV() -const e = require("./endpointandregisterchain.js") -const s = require("./subswap.js") -const a = require("./applicationregister.js") +u.loadContractENV(); +const e = require("./endpointandregisterchain.js"); +const s = require("./subswap.js"); +const a = require("./applicationregister.js"); main(); -async function main(){ - const step1ENV = await e.endpointAndRegisterChain() +async function main() { + const step1ENV = await e.endpointAndRegisterChain(); for (const [key, value] of Object.entries(step1ENV)) { - u.replaceOrAddENV('./mount/contract_deploy.env', key, value) - u.replaceOrAddENV('./mount/common.env', key, value) + u.replaceOrAddENV("./mount/contract_deploy.env", key, value); + u.replaceOrAddENV("./mount/common.env", key, value); } - const step2ENV = await s.subswap() + const step2ENV = await s.subswap(); for (const [key, value] of Object.entries(step2ENV)) { - u.replaceOrAddENV('./mount/contract_deploy.env', key, value) - u.replaceOrAddENV('./mount/common.env', key, value) + u.replaceOrAddENV("./mount/contract_deploy.env", key, value); + u.replaceOrAddENV("./mount/common.env", key, value); } - await a.applicationRegister() - - await setupSubnetWallets() -} + await a.applicationRegister(); + await setupSubnetWallets(); +} -async function setupSubnetWallets(){ - u.loadCommonENV() - if (!fs.existsSync('./mount/keys.json')) { - throw Error(`could not modify ${filepath}, file not mounted`) - } - const grandmasterPK = JSON.parse(fs.readFileSync('./mount/keys.json', 'utf8')).Grandmaster.PrivateKey - - try{ - new ethers.Wallet(process.env.SUBNET_WALLET_PK) - new ethers.Wallet(process.env.SUBNET_ZERO_WALLET_PK) - new ethers.Wallet(grandmasterPK) - }catch(error){ - console.log(error) - console.log('failed to setup wallets, invalid PK') - process.exit() +async function setupSubnetWallets() { + u.loadCommonENV(); + if (!fs.existsSync("./mount/keys.json")) { + throw Error(`could not modify ${filepath}, file not mounted`); + } + const grandmasterPK = JSON.parse(fs.readFileSync("./mount/keys.json", "utf8")) + .Grandmaster.PrivateKey; + + try { + new ethers.Wallet(process.env.SUBNET_WALLET_PK); + new ethers.Wallet(process.env.SUBNET_ZERO_WALLET_PK); + new ethers.Wallet(grandmasterPK); + } catch (error) { + console.log(error); + console.log("failed to setup wallets, invalid PK"); + process.exit(); } - - await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_WALLET_PK, 10_000_000) - await u.transferTokens(process.env.SUBNET_URL, grandmasterPK, process.env.SUBNET_ZERO_WALLET_PK, 1_000_000) + await u.transferTokens( + process.env.SUBNET_URL, + grandmasterPK, + process.env.SUBNET_WALLET_PK, + 10_000_000 + ); + await u.transferTokens( + process.env.SUBNET_URL, + grandmasterPK, + process.env.SUBNET_ZERO_WALLET_PK, + 1_000_000 + ); } From 8ddb6b1257152a9e6024134db7baa3d8d75810e7 Mon Sep 17 00:00:00 2001 From: Galaxy <30950645+GalaxySciTech@users.noreply.github.com> Date: Wed, 23 Apr 2025 18:21:29 +0400 Subject: [PATCH 15/17] add 2718 rlp parse --- .gitignore | 3 ++- endpoint/contracts/Endpoint.sol | 5 +++-- endpoint/contracts/libraries/RLPReader.sol | 22 +++++++++++++++++++--- endpoint/test/endpoint.js | 6 ++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 600d2d3..d44bc08 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode \ No newline at end of file +.vscode +yarn.lock \ No newline at end of file diff --git a/endpoint/contracts/Endpoint.sol b/endpoint/contracts/Endpoint.sol index 68fb0fe..b5e3b26 100644 --- a/endpoint/contracts/Endpoint.sol +++ b/endpoint/contracts/Endpoint.sol @@ -1,10 +1,10 @@ // SPDX-License-Identifier: MIT pragma solidity =0.8.23; +import {RLPReader} from "./libraries/RLPReader.sol"; import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; import {MerklePatricia} from "@polytope-labs/solidity-merkle-trees/src/MerklePatricia.sol"; import {IFullCheckpoint} from "./interfaces/IFullCheckpoint.sol"; -import {RLPReader} from "./libraries/RLPReader.sol"; import {Address} from "@openzeppelin/contracts/utils/Address.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; @@ -319,7 +319,8 @@ contract Endpoint is Ownable, ReentrancyGuard { function getReceipt( bytes memory receiptRlp ) public pure returns (Receipt memory) { - RLPReader.RLPItem[] memory items = receiptRlp.toRlpItem().toList(); + RLPReader.RLPItem[] memory items; + items = receiptRlp.toRlpItem().toList(); Receipt memory receipt; receipt.postStateOrStatus = items[0].toBytes(); diff --git a/endpoint/contracts/libraries/RLPReader.sol b/endpoint/contracts/libraries/RLPReader.sol index 890720c..226bc23 100644 --- a/endpoint/contracts/libraries/RLPReader.sol +++ b/endpoint/contracts/libraries/RLPReader.sol @@ -6,6 +6,7 @@ library RLPReader { uint8 constant STRING_LONG_START = 0xb8; uint8 constant LIST_SHORT_START = 0xc0; uint8 constant LIST_LONG_START = 0xf8; + uint8 constant LEGACY_TYPE = 0x7f; uint8 constant WORD_SIZE = 32; struct RLPItem { @@ -102,11 +103,9 @@ library RLPReader { /* * @param the RLP item containing the encoded list. */ - function toList( + function toLegacyList( RLPItem memory item ) internal pure returns (RLPItem[] memory) { - require(isList(item)); - uint256 items = numItems(item); RLPItem[] memory result = new RLPItem[](items); @@ -121,6 +120,23 @@ library RLPReader { return result; } + function toEip2718List( + RLPItem memory item + ) internal pure returns (RLPItem[] memory) { + RLPItem[] memory items = toLegacyList(item); + return toLegacyList(items[1]); + } + + function toList( + RLPItem memory item + ) internal pure returns (RLPItem[] memory) { + if (isList(item)) { + return toLegacyList(item); + } else { + return toEip2718List(item); + } + } + // @return indicator whether encoded payload is a list. negate this function call for isData. function isList(RLPItem memory item) internal pure returns (bool) { if (item.len == 0) return false; diff --git a/endpoint/test/endpoint.js b/endpoint/test/endpoint.js index 7bad622..cffcc9e 100644 --- a/endpoint/test/endpoint.js +++ b/endpoint/test/endpoint.js @@ -60,6 +60,12 @@ describe("xdc zero endpoint", () => { }); describe("test endpoint", () => { + it("test rlp resolve", async () => { + const res = await endpoint.getReceipt( + "0xb9048402f9048001830169a8b9010000000000000000000000000000000000000000000000001000000002000000000000000000800000000000100000400000000000000000000000000000000000004000800000000000000008000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000012000000000080000000000000000000000000000000000000280000000200000000000000000000002000000000000001000000000000000000000000000000000000000000000002000000020000004000000000000000000000000000000000000020000000000002000000000000000000000000000000000000008000000000000000f90375f89b94faa9365d018d7f3984a4fce381be157d8801b6aef863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000d602735eaeb1e81b673e456c631209c95fac446aa00000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000002d5d0d5669e6e9dc0000f901da94200760f7380b93dec8dfe03a23c38fa8d46e3949e1a0e9bded5f24a4168e4f3bf44e00298c993b22376aad8c58c7dda9718a54cbea82b901a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000003300000000000000000000000046638164b514b9d19a4ab3e35c62df5e107e7f27000000000000000000000000000000000000000000000000000000000000174b000000000000000000000000d6ae154a2901722031a8ed023851296bff7e9e3d00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000648bc392070000000000000000000000007589d4667b9bd10a93bf918e4eb459f1274bdf93000000000000000000000000000000000000000000002d5d0d5669e6e9dc0000000000000000000000000000d602735eaeb1e81b673e456c631209c95fac446a00000000000000000000000000000000000000000000000000000000f8f99446638164b514b9d19a4ab3e35c62df5e107e7f27e1a0f94f9ef28e17557f777d13ad02f21c0b557bdab8cded7835d95551ba17e25e3eb8c0000000000000000000000000000000000000000000000000000000000000174b000000000000000000000000d6ae154a2901722031a8ed023851296bff7e9e3d0000000000000000000000007589d4667b9bd10a93bf918e4eb459f1274bdf93000000000000000000000000faa9365d018d7f3984a4fce381be157d8801b6ae000000000000000000000000000000000000000000002d5d0d5669e6e9dc0000000000000000000000000000d602735eaeb1e81b673e456c631209c95fac446a" + ); + console.log(res) + }); it("shold be able to send message", async () => { await sua.simpleCall(chainId, rua.address); From 3d42381ed8bb706c4721c9d049821caddb418723 Mon Sep 17 00:00:00 2001 From: Galaxy <30950645+GalaxySciTech@users.noreply.github.com> Date: Thu, 24 Apr 2025 13:03:13 +0400 Subject: [PATCH 16/17] Update RLPReader.sol --- endpoint/contracts/libraries/RLPReader.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/endpoint/contracts/libraries/RLPReader.sol b/endpoint/contracts/libraries/RLPReader.sol index 226bc23..51e6f3a 100644 --- a/endpoint/contracts/libraries/RLPReader.sol +++ b/endpoint/contracts/libraries/RLPReader.sol @@ -6,7 +6,6 @@ library RLPReader { uint8 constant STRING_LONG_START = 0xb8; uint8 constant LIST_SHORT_START = 0xc0; uint8 constant LIST_LONG_START = 0xf8; - uint8 constant LEGACY_TYPE = 0x7f; uint8 constant WORD_SIZE = 32; struct RLPItem { From c60b0f6de0b986650b095f2279347bc5357b0b43 Mon Sep 17 00:00:00 2001 From: Galaxy <30950645+GalaxySciTech@users.noreply.github.com> Date: Thu, 24 Apr 2025 14:44:45 +0400 Subject: [PATCH 17/17] add parse transaction compatibility --- endpoint/contracts/Endpoint.sol | 11 ++++++++--- endpoint/test/endpoint.js | 9 +++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/endpoint/contracts/Endpoint.sol b/endpoint/contracts/Endpoint.sol index b5e3b26..b94a159 100644 --- a/endpoint/contracts/Endpoint.sol +++ b/endpoint/contracts/Endpoint.sol @@ -306,9 +306,14 @@ contract Endpoint is Ownable, ReentrancyGuard { bytes memory transactionRlp ) public pure returns (Transaction memory) { RLPReader.RLPItem[] memory items = transactionRlp.toRlpItem().toList(); - Transaction memory transaction; - transaction.to = items[3].toAddress(); + + if (items[3].len == 21) { + transaction.to = items[3].toAddress(); + } else { + transaction.to = items[5].toAddress(); + } + return transaction; } @@ -344,7 +349,7 @@ contract Endpoint is Ownable, ReentrancyGuard { return receipt; } - function sliceBytes(bytes memory data) private pure returns (bytes memory) { + function sliceBytes(bytes memory data) public pure returns (bytes memory) { require(data.length >= 64, "Data must be at least 64 bytes long"); bytes memory slicedData = new bytes(data.length - 64); diff --git a/endpoint/test/endpoint.js b/endpoint/test/endpoint.js index cffcc9e..e74a465 100644 --- a/endpoint/test/endpoint.js +++ b/endpoint/test/endpoint.js @@ -61,10 +61,15 @@ describe("xdc zero endpoint", () => { describe("test endpoint", () => { it("test rlp resolve", async () => { - const res = await endpoint.getReceipt( + const res1 = await endpoint.getReceipt( "0xb9048402f9048001830169a8b9010000000000000000000000000000000000000000000000001000000002000000000000000000800000000000100000400000000000000000000000000000000000004000800000000000000008000000000000000000000000000000000000000000000000020000000000000000000800000000000000000000000012000000000080000000000000000000000000000000000000280000000200000000000000000000002000000000000001000000000000000000000000000000000000000000000002000000020000004000000000000000000000000000000000000020000000000002000000000000000000000000000000000000008000000000000000f90375f89b94faa9365d018d7f3984a4fce381be157d8801b6aef863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000d602735eaeb1e81b673e456c631209c95fac446aa00000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000002d5d0d5669e6e9dc0000f901da94200760f7380b93dec8dfe03a23c38fa8d46e3949e1a0e9bded5f24a4168e4f3bf44e00298c993b22376aad8c58c7dda9718a54cbea82b901a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000003300000000000000000000000046638164b514b9d19a4ab3e35c62df5e107e7f27000000000000000000000000000000000000000000000000000000000000174b000000000000000000000000d6ae154a2901722031a8ed023851296bff7e9e3d00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000648bc392070000000000000000000000007589d4667b9bd10a93bf918e4eb459f1274bdf93000000000000000000000000000000000000000000002d5d0d5669e6e9dc0000000000000000000000000000d602735eaeb1e81b673e456c631209c95fac446a00000000000000000000000000000000000000000000000000000000f8f99446638164b514b9d19a4ab3e35c62df5e107e7f27e1a0f94f9ef28e17557f777d13ad02f21c0b557bdab8cded7835d95551ba17e25e3eb8c0000000000000000000000000000000000000000000000000000000000000174b000000000000000000000000d6ae154a2901722031a8ed023851296bff7e9e3d0000000000000000000000007589d4667b9bd10a93bf918e4eb459f1274bdf93000000000000000000000000faa9365d018d7f3984a4fce381be157d8801b6ae000000000000000000000000000000000000000000002d5d0d5669e6e9dc0000000000000000000000000000d602735eaeb1e81b673e456c631209c95fac446a" ); - console.log(res) + console.log(res1); + + const res2 = await endpoint.getTransaction( + "0xb9013602f9013233018502e90edd008502e90edd008301692894e29059ff09aa10a27b2421ef1009f85b4ff8387480b8c44cc9a5920000000000000000000000000000000000000000000000000000000000004b9c0000000000000000000000004ac7ec88de85f81400208257981a9e775fb7721e000000000000000000000000885113939950fd55597cbc30e33bde371a9a2b740000000000000000000000008bfbb31a1a5ab001f5f204d3eb85d7b16a9e60c0000000000000000000000000000000000000000000005554737f4bb7a6dc000000000000000000000000000000da8c65e0b80946dbe884c924c9c6c03f7acc6cc001a097a9e809292ffde59923b9f7ef35292f3e6c3fcfcc6ac2313c91afe2398c4c00a06147bf2f05cadf58052f2eab75a32066c25d068ce143f5b6b3fb700821d8473d" + ); + console.log(res2); }); it("shold be able to send message", async () => { await sua.simpleCall(chainId, rua.address);