Check Validators Balance #6
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
name: Check Signet BTC Balance | |
on: | |
workflow_dispatch: | |
inputs: | |
process_type: | |
description: 'Select process type' | |
required: true | |
default: 'check_balance' | |
type: choice | |
options: | |
- check_balance | |
jobs: | |
check-balance: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 # Get full git history to properly check files | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: '16' | |
- name: Install dependencies | |
run: npm install axios | |
- name: Check BTC Balance | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const fs = require('fs'); | |
const axios = require('axios'); | |
const path = require('path'); | |
async function checkBalance(btcAddress) { | |
try { | |
console.log(`Checking balance for BTC address: ${btcAddress}`); | |
const response = await axios.get(`https://mempool.space/signet/api/address/${btcAddress}`); | |
const balanceInBTC = response.data.chain_stats.funded_txo_sum / 100000000; | |
console.log(`Current balance: ${balanceInBTC} BTC`); | |
return balanceInBTC >= 1; | |
} catch (error) { | |
console.error(`Error checking balance: ${error.message}`); | |
throw error; | |
} | |
} | |
// Debug: Print current working directory and list files | |
console.log('Current working directory:', process.cwd()); | |
console.log('Directory contents:', fs.readdirSync('.')); | |
console.log('Validators directory contents:', fs.readdirSync('fiamma-testnet-1/bitvm2-staker-validators')); | |
// Get changed files in PR | |
const { data: files } = await github.rest.pulls.listFiles({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: context.issue.number | |
}); | |
console.log('Changed files in PR:', files.map(f => f.filename)); | |
let validatorFiles = files.filter(file => | |
file.filename.startsWith('fiamma-testnet-1/bitvm2-staker-validators/') && | |
file.filename.endsWith('.json') | |
); | |
console.log(`Found ${validatorFiles.length} validator files to process`); | |
for (const file of validatorFiles) { | |
console.log(`\nProcessing file: ${file.filename}`); | |
try { | |
// Get file content from PR | |
const { data: fileData } = await github.rest.repos.getContent({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
path: file.filename, | |
ref: context.payload.pull_request.head.sha | |
}); | |
const content = Buffer.from(fileData.content, 'base64').toString(); | |
console.log('File content:', content); | |
let validatorData; | |
try { | |
validatorData = JSON.parse(content); | |
} catch (error) { | |
console.error('Failed to parse JSON:', error); | |
core.setFailed(`Invalid JSON in ${file.filename}`); | |
continue; | |
} | |
if (!validatorData.btc_address) { | |
console.error('No BTC address found in validator data'); | |
core.setFailed(`No BTC address found in ${file.filename}`); | |
continue; | |
} | |
console.log(`Checking BTC address: ${validatorData.btc_address}`); | |
const hasEnoughBalance = await checkBalance(validatorData.btc_address); | |
if (!hasEnoughBalance) { | |
core.setFailed(`BTC address ${validatorData.btc_address} does not have minimum required balance of 1 BTC on Signet`); | |
continue; | |
} | |
console.log('✅ Balance check passed'); | |
} catch (error) { | |
console.error(`Error processing ${file.filename}:`, error); | |
core.setFailed(`Failed to process ${file.filename}: ${error.message}`); | |
} | |
} |