-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from dusk-network/genesis-event-scripts
Genesis event scripts
- Loading branch information
Showing
5 changed files
with
174 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
PROVIDER_URL="https://sepolia.infura.io/v3/" | ||
DUSK_MIGRATION_CONTRACT_ADDRESS="0x" | ||
|
||
ETH_MAINNET_PROVIDER_URL="https://mainnet.infura.io/v3/" | ||
ETH_ONRAMP_CONTRACT_ADDRESS="0x8787BbE53920B33411F7C9A91Ac321AF1ea1aa2d" | ||
ETH_ONRAMP_DEPLOY_BLOCK="21445561" | ||
|
||
BSC_MAINNET_PROVIDER_URL="https://bsc-mainnet.infura.io/v3/" | ||
BSC_ONRAMP_CONTRACT_ADDRESS="0x3886ab688feBfF60cE21E99251035F8E29Abca31" | ||
BSC_ONRAMP_DEPLOY_BLOCK="45046348" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
require('@dotenvx/dotenvx').config(); | ||
const { ethers } = require("ethers"); | ||
const fs = require("fs"); | ||
|
||
// Chain metadata to combine both calls to Ethereum and BSC | ||
const chains = [ | ||
{ | ||
name: "Ethereum", | ||
rpcUrl: process.env.ETH_MAINNET_PROVIDER_URL, | ||
contractAddress: process.env.ETH_ONRAMP_CONTRACT_ADDRESS, | ||
startBlock: process.env.ETH_ONRAMP_DEPLOY_BLOCK | ||
}, | ||
{ | ||
name: "Binance Smart Chain", | ||
rpcUrl: process.env.BSC_MAINNET_PROVIDER_URL, | ||
contractAddress: process.env.BSC_ONRAMP_CONTRACT_ADDRESS, | ||
startBlock: process.env.BSC_ONRAMP_DEPLOY_BLOCK | ||
}, | ||
]; | ||
|
||
// Genesis event ABIs | ||
const contractABI = [ | ||
"event GenesisDeposit(address indexed from, uint256 amount, string targetAddress)", | ||
"event GenesisStake(address indexed from, uint256 amount, string targetAddress)" | ||
]; | ||
|
||
// Function to merge entries based on events. | ||
// For example, to prevent two stake entries for the same key | ||
function mergeEntries(entries, valueKey) { | ||
const merged = {}; | ||
|
||
entries.forEach(({ address, [valueKey]: value }) => { | ||
const numericValue = BigInt(value.replace(/_/g, "")); | ||
if (!merged[address]) { | ||
merged[address] = numericValue; | ||
} else { | ||
merged[address] += numericValue; | ||
} | ||
}); | ||
|
||
// Convert back to the required format | ||
return Object.entries(merged).map(([address, value]) => ({ | ||
address, | ||
[valueKey]: value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "_"), | ||
})); | ||
} | ||
|
||
// Function to fetch events for a given chain configuration of the Onramp contract | ||
async function fetchEvents(chain) { | ||
const provider = new ethers.JsonRpcProvider(chain.rpcUrl); | ||
const contract = new ethers.Contract(chain.contractAddress, contractABI, provider); | ||
|
||
const fromBlock = chain.startBlock; | ||
const toBlock = "latest"; | ||
|
||
let stakeEntries = []; | ||
let moonlightEntries = []; | ||
|
||
try { | ||
// Fetch GenesisDeposit events | ||
const depositFilter = contract.filters.GenesisDeposit(); | ||
const depositEvents = await contract.queryFilter(depositFilter, BigInt(fromBlock), toBlock); | ||
|
||
// Fetch GenesisStake events | ||
const stakeFilter = contract.filters.GenesisStake(); | ||
const stakeEvents = await contract.queryFilter(stakeFilter, BigInt(fromBlock), toBlock); | ||
|
||
// Process GenesisDeposit events | ||
depositEvents.forEach((event) => { | ||
moonlightEntries.push({ | ||
address: event.args.targetAddress, | ||
balance: parseInt(event.args.amount.toString(), 10).toLocaleString("en-US").replace(/,/g, "_"), | ||
}); | ||
}); | ||
|
||
// Process GenesisStake events | ||
stakeEvents.forEach((event) => { | ||
stakeEntries.push({ | ||
address: event.args.targetAddress, | ||
amount: parseInt(event.args.amount.toString(), 10).toLocaleString("en-US").replace(/,/g, "_"), | ||
}); | ||
}); | ||
|
||
} catch (error) { | ||
console.error(`Error fetching events on ${chain.name}:`, error); | ||
} | ||
|
||
// Merge duplicate entries on a per event basis | ||
const mergedStakeEntries = mergeEntries(stakeEntries, 'amount'); | ||
const mergedMoonlightEntries = mergeEntries(moonlightEntries, 'balance'); | ||
|
||
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 = []; | ||
|
||
// Collect all GenesisDeposit and GenesisStake events for each chain config | ||
for (const chain of chains) { | ||
console.log(`Fetching events on ${chain.name}...`); | ||
const { stakeEntries, moonlightEntries } = await fetchEvents(chain); | ||
allStakeEntries = allStakeEntries.concat(stakeEntries); | ||
allMoonlightEntries = allMoonlightEntries.concat(moonlightEntries); | ||
} | ||
|
||
// Combine entries across chains to handle duplicate event entries globally | ||
allStakeEntries = mergeEntries(allStakeEntries, 'amount'); | ||
allMoonlightEntries = mergeEntries(allMoonlightEntries, 'balance'); | ||
|
||
// Create genesis data structure | ||
const genesisData = { | ||
stake: allStakeEntries, | ||
moonlight_account: allMoonlightEntries, | ||
}; | ||
|
||
// Generate TOML content | ||
const tomlContent = writeTOML(genesisData); | ||
|
||
// Write TOML content to a file | ||
fs.writeFileSync("genesis.toml", tomlContent); | ||
console.log("Generated genesis.toml file."); | ||
} | ||
|
||
main(); |