Skip to content

Commit

Permalink
fix: doge swap and add minimum amount
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Sep 22, 2023
1 parent 74ab2b8 commit 6f2b6c9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 87 deletions.
25 changes: 23 additions & 2 deletions packages/extension/src/providers/bitcoin/libs/utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -66,4 +87,4 @@ const getGasCostValues = async (
};
return gasCostValues;
};
export { isAddress, getGasCostValues };
export { isAddress, getGasCostValues, getTxInfo };
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>

<send-fee-select
:in-swap="false"
:selected="selectedFee"
Expand All @@ -73,7 +72,7 @@
/>

<send-alert
v-show="hasEnoughBalance && nativeBalanceAfterTransaction.isNeg()"
v-show="nativeBalanceAfterTransaction.isNeg() || (Number(sendAmount) < (props.network as BitcoinNetwork).dust && Number(sendAmount)>0)"
:native-symbol="network.name"
:price="selectedAsset.price || '0'"
:native-value="
Expand All @@ -83,6 +82,9 @@
)
"
:decimals="network.decimals"
:below-dust="Number(sendAmount) < (props.network as BitcoinNetwork).dust"
:dust="(props.network as BitcoinNetwork).dust.toString()"
:not-enough="nativeBalanceAfterTransaction.isNeg()"
/>

<div class="send-transaction__buttons">
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -163,25 +166,6 @@ const selectedAsset = ref<BTCToken>(loadingAsset);
const amount = ref<string>("");
const accountUTXOs = ref<HaskoinUnspentType[]>([]);
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";
Expand All @@ -196,29 +180,22 @@ const addressFrom = ref<string>(
const addressTo = ref<string>("");
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);
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}
</style>
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<template>
<div class="send-alert">
<alert-icon />
<p>
<p v-if="notEnough">
Not enough funds. You are<br />~{{
$filters.formatFloatingPointValue(nativeValue).value
}}
{{ nativeSymbol }} (${{
$filters.formatFiatValue(priceDifference).value
}}) short.
</p>
<p v-if="belowDust">Minimum amount: {{ dust }}</p>
</div>
</template>

Expand All @@ -21,6 +22,9 @@ interface IProps {
nativeSymbol: string;
nativeValue: string;
price?: string;
notEnough: boolean;
belowDust: boolean;
dust: string;
}
const props = defineProps<IProps>();
Expand Down
15 changes: 2 additions & 13 deletions packages/extension/src/ui/action/views/swap/libs/swap-txs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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();
Expand Down

1 comment on commit 6f2b6c9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.