Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #19 from BitCannaGlobal/fix_txs_listing
Browse files Browse the repository at this point in the history
fix transactions listing
  • Loading branch information
lukitsbrian authored Apr 27, 2021
2 parents 43c56c5 + 970b360 commit a7006a1
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 139 deletions.
68 changes: 34 additions & 34 deletions apis/cosmos-reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,24 +255,23 @@ const proposalTypeEnumDictionary = {

// map Cosmos SDK message types to Lunie message types
export function getMessageType(type) {
// different networks use different prefixes for the transaction types like cosmos/MsgSend vs core/MsgSend in Terra
const transactionTypeSuffix = type.split('/')[1]
switch (transactionTypeSuffix) {
case 'MsgSend':
case 'cosmos.bank.v1beta1.MsgSend':
return lunieMessageTypes.SEND
case 'MsgDelegate':
case 'cosmos.staking.v1beta1.MsgDelegate':
return lunieMessageTypes.STAKE
case 'MsgBeginRedelegate':
case 'cosmos.staking.v1beta1.MsgBeginRedelegate':
return lunieMessageTypes.RESTAKE
case 'MsgUndelegate':
case 'cosmos.staking.v1beta1.MsgUndelegate':
return lunieMessageTypes.UNSTAKE
case 'MsgWithdrawDelegationReward':
case 'cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward':
return lunieMessageTypes.CLAIM_REWARDS
case 'MsgSubmitProposal':
case 'cosmos.gov.v1beta1.MsgSubmitProposal':
return lunieMessageTypes.SUBMIT_PROPOSAL
case 'MsgVote':
case 'cosmos.gov.v1beta1.MsgVote':
return lunieMessageTypes.VOTE
case 'MsgDeposit':
case 'cosmos.gov.v1beta1.MsgDeposit':
return lunieMessageTypes.DEPOSIT
default:
return lunieMessageTypes.UNKNOWN
Expand Down Expand Up @@ -319,34 +318,35 @@ export function unstakeDetailsReducer(message) {

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

export function claimRewardsAmountReducer(transaction) {
const transactionClaimEvents =
transaction.events &&
transaction.events.filter((event) => event.type === `transfer`)
if (!transactionClaimEvents) {
return [{ denom: '', amount: 0 }]
}
// filter out unsuccessful messages
if (transaction.logs) {
transaction.logs.forEach((log, index) => {
if (log.success !== true) {
transactionClaimEvents.splice(index, 1)
const transactionClaimEvents = []

transaction.logs.forEach((log) => {
log.events.forEach((event) => {
if (event.type === `transfer`) {
transactionClaimEvents.push(event)
}
})
}
// if transactionClaimEvents is empty after the successful transaction check, we default it
})

if (transactionClaimEvents.length === 0) {
return [{ denom: '', amount: 0 }]
}
const amountAttributes = transactionClaimEvents
.map((tx) => tx.attributes)
.find((attributes) => attributes.length > 0)
.filter((attribute) => attribute.key === `amount`)

const amountAttributes = []
transactionClaimEvents.forEach((event) => {
event.attributes.forEach((attribute) => {
if (attribute.key === `amount`) {
amountAttributes.push(attribute)
}
})
})

const allClaimedRewards = amountAttributes
.map((amount) => amount.value)
.map((rewardValue) => rewardCoinReducer(rewardValue))
Expand Down Expand Up @@ -430,10 +430,10 @@ export function transactionDetailsReducer(type, message, transaction) {
export function claimRewardsMessagesAggregator(claimMessages) {
// reduce all withdraw messages to one one collecting the validators from all the messages
const onlyValidatorsAddressesArray = claimMessages.map(
(msg) => msg.value.validator_address
(msg) => msg.validator_address
)
return {
type: `xxx/MsgWithdrawDelegationReward`, // prefix omited as not important
'@type': `/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward`, // prefix omited as not important
value: {
validators: onlyValidatorsAddressesArray,
},
Expand Down Expand Up @@ -528,7 +528,7 @@ export function transactionReducer(transaction) {
} = transaction.tx.body.messages.reduce(
({ claimMessages, otherMessages }, message) => {
// we need to aggregate all withdraws as we display them together in one transaction
if (getMessageType(message.type) === lunieMessageTypes.CLAIM_REWARDS) {
if (getMessageType(message['@type']) === lunieMessageTypes.CLAIM_REWARDS) {
claimMessages.push(message)
} else {
otherMessages.push(message)
Expand All @@ -547,15 +547,15 @@ export function transactionReducer(transaction) {
? otherMessages.concat(claimMessage) // add aggregated claim message
: otherMessages
const returnedMessages = allMessages.map(
({ value: message, type }, messageIndex) => ({
(message, messageIndex) => ({
id: transaction.txhash,
type: getMessageType(type),
type: getMessageType(message['@type']),
hash: transaction.txhash,
networkId: network.id,
key: `${transaction.txhash}_${messageIndex}`,
height: transaction.height,
details: transactionDetailsReducer(
getMessageType(type),
getMessageType(message['@type']),
message,
transaction
),
Expand All @@ -570,7 +570,7 @@ export function transactionReducer(transaction) {
).events
),
rawMessage: {
type,
// type,
message,
},
})
Expand Down
97 changes: 48 additions & 49 deletions apis/cosmos-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,52 +96,51 @@ export default class CosmosAPI {
return slashingParams.params.signed_blocks_window
}

async getTransactions(address, pageNumber = 0) {
// // getting page count
// const [senderPage, recipientPage] = await Promise.all([
// this.getPageCount(`/cosmos/tx/v1beta1/txs?message.sender=${address}`),
// this.getPageCount(`/cosmos/tx/v1beta1/txs?transfer.recipient=${address}`),
// ])

// const requests = [
// this.loadPaginatedTxs(
// `/cosmos/tx/v1beta1/txs?message.sender=${address}`,
// senderPage - pageNumber
// ),
// this.loadPaginatedTxs(
// `/cosmos/tx/v1beta1/txs?transfer.recipient=${address}`,
// recipientPage - pageNumber
// ),
// ]
async getTransactions(address, pageNumber = 1) {
// getting page count
const [senderPage, recipientPage] = await Promise.all([
this.getPageCount(`/cosmos/tx/v1beta1/txs?events=message.sender='${address}'`),
this.getPageCount(`/cosmos/tx/v1beta1/txs?events=message.action='send'&events=transfer.recipient='${address}'`),
])

const requests = [
this.loadPaginatedTxs(
`/cosmos/tx/v1beta1/txs?events=message.sender='${address}'`,
senderPage - pageNumber + 1
),
this.loadPaginatedTxs(
`/cosmos/tx/v1beta1/txs?events=message.action='send'&events=transfer.recipient='${address}'`,
recipientPage - pageNumber + 1
),
]
// /*
// if it's a first requests we need to load two pages, instead of one,
// cause last page could contain less records than any other (even 1)
// To do this asynchronously we need to do it with Promise.all
// and not wait until last page is loaded
// */
// if (!pageNumber) {
// if (senderPage - pageNumber > 1) {
// requests.push(
// this.loadPaginatedTxs(
// `/cosmos/tx/v1beta1/txs?message.sender=${address}`,
// senderPage - pageNumber - 1
// )
// )
// }
// if (recipientPage - pageNumber > 1) {
// requests.push(
// this.loadPaginatedTxs(
// `/cosmos/tx/v1beta1/txs?transfer.recipient=${address}`,
// recipientPage - pageNumber - 1
// )
// )
// }
// }

// const txs = await Promise.all(requests).then(([...results]) =>
// [].concat(...results)
// )
const txs = await this.axios.get(`https://api.cosmostation.io/v1/account/txs/${address}`)
if (pageNumber === 1) {
if (senderPage - pageNumber > 0) {
requests.push(
this.loadPaginatedTxs(
`/cosmos/tx/v1beta1/txs?events=message.sender='${address}'`,
senderPage - pageNumber
)
)
}
if (recipientPage - pageNumber > 0) {
requests.push(
this.loadPaginatedTxs(
`/cosmos/tx/v1beta1/txs?events=message.action='send'&events=transfer.recipient='${address}'`,
recipientPage - pageNumber
)
)
}
}

const txs = await Promise.all(requests).then(([...results]) =>
[].concat(...results)
)

return this.reducers.transactionsReducer(txs)
}
Expand Down Expand Up @@ -187,7 +186,7 @@ export default class CosmosAPI {
}

return this.reducers.delegationReducer(
selfDelegation,
selfDelegation.delegation_response,
validator,
delegationEnum.ACTIVE
).amount
Expand Down Expand Up @@ -508,7 +507,6 @@ export default class CosmosAPI {
`cosmos/staking/v1beta1/delegations/${address}`
).catch(console.log) || []

console.log(delegations)
return delegations.length ? delegations
.map((delegation) =>
this.reducers.delegationReducer(
Expand Down Expand Up @@ -557,8 +555,7 @@ export default class CosmosAPI {
).catch(() => {
return []
})

return delegations.map((delegation) =>
return delegations.result.map((delegation) =>
this.reducers.delegationReducer(
delegation,
validator,
Expand Down Expand Up @@ -591,14 +588,16 @@ export default class CosmosAPI {
if (page < 1) {
return []
}
const pagination = `&limit=${PAGE_RECORDS_COUNT}&page=${page}`
const { txs } = await this.get(`${url}${pagination}`)
return txs || []

let offset = (page - 1) * PAGE_RECORDS_COUNT
const pagination = `&pagination.limit=${PAGE_RECORDS_COUNT}&pagination.offset=${offset}`
const { tx_responses } = await this.get(`${url}${pagination}`)
return tx_responses || []
}

async getPageCount(url) {
const page = await this.get(url + `&limit=${PAGE_RECORDS_COUNT}`)
return page.page_total
const response = await this.get(url + `&pagination.limit=${PAGE_RECORDS_COUNT}`)
return Math.ceil(response.pagination.total / PAGE_RECORDS_COUNT)
}
}

Expand Down
6 changes: 3 additions & 3 deletions components/address/ImportNameStep.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
type="text"
placeholder="Must have at least 3 characters"
/>
<FormMessage
<CommonFormMessage
v-if="$v.fieldName.$error && !$v.fieldName.required"
name="Name"
type="required"
/>
<FormMessage
<CommonFormMessage
v-if="$v.fieldName.$error && !$v.fieldName.minLength"
name="Name"
type="minLength"
min="3"
/>
<FormMessage
<CommonFormMessage
v-if="$v.fieldName.$error && !$v.fieldName.nameExists"
name="Name"
type="custom"
Expand Down
11 changes: 9 additions & 2 deletions components/address/ImportSeedStep.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
field-id="import-seed"
field-label="Seed Phrase"
>
<CommonFieldSeed
<AddressFieldSeed
id="import-seed"
:value="fieldSeed"
:placeholder="'Must be exactly 12 or 24 words'"
Expand All @@ -33,7 +33,12 @@
</CommonFormGroup>
</div>
<div class="session-footer">
<CommonButton value="Next" type="submit" />
<CommonButton
value="Next"
type="submit"
:disabled="loading"
:loading="loading"
/>
</div>
</CommonForm>
</template>
Expand Down Expand Up @@ -69,6 +74,7 @@ export default {
},
data: () => ({
fieldSeed: undefined,
loading: false,
}),
mounted() {
this.fieldSeed = this.seed
Expand All @@ -77,6 +83,7 @@ export default {
onSubmit() {
this.$v.$touch()
if (this.$v.fieldSeed.$invalid || this.$v.fieldSeed.$invalid) return
this.loading = true
this.$emit('submit', this.fieldSeed)
},
},
Expand Down
8 changes: 4 additions & 4 deletions components/address/NewSeedStep.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<template>
<Form :submit="onSubmit.bind(this)">
<CommonForm :submit="onSubmit.bind(this)">
<h2 class="session-title">Seed Phrase</h2>
<div class="session-main bottom-indent reorder">
<Seed :value="fieldSeed" />
<AddressSeed :value="fieldSeed" />

<CommonFormGroup
:error="$v.fieldWarning.$error"
Expand All @@ -26,7 +26,7 @@
></label
>
</div>
<FormMessage
<CommonFormMessage
v-if="$v.fieldWarning.$error && !$v.fieldWarning.required"
name="Recovery confirmation"
type="required"
Expand All @@ -36,7 +36,7 @@
<div class="session-footer">
<CommonButton value="Next" type="submit" />
</div>
</Form>
</CommonForm>
</template>

<script>
Expand Down
Loading

0 comments on commit a7006a1

Please sign in to comment.