Skip to content

Commit

Permalink
Merge pull request #312 from harmony-one/update_payments_v2
Browse files Browse the repository at this point in the history
Fix numbers formatting for payments v2
  • Loading branch information
ahiipsa authored Sep 27, 2023
2 parents 703a24c + 4cb3e5a commit 424285b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
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

0 comments on commit 424285b

Please sign in to comment.