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

Fix numbers formatting for payments v2 #312

Merged
merged 4 commits into from
Sep 27, 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
2 changes: 1 addition & 1 deletion src/database/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ChatService {
const oldValue = new bn(account.oneCreditAmount)
const newValue = new bn(amount).plus(oldValue)

return await chatRepository.update({ accountId }, { oneCreditAmount: newValue.toString() })
return await chatRepository.update({ accountId }, { oneCreditAmount: newValue.toFixed() })
}

public async getUserCredits (
Expand Down
24 changes: 16 additions & 8 deletions src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface CoinGeckoResponse {
}

export class BotPayments {
private readonly hotWallet: Account
private readonly holderAddress = config.payment.holderAddress
private readonly logger: Logger
private readonly web3: Web3
Expand Down Expand Up @@ -50,9 +49,6 @@ export class BotPayments {
} else {
this.logger.info(`Payments holder address: ${this.holderAddress}`)
}

this.hotWallet = this.getUserAccount('hot_wallet') as Account
this.logger.info(`Hot wallet address: ${this.hotWallet.address}`)
}

public bootstrap (): void {
Expand Down Expand Up @@ -119,10 +115,10 @@ export class BotPayments {

if (availableBalance.minus(txFee).gt(0)) {
try {
this.logger.info(`User ${accountId} transfer funds ${availableBalance.toString()} ONE to multisig wallet: ${this.holderAddress}...`)
this.logger.info(`User ${accountId} ${userAccount.address} transfer funds ${availableBalance.toFixed()} ONE to multisig wallet: ${this.holderAddress}...`)
await this.transferUserFundsToHolder(accountId, userAccount, availableBalance)
const { totalCreditsAmount } = await chatService.getUserCredits(accountId)
this.logger.info(`User ${accountId} ${userAccount.address} hot wallet funds "${availableBalance.toString()}" ONE transferred to holder address ${this.holderAddress}. ONE credits balance: ${totalCreditsAmount.toString()}.`)
this.logger.info(`User ${accountId} ${userAccount.address} hot wallet funds "${availableBalance.toFixed()}" ONE transferred to holder address ${this.holderAddress}. ONE credits balance: ${totalCreditsAmount.toString()}.`)
} catch (e) {
Sentry.captureException(e)
this.logger.error(
Expand Down Expand Up @@ -200,8 +196,20 @@ export class BotPayments {
}

private async getTransactionFee (): Promise<bn> {
const gasPrice = await this.web3.eth.getGasPrice()
return bn(gasPrice.toString()).multipliedBy(21000)
const estimatedFee = await this.estimateTransferFee()
return bn(estimatedFee)
}

private async estimateTransferFee (): Promise<number> {
const web3 = new Web3(this.rpcURL)
const gasPrice = await web3.eth.getGasPrice()
const txBody = {
from: this.holderAddress,
to: this.holderAddress,
value: web3.utils.toHex('0')
}
const estimatedGas = await web3.eth.estimateGas(txBody)
return estimatedGas * +gasPrice
}

private async transferFunds (
Expand Down