Skip to content

Commit ecbf315

Browse files
fix: add generic max calcutation composable
1 parent f9a7e0f commit ecbf315

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

src/protocols/ethereum/composables/ethMaxAmount.ts renamed to src/composables/coinMaxAmount.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ import {
77
useBalances,
88
} from '@/composables';
99
import { ProtocolAdapterFactory } from '@/lib/ProtocolAdapterFactory';
10-
import { PROTOCOLS } from '@/constants';
1110
import { toShiftedBigNumber } from '@/utils';
1211

13-
interface EthMaxAmountOptions extends MaxAmountOptions {
12+
interface CoinMaxAmountOptions extends MaxAmountOptions {
1413
fee: Ref<BigNumberPublic>;
1514
}
1615

1716
/**
1817
* Composable that allows to use real max amount of selected token
1918
* considering the fee that needs to be paid.
2019
*/
21-
export function useEthMaxAmount({ formModel, fee }: EthMaxAmountOptions) {
20+
export function useCoinMaxAmount({ formModel, fee }: CoinMaxAmountOptions) {
2221
const { balance } = useBalances();
2322

24-
const isEthCoin = computed(() => {
25-
const ethAdapter = ProtocolAdapterFactory.getAdapter(PROTOCOLS.ethereum);
26-
return formModel.value?.selectedAsset?.contractId === ethAdapter.coinContractId;
23+
const isCoin = computed(() => {
24+
if (!formModel.value.selectedAsset?.protocol) {
25+
return false;
26+
}
27+
const adapter = ProtocolAdapterFactory.getAdapter(formModel.value.selectedAsset?.protocol!);
28+
return formModel.value?.selectedAsset?.contractId === adapter.coinContractId;
2729
});
2830
const selectedTokenBalance = computed(
2931
() => new BigNumber(
@@ -35,8 +37,11 @@ export function useEthMaxAmount({ formModel, fee }: EthMaxAmountOptions) {
3537
);
3638

3739
const max = computed(() => {
38-
if (balance.value && isEthCoin.value) {
39-
const maxAmount = balance.value.minus(fee.value);
40+
if (balance.value && isCoin.value) {
41+
const maxAmount = balance.value
42+
.minus(fee.value)
43+
.dividedBy(formModel.value.addresses?.length || 1)
44+
.decimalPlaces(formModel.value.selectedAsset?.decimals!);
4045
return (maxAmount.isPositive() ? maxAmount : 0).toString();
4146
}
4247
return selectedTokenBalance.value.toString();

src/protocols/bitcoin/components/TransferSendForm.vue

+22-6
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
>
4343
<template #label-after>
4444
<BtnMaxAmount
45-
:is-max="formModel?.amount?.toString() === max.toString()"
46-
@click="setMaxAmount"
45+
:is-max="shouldUseMaxAmount"
46+
@click="toggleMaxAmount"
4747
/>
4848
</template>
4949
</TransferSendAmount>
@@ -88,6 +88,7 @@ import {
8888
useNetworks,
8989
} from '@/composables';
9090
import { useTransferSendForm } from '@/composables/transferSendForm';
91+
import { useCoinMaxAmount } from '@/composables/coinMaxAmount';
9192
import { NETWORK_TYPE_TESTNET, PROTOCOLS } from '@/constants';
9293
import {
9394
executeAndSetInterval,
@@ -141,6 +142,7 @@ export default defineComponent({
141142
142143
const hasMultisigTokenWarning = ref(false);
143144
const isUrlTippingEnabled = ref(false);
145+
const shouldUseMaxAmount = ref(false);
144146
145147
const {
146148
formModel,
@@ -172,7 +174,8 @@ export default defineComponent({
172174
));
173175
174176
const numericFee = computed(() => +fee.value.toFixed());
175-
const max = computed(() => balance.value.minus(fee.value));
177+
178+
const { max } = useCoinMaxAmount({ formModel, fee });
176179
177180
function emitCurrentFormModelState() {
178181
const inputPayload: TransferFormModel = {
@@ -194,8 +197,11 @@ export default defineComponent({
194197
}
195198
}
196199
197-
function setMaxAmount() {
198-
formModel.value.amount = max.value.isPositive() ? max.value.toString() : '0';
200+
function toggleMaxAmount() {
201+
shouldUseMaxAmount.value = !shouldUseMaxAmount.value;
202+
if (shouldUseMaxAmount.value) {
203+
formModel.value.amount = max.value;
204+
}
199205
}
200206
201207
async function updateFeeList() {
@@ -257,6 +263,15 @@ export default defineComponent({
257263
}
258264
});
259265
266+
watch(
267+
max,
268+
(newMax) => {
269+
if (shouldUseMaxAmount.value) {
270+
formModel.value.amount = newMax;
271+
}
272+
},
273+
);
274+
260275
watch(
261276
hasError,
262277
(val) => emit('error', val),
@@ -291,10 +306,11 @@ export default defineComponent({
291306
balance,
292307
max,
293308
clearPayload,
309+
shouldUseMaxAmount,
294310
scanTransferQrCode,
295311
handleAssetChange,
296312
submit,
297-
setMaxAmount,
313+
toggleMaxAmount,
298314
toBitcoin,
299315
};
300316
},

src/protocols/ethereum/components/TransferSendForm.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import {
8787
} from '@/composables';
8888
import { useEthFeeCalculation } from '@/protocols/ethereum/composables/ethFeeCalculation';
8989
import { useTransferSendForm } from '@/composables/transferSendForm';
90+
import { useCoinMaxAmount } from '@/composables/coinMaxAmount';
9091
import { NETWORK_TYPE_TESTNET, PROTOCOLS } from '@/constants';
9192
import { executeAndSetInterval } from '@/utils';
9293
import { ProtocolAdapterFactory } from '@/lib/ProtocolAdapterFactory';
@@ -95,7 +96,6 @@ import {
9596
ETH_COIN_SYMBOL,
9697
ETH_PROTOCOL_NAME,
9798
} from '@/protocols/ethereum/config';
98-
import { useEthMaxAmount } from '@/protocols/ethereum/composables/ethMaxAmount';
9999
import { useEthNetworkSettings } from '@/protocols/ethereum/composables/ethNetworkSettings';
100100
import { getTokenTransferGasLimit } from '@/protocols/ethereum/helpers';
101101
@@ -173,7 +173,7 @@ export default defineComponent({
173173
174174
const shouldUseMaxAmount = ref(false);
175175
176-
const { max } = useEthMaxAmount({ formModel, fee: maxFee });
176+
const { max } = useCoinMaxAmount({ formModel, fee: maxFee });
177177
178178
const numericFee = computed(() => +fee.value.toFixed());
179179
const numericMaxFee = computed(() => +maxFee.value.toFixed());

0 commit comments

Comments
 (0)