Skip to content

Commit

Permalink
Ana/fix get transaction success from logs (#554)
Browse files Browse the repository at this point in the history
* fix get transaction success from logs

* fix typo and simplify istxsucceeded logic

* check if claimed message has failed and replace

* move check if claim message failed and replace

* fix everything. all good
  • Loading branch information
Bitcoinera authored Apr 2, 2020
1 parent e389106 commit a45f599
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
60 changes: 20 additions & 40 deletions lib/reducers/cosmosV2-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,27 @@ function unstakeDetailsReducer(message, reducers) {
}
}

function claimRewardsDetailsReducer(
message,
reducers,
transaction,
stakingDenom
) {
function claimRewardsDetailsReducer(message, reducers, transaction) {
return {
from: message.validators,
amounts: claimRewardsAmountReducer(transaction, reducers, stakingDenom)
amounts: claimRewardsAmountReducer(transaction, reducers)
}
}

function claimRewardsAmountReducer(transaction, reducers, stakingDenom) {
const isTxSucceded =
transaction.success ||
transaction.logs.find(({ success }) => success === true)
if (!isTxSucceded) {
return [
{
amount: 0,
denom: reducers.denomLookup(stakingDenom)
}
]
}
const allClaimedRewards = transaction.events
function claimRewardsAmountReducer(transaction, reducers) {
// first collect all the claimed amounts from events
let transactionClaimEvents = transaction.events
.find(event => event.type === `transfer`)
.attributes.filter(attribute => attribute.key === `amount`)
// filter out unsuccessful messages
if (transaction.logs) {
transaction.logs.forEach((log, index) => {
if (log.success !== true) {
transactionClaimEvents.splice(index, 1)
}
})
}
const allClaimedRewards = transactionClaimEvents
.map(amount => amount.value)
.map(rewardValue => reducers.rewardCoinReducer(rewardValue))
const aggregatedClaimRewardsObject = allClaimedRewards.reduce(
Expand Down Expand Up @@ -138,13 +132,7 @@ function depositDetailsReducer(message, reducers) {
}

// function to map cosmos messages to our details format
function transactionDetailsReducer(
type,
message,
reducers,
transaction,
stakingDenom
) {
function transactionDetailsReducer(type, message, reducers, transaction) {
let details
switch (type) {
case lunieMessageTypes.SEND:
Expand All @@ -160,12 +148,7 @@ function transactionDetailsReducer(
details = unstakeDetailsReducer(message, reducers)
break
case lunieMessageTypes.CLAIM_REWARDS:
details = claimRewardsDetailsReducer(
message,
reducers,
transaction,
stakingDenom
)
details = claimRewardsDetailsReducer(message, reducers, transaction)
break
case lunieMessageTypes.SUBMIT_PROPOSAL:
details = submitProposalDetailsReducer(message, reducers)
Expand Down Expand Up @@ -222,7 +205,7 @@ function proposalReducer(
}
}

function transactionReducerV2(transaction, reducers, stakingDenom) {
function transactionReducerV2(transaction, reducers) {
try {
// TODO check if this is anywhere not an array
let fees
Expand Down Expand Up @@ -265,8 +248,7 @@ function transactionReducerV2(transaction, reducers, stakingDenom) {
getMessageType(type),
value,
reducers,
transaction,
stakingDenom
transaction
),
timestamp: transaction.timestamp,
memo: transaction.tx.value.memo,
Expand All @@ -293,15 +275,13 @@ function transactionReducerV2(transaction, reducers, stakingDenom) {
}
}

function transactionsReducerV2(txs, reducers, stakingDenom) {
function transactionsReducerV2(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
return reversedTxs.reduce((collection, transaction) => {
return collection.concat(
transactionReducerV2(transaction, reducers, stakingDenom)
)
return collection.concat(transactionReducerV2(transaction, reducers))
}, [])
}

Expand Down
3 changes: 1 addition & 2 deletions lib/source/cosmosV2-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ class CosmosV2API extends CosmosV0API {
[].concat(...results)
)

const stakingDenom = await this.getStakingDenom()
return this.reducers.transactionsReducerV2(txs, this.reducers, stakingDenom)
return this.reducers.transactionsReducerV2(txs, this.reducers)
}

async getTransactionsByHeight(height) {
Expand Down

0 comments on commit a45f599

Please sign in to comment.