Skip to content

Commit

Permalink
Fabo/terra returns (#389)
Browse files Browse the repository at this point in the history
* better error logging

* add result selector to query

* split emoney getAllValidators from Terra

* add expected returns querying to terra

* filter out empty returns

* remove not used code

* missing return

* limit decimals

* fix not found in terra testnet

* temporary hacks

* Revert "temporary hacks"

This reverts commit 88d0918af70489422d6d5d84909df1186a23fa1c.

* Revert "fix not found in terra testnet"

This reverts commit 19a4eca6eb4a9eb2050b368b270d4a39847e016a.

* add terra testnet api

* switch to show all decimals

Co-authored-by: Ana G. <[email protected]>
  • Loading branch information
faboweb and Bitcoinera authored Mar 5, 2020
1 parent 713d9db commit bad108c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
4 changes: 2 additions & 2 deletions data/networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ module.exports = [
id: 'terra-testnet',
title: 'Terra Testnet',
chain_id: 'soju-0013',
api_url: 'https://terra-testnet.lunie.io',
api_url: 'https://soju-fcd.terra.dev',
rpc_url: 'wss://terra-testnet.lunie.io/websocket',
bech32_prefix: 'terra',
address_prefix: 'terra',
Expand Down Expand Up @@ -186,7 +186,7 @@ module.exports = [
enabled: true,
icon: 'https://app.lunie.io/img/networks/emoney-testnet.png',
slug: 'emoney-testnet'
},
}
// {
// id: 'polkadot-testnet',
// title: 'Kusama',
Expand Down
2 changes: 1 addition & 1 deletion lib/block-listeners/cosmos-node-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class CosmosNodeSubscription {
})
})
} catch (error) {
console.error('newBlockHandler failed')
console.error('newBlockHandler failed', error)
Sentry.captureException(error)
}
this.cosmosAPI.memoizedResults.clear()
Expand Down
4 changes: 2 additions & 2 deletions lib/source/cosmosV2-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class CosmosV2API extends CosmosV0API {
this.reducers = require('../reducers/cosmosV2-reducers')
}

async query(url) {
async query(url, resultSelector = 'result') {
const response = await this.getRetry(url)
return response.result
return response[resultSelector]
}

async getValidatorSigningInfos() {
Expand Down
9 changes: 9 additions & 0 deletions lib/source/emoneyV0-source.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const TerraV3API = require('./terraV3-source')
const CosmosV0API = require('./cosmosV0-source')
const BigNumber = require('bignumber.js')
const fetch = require('node-fetch')
const apiURL = `https://api.exchangeratesapi.io/latest?`
Expand Down Expand Up @@ -55,6 +56,14 @@ class EMoneyV0API extends TerraV3API {
return await mapTotalBackedValues()
}

getAnnualProvision() {
return 0
}

async getAllValidators() {
return CosmosV0API.prototype.getAllValidators.call(this)
}

async getExpectedReturns(validator) {
const inflations = await this.getTokensInflations()
const totalBackedValues = await this.getTotalBackedValues()
Expand Down
39 changes: 23 additions & 16 deletions lib/source/terraV3-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const BigNumber = require('bignumber.js')
const { keyBy } = require('lodash')
const { pubkeyToAddress } = require('../tools')

// Terra is provisioning this amount manually https://medium.com/terra-money/project-santa-community-initiative-b8ab6c4d79be
const annualProvision = '21700000000000' // 21.7 million in uluna

const gasPrices = [
{
denom: 'ukrw',
Expand Down Expand Up @@ -36,14 +33,20 @@ class TerraV3API extends CosmosV2API {
}

async getAllValidators(height) {
let [validators, validatorSet, signedBlocksWindow] = await Promise.all([
let [
validators,
validatorSet,
signedBlocksWindow,
expectedReturns
] = await Promise.all([
Promise.all([
this.query(`staking/validators?status=unbonding`),
this.query(`staking/validators?status=bonded`),
this.query(`staking/validators?status=unbonded`)
]).then(validatorGroups => [].concat(...validatorGroups)),
this.getAllValidatorSets(height),
this.getSignedBlockWindow()
this.getSignedBlockWindow(),
this.query(`v1/staking`, 'validators')
])

// create a dictionary to reduce array lookups
Expand All @@ -59,6 +62,8 @@ class TerraV3API extends CosmosV2API {
'address'
)

const expectedReturnsMap = keyBy(expectedReturns, 'operatorAddress')

validators.forEach(validator => {
const consensusAddress = pubkeyToAddress(
validator.consensus_pubkey,
Expand All @@ -72,23 +77,25 @@ class TerraV3API extends CosmosV2API {
validator.signing_info = signingInfos[consensusAddress]
})

return validators.map(validator =>
this.reducers.validatorReducer(
return validators.map(validator => {
const lunieValidator = this.reducers.validatorReducer(
this.networkId,
signedBlocksWindow,
validator,
annualProvision
validator
)
)

lunieValidator.expectedReturns = expectedReturnsMap[
lunieValidator.operatorAddress
]
? expectedReturnsMap[lunieValidator.operatorAddress].stakingReturn
: 0

return lunieValidator
})
}

async getExpectedReturns(validator) {
const expectedReturns = this.reducers.expectedRewardsPerToken(
validator,
validator.commission,
annualProvision
)
return expectedReturns
return Number(validator.expectedReturns).toFixed(4)
}

// Terra will be the root source for functions specific to Tendermint multidenom networks
Expand Down

0 comments on commit bad108c

Please sign in to comment.