From b9a6630650c278e021ece6b26d59ce977b273ec1 Mon Sep 17 00:00:00 2001 From: fiamma-builder Date: Fri, 15 Nov 2024 14:41:33 +0800 Subject: [PATCH] ci: update pull request ci check --- .github/workflows/check-existing-prs.yml | 2 +- .../scripts/send-fiamma-tokens.js | 128 ++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 fiamma-testnet-1/bitvm2-staker-validators/scripts/send-fiamma-tokens.js diff --git a/.github/workflows/check-existing-prs.yml b/.github/workflows/check-existing-prs.yml index 95ffdad..f0f2722 100644 --- a/.github/workflows/check-existing-prs.yml +++ b/.github/workflows/check-existing-prs.yml @@ -68,7 +68,7 @@ jobs: let validatorFileFound = false; for (const file of files.data) { - if (file.filename.startsWith('validators/')) { + if (file.filename.startsWith('bitvm2-staker-validators/')) { validatorFileFound = true; console.log(`Found validator file: ${file.filename}`); diff --git a/fiamma-testnet-1/bitvm2-staker-validators/scripts/send-fiamma-tokens.js b/fiamma-testnet-1/bitvm2-staker-validators/scripts/send-fiamma-tokens.js new file mode 100644 index 0000000..3977dfe --- /dev/null +++ b/fiamma-testnet-1/bitvm2-staker-validators/scripts/send-fiamma-tokens.js @@ -0,0 +1,128 @@ +const fs = require('fs'); +const path = require('path'); +const { exec } = require('child_process'); +const util = require('util'); +const execAsync = util.promisify(exec); + +// Configuration +const config = { + binary: 'fiammad', + chainId: 'fiamma-testnet', + nodeUrl: 'https://rpc-testnet.fiamma.network:26657', + amount: '1000000ufiamma', // 1 FIAMMA + validatorsDir: path.join(__dirname, '../validators'), + // Get mnemonic from environment variable + mnemonic: process.env.FAUCET_MNEMONIC +}; + +async function setupWallet() { + try { + console.log('šŸ”‘ Setting up wallet...'); + // Recover wallet using mnemonic from environment variable + const command = `echo "${config.mnemonic}" | ${config.binary} keys add faucet --recover --keyring-backend test`; + await execAsync(command); + console.log('āœ… Wallet setup completed'); + } catch (error) { + console.error('āŒ Failed to setup wallet:', error.message); + throw error; + } +} + +async function sendTokens(address) { + try { + const command = `${config.binary} tx bank send \ + faucet \ + ${address} \ + ${config.amount} \ + --chain-id ${config.chainId} \ + --node ${config.nodeUrl} \ + --keyring-backend test \ + --yes`; + + const { stdout } = await execAsync(command); + return stdout; + } catch (error) { + throw new Error(`Failed to send tokens: ${error.message}`); + } +} + +async function processValidators() { + console.log('šŸ“‚ Reading validators directory...'); + + // Create or load send history log + const sendLog = path.join(__dirname, 'token-send-log.json'); + let sentAddresses = {}; + + // Load existing send history if available + if (fs.existsSync(sendLog)) { + sentAddresses = JSON.parse(fs.readFileSync(sendLog, 'utf8')); + } + + // Read all validator files + const files = fs.readdirSync(config.validatorsDir); + + console.log(`Found ${files.length} validator files`); + + for (const file of files) { + if (!file.endsWith('.json')) continue; + + const filePath = path.join(config.validatorsDir, file); + console.log(`\nšŸ“„ Processing ${file}...`); + + try { + const data = JSON.parse(fs.readFileSync(filePath, 'utf8')); + const { fiamma_address } = data; + + if (!fiamma_address) { + console.log(`āš ļø No Fiamma address found in ${file}`); + continue; + } + + // Check if tokens were already sent + if (sentAddresses[fiamma_address]) { + console.log(`ā­ļø Tokens already sent to ${fiamma_address}`); + continue; + } + + console.log(`šŸ’ø Sending tokens to ${fiamma_address}...`); + const result = await sendTokens(fiamma_address); + + // Record successful send + sentAddresses[fiamma_address] = { + file, + timestamp: new Date().toISOString(), + amount: config.amount + }; + + // Update send history log + fs.writeFileSync(sendLog, JSON.stringify(sentAddresses, null, 2)); + + console.log(`āœ… Tokens sent successfully to ${fiamma_address}`); + console.log('Transaction result:', result); + + // Add delay to avoid rate limiting + await new Promise(resolve => setTimeout(resolve, 2000)); + + } catch (error) { + console.error(`āŒ Error processing ${file}:`, error.message); + } + } + + console.log('\nāœ… All validators processed'); +} + +async function main() { + try { + if (!config.mnemonic) { + throw new Error('FAUCET_MNEMONIC environment variable is not set'); + } + + await setupWallet(); + await processValidators(); + } catch (error) { + console.error('āŒ Script failed:', error.message); + process.exit(1); + } +} + +main(); \ No newline at end of file