Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bond): Solana deposit display #132

Merged
merged 6 commits into from
Nov 6, 2024
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Alert, Button, Flex, Form, InputNumber, Spin, Typography } from 'antd';
import { Alert, Button, Form, InputNumber, Table, Typography } from 'antd';
import { isNumber } from 'lodash';
import pDebounce from 'p-debounce';
import { useState } from 'react';

import { getCommaSeparatedNumber, notifyError } from '@autonolas/frontend-library';

import { NA } from 'libs/util-constants/src';
import { useSvmConnectivity } from 'common-util/hooks/useSvmConnectivity';

import { SVM_AMOUNT_DIVISOR } from './constants';
Expand All @@ -19,12 +20,13 @@ export const WsolDeposit = () => {
const [estimatedQuote, setEstimatedQuote] = useState(null);
const [isEstimating, setIsEstimating] = useState(false);
const [isDepositing, setIsDepositing] = useState(false);
const [bridgedTokenAmount, setBridgedTokenAmount] = useState(null);
const [quoteLiquidity, setQuoteLiquidity] = useState(null);

const {
getDepositIncreaseLiquidityQuote: fn,
getDepositTransformedQuote,
deposit,
bridgedTokenAmount,
} = useWsolDeposit();
const getDepositQuote = pDebounce(fn, 500);

Expand Down Expand Up @@ -59,10 +61,8 @@ export const WsolDeposit = () => {
const sol = form.getFieldValue('sol');
const slippage = form.getFieldValue('slippage');

const bridgedToken = await deposit({ slippage, sol });
if (Number(bridgedToken) > 0) {
setBridgedTokenAmount(bridgedToken / SVM_AMOUNT_DIVISOR);
}
const quoteLiquidityAmount = await deposit({ slippage, sol });
setQuoteLiquidity(quoteLiquidityAmount / SVM_AMOUNT_DIVISOR);
} catch (error) {
console.error(error);
} finally {
Expand All @@ -72,7 +72,10 @@ export const WsolDeposit = () => {

const isDepositButtonDisabled = isEstimating || isDepositing || !isSvmWalletConnected;
const estimatedOutput =
getCommaSeparatedNumber((estimatedQuote?.liquidity || 0) / SVM_AMOUNT_DIVISOR) || '--';
getCommaSeparatedNumber((estimatedQuote?.liquidity || 0) / SVM_AMOUNT_DIVISOR) || NA;
const bridgedTokenAmountValue = bridgedTokenAmount
? getCommaSeparatedNumber(bridgedTokenAmount)
: NA;

return (
<>
Expand Down Expand Up @@ -118,12 +121,33 @@ export const WsolDeposit = () => {
</Form.Item>

<Form.Item>
<Spin spinning={isEstimating} size="small">
<Flex vertical gap="small" className="mb-16">
<Text strong>ESTIMATED OUTPUT</Text>
<Text>{`${estimatedOutput} WSOL-OLAS LP`}</Text>
</Flex>
</Spin>
<Table
columns={[
{
title: 'Estimated output',
dataIndex: 'estimated',
width: '50%',
},
{
title: 'Bridged token amount',
dataIndex: 'bridgedTokenAmount',
mohandast52 marked this conversation as resolved.
Show resolved Hide resolved
width: '50%',
},
]}
dataSource={[
{
key: '1',
estimated: `${estimatedOutput} WSOL-OLAS LP`,
bridgedTokenAmount: `${bridgedTokenAmountValue} WSOL-OLAS LP`,
},
]}
bordered
loading={isEstimating}
rowHoverable={false}
pagination={false}
style={{ width: '100%' }}
className="mb-16"
/>

<Button
size="large"
Expand All @@ -137,12 +161,12 @@ export const WsolDeposit = () => {
</Form.Item>
</Form>

{bridgedTokenAmount && (
{quoteLiquidity && (
<Alert
message={
<>
You received
<Text strong>{` ${getCommaSeparatedNumber(bridgedTokenAmount)} WSOL-OLAS LP`}</Text>
<Text strong>{` ${getCommaSeparatedNumber(quoteLiquidity)} WSOL-OLAS LP`}</Text>
</>
}
type="success"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export const WHIRLPOOL_CONFIG_ID = new web3.PublicKey(
export const [tickLowerIndex, tickUpperIndex] = TickUtil.getFullRangeTickIndex(TICK_SPACING);
export const CONNECT_SVM_WALLET = 'Please connect your phantom wallet';

// 10^8
export const SVM_AMOUNT_DIVISOR = 100000000;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useWallet } from '@solana/wallet-adapter-react';
import { SystemProgram, Transaction } from '@solana/web3.js';
import Decimal from 'decimal.js';
import Link from 'next/link';
import { useCallback, useEffect, useState } from 'react';

import { notifyError, notifySuccess } from '@autonolas/frontend-library';

Expand All @@ -30,6 +31,7 @@ import {
POSITION_MINT,
PROGRAM_ID,
SOL,
SVM_AMOUNT_DIVISOR,
TICK_ARRAY_LOWER,
TICK_ARRAY_UPPER,
TOKEN_VAULT_A,
Expand Down Expand Up @@ -118,10 +120,27 @@ export const useWsolDeposit = () => {
const { svmWalletPublicKey, connection, anchorProvider } = useSvmConnectivity();
const { getWhirlpoolData } = useWhirlpool();
const { signTransaction } = useWallet();
const [bridgedTokenAmount, setBridgedTokenAmount] = useState(null);

const customGetOrCreateAssociatedTokenAccount = useGetOrCreateAssociatedTokenAccount();
const program = new Program(idl, PROGRAM_ID, anchorProvider);

const getLatestBridgeTokenAmount = useCallback(async () => {
mohandast52 marked this conversation as resolved.
Show resolved Hide resolved
if (!svmWalletPublicKey) return;
if (!connection) return;

getBridgeTokenAmount(connection, svmWalletPublicKey).then((bridgedToken) => {
const token = bridgedToken.toString();
if (Number(token) > 0) {
setBridgedTokenAmount(token / SVM_AMOUNT_DIVISOR);
}
});
}, [connection, svmWalletPublicKey]);

useEffect(() => {
getLatestBridgeTokenAmount();
}, [getLatestBridgeTokenAmount]);

const getDepositIncreaseLiquidityQuote = async ({ sol, slippage }) => {
const { whirlpoolData, whirlpoolTokenA, whirlpoolTokenB } = await getWhirlpoolData();
const slippageTolerance = Percentage.fromDecimal(new Decimal(slippage));
Expand Down Expand Up @@ -315,13 +334,15 @@ export const useWsolDeposit = () => {
return null;
}

const bridgedToken = await getBridgeTokenAmount(connection, svmWalletPublicKey);
return bridgedToken.toString();
await getLatestBridgeTokenAmount(); // refetch bridged token amount

return quote.liquidityAmount.toString();
};

return {
getDepositIncreaseLiquidityQuote,
getDepositTransformedQuote,
deposit,
bridgedTokenAmount,
};
};
Loading