Skip to content

Commit

Permalink
michiel/remove reducer v0 (#535)
Browse files Browse the repository at this point in the history
* Remove reducer v0

* Update cosmosV0-source.js

* linted

Co-authored-by: Fabian <[email protected]>
  • Loading branch information
michielmulders and faboweb authored Mar 30, 2020
1 parent ab3ead1 commit 49b47b8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 169 deletions.
31 changes: 0 additions & 31 deletions lib/message-types.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
const cosmosMessageType = {
SEND: 'cosmos-sdk/MsgSend',
CREATE_VALIDATOR: 'cosmos-sdk/MsgCreateValidator',
EDIT_VALIDATOR: 'cosmos-sdk/MsgEditValidator',
DELEGATE: 'cosmos-sdk/MsgDelegate',
UNDELEGATE: 'cosmos-sdk/MsgUndelegate',
BEGIN_REDELEGATE: 'cosmos-sdk/MsgBeginRedelegate',
UNJAIL: 'cosmos-sdk/MsgUnjail',
SUBMIT_PROPOSAL: 'cosmos-sdk/MsgSubmitProposal',
DEPOSIT: 'cosmos-sdk/MsgDeposit',
VOTE: 'cosmos-sdk/MsgVote',
SET_WITHDRAW_ADDRESS: 'cosmos-sdk/MsgSetWithdrawAddress',
WITHDRAW_DELEGATION_REWARD: 'cosmos-sdk/MsgWithdrawDelegationReward',
WITHDRAW_VALIDATOR_COMMISSION: 'cosmos-sdk/MsgWithdrawValidatorCommission',
MULTI_SEND: 'cosmos-sdk/MsgMultiSend'
}

const cosmosWhitelistedMessageTypes = new Set([
`MsgSend`,
`MsgDelegate`,
`MsgBeginRedelegate`,
`MsgUndelegate`,
`MsgVote`,
`MsgDeposit`,
`MsgWithdrawDelegationReward`,
`MsgSubmitProposal`,
`MsgMultiSend`
])

const lunieMessageTypes = {
SEND: `SendTx`,
STAKE: `StakeTx`,
Expand All @@ -40,7 +11,5 @@ const lunieMessageTypes = {
}

module.exports = {
cosmosMessageType,
cosmosWhitelistedMessageTypes,
lunieMessageTypes
}
115 changes: 1 addition & 114 deletions lib/reducers/cosmosV0-reducers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const { uniqWith, sortBy, reverse, flatten } = require('lodash')
const { cosmosMessageType } = require('../message-types')
const { cosmosWhitelistedMessageTypes } = require('../../lib/message-types')
const { flatten } = require('lodash')
const BigNumber = require('bignumber.js')
const Sentry = require('@sentry/node')

/**
* Modify the following reducers with care as they are used for ./cosmosV2-reducer.js as well
Expand Down Expand Up @@ -450,56 +447,6 @@ async function totalStakeFiatValueReducer(
)
}

function getGroupByType(transactionType) {
const transactionGroup = {
[cosmosMessageType.SEND]: 'banking',
[cosmosMessageType.MULTI_SEND]: 'banking',
[cosmosMessageType.CREATE_VALIDATOR]: 'staking',
[cosmosMessageType.EDIT_VALIDATOR]: 'staking',
[cosmosMessageType.DELEGATE]: 'staking',
[cosmosMessageType.UNDELEGATE]: 'staking',
[cosmosMessageType.BEGIN_REDELEGATE]: 'staking',
[cosmosMessageType.UNJAIL]: 'staking',
[cosmosMessageType.SUBMIT_PROPOSAL]: 'governance',
[cosmosMessageType.DEPOSIT]: 'governance',
[cosmosMessageType.VOTE]: 'governance',
[cosmosMessageType.SET_WITHDRAW_ADDRESS]: 'distribution',
[cosmosMessageType.WITHDRAW_DELEGATION_REWARD]: 'distribution',
[cosmosMessageType.WITHDRAW_VALIDATOR_COMMISSION]: 'distribution'
}

return transactionGroup[transactionType] || 'unknown'
}

function undelegationEndTimeReducer(transaction) {
if (transaction.tags) {
if (transaction.tags.find(tx => tx.key === `end-time`)) {
return transaction.tags.filter(tx => tx.key === `end-time`)[0].value
}
} else {
return null
}
}

function formatTransactionsReducer(txs, reducers) {
const duplicateFreeTxs = uniqWith(txs, (a, b) => a.txhash === b.txhash)
const sortedTxs = sortBy(duplicateFreeTxs, ['timestamp'])
const reversedTxs = reverse(sortedTxs)
// here we filter out all transactions related to validators
let filteredMsgs = []
reversedTxs.forEach(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 reversedTxs.map(tx => transactionReducer(tx, reducers))
}

function extractInvolvedAddresses(transaction) {
// If the transaction has failed, it doesn't get tagged
if (!Array.isArray(transaction.tags)) return []
Expand All @@ -517,62 +464,6 @@ function extractInvolvedAddresses(transaction) {
return involvedAddresses
}

// to be able to catch all validators from a multi-claim reward tx, we need to capture
// more than just the first value message.
function txMultiClaimRewardReducer(txMessages) {
const filteredMessages = txMessages.filter(
msg => msg.type.split('/')[1] === `MsgWithdrawDelegationReward`
)
return filteredMessages.length > 0 ? filteredMessages : null
}

function transactionReducer(transaction, reducers) {
try {
let fee = coinReducer(false)
if (Array.isArray(transaction.tx.value.fee.amount)) {
fee = coinReducer(transaction.tx.value.fee.amount[0])
} else {
fee = coinReducer(transaction.tx.value.fee.amount)
}

const result = {
type: transaction.tx.value.msg[0].type,
group: getGroupByType(transaction.tx.value.msg[0].type),
hash: transaction.txhash,
height: Number(transaction.height),
timestamp: transaction.timestamp,
gasUsed: transaction.gas_used,
gasWanted: transaction.gas_wanted,
success: transaction.logs ? transaction.logs[0].success : false,
log: transaction.logs
? transaction.logs[0].log
: JSON.parse(transaction.raw_log).message,
memo: transaction.tx.value.memo,
fee,
signature: transaction.tx.value.signatures[0].signature,
value: JSON.stringify(transaction.tx.value.msg[0].value),
raw: transaction,
withdrawValidators: JSON.stringify(
txMultiClaimRewardReducer(transaction.tx.value.msg)
),
undelegationEndTime: reducers.undelegationEndTimeReducer(transaction)
}

return result
} catch (err) {
Sentry.withScope(function(scope) {
scope.setExtra('transaction', {
...transaction,
raw: JSON.stringify(transaction)
})
Sentry.captureException(err)
})
return {
raw: transaction
}
}
}

module.exports = {
proposalReducer,
governanceParameterReducer,
Expand All @@ -584,14 +475,11 @@ module.exports = {
gasPriceReducer,
rewardCoinReducer,
balanceReducer,
transactionReducer,
undelegationReducer,
rewardReducer,
overviewReducer,
accountInfoReducer,
calculateTokens,
undelegationEndTimeReducer,
formatTransactionsReducer,

atoms,
proposalBeginTime,
Expand All @@ -600,7 +488,6 @@ module.exports = {
getTotalVotePercentage,
getValidatorStatus,
expectedRewardsPerToken,
getGroupByType,
denomLookup,
extractInvolvedAddresses
}
2 changes: 0 additions & 2 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,6 @@ const resolvers = {
logOverview(overview, fingerprint)
return overview
},
transactions: (_, { networkId, address, pageNumber }, { dataSources }) =>
remoteFetch(dataSources, networkId).getTransactions(address, pageNumber),
transactionsV2: (_, { networkId, address, pageNumber }, { dataSources }) =>
remoteFetch(dataSources, networkId).getTransactionsV2(
address,
Expand Down
5 changes: 0 additions & 5 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,6 @@ const typeDefs = gql`
operatorAddress: String
fiatCurrency: String
): [Reward]
transactions(
networkId: String!
address: String!
pageNumber: Int
): [Transaction]
transactionsV2(
networkId: String!
address: String!
Expand Down
34 changes: 17 additions & 17 deletions lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,23 +511,6 @@ class CosmosV0API extends RESTDataSource {
)
}

async loadPaginatedTxs(url, page = 1, totalAmount = 0) {
const pagination = `&limit=1000000000&page=${page}`
let allTxs = []

const { txs, total_count } = await this.getRetry(`${url}${pagination}`)
allTxs = allTxs.concat(txs)

// 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)
)
}

return allTxs
}

async getTransactions(address) {
this.checkAddress(address)

Expand All @@ -551,6 +534,23 @@ class CosmosV0API extends RESTDataSource {
]).then(transactionGroups => [].concat(...transactionGroups))
return this.reducers.formatTransactionsReducer(txs, this.reducers)
}

async loadPaginatedTxs(url, page = 1, totalAmount = 0) {
const pagination = `&limit=1000000000&page=${page}`
let allTxs = []

const { txs, total_count } = await this.getRetry(`${url}${pagination}`)
allTxs = allTxs.concat(txs)

// 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)
)
}

return allTxs
}
}

module.exports = CosmosV0API

0 comments on commit 49b47b8

Please sign in to comment.