Skip to content

Commit a35ef9a

Browse files
committed
feat: implement BalanceStore for managing user balances and integrate billing history with enhanced transaction details
1 parent d7cfb82 commit a35ef9a

File tree

14 files changed

+270
-151
lines changed

14 files changed

+270
-151
lines changed

scripts/swagger.yaml

Lines changed: 132 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3460,8 +3460,7 @@ components:
34603460
- type
34613461
- amount
34623462
- currency
3463-
- reason
3464-
- additional_info
3463+
- info
34653464
- created_at
34663465
properties:
34673466
id:
@@ -3480,14 +3479,140 @@ components:
34803479
example: "123000000000"
34813480
currency:
34823481
$ref: '#/components/schemas/CryptoCurrency'
3483-
reason:
3484-
type: string
3485-
additional_info:
3486-
type: string
3482+
info:
3483+
$ref: '#/components/schemas/BillingTxInfo'
34873484
created_at:
34883485
type: integer
34893486
format: int64
34903487
example: 1690889913000
3488+
BillingTxInfo:
3489+
type: object
3490+
required:
3491+
- reason
3492+
# - meta
3493+
# additionalProperties: false
3494+
properties:
3495+
reason:
3496+
type: string
3497+
enum:
3498+
- tonapi_monthly_payment
3499+
- tonapi_instant_payment
3500+
- tonapi_change_tier
3501+
- liteproxy_monthly_payment
3502+
- liteproxy_change_tier
3503+
- testnet_tons_purchase
3504+
- cnft_indexing_payment
3505+
- message_package_purchase
3506+
- promo_code_activation
3507+
- chatgpt_request_payment
3508+
- streaming_api_payment
3509+
- replenishment_of_deposit
3510+
- analytics_request_payment
3511+
- other
3512+
# meta:
3513+
# oneOf:
3514+
# - $ref: '#/components/schemas/TonapiMonthlyPaymentMeta'
3515+
# - $ref: '#/components/schemas/TonapiInstantPaymentMeta'
3516+
# - $ref: '#/components/schemas/TonapiChangeTierMeta'
3517+
# - $ref: '#/components/schemas/LiteproxyMonthlyPaymentMeta'
3518+
# - $ref: '#/components/schemas/LiteproxyChangeTierMeta'
3519+
# - $ref: '#/components/schemas/TestnetTonsPurchaseMeta'
3520+
# - $ref: '#/components/schemas/CnftIndexingPaymentMeta'
3521+
# - $ref: '#/components/schemas/MessagePackagePurchaseMeta'
3522+
# - $ref: '#/components/schemas/PromoCodeActivationMeta'
3523+
# - $ref: '#/components/schemas/StreamingApiPaymentMeta'
3524+
# - $ref: '#/components/schemas/ReplenishmentOfDepositMeta'
3525+
# - $ref: '#/components/schemas/AnalyticsRequestPaymentMeta'
3526+
# - type: object
3527+
# additionalProperties: true
3528+
TonapiMonthlyPaymentMeta:
3529+
type: object
3530+
required:
3531+
- tier
3532+
properties:
3533+
tier:
3534+
$ref: '#/components/schemas/Tier'
3535+
TonapiInstantPaymentMeta:
3536+
type: object
3537+
required:
3538+
- tier
3539+
properties:
3540+
tier:
3541+
$ref: '#/components/schemas/Tier'
3542+
TonapiChangeTierMeta:
3543+
type: object
3544+
required:
3545+
- new_tier
3546+
properties:
3547+
new_tier:
3548+
$ref: '#/components/schemas/Tier'
3549+
LiteproxyMonthlyPaymentMeta:
3550+
type: object
3551+
required:
3552+
- tier
3553+
properties:
3554+
tier:
3555+
$ref: '#/components/schemas/LiteproxyTier'
3556+
LiteproxyChangeTierMeta:
3557+
type: object
3558+
required:
3559+
- new_tier
3560+
properties:
3561+
new_tier:
3562+
$ref: '#/components/schemas/LiteproxyTier'
3563+
TestnetTonsPurchaseMeta:
3564+
type: object
3565+
required:
3566+
- testnet_coins
3567+
properties:
3568+
testnet_coins:
3569+
type: string
3570+
x-js-format: bigint
3571+
CnftIndexingPaymentMeta:
3572+
type: object
3573+
required:
3574+
- collection
3575+
- count
3576+
properties:
3577+
collection:
3578+
type: string
3579+
count:
3580+
type: integer
3581+
MessagePackagePurchaseMeta:
3582+
type: object
3583+
required:
3584+
- count
3585+
properties:
3586+
count:
3587+
type: integer
3588+
PromoCodeActivationMeta:
3589+
type: object
3590+
required:
3591+
- id
3592+
properties:
3593+
id:
3594+
type: string
3595+
StreamingApiPaymentMeta:
3596+
type: object
3597+
required:
3598+
- period
3599+
properties:
3600+
period:
3601+
type: integer
3602+
ReplenishmentOfDepositMeta:
3603+
type: object
3604+
required:
3605+
- tx_hash
3606+
properties:
3607+
tx_hash:
3608+
type: string
3609+
AnalyticsRequestPaymentMeta:
3610+
type: object
3611+
required:
3612+
- time
3613+
properties:
3614+
time:
3615+
type: integer
34913616
Balance:
34923617
type: object
34933618
required:
@@ -4684,6 +4809,7 @@ components:
46844809
schema:
46854810
type: object
46864811
required:
4812+
- history
46874813
- usdt_balance
46884814
properties:
46894815
usdt_balance:
@@ -5509,4 +5635,3 @@ components:
55095635
type: integer
55105636
format: int64
55115637
example: 1000000000
5512-

src/entities/balance/model/balance.store.ts renamed to src/entities/balance/balance.store.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
tonapiMainnet,
1010
toDecimals
1111
} from 'src/shared';
12-
import { ProjectsStore } from '../../project/model/projects.store';
12+
import { ProjectsStore } from '../project/model/projects.store';
1313
import { createStandaloneToast } from '@chakra-ui/react';
1414

1515
const USDT_DECIMALS = 6;
@@ -117,27 +117,27 @@ export class BalanceStore {
117117
amount: BigInt(usdt_balance.amount),
118118
promo_amount: BigInt(usdt_balance.promo_amount)
119119
},
120-
...(ton_balance && { ton: {
121-
amount: BigInt(ton_balance.amount),
122-
promo_amount: BigInt(ton_balance.promo_amount)
123-
} })
120+
...(ton_balance && {
121+
ton: {
122+
amount: BigInt(ton_balance.amount),
123+
promo_amount: BigInt(ton_balance.promo_amount)
124+
}
125+
})
124126
};
125127
});
126128

127-
fetchTonRate = this.tonRate$.createAsyncAction(async () => {
128-
const data = await tonapiMainnet.rates.getRates({
129-
tokens: ['ton'],
130-
currencies: ['usd']
131-
});
132-
133-
return data.rates['TON'].prices?.USD;
134-
});
129+
fetchTonRate = this.tonRate$.createAsyncAction(
130+
(): Promise<number | undefined> =>
131+
tonapiMainnet.rates
132+
.getRates({ tokens: ['ton'], currencies: ['usd'] })
133+
.then(data => data.rates['TON'].prices?.USD)
134+
);
135135

136136
fetchDepositAddress = this.depositAddress$.createAsyncAction(
137137
async (): Promise<RefillAddresses> =>
138138
await apiClient.api
139139
.getDepositAddress(this.projectsStore.selectedProject!.id)
140-
.then(response => response.data as RefillAddresses)
140+
.then(response => response.data)
141141
);
142142

143143
applyPromoCode = createAsyncAction(async (promoCode: string) => {

src/entities/balance/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export * from './ui';
2-
export * from './model';
2+
export * from './balance.store';

src/entities/balance/model/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/entities/balance/model/interfaces/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/entities/balance/model/interfaces/portfolio.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/entities/balance/model/interfaces/refill.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/features/billing/model/billing.store.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@ import {
33
apiClient,
44
createAsyncAction,
55
createImmediateReaction,
6+
CRYPTO_CURRENCY,
67
DTOBillingTransaction,
78
DTOBillingTransactionTypeEnum,
9+
DTOBillingTxInfoReasonEnum,
810
DTOCryptoCurrency,
911
Loadable,
12+
TokenCurrencyAmount,
1013
TonCurrencyAmount,
1114
UsdCurrencyAmount
1215
} from 'src/shared';
13-
import { BillingHistory, BillingHistoryPaymentItem, BillingHistoryRefillItem } from './interfaces';
1416
import { projectsStore, balanceStore } from 'src/shared/stores';
1517

18+
export type BillingHistoryItem = {
19+
id: string;
20+
date: Date;
21+
amount: TonCurrencyAmount | UsdCurrencyAmount;
22+
type: DTOBillingTransactionTypeEnum;
23+
reason: DTOBillingTxInfoReasonEnum;
24+
};
25+
1626
export class BillingStore {
1727
billingHistory$ = new Loadable<DTOBillingTransaction[]>([]);
1828

@@ -30,9 +40,9 @@ export class BillingStore {
3040
return this.billingHistory.length;
3141
}
3242

33-
get billingHistory(): BillingHistory {
43+
get billingHistory(): BillingHistoryItem[] {
3444
return (this.billingHistory$.value || []).map(dtoTx =>
35-
mapDTOTransactionToHistoryItem(dtoTx)
45+
mapDTOTransactionToBillingHistoryItem(dtoTx)
3646
);
3747
}
3848

@@ -105,38 +115,28 @@ export class BillingStore {
105115
}
106116
}
107117

108-
function mapDTOTransactionToHistoryItem(
118+
function mapDTOTransactionToBillingHistoryItem(
109119
dtoTx: DTOBillingTransaction
110-
): BillingHistoryRefillItem | BillingHistoryPaymentItem {
120+
): BillingHistoryItem {
111121
const date = new Date(dtoTx.created_at);
112-
const id = date.getTime();
113122
const amount = mapDTOCurrencyToAmount(dtoTx.currency, dtoTx.amount);
114123

115-
if (dtoTx.type === DTOBillingTransactionTypeEnum.DTODeposit) {
116-
return {
117-
id,
118-
date,
119-
amount,
120-
type: 'deposit' as const,
121-
action: 'refill' as const
122-
};
123-
} else {
124-
return {
125-
id,
126-
date,
127-
amount,
128-
action: 'payment' as const,
129-
name: dtoTx.reason
130-
};
131-
}
124+
return {
125+
id: dtoTx.id,
126+
date,
127+
amount,
128+
type: dtoTx.type,
129+
reason: dtoTx.info.reason,
130+
// meta: dtoTx.info.meta,
131+
};
132132
}
133133

134134
function mapDTOCurrencyToAmount(currency: DTOCryptoCurrency, amount: string) {
135135
switch (currency) {
136136
case DTOCryptoCurrency.DTO_TON:
137137
return new TonCurrencyAmount(amount);
138138
case DTOCryptoCurrency.DTO_USDT:
139-
return new UsdCurrencyAmount(amount);
139+
return new TokenCurrencyAmount({ weiAmount: amount, currency: CRYPTO_CURRENCY.USDT, decimals: 6 });
140140
default:
141141
throw new Error(`Unknown currency: ${currency}`);
142142
}

src/features/billing/model/interfaces/billing-history.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
export type { Payment } from './payment';
2-
export type {
3-
BillingHistory,
4-
BillingHistoryItem,
5-
BillingHistoryPaymentItem,
6-
BillingHistoryRefillItem
7-
} from './billing-history';
81
export type { Subscription } from './subscription';

0 commit comments

Comments
 (0)