Skip to content

Commit

Permalink
Merge pull request #760 from eco-stake/get-addresses-from-grants
Browse files Browse the repository at this point in the history
Avoid delegators query and use all grants if possible
  • Loading branch information
tombeynon authored Dec 1, 2023
2 parents 9c46327 + d00c004 commit 1eecaa9
Showing 1 changed file with 54 additions and 36 deletions.
90 changes: 54 additions & 36 deletions src/autostake/NetworkRunner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,12 @@ export default class NetworkRunner {
timeStamp("Running with options:", this.opts)
this.balance = await this.getBalance() || 0

if (addresses) {
timeStamp("Checking", addresses.length, "addresses for grants...")
} else {
timeStamp('Finding delegators...')
addresses = await this.getDelegations().then(delegations => {
return delegations.map(delegation => {
if (delegation.balance.amount === 0) return

return delegation.delegation.delegator_address
})
})
timeStamp("Checking", addresses.length, "delegators for grants...")
let grantedAddresses = await this.getAddressesFromGrants(addresses)
if(grantedAddresses === false){
timeStamp('All grants query not supported, falling back to checking delegators...')
grantedAddresses = await this.getAddressesFromDelegators(addresses)
}

let grantedAddresses = await this.getGrantedAddresses(addresses)

timeStamp("Found", grantedAddresses.length, "addresses with valid grants...")
if (grantedAddresses.length) {
await this.autostake(grantedAddresses)
Expand All @@ -102,48 +92,77 @@ export default class NetworkRunner {
)
}

getDelegations() {
let timeout = this.opts.delegationsTimeout
let pageSize = this.opts.batchPageSize
return this.queryClient.getAllValidatorDelegations(this.operator.address, pageSize, { timeout }, (pages) => {
timeStamp("...batch", pages.length)
return this.throttleQuery()
}).catch(error => {
throw new Error(`Failed to get delegations: ${error.message || error}`)
})
}

async getGrantedAddresses(addresses) {
async getAddressesFromGrants(addresses) {
const { botAddress, address } = this.operator
let timeout = this.opts.delegationsTimeout
let pageSize = this.opts.batchPageSize
let allGrants
try {
timeStamp('Finding all grants...')
allGrants = await this.queryClient.getGranteeGrants(botAddress, { timeout, pageSize }, (pages) => {
timeStamp("...batch", pages.length)
return this.throttleQuery()
})
} catch (e) { }
let grantCalls = addresses.map(item => {
} catch (error) {
if(error.response?.status === 501){
return false
}else{
throw new Error('Failed to load grants')
}
}
if(addresses){
timeStamp("Checking", addresses.length, "addresses for grants...")
}else{
addresses = allGrants.map(grant => grant.granter)
}
let addressGrants = _.uniq(addresses).map(item => {
return this.parseGrantResponse(allGrants, botAddress, item, address)
})
return _.compact(addressGrants.flat())
}

async getAddressesFromDelegators(addresses) {
if(!addresses){
timeStamp('Finding delegators...')
addresses = await this.getDelegations().then(delegations => {
return delegations.map(delegation => {
if (delegation.balance.amount === 0) return

return delegation.delegation.delegator_address
})
})
timeStamp("Checking", addresses.length, "delegators for grants...")
}else{
timeStamp("Checking", addresses.length, "addresses for grants...")
}
let grantCalls = _.uniq(addresses).map(item => {
return async () => {
if (allGrants) return this.parseGrantResponse(allGrants, botAddress, item, address)
try {
return await this.getGrants(item)
return await this.getGrantsIndividually(item)
} catch (error) {
this.setError(item, `Failed to get grants: ${error.message}`)
}
}
})
let grantedAddresses = await mapSync(grantCalls, this.opts.batchQueries, (batch, index) => {
if(!allGrants){
timeStamp('...batch', index + 1)
return this.throttleQuery()
}
timeStamp('...batch', index + 1)
return this.throttleQuery()
})
return _.compact(grantedAddresses.flat())
}

getGrants(delegatorAddress) {
getDelegations() {
let timeout = this.opts.delegationsTimeout
let pageSize = this.opts.batchPageSize
return this.queryClient.getAllValidatorDelegations(this.operator.address, pageSize, { timeout }, (pages) => {
timeStamp("...batch", pages.length)
return this.throttleQuery()
}).catch(error => {
throw new Error(`Failed to get delegations: ${error.message || error}`)
})
}

getGrantsIndividually(delegatorAddress) {
const { botAddress, address } = this.operator
let timeout = this.opts.queryTimeout
return this.queryClient.getGrants(botAddress, delegatorAddress, { timeout })
Expand All @@ -162,7 +181,6 @@ export default class NetworkRunner {
let grantValidators, maxTokens
if (result.stakeGrant) {
if (result.stakeGrant.authorization['@type'] === "/cosmos.authz.v1beta1.GenericAuthorization") {
timeStamp(delegatorAddress, "Using GenericAuthorization, allowed")
grantValidators = [validatorAddress];
} else {
const { allow_list, deny_list } = result.stakeGrant.authorization
Expand Down

0 comments on commit 1eecaa9

Please sign in to comment.