Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get validators in batches #59

Merged
merged 2 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ COPY package*.json ./
RUN npm install
COPY . ./

ENV NODE_ENV=development

EXPOSE 3000
CMD npm run start
4 changes: 2 additions & 2 deletions scripts/autostake.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class Autostake {
}

getDelegations(client) {
return client.restClient.getAllValidatorDelegations(client.operator.address, 250, (batches, total) => {
console.log("...batch", batches.length)
return client.restClient.getAllValidatorDelegations(client.operator.address, 250, (pages) => {
console.log("...batch", pages.length)
}).catch(error => {
console.log("ERROR:", error)
process.exit()
Expand Down
8 changes: 6 additions & 2 deletions src/components/Delegations.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class Delegations extends React.Component {
}

async componentDidUpdate(prevProps){
if(this.props.network !== prevProps.network){
clearInterval(this.state.refreshInterval);
}

if(!this.props.address) return

if(this.props.address !== prevProps.address){
Expand Down Expand Up @@ -77,7 +81,7 @@ class Delegations extends React.Component {
this.setState({ rewards: rewards });
},
(error) => {
if([404, 500].includes(error.response.status)){
if([404, 500].includes(error.response && error.response.status)){
this.setState({ rewards: {} });
}else{
this.setState({ error: 'Failed to get rewards. Please refresh' });
Expand Down Expand Up @@ -107,7 +111,7 @@ class Delegations extends React.Component {
operatorGrants: _.set(state.operatorGrants, botAddress, operatorGrant)
}))
}, (error) => {
if (error.response.status === 501) {
if (error.response && error.response.status === 501) {
this.setState({ authzMissing: true });
}else{
this.setState({ error: 'Failed to get grants. Please refresh' });
Expand Down
10 changes: 8 additions & 2 deletions src/components/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ class Wallet extends React.Component {
}

componentDidUpdate(prevProps) {
if(this.props.network !== prevProps.network){
clearInterval(this.state.refreshInterval);
}

if(!this.props.address) return

if(this.props.address !== prevProps.address){
clearInterval(this.state.refreshInterval);
this.getDelegations()
this.refreshInterval()
}
Expand All @@ -34,7 +41,6 @@ class Wallet extends React.Component {
}

refreshInterval(){
clearInterval(this.state.refreshInterval);
const interval = setInterval(() => {
this.getDelegations()
}, 30_000)
Expand All @@ -55,7 +61,7 @@ class Wallet extends React.Component {
});
},
(error) => {
if(error.response && [404, 500].includes(error.response.status)){
if([404, 500].includes(error.response && error.response.status)){
this.setState({
isLoaded: true,
delegations: {},
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import reportWebVitals from './utils/reportWebVitals';
Bugsnag.start({
apiKey: '5cda10bb1c98f351cd0b722a1535d8c2',
plugins: [new BugsnagPluginReact()],
enabledReleaseStages: [ 'production', 'staging' ]
enabledReleaseStages: [ 'production', 'staging' ],
releaseStage: process.env.NODE_ENV
})

const ErrorBoundary = Bugsnag.getPlugin('react')
Expand Down
2 changes: 1 addition & 1 deletion src/utils/Network.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Network = async (data) => {
}

const getValidators = () => {
return restClient.getValidators()
return restClient.getAllValidators(150)
}

return {
Expand Down
66 changes: 41 additions & 25 deletions src/utils/RestClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ const RestClient = async (chainId, restUrls) => {

const restUrl = await findAvailableUrl(Array.isArray(restUrls) ? restUrls : [restUrls])

const getValidators = () => {
return axios.get(restUrl + "/cosmos/staking/v1beta1/validators?status=BOND_STATUS_BONDED&pagination.limit=500")
const getAllValidators = (pageSize, pageCallback) => {
return getAllPages((nextKey) => {
return getValidators(pageSize, nextKey)
}, pageCallback).then(pages => {
const validators = _.shuffle(pages.map(el => el.validators).flat())
return validators.reduce((a, v) => ({ ...a, [v.operator_address]: v}), {})
})
}

const getValidators = (pageSize, nextKey) => {
const searchParams = new URLSearchParams()
searchParams.append('status', 'BOND_STATUS_BONDED')
if(pageSize) searchParams.append('pagination.limit', pageSize)
if(nextKey) searchParams.append('pagination.key', nextKey)
return axios.get(restUrl + "/cosmos/staking/v1beta1/validators?" + searchParams.toString())
.then(res => res.data)
.then(
(result) => {
const validators = _.shuffle(result.validators).reduce((a, v) => ({ ...a, [v.operator_address]: v}), {})
return validators
}
)
}

const getBalance = (address, denom) => {
Expand Down Expand Up @@ -82,20 +89,12 @@ const RestClient = async (chainId, restUrls) => {
)
}

const getAllValidatorDelegations = async (validatorAddress, pageSize, pageCallback) => {
let batches = []
let nextKey, error
do {
try {
const result = await getValidatorDelegations(validatorAddress, pageSize, nextKey)
batches.push(result.delegation_responses)
nextKey = result.pagination.next_key
if(pageCallback) pageCallback(batches, result.pagination.total)
} catch (err) {
error = err
}
} while (nextKey && !error)
return batches.flat()
const getAllValidatorDelegations = (validatorAddress, pageSize, pageCallback) => {
return getAllPages((nextKey) => {
return getValidatorDelegations(validatorAddress, pageSize, nextKey)
}, pageCallback).then(pages => {
return pages.map(el => el.delegation_responses).flat()
})
}

const getValidatorDelegations = (validatorAddress, pageSize, nextKey) => {
Expand All @@ -107,6 +106,22 @@ const RestClient = async (chainId, restUrls) => {
.then(res => res.data)
}

const getAllPages = async (getPage, pageCallback) => {
let pages = []
let nextKey, error
do {
try {
const result = await getPage(nextKey)
pages.push(result)
nextKey = result.pagination.next_key
if(pageCallback) pageCallback(pages)
} catch (err) {
error = err
}
} while (nextKey && !error)
return pages
}

function findAvailableUrl(urls){
return findAsync(urls, (url) => {
return axios.get(url + '/node_info', {timeout: 2000})
Expand All @@ -122,13 +137,14 @@ const RestClient = async (chainId, restUrls) => {
return {
connected: !!restUrl,
restUrl,
getAllValidators,
getValidators,
getAllValidatorDelegations,
getValidatorDelegations,
getBalance,
getDelegations,
getRewards,
getGrants,
getAllValidatorDelegations,
getValidatorDelegations
getGrants
}
}

Expand Down
1 change: 0 additions & 1 deletion src/utils/SigningClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const SigningClient = async (rpcUrl, chainId, defaultGasPrice, signer, key) => {
const getFee = (gas, gasPrice) => {
if(!gas) gas = 200_000
if(!gasPrice) gasPrice = GasPrice.fromString(defaultGasPrice);
console.log(gas, gasPrice, defaultGasPrice)
return calculateFee(gas, gasPrice);
}

Expand Down