diff --git a/packages/extension/src/providers/bitcoin/libs/utils.ts b/packages/extension/src/providers/bitcoin/libs/utils.ts index 0c72aee8b..42f61c600 100644 --- a/packages/extension/src/providers/bitcoin/libs/utils.ts +++ b/packages/extension/src/providers/bitcoin/libs/utils.ts @@ -1,9 +1,10 @@ -import { BitcoinNetworkInfo } from "../types"; +import { BitcoinNetworkInfo, HaskoinUnspentType } from "../types"; import { address as BTCAddress } from "bitcoinjs-lib"; import { GasPriceTypes } from "@/providers/common/types"; import { fromBase } from "@enkryptcom/utils"; import BigNumber from "bignumber.js"; import { BitcoinNetwork } from "../types/bitcoin-network"; +import { BTCTxInfo } from "../ui/types"; const isAddress = (address: string, network: BitcoinNetworkInfo): boolean => { try { @@ -13,6 +14,26 @@ const isAddress = (address: string, network: BitcoinNetworkInfo): boolean => { return false; } }; + +const getTxInfo = (utxos: HaskoinUnspentType[]): BTCTxInfo => { + const txInfo: BTCTxInfo = { + inputs: [], + outputs: [], + }; + utxos.forEach((u) => { + txInfo.inputs.push({ + hash: u.txid, + index: u.index, + raw: u.raw, + witnessUtxo: { + script: u.pkscript, + value: u.value, + }, + }); + }); + return txInfo; +}; + const getGasCostValues = async ( network: BitcoinNetwork, byteSize: number, @@ -66,4 +87,4 @@ const getGasCostValues = async ( }; return gasCostValues; }; -export { isAddress, getGasCostValues }; +export { isAddress, getGasCostValues, getTxInfo }; diff --git a/packages/extension/src/providers/bitcoin/ui/btc-verify-transaction.vue b/packages/extension/src/providers/bitcoin/ui/btc-verify-transaction.vue index 78f5d2ce1..dfaca4d4c 100644 --- a/packages/extension/src/providers/bitcoin/ui/btc-verify-transaction.vue +++ b/packages/extension/src/providers/bitcoin/ui/btc-verify-transaction.vue @@ -148,7 +148,7 @@ import { calculateSizeBasedOnType } from "./libs/tx-size"; import { getGasCostValues } from "../libs/utils"; import { computed } from "@vue/reactivity"; import { toBN } from "web3-utils"; -import { BTCTxInfo } from "./types"; +import { getTxInfo as getBTCTxInfo } from "../libs/utils"; const isProcessing = ref(false); const isOpenSelectFee = ref(false); @@ -243,21 +243,7 @@ const updateUTXOs = async () => { }; const getTxInfo = () => { - const txInfo: BTCTxInfo = { - inputs: [], - outputs: [], - }; - accountUTXOs.value.forEach((u) => { - txInfo.inputs.push({ - hash: u.txid, - index: u.index, - raw: u.raw, - witnessUtxo: { - script: u.pkscript, - value: u.value, - }, - }); - }); + const txInfo = getBTCTxInfo(accountUTXOs.value); const balance = toBN(TokenBalance.value); const toAmount = toBN(tx.value.value.toString()); const currentFee = toBN( diff --git a/packages/extension/src/providers/bitcoin/ui/send-transaction/index.vue b/packages/extension/src/providers/bitcoin/ui/send-transaction/index.vue index 80182b591..0772a5d98 100644 --- a/packages/extension/src/providers/bitcoin/ui/send-transaction/index.vue +++ b/packages/extension/src/providers/bitcoin/ui/send-transaction/index.vue @@ -51,11 +51,10 @@ v-if="isSendToken" :amount="amount" :fiat-value="selectedAsset.price" - :has-enough-balance="hasEnoughBalance" + :has-enough-balance="!nativeBalanceAfterTransaction.isNeg()" @update:input-amount="inputAmount" @update:input-set-max="setMaxValue" /> -
@@ -134,7 +136,8 @@ import { getGasCostValues, isAddress } from "../../libs/utils"; import BitcoinAPI from "@/providers/bitcoin/libs/api"; import { calculateSizeBasedOnType } from "../libs/tx-size"; import { HaskoinUnspentType } from "../../types"; -import { VerifyTransactionParams, BTCTxInfo } from "../types"; +import { VerifyTransactionParams } from "../types"; +import { getTxInfo as getBTCTxInfo } from "@/providers/bitcoin/libs/utils"; const props = defineProps({ network: { @@ -163,25 +166,6 @@ const selectedAsset = ref(loadingAsset); const amount = ref(""); const accountUTXOs = ref([]); -const hasEnoughBalance = computed(() => { - if (!isValidDecimals(sendAmount.value, selectedAsset.value.decimals!)) { - return false; - } - if (Number(sendAmount.value) < (props.network as BitcoinNetwork).dust) { - return false; - } - return toBN(selectedAsset.value.balance ?? "0").gte( - toBN(toBase(sendAmount.value ?? "0", selectedAsset.value.decimals!)).add( - toBN( - toBase( - gasCostValues.value[selectedFee.value].nativeValue, - selectedAsset.value.decimals! - ) - ) - ) - ); -}); - const sendAmount = computed(() => { if (amount.value && amount.value !== "") return amount.value; return "0"; @@ -196,29 +180,22 @@ const addressFrom = ref( const addressTo = ref(""); const isLoadingAssets = ref(true); -const nativeBalance = computed(() => { - const accountIndex = props.accountInfo.activeAccounts.findIndex( - (acc) => acc.address === addressFrom.value - ); - if (accountIndex !== -1) { - const balance = props.accountInfo.activeBalances[accountIndex]; - if (balance !== "~") { - return toBase(balance, props.network.decimals); - } - } - return "0"; -}); - onMounted(async () => { fetchAssets().then(setBaseCosts); }); const nativeBalanceAfterTransaction = computed(() => { - if (nativeBalance.value) { - const rawAmount = toBN( - toBase(sendAmount.value, selectedAsset.value.decimals!) + if (selectedAsset.value) { + return toBN(selectedAsset.value.balance ?? "0").sub( + toBN(toBase(sendAmount.value ?? "0", selectedAsset.value.decimals!)).add( + toBN( + toBase( + gasCostValues.value[selectedFee.value].nativeValue, + selectedAsset.value.decimals! + ) + ) + ) ); - return toBN(nativeBalance.value).sub(rawAmount); } return toBN(0); }); @@ -373,21 +350,7 @@ const selectFee = (type: GasPriceTypes) => { const sendAction = async () => { const keyring = new PublicKeyRing(); const fromAccountInfo = await keyring.getAccount(addressFrom.value); - const txInfo: BTCTxInfo = { - inputs: [], - outputs: [], - }; - accountUTXOs.value.forEach((u) => { - txInfo.inputs.push({ - hash: u.txid, - index: u.index, - raw: u.raw, - witnessUtxo: { - script: u.pkscript, - value: u.value, - }, - }); - }); + const txInfo = getBTCTxInfo(accountUTXOs.value); const balance = toBN(selectedAsset.value.balance!); const toAmount = toBN(toBase(sendAmount.value, selectedAsset.value.decimals)); const currentFee = toBN( @@ -504,4 +467,12 @@ const toggleSelector = (isTokenSend: boolean) => { } } } +p { + font-weight: 400; + font-size: 14px; + line-height: 20px; + letter-spacing: 0.25px; + color: @error; + margin: 0; +} diff --git a/packages/extension/src/providers/common/ui/send-transaction/send-alert.vue b/packages/extension/src/providers/common/ui/send-transaction/send-alert.vue index f115c963b..5c223ca3b 100644 --- a/packages/extension/src/providers/common/ui/send-transaction/send-alert.vue +++ b/packages/extension/src/providers/common/ui/send-transaction/send-alert.vue @@ -1,7 +1,7 @@ @@ -21,6 +22,9 @@ interface IProps { nativeSymbol: string; nativeValue: string; price?: string; + notEnough: boolean; + belowDust: boolean; + dust: string; } const props = defineProps(); diff --git a/packages/extension/src/ui/action/views/swap/libs/swap-txs.ts b/packages/extension/src/ui/action/views/swap/libs/swap-txs.ts index 8119f259d..eebadf263 100644 --- a/packages/extension/src/ui/action/views/swap/libs/swap-txs.ts +++ b/packages/extension/src/ui/action/views/swap/libs/swap-txs.ts @@ -17,7 +17,7 @@ import { ApiPromise } from "@polkadot/api"; import { TransactionType } from "../types"; import { BitcoinNetwork } from "@/providers/bitcoin/types/bitcoin-network"; import BitcoinAPI from "@/providers/bitcoin/libs/api"; -import { BTCTxInfo } from "@/providers/bitcoin/ui/types"; +import { getTxInfo as getBTCTxInfo } from "@/providers/bitcoin/libs/utils"; import { toBN } from "web3-utils"; export const getSubstrateNativeTransation = async ( @@ -45,21 +45,10 @@ export const getBitcoinNativeTransaction = async ( ) => { const api = (await network.api()) as BitcoinAPI; const utxos = await api.getUTXOs(tx.from); - const txInfo: BTCTxInfo = { - inputs: [], - outputs: [], - }; + const txInfo = getBTCTxInfo(utxos); let balance = 0; utxos.forEach((u) => { balance += u.value; - txInfo.inputs.push({ - hash: u.txid, - index: u.index, - witnessUtxo: { - script: u.pkscript, - value: u.value, - }, - }); }); const toAmount = toBN(tx.value); const remainder = balance - toAmount.toNumber();