Skip to content

Commit

Permalink
feat: update btc balance logic
Browse files Browse the repository at this point in the history
  • Loading branch information
naturexie committed Jan 21, 2025
1 parent 842a58e commit 6c72fa0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 43 deletions.
15 changes: 10 additions & 5 deletions components/Modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState, createContext } from "react";
import { useEffect, useState, createContext, useMemo } from "react";
import { Modal as MUIModal, Box, useTheme } from "@mui/material";

import Decimal from "decimal.js";
Expand Down Expand Up @@ -49,6 +49,7 @@ import {
CollateralTypeSelectorRepay,
} from "./CollateralTypeSelector";
import { useBtcAction } from "../../hooks/useBtcBalance";
import { beautifyPrice } from "../../utils/beautyNumber";

export const ModalContext = createContext(null) as any;
const Modal = () => {
Expand Down Expand Up @@ -82,12 +83,16 @@ const Modal = () => {
const maxBorrowAmountPositions = useAppSelector(getBorrowMaxAmount(tokenId));
const maxWithdrawAmount = useAppSelector(getWithdrawMaxAmount(tokenId));
const repayPositions = useAppSelector(getRepayPositions(tokenId));
const { availableBalance: btcAvailableBalance, receiveAmount } = useBtcAction({
const { availableBalance: btcAvailableBalance, totalFeeAmount } = useBtcAction({
tokenId: asset?.tokenId || "",
decimals: asset?.decimals || 0,
inputAmount: amount,
action,
});
const receiveAmount = useMemo(() => {
return Decimal.max(new Decimal(amount || 0).minus(totalFeeAmount || 0), 0).toFixed(
asset?.decimals || 0,
Decimal.ROUND_DOWN,
);
}, [totalFeeAmount, amount]);
const activePosition =
action === "Repay" || action === "Borrow"
? selectedCollateralType
Expand Down Expand Up @@ -205,7 +210,7 @@ const Modal = () => {
available$={available$}
/>
<div className="flex flex-col gap-4 mt-6">
{isBtcWithdraw ? <Receive value={receiveAmount} /> : null}
{isBtcWithdraw ? <Receive value={beautifyPrice(receiveAmount) as string} /> : null}
<HealthFactor value={healthFactor} />
{repay_to_lp ? (
<HealthFactor value={single_healthFactor} title="Health Factor(Single)" />
Expand Down
51 changes: 18 additions & 33 deletions hooks/useBtcBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,46 @@ export function useBtcAction({
tokenId,
decimals,
updaterCounter,
inputAmount,
action,
}: {
tokenId: string;
decimals: number;
updaterCounter?: number;
inputAmount?: string | number;
action?: string;
}) {
const [balance, setBalance] = useState<number>(0);
const [receiveAmount, setReceiveAmount] = useState<string>("0");
const [totalFeeAmount, setTotalFeeAmount] = useState<number>(0);
const [availableBalance, setAvailableBalance] = useState<number>(0);
const btcSelector = useBtcWalletSelector();
const expandInputAmount = expandToken(inputAmount || 0, decimals, 0);
const isBtcTokenId = tokenId == NBTCTokenId;
const env = "private_mainnet";
useDebounce(
() => {
if (btcSelector?.account && isBtcTokenId) {
getBtcBalance().then((res) => {
getBtcBalance().then(async (res) => {
const { balance: btcBalance, availableBalance: btcAvailableBalance } = res;
const expandAvailableBalance = expandToken(btcAvailableBalance, decimals);
const { protocolFee, repayAmount } = await getDepositAmount(expandAvailableBalance, {
env,
});
const totalFeeAmount = shrinkToken(
new Decimal(protocolFee || 0).plus(repayAmount).toFixed(),
decimals,
);
const avaBalance = Decimal.max(
0,
new Decimal(btcAvailableBalance).minus(totalFeeAmount).toFixed(),
).toFixed();
setBalance(btcBalance || 0);
setAvailableBalance(btcAvailableBalance || 0);
setAvailableBalance(+(avaBalance || 0));
setTotalFeeAmount(+(totalFeeAmount || 0));
});
}
},
500,
[btcSelector?.account, updaterCounter, isBtcTokenId],
);
useDebounce(
() => {
if (btcSelector?.account && isBtcTokenId && action == "Withdraw") {
const inputAmountDecimal = new Decimal(expandInputAmount || 0);
if (inputAmountDecimal.lte(0)) {
setReceiveAmount("0");
} else {
getDepositAmount(expandInputAmount, { env: "private_mainnet" }).then(
({ protocolFee }) => {
setReceiveAmount(
shrinkToken(
new Decimal(inputAmountDecimal).minus(protocolFee || 0).toFixed(0),
decimals,
),
);
},
);
}
}
},
500,
[btcSelector?.account, inputAmount, isBtcTokenId, action],
);

return {
balance,
availableBalance,
receiveAmount,
totalFeeAmount,
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"big.js": "^6.2.2",
"bn.js": "^5.2.1",
"borsh": "^0.7.0",
"btc-wallet": "^0.4.7-beta",
"btc-wallet": "^0.4.8-beta",
"crypto-browserify": "3.12.0",
"dayjs": "1.11.10",
"decimal.js": "^10.4.1",
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6921,10 +6921,10 @@ [email protected]:
dependencies:
node-int64 "^0.4.0"

btc-wallet@^0.4.7-beta:
version "0.4.7-beta"
resolved "https://registry.yarnpkg.com/btc-wallet/-/btc-wallet-0.4.7-beta.tgz#af2e98ce81875ab800820681c9c9b02c790d3030"
integrity sha512-ATE/tCvEHU3GO+BPLHC+0b7xfmIh41ZXxVDLhyiCfxDKVwQtGbtHKv1nP7OvJsLSZQcWaL/Iupg6hvugdSXWcg==
btc-wallet@^0.4.8-beta:
version "0.4.8-beta"
resolved "https://registry.yarnpkg.com/btc-wallet/-/btc-wallet-0.4.8-beta.tgz#144e4292f9e93fc963f57408b1c2f9a145b9c3c1"
integrity sha512-vPSji2XgCHCtWCHzzao5k9cbG/C1jVIGQ5izE6FvFJIugGas6SFRdh0YtsiZ+CkGpIzq7YSeI9VMYikusTlnyw==
dependencies:
"@ethereumjs/util" "^9.0.1"
"@near-js/transactions" "^1.3.0"
Expand Down

0 comments on commit 6c72fa0

Please sign in to comment.