Skip to content

Commit

Permalink
Ana/handle "address not from this network" error (#263)
Browse files Browse the repository at this point in the history
* add check address function for all queries

* apply suggestions
  • Loading branch information
Bitcoinera authored and faboweb committed Jan 14, 2020
1 parent bbe8d7a commit 20630aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CosmosV0API extends RESTDataSource {
this.baseURL = network.api_url
this.initialize({})
this.networkId = network.id
this.networkTitle = network.title
this.delegatorBech32Prefix = network.bech32_prefix
this.validatorConsensusBech32Prefix = `${network.bech32_prefix}valcons`

Expand Down Expand Up @@ -55,6 +56,14 @@ class CosmosV0API extends RESTDataSource {
return involvedAddresses
}

checkAddress(address) {
if (!address.startsWith(this.delegatorBech32Prefix)) {
throw new UserInputError(
`The address you entered doesn't belong to the ${this.networkTitle} network`
)
}
}

async getTransactionsByHeight(height) {
const txs = await this.get(`txs?tx.height=${height}`)
return Array.isArray(txs)
Expand Down Expand Up @@ -248,6 +257,7 @@ class CosmosV0API extends RESTDataSource {
}

async getDelegatorVote({ proposalId, address }) {
this.checkAddress(address)
const response = await this.query(`gov/proposals/${proposalId}/votes`)
const votes = response || []
const vote = votes.find(({ voter }) => voter === address) || {}
Expand Down Expand Up @@ -275,6 +285,7 @@ class CosmosV0API extends RESTDataSource {
}

async getBalancesFromAddress(address) {
this.checkAddress(address)
const response = await this.query(`bank/balances/${address}`)
const balances = response || []
return balances.map(this.reducers.coinReducer)
Expand All @@ -288,6 +299,7 @@ class CosmosV0API extends RESTDataSource {
}

async getDelegationsForDelegatorAddress(address, validatorsDictionary) {
this.checkAddress(address)
let delegations =
(await this.query(`staking/delegators/${address}/delegations`)) || []

Expand All @@ -309,6 +321,7 @@ class CosmosV0API extends RESTDataSource {
}

async getUndelegationsForDelegatorAddress(address, validatorsDictionary) {
this.checkAddress(address)
let undelegations =
(await this.query(
`staking/delegators/${address}/unbonding_delegations`
Expand Down Expand Up @@ -339,6 +352,7 @@ class CosmosV0API extends RESTDataSource {
}

async getDelegationForValidator(delegatorAddress, validator) {
this.checkAddress(delegatorAddress)
const operatorAddress = validator.operatorAddress
const delegation = await this.query(
`staking/delegators/${delegatorAddress}/delegations/${operatorAddress}`
Expand Down Expand Up @@ -366,6 +380,7 @@ class CosmosV0API extends RESTDataSource {
}

async getRewards(delegatorAddress, validatorsDictionary, delegations = null) {
this.checkAddress(delegatorAddress)
if (!delegations) {
delegations = await this.getDelegationsForDelegatorAddress(
delegatorAddress,
Expand All @@ -389,6 +404,7 @@ class CosmosV0API extends RESTDataSource {
}

async getOverview(delegatorAddress, validatorsDictionary) {
this.checkAddress(delegatorAddress)
const [
balances,
delegations,
Expand Down Expand Up @@ -421,6 +437,7 @@ class CosmosV0API extends RESTDataSource {
}

async getTransactions(address) {
this.checkAddress(address)
const pagination = `&limit=${1000000000}`

const txs = await Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions lib/source/cosmosV2-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CosmosV2API extends CosmosV0API {
}

async getTransactions(address) {
this.checkAddress(address)
const pagination = `&limit=${1000000000}`

const txs = await Promise.all([
Expand Down Expand Up @@ -74,6 +75,7 @@ class CosmosV2API extends CosmosV0API {
}

async getRewards(delegatorAddress, validatorsDictionary) {
this.checkAddress(delegatorAddress)
const result = await this.query(
`distribution/delegators/${delegatorAddress}/rewards`
)
Expand Down
1 change: 1 addition & 0 deletions lib/source/regenV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class RegenV0API extends CosmosV0API {
}

async getTransactions(address) {
this.checkAddress(address)
const pagination = `&limit=${1000000000}`

const txs = await Promise.all([
Expand Down

0 comments on commit 20630aa

Please sign in to comment.