Skip to content

Commit

Permalink
fix: patch trustline and wallet warning bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mootz12 committed Sep 18, 2024
1 parent 174426f commit a7e8cb5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 49 deletions.
5 changes: 2 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blend-ui",
"version": "1.1.0",
"version": "1.1.1",
"private": true,
"type": "module",
"scripts": {
Expand All @@ -21,7 +21,6 @@
"@stellar/freighter-api": "^3.0.0",
"@stellar/stellar-sdk": "12.2.0",
"@tanstack/react-query": "5.52.0",
"bignumber.js": "^9.0.2",
"copy-to-clipboard": "^3.3.3",
"next": "^14.2.11",
"react": "^18.2.0",
Expand Down
33 changes: 17 additions & 16 deletions src/components/borrow/BorrowAnvil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ export const BorrowAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId }
setLoadingEstimate(false);
});

async function handleAddAssetTrustline() {
if (connected && reserve?.tokenMetadata?.asset) {
const reserveAsset = reserve?.tokenMetadata?.asset;
await createTrustline(reserveAsset);
}
}

const AddTrustlineButton = (
<OpaqueButton
onClick={handleAddAssetTrustline}
palette={theme.palette.warning}
sx={{ padding: '6px 24px', margin: '12px auto' }}
>
Add {reserve?.tokenMetadata.asset?.code} Trustline
</OpaqueButton>
);

const { isSubmitDisabled, isMaxDisabled, reason, disabledType, extraContent, isError } =
useMemo(() => {
const hasTokenTrustline = !requiresTrustline(horizonAccount, reserve?.tokenMetadata?.asset);
Expand Down Expand Up @@ -142,15 +159,6 @@ export const BorrowAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId }
newPositionEstimate && Number.isFinite(newPositionEstimate?.borrowLimit)
? newPositionEstimate?.borrowLimit
: 0;
const AddTrustlineButton = (
<OpaqueButton
onClick={handleAddAssetTrustline}
palette={theme.palette.warning}
sx={{ padding: '6px 24px', margin: '12px auto' }}
>
Add {reserve?.tokenMetadata.asset?.code} Trustline
</OpaqueButton>
);

const handleBorrowMax = () => {
if (reserve && assetToBase && curPositionEstimate) {
Expand All @@ -169,13 +177,6 @@ export const BorrowAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId }
}
};

async function handleAddAssetTrustline() {
if (connected && reserve?.tokenMetadata?.asset) {
const reserveAsset = reserve?.tokenMetadata?.asset;
await createTrustline(reserveAsset);
}
}

return (
<Row>
<Section
Expand Down
10 changes: 6 additions & 4 deletions src/components/common/WalletWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const WalletWarning = () => {
const [openCon, setOpenCon] = React.useState(false);
const [openError, setOpenError] = React.useState(false);

const { refetch: refetchAccount, isError: isError } = useHorizonAccount();
const { data: account, refetch: refetchAccount } = useHorizonAccount();

const notFound = account === undefined;

const handleConnectWallet = (successful: boolean) => {
if (successful) {
Expand All @@ -31,19 +33,19 @@ export const WalletWarning = () => {
};

useEffect(() => {
if (connected && isError === true) {
if (connected && notFound === true) {
refetchAccount();
const refreshInterval = setInterval(async () => {
await refetchAccount();
}, 3 * 1000);
return () => clearInterval(refreshInterval);
}
}, [refetchAccount, connected, isError, walletAddress]);
}, [refetchAccount, connected, notFound, walletAddress]);

return (
<>
{connected ? (
isError === true ? (
notFound === true ? (
<Row
sx={{
background: theme.palette.warning.opaque,
Expand Down
34 changes: 17 additions & 17 deletions src/components/withdraw/WithdrawAnvil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ export const WithdrawAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId
setLoadingEstimate(false);
});

async function handleAddAssetTrustline() {
if (connected && reserve?.tokenMetadata?.asset) {
const reserveAsset = reserve?.tokenMetadata?.asset;
await createTrustline(reserveAsset);
}
}

const AddTrustlineButton = (
<OpaqueButton
onClick={handleAddAssetTrustline}
palette={theme.palette.warning}
sx={{ padding: '6px 24px', margin: '12px auto' }}
>
Add {reserve?.tokenMetadata.asset?.code} Trustline
</OpaqueButton>
);

const { isSubmitDisabled, isMaxDisabled, reason, disabledType, extraContent, isError } =
useMemo(() => {
const hasTokenTrustline = !requiresTrustline(horizonAccount, reserve?.tokenMetadata?.asset);
Expand Down Expand Up @@ -134,16 +151,6 @@ export const WithdrawAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId
? newPositionsEstimate?.borrowLimit
: 0;

const AddTrustlineButton = (
<OpaqueButton
onClick={handleAddAssetTrustline}
palette={theme.palette.warning}
sx={{ padding: '6px 24px', margin: '12px auto' }}
>
Add {reserve?.tokenMetadata.asset?.code} Trustline
</OpaqueButton>
);

const handleWithdrawAmountChange = (withdrawInput: string) => {
if (reserve && poolUser) {
let curSupplied = poolUser.getCollateralFloat(reserve);
Expand Down Expand Up @@ -178,13 +185,6 @@ export const WithdrawAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId
}
};

async function handleAddAssetTrustline() {
if (connected && reserve?.tokenMetadata?.asset) {
const reserveAsset = reserve?.tokenMetadata?.asset;
await createTrustline(reserveAsset);
}
}

return (
<Row>
<Section
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/DefaultLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function DefaultLayout({ children }: { children: ReactNode }) {
<Row>
<WalletWarning />
</Row>
{faucet_pool && isTestnet && (
{faucet_pool !== undefined && isTestnet && (
<Row>
<FaucetBanner poolId={faucet_pool} />
</Row>
Expand Down
22 changes: 16 additions & 6 deletions src/utils/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// Information display formatting utils
import BigNumber from 'bignumber.js';

const POSTFIXES = ['', 'k', 'M', 'B', 'T', 'P', 'E', 'Z', 'Y'];

Expand Down Expand Up @@ -29,28 +28,39 @@ export function toBalance(
}

let visibleDecimals = 0;
if (amount === 0) {
if (numValue === 0) {
visibleDecimals = 0;
} else {
if (amount > 1) {
if (numValue >= 10) {
visibleDecimals = 2;
} else {
visibleDecimals = decimals ?? 7;
visibleDecimals = Math.min(decimals ?? 7, 7);
}
}

if (numValue === 0) {
return '0';
}

const absValue = Math.abs(numValue);
if (absValue < 10) {
return numValue.toFixed(visibleDecimals);
} else if (absValue < 10000) {
return numValue.toFixed(2);
}

const minValue = 10 ** -(visibleDecimals as number);
const isSmallerThanMin = numValue !== 0 && Math.abs(numValue) < Math.abs(minValue);
let adjAmount = isSmallerThanMin ? minValue : numValue;

const bnValue = new BigNumber(numValue);
const bnValue = numValue;

const integerPlaces = bnValue.toFixed(0).length;
const postfixIndex = Math.min(
Math.floor(integerPlaces ? (integerPlaces - 1) / 3 : 0),
POSTFIXES.length - 1
);
adjAmount = bnValue.shiftedBy(-3 * postfixIndex).toNumber();
adjAmount = numValue / Math.pow(10, 3 * postfixIndex);

const formattedStr = new Intl.NumberFormat('en-US', {
maximumFractionDigits: visibleDecimals,
Expand Down

0 comments on commit a7e8cb5

Please sign in to comment.