Skip to content

Commit

Permalink
new config deploy scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Feb 17, 2025
1 parent ee7bc1b commit 9d812dd
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 37 deletions.
31 changes: 20 additions & 11 deletions cicd/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ const u = require("./util.js");
main();

async function main() {
u.loadContractENV();
console.log("start deploying full CSC");
initDeployFull();
await configureFiles();
deployFull();
exportFull();
const contractENV = exportFull();
for (const [key, value] of Object.entries(contractENV)) {
u.replaceOrAddENV('./mount/contract_deploy.env', key, value)
u.replaceOrAddENV('./mount/common.env', key, value)
}

}

function initDeployFull() {
Expand Down Expand Up @@ -78,16 +84,19 @@ function exportFull() {
"SUCCESS deploy full csc, please include the following line in your common.env"
);
console.log(`CHECKPOINT_CONTRACT=${config.fullCSC}\n`);
fs.appendFileSync(
"mount/csc.env",
`\nFULL_CSC=${config.fullCSC}\n`,
"utf-8",
(err) => {
if (err) {
throw Error("error writing mount/csc.env, " + err);
}
}
);
// fs.appendFileSync(
// "mount/csc.env",
// `\nFULL_CSC=${config.fullCSC}\n`,
// "utf-8",
// (err) => {
// if (err) {
// throw Error("error writing mount/csc.env, " + err);
// }
// }
// );
return {
CHECKPOINT_CONTRACT: `CHECKPOINT_CONTRACT=${config.fullCSC}`
}
}

function parseFullOut(outString) {
Expand Down
32 changes: 19 additions & 13 deletions cicd/lite.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
process.chdir(__dirname);
const { execSync } = require("child_process");
const fs = require("node:fs");
const env = require("dotenv").config({ path: "mount/.env" });
const config = {
relativePath: "../",
};
Expand All @@ -11,10 +9,15 @@ main();

async function main() {
console.log("start deploying lite CSC");
u.loadContractENV();
initDeployLite();
await configureFiles();
deployLite();
exportLite();
const contractENV = exportLite();
for (const [key, value] of Object.entries(contractENV)) {
u.replaceOrAddENV('./mount/contract_deploy.env', key, value)
u.replaceOrAddENV('./mount/common.env', key, value)
}
}

function initDeployLite() {
Expand Down Expand Up @@ -78,16 +81,19 @@ function exportLite() {
"SUCCESS deploy lite csc, please include the following line in your common.env"
);
console.log(`CHECKPOINT_CONTRACT=${config.liteCSC}\n`);
fs.appendFileSync(
"mount/csc.env",
`\nLITE_CSC=${config.liteCSC}\n`,
"utf-8",
(err) => {
if (err) {
throw Error("error writing mount/csc.env, " + err);
}
}
);
// fs.appendFileSync(
// "mount/csc.env",
// `\nLITE_CSC=${config.liteCSC}\n`,
// "utf-8",
// (err) => {
// if (err) {
// throw Error("error writing mount/csc.env, " + err);
// }
// }
// );
return {
CHECKPOINT_CONTRACT: `CHECKPOINT_CONTRACT=${config.liteCSC}`
}
}


Expand Down
49 changes: 38 additions & 11 deletions cicd/reversefull.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ const u = require("./util.js");
main();

async function main() {
u.loadContractENV();
console.log("start deploying reverse CSC");
initDeployReverse();
await configureFiles();
deployReverse();
exportReverse();
const contractENV = exportReverse();
for (const [key, value] of Object.entries(contractENV)) {
u.replaceOrAddENV('./mount/contract_deploy.env', key, value)
u.replaceOrAddENV('./mount/common.env', key, value)
}
await setupSubnetWallets();
}

function initDeployReverse() {
Expand Down Expand Up @@ -78,16 +84,19 @@ function exportReverse() {
"SUCCESS deploy reverse csc, please include the following line in your common.env"
);
console.log(`REVERSE_CHECKPOINT_CONTRACT=${config.reverseCSC}\n`);
fs.appendFileSync(
"mount/csc.env",
`\nREVERSE_CSC=${config.reverseCSC}\n`,
"utf-8",
(err) => {
if (err) {
throw Error("error writing mount/csc.env, " + err);
}
}
);
// fs.appendFileSync(
// "mount/csc.env",
// `\nREVERSE_CSC=${config.reverseCSC}\n`,
// "utf-8",
// (err) => {
// if (err) {
// throw Error("error writing mount/csc.env, " + err);
// }
// }
// );
return {
REVERSE_CHECKPOINT_CONTRACT: `REVERSE_CHECKPOINT_CONTRACT=${config.reverseCSC}`
}
}

function parseReverseOut(outString) {
Expand Down Expand Up @@ -132,3 +141,21 @@ function writeReverseDeployJson(v2esbn) {
// "gsbn": 1500751
// },
}

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(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, 1000000)
}
120 changes: 118 additions & 2 deletions cicd/util.js
Original file line number Diff line number Diff line change
@@ -1,8 +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");
const axios = require("axios");

Expand Down Expand Up @@ -116,10 +115,127 @@ async function getEpochParentnet(config) {
return epochBlockNum
}


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)
}

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 = {
callExec,
writeEnv,
writeNetworkJson,
getGapSubnet,
getEpochParentnet,
loadContractENV,
loadCommonENV,
replaceENV,
addENV,
replaceOrAddENV,
transferTokens,
};

0 comments on commit 9d812dd

Please sign in to comment.