Skip to content

Commit

Permalink
Merge pull request #157 from blend-capital/fix-wallet-status-banner
Browse files Browse the repository at this point in the history
Fix wallet status banner
  • Loading branch information
mootz12 authored Sep 19, 2024
2 parents 174426f + 3d9979e commit 1cabddf
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .env.testnet
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ NEXT_PUBLIC_STELLAR_EXPERT_URL=https://stellar.expert/explorer/testnet
NEXT_PUBLIC_RPC_URL=https://soroban-testnet.stellar.org
NEXT_PUBLIC_HORIZON_URL=https://horizon-testnet.stellar.org
NEXT_PUBLIC_PASSPHRASE=Test SDF Network ; September 2015
NEXT_PUBLIC_BACKSTOP=CBNRD2GEJP4Y3H3TG3TDNJFGOVBIBBL6SDQ5LUPT4LUQP62T3LLRGHPK
NEXT_PUBLIC_BACKSTOP=CDJQJS3TLZ6LEWSBOZ2E6QEZZ4NS3JU5LDT2BT7A4UHWAXPLSNE6MM37
NEXT_PUBLIC_USDC_ISSUER=GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56
NEXT_PUBLIC_BLND_ISSUER=GATALTGTWIOT6BUDBCZM3Q4OQ4BO2COLOAZ7IYSKPLC2PMSOPPGF5V56
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
35 changes: 18 additions & 17 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 All @@ -105,7 +122,7 @@ export const BorrowAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId }
} else {
return getErrorFromSim(toBorrow, decimals, loading, simResponse, undefined);
}
}, [toBorrow, simResponse, poolUser]);
}, [toBorrow, simResponse, poolUser, horizonAccount]);

if (pool === undefined || reserve === undefined) {
return <Skeleton />;
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
36 changes: 18 additions & 18 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 All @@ -105,7 +122,7 @@ export const WithdrawAnvil: React.FC<ReserveComponentProps> = ({ poolId, assetId
} else {
return getErrorFromSim(toWithdraw, decimals, loading, simResponse, undefined);
}
}, [toWithdraw, simResponse, loading]);
}, [toWithdraw, simResponse, loading, horizonAccount]);

if (pool === undefined || reserve === undefined) {
return <Skeleton />;
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

1 comment on commit 1cabddf

@github-actions
Copy link

Choose a reason for hiding this comment

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

This commit was deployed on ipfs

Please sign in to comment.