From dca2cf0cf55daddcc4f5337be376ae221a92780b Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Wed, 27 Sep 2023 08:34:27 +0100 Subject: [PATCH 1/4] Update numbers formatting in payments v2 --- src/database/chat.service.ts | 2 +- src/modules/payment/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/database/chat.service.ts b/src/database/chat.service.ts index b03f9e9a..5170bb67 100644 --- a/src/database/chat.service.ts +++ b/src/database/chat.service.ts @@ -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 ( diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 514088bc..a3d7f230 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -119,10 +119,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( From da542f4c36a5a8acf7424dba864b6796dbbeac56 Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Wed, 27 Sep 2023 08:45:06 +0100 Subject: [PATCH 2/4] Remove useless common hot wallet address --- src/modules/payment/index.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index a3d7f230..8561d70c 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -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 @@ -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 { From 6f5fa30cc7612840214d92cbf53b3dda70527245 Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Wed, 27 Sep 2023 09:21:02 +0100 Subject: [PATCH 3/4] Estimate transaction fee --- src/modules/payment/index.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 8561d70c..701d48bf 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -196,8 +196,20 @@ export class BotPayments { } private async getTransactionFee (): Promise { - const gasPrice = await this.web3.eth.getGasPrice() - return bn(gasPrice.toString()).multipliedBy(21000) + const estimatedFee = await this.estimateTransferFee() + return bn(estimatedFee) + } + + private async estimateTransferFee() { + 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 ( From 4cb3e5aceea760f85d2352209ce52987aa0d06e8 Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Wed, 27 Sep 2023 09:24:27 +0100 Subject: [PATCH 4/4] Fix linter warnings --- src/modules/payment/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 701d48bf..8e2a7560 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -200,13 +200,13 @@ export class BotPayments { return bn(estimatedFee) } - private async estimateTransferFee() { + private async estimateTransferFee (): Promise { 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'), + value: web3.utils.toHex('0') } const estimatedGas = await web3.eth.estimateGas(txBody) return estimatedGas * +gasPrice