From fddd9ac2613c4b7a448210e8a5e5dbb47d74ed52 Mon Sep 17 00:00:00 2001 From: "yevhen.burkovskyi" Date: Fri, 12 Jul 2024 10:17:17 +0300 Subject: [PATCH] fix: add pagination, amount calculation for success transactions --- src/core/lib/okp4/okp4.service.ts | 4 +- .../responses/rewards-history.response.ts | 9 +++++ .../get-wallet-rewards-history.schema.ts | 4 ++ src/modules/wallet/wallet.service.ts | 40 ++++++++++++++----- 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/core/lib/okp4/okp4.service.ts b/src/core/lib/okp4/okp4.service.ts index 9db1f35..99798f0 100644 --- a/src/core/lib/okp4/okp4.service.ts +++ b/src/core/lib/okp4/okp4.service.ts @@ -317,8 +317,8 @@ export class Okp4Service { if(limit !== undefined && offset !== undefined) { pagination = { - "pagination.offset": offset.toString(), - "pagination.limit": limit.toString(), + "page": offset.toString(), + "limit": limit.toString(), "pagination.count_total": true.toString(), } } diff --git a/src/core/lib/okp4/responses/rewards-history.response.ts b/src/core/lib/okp4/responses/rewards-history.response.ts index 6a87903..22231ac 100644 --- a/src/core/lib/okp4/responses/rewards-history.response.ts +++ b/src/core/lib/okp4/responses/rewards-history.response.ts @@ -2,6 +2,7 @@ import { WithPaginationResponse } from "./with-pagination.response"; export type RewardsHistoryResponse = WithPaginationResponse<{ tx_responses: Tx[]; + total: string; }>; @@ -10,6 +11,14 @@ export interface Tx { code: number; timestamp: string; tx: { + auth_info: { + fee: { + amount: Array<{ + denom: string; + amount: string; + }> + } + }, body: { messages: Array<{ "@type": string diff --git a/src/modules/wallet/schemas/get-wallet-rewards-history.schema.ts b/src/modules/wallet/schemas/get-wallet-rewards-history.schema.ts index 3c7a270..9d160b0 100644 --- a/src/modules/wallet/schemas/get-wallet-rewards-history.schema.ts +++ b/src/modules/wallet/schemas/get-wallet-rewards-history.schema.ts @@ -2,4 +2,8 @@ import * as Joi from "joi"; export const GetWalletRewardsHistorySchema = Joi.object({ address: Joi.string().required(), + limit: Joi.number().optional(), + offset: Joi.number().optional(), }) + .and("limit", "offset") + .required(); diff --git a/src/modules/wallet/wallet.service.ts b/src/modules/wallet/wallet.service.ts index 20efd0c..ea00728 100644 --- a/src/modules/wallet/wallet.service.ts +++ b/src/modules/wallet/wallet.service.ts @@ -6,6 +6,7 @@ import { Tx } from "@core/lib/okp4/responses/rewards-history.response"; import { extractNumbers } from "@utils/exctract-numbers"; import { WalletCache } from "./wallet-cache"; import { hash } from "@utils/create-hash"; +import Big from "big.js"; @Injectable() export class WalletService { @@ -41,28 +42,45 @@ export class WalletService { private async fetchAndCacheRewardsHistory({ address, limit, offset }: GetWalletRewardsHistoryDto) { const res = await this.okp4Service.getWalletRewardsHistory(address, limit, offset); const historyView = res.tx_responses.map(tx => this.walletRewardHistoryView(tx)); - await this.cache.setWalletRewardHistory(hash({ address, limit, offset }), historyView); - return historyView; + const pagination = { + total: +res.total, + limit: limit === undefined ? null : limit, + offset: offset === undefined ? null : offset, + }; + await this.cache.setWalletRewardHistory(hash({ address, limit, offset }), { + history: historyView, + pagination, + }); + return { + history: historyView, + pagination, + }; } private walletRewardHistoryView(tx: Tx) { - const coinSpendEvent = tx.events.find(event => event.type === 'coin_spent'); + const coinSpentAmount = tx.events.reduce((acc, event) => { + if(event.type === 'coin_spent') { + const amountWithDenom = event.attributes.find(attribute => attribute.key === 'amount'); + if(amountWithDenom) { + acc = acc + extractNumbers(amountWithDenom?.value)[0]; + } + } + return acc; + }, 0); + const feeAmount = tx.tx.auth_info.fee.amount.reduce((acc, fee) => { + acc = acc + +fee.amount; + return acc; + }, 0); const messages = tx.tx.body.messages.map(message => { const splitted = message["@type"].split('.'); return splitted[splitted.length - 1]; }); - let amount = 0; - - if(coinSpendEvent) { - const amountAttribute = coinSpendEvent.attributes.find(attribute => attribute.key === 'amount'); - amountAttribute && (amount = extractNumbers(amountAttribute?.value)[0]); - } return { txHash: tx.txhash, - result: tx.code ? 'Success' : 'Failed', + result: tx.code ? 'Failed' : 'Success', messages, - amount, + amount: Big(coinSpentAmount).minus(feeAmount).div(1_000_000).toString(), time: tx.timestamp } }