From 28679089ea8349d4acaa02ba71ff746cfed4fc47 Mon Sep 17 00:00:00 2001 From: Ivanov N Date: Thu, 26 Dec 2024 18:44:53 +0300 Subject: [PATCH 1/4] fix: bug kly amount more than n characters --- src/components/SendFundsForm.vue | 9 +++++- src/lib/klayr/klayr-utils.ts | 53 +++++++++++++++++--------------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/components/SendFundsForm.vue b/src/components/SendFundsForm.vue index bd624e61e..32382795b 100644 --- a/src/components/SendFundsForm.vue +++ b/src/components/SendFundsForm.vue @@ -527,7 +527,14 @@ export default { () => isErc20(this.currency) ? this.ethBalance >= this.transferFee || this.$t('transfer.error_not_enough_eth_fee') - : true + : true, + (v) => { + const isKlyTransfer = this.currency === Cryptos.KLY + if (!isKlyTransfer) return true + const MAX_UINT64 = BigInt('18446744073709551615') + const isKlyTransferAllowed = isKlyTransfer && this.transferFee && v < MAX_UINT64 + return isKlyTransferAllowed || this.$t('transfer.error_incorrect_amount') + } ] } }, diff --git a/src/lib/klayr/klayr-utils.ts b/src/lib/klayr/klayr-utils.ts index 8f4a521a9..05f7eccbf 100644 --- a/src/lib/klayr/klayr-utils.ts +++ b/src/lib/klayr/klayr-utils.ts @@ -232,32 +232,37 @@ type EstimateFeeParams = { * @param params Transaction params */ export function estimateFee(params?: EstimateFeeParams) { - const { - amount = '1', - keyPair = KLY_DEMO_ACCOUNT.keyPair, - recipientAddress = KLY_DEMO_ACCOUNT.address, - data = '', - nonce = 0, - isNewAccount - } = params || {} + try { + const { + amount = '1', + keyPair = KLY_DEMO_ACCOUNT.keyPair, + recipientAddress = KLY_DEMO_ACCOUNT.address, + data = '', + nonce = 0, + isNewAccount + } = params || {} - const transaction = createTransaction( - { - publicKey: Buffer.from(keyPair.publicKey, 'hex'), - secretKey: Buffer.from(keyPair.secretKey, 'hex') - }, - recipientAddress, - amount, - 1, - nonce, - data - ) - const transactionBytes = hexToBytes(transaction.hex) + const transaction = createTransaction( + { + publicKey: Buffer.from(keyPair.publicKey, 'hex'), + secretKey: Buffer.from(keyPair.secretKey, 'hex') + }, + recipientAddress, + amount, + 1, + nonce, + data + ) + const transactionBytes = hexToBytes(transaction.hex) - const fee = BigInt(transactionBytes.length) * KLY_MIN_FEE_PER_BYTE - const transferToNewAccountFee = isNewAccount ? KLY_TRANSFER_TO_NEW_ACCOUNT_FEE : BigInt(0) + const fee = BigInt(transactionBytes.length) * KLY_MIN_FEE_PER_BYTE + const transferToNewAccountFee = isNewAccount ? KLY_TRANSFER_TO_NEW_ACCOUNT_FEE : BigInt(0) - const totalFee = fee + transferToNewAccountFee + const totalFee = fee + transferToNewAccountFee - return convertBeddowsTokly(totalFee.toString()) + return convertBeddowsTokly(totalFee.toString()) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (err) { + return 0 + } } From fe08d661dcb21eb6a253791f0246fafb72adf77b Mon Sep 17 00:00:00 2001 From: Ivanov N Date: Thu, 26 Dec 2024 22:13:34 +0300 Subject: [PATCH 2/4] fixes per comments --- src/components/SendFundsForm.vue | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/components/SendFundsForm.vue b/src/components/SendFundsForm.vue index 32382795b..141e8a5b4 100644 --- a/src/components/SendFundsForm.vue +++ b/src/components/SendFundsForm.vue @@ -198,6 +198,8 @@ import QrcodeCapture from '@/components/QrcodeCapture.vue' import QrcodeScannerDialog from '@/components/QrcodeScannerDialog.vue' import get from 'lodash/get' import { BigNumber } from 'bignumber.js' +import * as transactions from '@klayr/transactions' +import { KLY_DECIMALS } from '@/lib/klayr/klayr-constants' import { INCREASE_FEE_MULTIPLIER, @@ -532,7 +534,9 @@ export default { const isKlyTransfer = this.currency === Cryptos.KLY if (!isKlyTransfer) return true const MAX_UINT64 = BigInt('18446744073709551615') - const isKlyTransferAllowed = isKlyTransfer && this.transferFee && v < MAX_UINT64 + const isKlyTransferAllowed = + this.transferFee && + transactions.convertklyToBeddows(v.toFixed(KLY_DECIMALS)) < MAX_UINT64 return isKlyTransferAllowed || this.$t('transfer.error_incorrect_amount') } ] From f4141c03de035e07d00bbc41ffd898c299d3b5bf Mon Sep 17 00:00:00 2001 From: Ivanov N Date: Fri, 27 Dec 2024 12:32:19 +0300 Subject: [PATCH 3/4] fix: per comments --- src/components/SendFundsForm.vue | 3 +- src/lib/klayr/klayr-utils.ts | 53 +++++++++++++--------------- src/store/modules/kly/kly-getters.js | 17 +++++---- 3 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/components/SendFundsForm.vue b/src/components/SendFundsForm.vue index 141e8a5b4..53ecdbedb 100644 --- a/src/components/SendFundsForm.vue +++ b/src/components/SendFundsForm.vue @@ -229,6 +229,8 @@ import { isStringEqualCI } from '@/lib/textHelpers' import { formatSendTxError } from '@/lib/txVerify' import { AllCryptos } from '@/lib/constants/cryptos' +import { MAX_UINT64 } from '@klayr/validator' + /** * @returns {string | boolean} */ @@ -533,7 +535,6 @@ export default { (v) => { const isKlyTransfer = this.currency === Cryptos.KLY if (!isKlyTransfer) return true - const MAX_UINT64 = BigInt('18446744073709551615') const isKlyTransferAllowed = this.transferFee && transactions.convertklyToBeddows(v.toFixed(KLY_DECIMALS)) < MAX_UINT64 diff --git a/src/lib/klayr/klayr-utils.ts b/src/lib/klayr/klayr-utils.ts index 05f7eccbf..8f4a521a9 100644 --- a/src/lib/klayr/klayr-utils.ts +++ b/src/lib/klayr/klayr-utils.ts @@ -232,37 +232,32 @@ type EstimateFeeParams = { * @param params Transaction params */ export function estimateFee(params?: EstimateFeeParams) { - try { - const { - amount = '1', - keyPair = KLY_DEMO_ACCOUNT.keyPair, - recipientAddress = KLY_DEMO_ACCOUNT.address, - data = '', - nonce = 0, - isNewAccount - } = params || {} + const { + amount = '1', + keyPair = KLY_DEMO_ACCOUNT.keyPair, + recipientAddress = KLY_DEMO_ACCOUNT.address, + data = '', + nonce = 0, + isNewAccount + } = params || {} - const transaction = createTransaction( - { - publicKey: Buffer.from(keyPair.publicKey, 'hex'), - secretKey: Buffer.from(keyPair.secretKey, 'hex') - }, - recipientAddress, - amount, - 1, - nonce, - data - ) - const transactionBytes = hexToBytes(transaction.hex) + const transaction = createTransaction( + { + publicKey: Buffer.from(keyPair.publicKey, 'hex'), + secretKey: Buffer.from(keyPair.secretKey, 'hex') + }, + recipientAddress, + amount, + 1, + nonce, + data + ) + const transactionBytes = hexToBytes(transaction.hex) - const fee = BigInt(transactionBytes.length) * KLY_MIN_FEE_PER_BYTE - const transferToNewAccountFee = isNewAccount ? KLY_TRANSFER_TO_NEW_ACCOUNT_FEE : BigInt(0) + const fee = BigInt(transactionBytes.length) * KLY_MIN_FEE_PER_BYTE + const transferToNewAccountFee = isNewAccount ? KLY_TRANSFER_TO_NEW_ACCOUNT_FEE : BigInt(0) - const totalFee = fee + transferToNewAccountFee + const totalFee = fee + transferToNewAccountFee - return convertBeddowsTokly(totalFee.toString()) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - } catch (err) { - return 0 - } + return convertBeddowsTokly(totalFee.toString()) } diff --git a/src/store/modules/kly/kly-getters.js b/src/store/modules/kly/kly-getters.js index 036de8b9e..f0f3da7ff 100644 --- a/src/store/modules/kly/kly-getters.js +++ b/src/store/modules/kly/kly-getters.js @@ -5,12 +5,17 @@ export default { ...baseGetters, fee: (state) => (amount, recipientAddress, data, isNewAccount) => { - return estimateFee({ - amount, - data, - isNewAccount, - nonce: state.nonce - }) + try { + return estimateFee({ + amount, + data, + isNewAccount, + nonce: state.nonce + }) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + } catch (error) { + return 0 + } }, height(state) { From 0ea3c4b7b7625e0e8537a4342602f7ee18f57324 Mon Sep 17 00:00:00 2001 From: Ivanov N Date: Fri, 27 Dec 2024 13:42:59 +0300 Subject: [PATCH 4/4] fix: per comments 2 --- src/store/modules/kly/kly-getters.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/store/modules/kly/kly-getters.js b/src/store/modules/kly/kly-getters.js index f0f3da7ff..68dc65022 100644 --- a/src/store/modules/kly/kly-getters.js +++ b/src/store/modules/kly/kly-getters.js @@ -12,8 +12,7 @@ export default { isNewAccount, nonce: state.nonce }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - } catch (error) { + } catch { return 0 } },