Skip to content

Commit

Permalink
Merge pull request #725 from eco-stake/handle-unknown-networks
Browse files Browse the repository at this point in the history
Handle unknown networks
  • Loading branch information
tombeynon authored Apr 4, 2023
2 parents f18cc70 + 98df45f commit de58cdd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/autostake/NetworkRunner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ export default class NetworkRunner {
}
})
let grantedAddresses = await mapSync(grantCalls, this.opts.batchQueries, (batch, index) => {
timeStamp('...batch', index + 1)
return this.throttleQuery()
if(!allGrants){
timeStamp('...batch', index + 1)
return this.throttleQuery()
}
})
return _.compact(grantedAddresses.flat())
}
Expand Down Expand Up @@ -314,7 +316,7 @@ export default class NetworkRunner {
}

async throttleQuery(){
if(!this.opts.queryThrottle) return
if(!this.opts.queryThrottle) return

await new Promise(r => setTimeout(r, this.opts.queryThrottle));
}
Expand Down
18 changes: 13 additions & 5 deletions src/autostake/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import EthSigner from '../utils/EthSigner.mjs';

export default function Autostake(mnemonic, opts) {
opts = opts || {}
let failed = false

if (!mnemonic) {
timeStamp('Please provide a MNEMONIC environment variable')
Expand All @@ -26,7 +27,10 @@ export default function Autostake(mnemonic, opts) {
async function run(networkNames, networksOverridePath) {
const networks = getNetworksData(networksOverridePath)
for (const name of networkNames) {
if (name && !networks.map(el => el.name).includes(name)) return timeStamp('Invalid network name:', name)
if (name && !networks.map(el => el.name).includes(name)){
// Assume network will be found in Chain Registry
networks.push({ name, path: name })
}
}
const calls = networks.map(data => {
return async () => {
Expand All @@ -37,7 +41,7 @@ export default function Autostake(mnemonic, opts) {
health.started('⚛')
const results = await runWithRetry(data, health)
const { success, skipped } = results[results.length - 1] || {}
if(!skipped){
if(!skipped && !failed){
health.log(`Autostake ${success ? 'completed' : 'failed'} after ${results.length} attempt(s)`)
results.forEach(({networkRunner, error}, index) => {
health.log(`Attempt ${index + 1}:`)
Expand Down Expand Up @@ -76,7 +80,7 @@ export default function Autostake(mnemonic, opts) {
error = e.message
}
runners.push({ success: networkRunner?.didSucceed(), networkRunner, error, failedAddresses })
if (!networkRunner?.didSucceed() && !networkRunner?.forceFail && retries < maxRetries) {
if (!networkRunner?.didSucceed() && !networkRunner?.forceFail && retries < maxRetries && !failed) {
await logResults(health, networkRunner, error, `Failed attempt ${retries + 1}/${maxRetries + 1}, retrying in 30 seconds...`)
await new Promise(r => setTimeout(r, 30 * 1000));
return await runWithRetry(data, health, retries + 1, runners)
Expand Down Expand Up @@ -111,8 +115,12 @@ export default function Autostake(mnemonic, opts) {
let config = { ...opts }
try {
await network.load()
} catch {
throw new Error('Unable to load network data for', network.name)
} catch(e) {
if(e.response.status === 404){
failed = true
throw new Error(`${network.name} not found in Chain Registry`)
}
throw new Error(`Unable to load network data for ${network.name}`)
}

timeStamp('Loaded', network.prettyName)
Expand Down

0 comments on commit de58cdd

Please sign in to comment.