diff --git a/.github/workflows/development.yml b/.github/workflows/development.yml index cea5618389..4eb92d1b84 100644 --- a/.github/workflows/development.yml +++ b/.github/workflows/development.yml @@ -25,6 +25,6 @@ jobs: - name: Installing Docker Compose run: ssh root@157.245.121.175 "sudo curl -L \"https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose; sudo chmod +x /usr/local/bin/docker-compose" - name: Run on Digital Ocean - run: ssh root@157.245.121.175 "docker image prune -a -f; docker load < lunieapi.tgz;export HASURA_URL="https://staging-db.lunie.io/v1/graphql"; export HASURA_ADMIN_KEY="${{ secrets.LUNIE_STAGING_DB_KEY }}"; export SENTRY_DSN="${{ secrets.SENTRY_DSN }}"; docker stack deploy -c docker-compose.yml lunieapi" + run: ssh root@157.245.121.175 "docker image prune -a -f; docker load < lunieapi.tgz;export HASURA_URL="https://staging-db.lunie.io/v1/graphql"; export HASURA_ADMIN_KEY="${{ secrets.LUNIE_STAGING_DB_KEY }}"; export SENTRY_DSN="${{ secrets.SENTRY_DSN }}"; docker stack deploy -c docker-compose.yml lunieapi; docker service update --image lunieapi:latest --force lunieapi_lunieapi" - name: Setting up cron job for pm2 metrics export run: ssh root@157.245.121.175 "mkdir /logs -p; touch /logs/show; line='*/1 * * * * docker exec lunieapi pm2 show 0 > /logs/show; perl /root/pm2metrics.pl'; crontab -l | grep -q 'lunieapi pm2' && true || (crontab -l; echo "$line" ) | crontab -" diff --git a/.github/workflows/production.yml b/.github/workflows/production.yml index b00160d3db..f775579143 100644 --- a/.github/workflows/production.yml +++ b/.github/workflows/production.yml @@ -25,6 +25,6 @@ jobs: - name: Installing Docker Compose run: ssh root@167.71.107.214 "sudo curl -L \"https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose; sudo chmod +x /usr/local/bin/docker-compose" - name: Run on Digital Ocean - run: ssh root@167.71.107.214 "docker image prune -a -f; docker load < lunieapi.tgz;export HASURA_URL="https://production-db.lunie.io/v1/graphql"; export HASURA_ADMIN_KEY="${{ secrets.LUNIE_PRODUCTION_DB_KEY }}"; export SENTRY_DSN="${{ secrets.SENTRY_DSN_PRODUCTION }}"; docker stack deploy -c docker-compose.yml lunieapi" + run: ssh root@167.71.107.214 "docker image prune -a -f; docker load < lunieapi.tgz;export HASURA_URL="https://production-db.lunie.io/v1/graphql"; export HASURA_ADMIN_KEY="${{ secrets.LUNIE_PRODUCTION_DB_KEY }}"; export SENTRY_DSN="${{ secrets.SENTRY_DSN_PRODUCTION }}"; docker stack deploy -c docker-compose.yml lunieapi; docker service update --image lunieapi:latest --force lunieapi_lunieapi" - name: Setting up cron job for pm2 metrics export run: ssh root@167.71.107.214 "mkdir /logs -p; touch /logs/show; line='*/1 * * * * docker exec lunieapi pm2 show 0 > /logs/show; perl /root/pm2metrics.pl'; crontab -l | grep -q 'lunieapi pm2' && true || (crontab -l; echo "$line" ) | crontab -" diff --git a/lib/block-listeners/cosmos-node-subscription.js b/lib/block-listeners/cosmos-node-subscription.js index 3941464be7..434014b633 100644 --- a/lib/block-listeners/cosmos-node-subscription.js +++ b/lib/block-listeners/cosmos-node-subscription.js @@ -21,6 +21,7 @@ class CosmosNodeSubscription { this.network = network this.cosmosAPI = new CosmosApiClass(network) this.store = store + this.validators = [] const networkSchemaName = this.network.id.replace(/-/g, '_') this.db = new database(config)(networkSchemaName) this.chainHangup = undefined @@ -120,7 +121,30 @@ class CosmosNodeSubscription { // this adds all the validator addresses to the database so we can easily check in the database which ones have an image and which ones don't async updateDBValidatorProfiles(validators) { - const validatorRows = validators.map(({ operatorAddress, name }) => ({ + // filter only new validators + let newValidators = validators.filter( + validator => + !this.validators.find( + v => + v.address == validator.operatorAddress && v.name == validator.name // in case if validator name was changed + ) + ) + // save all new validators to an array + this.validators = [ + ...this.validators.filter( + ({ operatorAddress }) => + !newValidators.find( + ({ operatorAddress: newValidatorOperatorAddress }) => + newValidatorOperatorAddress === operatorAddress + ) + ), + ...newValidators.map(v => ({ + address: v.operatorAddress, + name: v.name + })) + ] + // update only new onces + const validatorRows = newValidators.map(({ operatorAddress, name }) => ({ operator_address: operatorAddress, name })) diff --git a/lib/controller/transaction/index.js b/lib/controller/transaction/index.js index b18b8c601f..d35af23f9c 100644 --- a/lib/controller/transaction/index.js +++ b/lib/controller/transaction/index.js @@ -189,6 +189,11 @@ async function pollTransactionSuccess( } assertOk(res) + // publishUserTransactionAdded is also done in the block subscription + // but also here as a fallback + // TODO the client might now update twice as it receives the success twice, could be fine though + const transaction = reducers.transactionReducer(res, reducers) + publishUserTransactionAdded(networkId, senderAddress, transaction) } catch (error) { console.error('TX failed:', hash, error) let transaction diff --git a/lib/reducers/cosmosV0-reducers.js b/lib/reducers/cosmosV0-reducers.js index d3f070aac7..bacb3f0617 100644 --- a/lib/reducers/cosmosV0-reducers.js +++ b/lib/reducers/cosmosV0-reducers.js @@ -283,8 +283,28 @@ function coinReducer(coin) { } } -function balanceReducer(coin) { - return coin +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, gasPrices) { + return { + ...coin, + gasPrice: gasPriceReducer( + gasPrices.find(gasPrice => denomLookup(gasPrice.denom) === coin.denom) + ).price + } } function delegationReducer(delegation, validator) { @@ -312,7 +332,7 @@ function undelegationReducer(undelegation, validator) { function rewardReducer(reward, validator) { return { amount: atoms(reward.amount), - denom: reward.denom, + denom: denomLookup(reward.denom), validator } } @@ -401,18 +421,18 @@ function formatTransactionsReducer(txs, reducers) { const sortedTxs = sortBy(duplicateFreeTxs, ['timestamp']) const reversedTxs = reverse(sortedTxs) // here we filter out all transactions related to validators - let filteredTxs = [] + let filteredMsgs = [] reversedTxs.forEach(transaction => { - const index = transaction.tx.value.msg.findIndex(msg => - cosmosWhitelistedMessageTypes.has(msg.type.split('/')[1]) - ) - // only push transactions messages supported by Lunie - if (index !== -1) { - transaction.tx.value.msg = [transaction.tx.value.msg[index]] - filteredTxs.push(transaction) - } + transaction.tx.value.msg.forEach(msg => { + // only push transactions messages supported by Lunie + if (cosmosWhitelistedMessageTypes.has(msg.type.split('/')[1])) { + filteredMsgs.push(msg) + } + }) + transaction.tx.value.msg = filteredMsgs + filteredMsgs = [] }) - return filteredTxs.map(tx => transactionReducer(tx, reducers)) + return reversedTxs.map(tx => transactionReducer(tx, reducers)) } function transactionReducerV2(transaction, reducers) { @@ -635,6 +655,7 @@ module.exports = { blockReducer, delegationReducer, coinReducer, + gasPriceReducer, balanceReducer, transactionReducer, undelegationReducer, diff --git a/lib/reducers/emoneyV0-reducers.js b/lib/reducers/emoneyV0-reducers.js index 6ddbc9c545..b3cffc3fc1 100644 --- a/lib/reducers/emoneyV0-reducers.js +++ b/lib/reducers/emoneyV0-reducers.js @@ -67,54 +67,35 @@ async function expectedRewardsPerToken( inflations, totalBackedValues ) { - const percentTotalStakedNGM = BigNumber(validator.votingPower).times(1000) - const totalNGMStakedToValidator = validator.tokens - const division = BigNumber(percentTotalStakedNGM) + const percentOfAllEligibleStake = validator.votingPower + const totalStakeToValidator = validator.tokens // used to answer the question "How many rewards per ONE token invested?" + const percentOfAllRewardsPerToken = BigNumber(percentOfAllEligibleStake) .times(BigNumber(1).minus(BigNumber(commission))) - .div(BigNumber(totalNGMStakedToValidator)) + .div(BigNumber(totalStakeToValidator)) + // Now we need to multiply each total supply of backed tokens with its corresponding // inflation - let accumulator = BigNumber(0) const totalBackedValueDictionary = _.keyBy(totalBackedValues, 'denom') - - inflations.forEach(inflation => { - accumulator = BigNumber(accumulator) - .plus(accumulator) - .plus( - BigNumber(inflation.inflation) - .times(totalBackedValueDictionary[inflation.denom].amount) - .div(1000000) + const rewardsSumInEur = inflations.reduce((sum, inflation) => { + return BigNumber(sum).plus( + BigNumber(inflation.inflation).times( + totalBackedValueDictionary[inflation.denom].eurValue // we use the eur value to be able to sum up the individual token values ) - }) - const totalBackedValuesTimesInflations = accumulator - // First we calculate the total value of rewards we get for staking one single - // NGM in this particular validator - let delegatorSharePerToken = totalBackedValuesTimesInflations.div(division) - // In testnet some validators have commission at 100% and therefore division is - // equal to 0. Dividing by 0 we get infinity, and we want to make sure that - // we don't display such value - const infinity = new BigNumber(Infinity) - // This is just how BigNumber works. Comparing to 0 and if true, bignumber equals the given parameter. - // Documentation on this method can be found here: https://mikemcl.github.io/bignumber.js/#cmp - delegatorSharePerToken = - delegatorSharePerToken.comparedTo(infinity) === 0 - ? BigNumber(0) - : delegatorSharePerToken.div(1000000) - // Now we get the total net value in EUR of all token's total supplies - let eurAccumulator = 0 - totalBackedValues.forEach(totalBackedValue => { - eurAccumulator += totalBackedValue.eurValue - }) - const totalEURGains = eurAccumulator * delegatorSharePerToken.toNumber() + ) + }, 0) + + // now we calculate the total rewards in eur per token delegated to the validator + const totalEURGainsPerTokenInvested = BigNumber(rewardsSumInEur).times( + percentOfAllRewardsPerToken + ) + // How many NGM tokens can we buy with the total gain in EUR we make in a year's time? // 0.50€ is the price the NGM tokens will be first sold. Therefore, this is the official value // until they reach an exchange const pricePerNGM = 0.5 - const ngmGains = totalEURGains / pricePerNGM - // We divide by 1 because we assume 1 NGM > gain per 1 NGM - const expectedReturns = parseFloat(ngmGains / 1).toFixed(2) - // TODO the FE is the one converting to percentage now, so we need to divide by 100 - return expectedReturns / 100 + const ngmGains = totalEURGainsPerTokenInvested / pricePerNGM + + return ngmGains.toFixed(2) // we don't need more then a precision of 2 } module.exports = { diff --git a/lib/reducers/terraV3-reducers.js b/lib/reducers/terraV3-reducers.js index bb89c4316b..ba2e873456 100644 --- a/lib/reducers/terraV3-reducers.js +++ b/lib/reducers/terraV3-reducers.js @@ -1,6 +1,5 @@ const cosmosV2Reducers = require('./cosmosV2-reducers') -const BigNumber = require('bignumber.js') -const { atoms, denomLookup } = cosmosV2Reducers +const { atoms, denomLookup, gasPriceReducer } = cosmosV2Reducers // Terra has a slightly different structure and needs its own undelegationEndTimeReducer function undelegationEndTimeReducer(transaction) { @@ -25,25 +24,10 @@ 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, + fiatValue: fiatValue || 0, gasPrice: gasPriceReducer( gasPrices.find(gasprice => denomLookup(gasprice.denom) === coin.denom) ).price @@ -62,7 +46,7 @@ function rewardReducer(rewards, validatorsDictionary) { formattedRewards.forEach(({ reward, validator }) => reward.forEach(denomReward => { multiDenomRewardsArray.push({ - denom: denomReward.denom, + denom: denomLookup(denomReward.denom), amount: atoms(denomReward.amount), validator: validator }) diff --git a/lib/source/cosmosV0-source.js b/lib/source/cosmosV0-source.js index 39b67026cb..2d9f0f8514 100644 --- a/lib/source/cosmosV0-source.js +++ b/lib/source/cosmosV0-source.js @@ -5,6 +5,17 @@ const _ = require('lodash') const { encodeB32, decodeB32, pubkeyToAddress } = require('../tools') const { UserInputError } = require('apollo-server') +const gasPrices = [ + { + denom: `uatom`, + price: '0.65e-2' + }, + { + denom: `umuon`, + price: '0.65e-2' + } +] + class CosmosV0API extends RESTDataSource { constructor(network) { super() @@ -14,6 +25,7 @@ class CosmosV0API extends RESTDataSource { this.networkTitle = network.title this.delegatorBech32Prefix = network.address_prefix this.validatorConsensusBech32Prefix = `${network.address_prefix}valcons` + this.gasPrices = gasPrices this.setReducers() } @@ -29,6 +41,15 @@ class CosmosV0API extends RESTDataSource { this.reducers = require('../reducers/cosmosV0-reducers') } + // hacky way to get error text + async getError(url) { + try { + return await this.get(url) + } catch (error) { + return error.extensions.response.body.error + } + } + async getRetry(url, intent = 0) { // check cache size, and flush it if it's bigger than something if ((await this.cache.getTotalSize()) > 100000) { @@ -322,7 +343,7 @@ class CosmosV0API extends RESTDataSource { let balances = response || [] const coins = balances.map(this.reducers.coinReducer) return coins.map(coin => { - return this.reducers.balanceReducer(coin) + return this.reducers.balanceReducer(coin, this.gasPrices) }) } diff --git a/lib/source/cosmosV2-source.js b/lib/source/cosmosV2-source.js index bee3345d3e..958b9231cf 100644 --- a/lib/source/cosmosV2-source.js +++ b/lib/source/cosmosV2-source.js @@ -1,4 +1,5 @@ const CosmosV0API = require('./cosmosV0-source') +const PAGE_RECORDS_COUNT = 100 class CosmosV2API extends CosmosV0API { setReducers() { @@ -16,28 +17,55 @@ class CosmosV2API extends CosmosV0API { } async loadPaginatedTxs(url, page = 1, totalAmount = 0) { - const pagination = `&limit=1000000000&page=${page}` + if (page < 1) { + return [] + } + const pagination = `&limit=${PAGE_RECORDS_COUNT}&page=${page}` let allTxs = [] const { txs, total_count } = await this.getRetry(`${url}${pagination}`) allTxs = allTxs.concat(txs) - + // limitation by totalAmount or by page limit + if (totalAmount !== 0 || page === 1) { + return allTxs + } // there is a bug in page_number in gaia-13007 so we can't use is if (allTxs.length + totalAmount < Number(total_count)) { return allTxs.concat( - await this.loadPaginatedTxs(url, page + 1, totalAmount + allTxs.length) + await this.loadPaginatedTxs(url, page - 1, totalAmount + allTxs.length) ) } return allTxs } + async getPageCount(url) { + // hacky way to get page count — requesting too many pages and fetching the error + let error = await this.getError( + url + `&limit=${PAGE_RECORDS_COUNT}&page=10000000000000` + ) + /* The error message received looks like this: + * { + * error: "TxSearch: response error: RPC error -32603 - Internal error: page should be within [0, 7] range, given 100" + * } + * We are therefor looking for [0, 7]. The latter of the 2 array entries gives us the last page. + */ + const pages = error.match(/\[[0-9]*, ([0-9]*)\]/) + return pages[1] + } + async getTransactions(address) { this.checkAddress(address) + // getting page count + const senderPage = await this.getPageCount(`/txs?message.sender=${address}`) + const recipientPage = await this.getPageCount( + `/txs?transfer.recipient=${address}` + ) + const txs = await Promise.all([ - this.loadPaginatedTxs(`/txs?message.sender=${address}`), - this.loadPaginatedTxs(`/txs?transfer.recipient=${address}`) + this.loadPaginatedTxs(`/txs?message.sender=${address}`, senderPage), + this.loadPaginatedTxs(`/txs?transfer.recipient=${address}`, recipientPage) ]).then(([senderTxs, recipientTxs]) => [].concat(senderTxs, recipientTxs)) return this.reducers.formatTransactionsReducer(txs, this.reducers) diff --git a/lib/source/emoneyV0-source.js b/lib/source/emoneyV0-source.js index 7f7a88fdd4..2d1bbc3994 100644 --- a/lib/source/emoneyV0-source.js +++ b/lib/source/emoneyV0-source.js @@ -6,23 +6,23 @@ const apiURL = `https://api.exchangeratesapi.io/latest?` const gasPrices = [ { denom: 'echf', - price: '0.400000' + price: '0.400' }, { denom: 'eeur', - price: '0.400000' + price: '0.400' }, { denom: 'ejpy', - price: '48.800000' + price: '48.800' }, { denom: 'eusd', - price: '0.440000' + price: '0.440' }, { denom: 'ungm', - price: '0.800000' + price: '0.800' } ] diff --git a/lib/source/terraV3-source.js b/lib/source/terraV3-source.js index 645f405d15..5013ec68e7 100644 --- a/lib/source/terraV3-source.js +++ b/lib/source/terraV3-source.js @@ -9,23 +9,23 @@ const annualProvision = '21700000000000' // 21.7 million in uluna const gasPrices = [ { denom: 'ukrw', - price: '0.091000' + price: '0.091' }, { denom: 'uluna', - price: '0.001000' + price: '0.001' }, { denom: 'umnt', - price: '0.091000' + price: '0.091' }, { denom: 'usdr', - price: '0.091000' + price: '0.091' }, { denom: 'uusd', - price: '0.091000' + price: '0.091' } ]