Skip to content

Commit

Permalink
fix: add pagination, amount calculation for success transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhen-burkovskyi committed Jul 12, 2024
1 parent 39c70b3 commit fddd9ac
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/core/lib/okp4/okp4.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/core/lib/okp4/responses/rewards-history.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { WithPaginationResponse } from "./with-pagination.response";

export type RewardsHistoryResponse = WithPaginationResponse<{
tx_responses: Tx[];
total: string;
}>;


Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
40 changes: 29 additions & 11 deletions src/modules/wallet/wallet.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit fddd9ac

Please sign in to comment.