Skip to content

Commit

Permalink
Add custom TOML writer to handle numbers for our use-case
Browse files Browse the repository at this point in the history
  • Loading branch information
HDauven committed Dec 27, 2024
1 parent 194a54d commit 9fe5f07
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
14 changes: 1 addition & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"hardhat": "^2.22.8"
},
"dependencies": {
"@dotenvx/dotenvx": "^1.14.0",
"smol-toml": "^1.3.1"
"@dotenvx/dotenvx": "^1.14.0"
}
}
}
32 changes: 27 additions & 5 deletions scripts/genesisEvents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require('@dotenvx/dotenvx').config();
const { ethers } = require("ethers");
const toml = require("smol-toml");
const fs = require("fs");

// Chain metadata to combine both calls to Ethereum and BSC
Expand Down Expand Up @@ -93,6 +92,29 @@ async function fetchEvents(chain) {
return { stakeEntries: mergedStakeEntries, moonlightEntries: mergedMoonlightEntries };
}

// Custom TOML writer to handle our number formatting
function writeTOML(data) {
let tomlContent = "";

if (data.stake) {
data.stake.forEach((entry, index) => {
tomlContent += "[[stake]]\n";
tomlContent += `address = '${entry.address}'\n`;
tomlContent += `amount = ${entry.amount}\n\n`;
});
}

if (data.moonlight_account) {
data.moonlight_account.forEach((entry) => {
tomlContent += "[[moonlight_account]]\n";
tomlContent += `address = '${entry.address}'\n`;
tomlContent += `balance = ${entry.balance}\n\n`;
});
}

return tomlContent.trim();
}

async function main() {
let allStakeEntries = [];
let allMoonlightEntries = [];
Expand All @@ -115,11 +137,11 @@ async function main() {
moonlight_account: allMoonlightEntries,
};

// Convert to TOML format
const genesisToml = toml.stringify(genesisData);
// Generate TOML content
const tomlContent = writeTOML(genesisData);

// Write events to genesis.toml
fs.writeFileSync("genesis.toml", genesisToml);
// Write TOML content to a file
fs.writeFileSync("genesis.toml", tomlContent);
console.log("Generated genesis.toml file.");
}

Expand Down

0 comments on commit 9fe5f07

Please sign in to comment.