Skip to content

Commit

Permalink
Ana/add only tokens gas prices 2nd attempt (#344)
Browse files Browse the repository at this point in the history
* add terra and emoney gas price. terra reducer

* delete fiatvalue from get balances in cosmos

* add emoney reducer

* hyper important emoney fixes

* fix for emoney denoms

* change gas price to micro units

* add emoney denoms to denomlookup

* transform to microunit also for terra tokens

* return null for emoney gas prices

* add harcoded gas prices

* correct emoney hardcoded values

* hardcoded terra gas prices. not working

* update hardcoded values to working ones

* delete unrelated changes to gas prices

* apply suggestions except coinreducer

* they call me mr coinreducer

* change amount for price. add gas price reducer

* change naming in gas price reducer

* add error message

Co-authored-by: Fabian <[email protected]>
  • Loading branch information
Bitcoinera and faboweb authored Feb 19, 2020
1 parent ec6a9ac commit e93ab67
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 26 deletions.
10 changes: 4 additions & 6 deletions lib/reducers/cosmosV0-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,8 @@ function coinReducer(coin) {
}
}

function balanceReducer(coin, fiatValue) {
return {
...coin,
fiatValue
}
function balanceReducer(coin) {
return coin
}

function delegationReducer(delegation, validator) {
Expand Down Expand Up @@ -656,5 +653,6 @@ module.exports = {
getTotalVotePercentage,
getValidatorStatus,
expectedRewardsPerToken,
getGroupByType
getGroupByType,
denomLookup
}
29 changes: 28 additions & 1 deletion lib/reducers/terraV3-reducers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cosmosV2Reducers = require('./cosmosV2-reducers')
const { atoms } = cosmosV2Reducers
const BigNumber = require('bignumber.js')
const { atoms, denomLookup } = cosmosV2Reducers

// Terra has a slightly different structure and needs its own undelegationEndTimeReducer
function undelegationEndTimeReducer(transaction) {
Expand All @@ -24,6 +25,31 @@ function undelegationEndTimeReducer(transaction) {
}
}

function gasPriceReducer(gasPrice) {
if (!gasPrice) {
throw new Error(
'The token you are trying to request data for is not supported by Lunie.'
)
}

// we want to show only atoms as this is what users know
const denom = denomLookup(gasPrice.denom)
return {
denom: denom,
price: BigNumber(gasPrice.price).div(1000000) // Danger: this might not be the case for all future tokens
}
}

function balanceReducer(coin, fiatValue, gasPrices) {
return {
...coin,
fiatValue,
gasPrice: gasPriceReducer(
gasPrices.find(gasprice => denomLookup(gasprice.denom) === coin.denom)
).price
}
}

function rewardReducer(rewards, validatorsDictionary) {
const formattedRewards = rewards.map(
reward =>
Expand All @@ -47,6 +73,7 @@ function rewardReducer(rewards, validatorsDictionary) {

module.exports = {
...cosmosV2Reducers,
balanceReducer,
rewardReducer,
undelegationEndTimeReducer
}
1 change: 1 addition & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const typeDefs = gql`
denom: String!
amount: String!
fiatValue: String
gasPrice: String
}
type Proposal {
Expand Down
21 changes: 2 additions & 19 deletions lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,30 +316,13 @@ class CosmosV0API extends RESTDataSource {
return this.reducers.blockReducer(this.networkId, block, transactions)
}

async calculateFiatValues(balances, fiatCurrency) {
return Promise.all(
balances.map(balance => ({
denom: this.reducers.coinReducer(balance).denom,
fiatValue: this.calculateFiatValue
? this.calculateFiatValue(balance, fiatCurrency)
: null
}))
)
}

async getBalancesFromAddress(address, fiatCurrency = `EUR`) {
async getBalancesFromAddress(address) {
this.checkAddress(address)
const response = await this.query(`bank/balances/${address}`)
let balances = response || []
const coins = balances.map(this.reducers.coinReducer)
// We calculate the fiatValue field for networks with multiple tokens
// For now, it is just e-Money
const fiatBalances = await this.calculateFiatValues(balances, fiatCurrency)
return coins.map(coin => {
return this.reducers.balanceReducer(
coin,
fiatBalances.find(({ denom }) => denom === coin.denom).fiatValue
)
return this.reducers.balanceReducer(coin)
})
}

Expand Down
24 changes: 24 additions & 0 deletions lib/source/emoneyV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,33 @@ const BigNumber = require('bignumber.js')
const fetch = require('node-fetch')
const apiURL = `https://api.exchangeratesapi.io/latest?`

const gasPrices = [
{
denom: 'echf',
price: '0.400000'
},
{
denom: 'eeur',
price: '0.400000'
},
{
denom: 'ejpy',
price: '48.800000'
},
{
denom: 'eusd',
price: '0.440000'
},
{
denom: 'ungm',
price: '0.800000'
}
]

class EMoneyV0API extends TerraV3API {
setReducers() {
this.reducers = require('../reducers/emoneyV0-reducers')
this.gasPrices = gasPrices
}

// Here we query for the current inflation rates for all the backed tokens
Expand Down
52 changes: 52 additions & 0 deletions lib/source/terraV3-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,33 @@ 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',
price: '0.091000'
},
{
denom: 'uluna',
price: '0.001000'
},
{
denom: 'umnt',
price: '0.091000'
},
{
denom: 'usdr',
price: '0.091000'
},
{
denom: 'uusd',
price: '0.091000'
}
]

class TerraV3API extends CosmosV2API {
setReducers() {
this.reducers = require('../reducers/terraV3-reducers')
this.gasPrices = gasPrices
}

async getAllValidators(height) {
Expand Down Expand Up @@ -78,6 +102,34 @@ class TerraV3API extends CosmosV2API {
)
return this.reducers.rewardReducer(rewards, validatorsDictionary)
}

async calculateFiatValues(balances, fiatCurrency) {
return Promise.all(
balances.map(balance => ({
denom: this.reducers.coinReducer(balance).denom,
fiatValue: this.calculateFiatValue
? this.calculateFiatValue(balance, fiatCurrency)
: null
}))
)
}

async getBalancesFromAddress(address, fiatCurrency = `EUR`) {
this.checkAddress(address)
const response = await this.query(`bank/balances/${address}`)
let balances = response || []
const coins = balances.map(this.reducers.coinReducer)
// We calculate the fiatValue field for networks with multiple tokens
// For now, it is just e-Money (probably also Terra)
const fiatBalances = await this.calculateFiatValues(balances, fiatCurrency)
return coins.map(coin => {
return this.reducers.balanceReducer(
coin,
fiatBalances.find(({ denom }) => denom === coin.denom).fiatValue,
this.gasPrices
)
})
}
}

module.exports = TerraV3API

0 comments on commit e93ab67

Please sign in to comment.