Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoice refactoring #319

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/database/entities/Invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export class Invoice {
@Column()
itemId!: string

@Column()
amount!: number
@Column({ type: 'varchar' })
amount!: string

@Column({ nullable: true })
telegramPaymentChargeId!: string
Expand Down
2 changes: 1 addition & 1 deletion src/database/invoice.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const invoiceRepository = AppDataSource.getRepository(Invoice)
export interface InvoiceParams {
tgUserId: number
accountId: number
amount: number
amount: string
itemId: string
currency?: string
}
Expand Down
13 changes: 13 additions & 0 deletions src/database/migrations/1695920334202-ChangeInvoiceAmountType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { type MigrationInterface, type QueryRunner } from 'typeorm'

export class ChangeInvoiceAmountType1695920334202 implements MigrationInterface {
name = 'ChangeInvoiceAmountType1695920334202'

public async up (queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE varchar')
}

public async down (queryRunner: QueryRunner): Promise<void> {
await queryRunner.query('ALTER TABLE "invoice" ALTER COLUMN "amount" TYPE integer')
}
}
1 change: 1 addition & 0 deletions src/database/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class StatsService {
const queryBuilder = logRepository.createQueryBuilder('logs')
.select('distinct(logs.accountId)')
.where(`logs.createdAt >= TO_TIMESTAMP(${dateStart})`)
.orderBy('logs.createdAt desc')
.limit(20)

return await queryBuilder.execute()
Expand Down
21 changes: 11 additions & 10 deletions src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type Account, type TransactionReceipt } from 'web3-core'
import axios from 'axios'
import bn, { BigNumber } from 'bignumber.js'
import config from '../../config'
import { chatService, statsService } from '../../database/services'
import { chatService, invoiceService, statsService } from '../../database/services'
import { type OnCallBackQueryData, type OnMessageContext } from '../types'
import { LRUCache } from 'lru-cache'
import { freeCreditsFeeCounter } from '../../metrics/prometheus'
Expand All @@ -13,6 +13,7 @@ import { sendMessage } from '../open-ai/helpers'
import * as Sentry from '@sentry/node'
import { InlineKeyboard } from 'grammy'
import { Callbacks } from '../types'
import { type InvoiceParams } from '../../database/invoice.service'

interface CoinGeckoResponse {
harmony: {
Expand Down Expand Up @@ -66,17 +67,17 @@ export class BotPayments {
userAccount: Account,
amount: BigNumber
): Promise<void> {
// const invoiceData: InvoiceParams = {
// tgUserId: accountId,
// accountId,
// amount: this.convertBigNumber(amount),
// itemId: 'deposit_one',
// currency: 'ONE'
// }
// const invoice = await invoiceService.create(invoiceData)
const invoiceData: InvoiceParams = {
tgUserId: accountId,
accountId,
amount: amount.toFixed(),
itemId: 'deposit_one',
currency: 'ONE'
}
const invoice = await invoiceService.create(invoiceData)
await this.transferFunds(userAccount, this.holderAddress, amount)
await chatService.depositOneCredits(accountId, amount.toFixed())
// await invoiceService.setSuccessStatus({ uuid: invoice.uuid, providerPaymentChargeId: '', telegramPaymentChargeId: '' })
await invoiceService.setSuccessStatus({ uuid: invoice.uuid, providerPaymentChargeId: '', telegramPaymentChargeId: '' })
}

private async runHotWalletsTask (): Promise<void> {
Expand Down
6 changes: 3 additions & 3 deletions src/modules/telegram_payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ export class TelegramPayments {
const providerPaymentChargeId = provider_payment_charge_id

const invoice = await invoiceService.setSuccessStatus({ uuid, telegramPaymentChargeId, providerPaymentChargeId })
const fiatCredits = await this.payments.getPriceInONE(invoice.amount)
const fiatCredits = await this.payments.getPriceInONE(+invoice.amount)

await chatService.depositFiatCredits(invoice.accountId, fiatCredits.toString())

this.logger.info(`Payment from @${ctx.message.from.username} $${invoice.amount / 100} was completed!`)
this.logger.info(`Payment from @${ctx.message.from.username} $${+invoice.amount / 100} was completed!`)
}

public async createPaymentInvoice (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> {
Expand Down Expand Up @@ -133,7 +133,7 @@ export class TelegramPayments {
amount
}]

const invoice = await invoiceService.create({ tgUserId, accountId, itemId, amount })
const invoice = await invoiceService.create({ tgUserId, accountId, itemId, amount: amount.toString() })
const payload = JSON.stringify({ uuid: invoice.uuid })
this.logger.info(`Send invoice: ${JSON.stringify({ tgUserId, accountId, itemId, amount })}`)
// const photoUrl = 'https://pbs.twimg.com/media/F5SofMsbgAApd2Y?format=png&name=small'
Expand Down