Update display for wormhole swap

This commit is contained in:
Nathaniel Parke 2021-04-15 10:18:35 +08:00
parent 7fb5f7f383
commit 11c674128d
3 changed files with 74 additions and 10 deletions

View File

@ -7,7 +7,7 @@
"@material-ui/core": "^4.11.2", "@material-ui/core": "^4.11.2",
"@material-ui/icons": "^4.11.2", "@material-ui/icons": "^4.11.2",
"@project-serum/serum": "^0.13.33", "@project-serum/serum": "^0.13.33",
"@project-serum/swap": "^0.0.11", "@project-serum/swap": "^0.0.13",
"@solana/spl-token-registry": "^0.2.1", "@solana/spl-token-registry": "^0.2.1",
"@solana/web3.js": "^0.87.2", "@solana/web3.js": "^0.87.2",
"@testing-library/jest-dom": "^5.11.6", "@testing-library/jest-dom": "^5.11.6",

View File

@ -14,12 +14,18 @@ import { swapApiRequest } from '../utils/swap/api';
import { getErc20Decimals } from '../utils/swap/eth.js'; import { getErc20Decimals } from '../utils/swap/eth.js';
import { useSendTransaction } from '../utils/notifications'; import { useSendTransaction } from '../utils/notifications';
import { signAndSendTransaction } from '../utils/tokens'; import { signAndSendTransaction } from '../utils/tokens';
import { divideBnToNumber } from "@project-serum/swap/lib";
import BN from 'bn.js'
import { Chip } from '@material-ui/core';
const SWAP_PROGRAM_ID = new PublicKey( const SWAP_PROGRAM_ID = new PublicKey(
'SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8', 'SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8',
); );
// const POOL_BASE = new PublicKey(
// 'CAXLccDUeS6egtNNEBLrxAqxSvuL6SwspqYX14JdKaiK',
// );
const POOL_BASE = new PublicKey( const POOL_BASE = new PublicKey(
'CAXLccDUeS6egtNNEBLrxAqxSvuL6SwspqYX14JdKaiK', 'BX4Y3jfmWwERaoNSonNpS4tRnBvUTgtRbbQra7D3DZSU',
); );
export default function SwapWormholeDialog({ export default function SwapWormholeDialog({
@ -36,6 +42,7 @@ export default function SwapWormholeDialog({
// * pool.accountInfo !== null => pool exists. // * pool.accountInfo !== null => pool exists.
const [pool, setPool] = useState(undefined); const [pool, setPool] = useState(undefined);
const [wormholeMintAddr, setWormholeMintAddr] = useState(null); const [wormholeMintAddr, setWormholeMintAddr] = useState(null);
const [swapPoolWormholeTokenHolding, setSwapPoolWormholeTokenHolding] = useState(0)
const [transferAmountString, setTransferAmountString] = useState(''); const [transferAmountString, setTransferAmountString] = useState('');
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const wallet = useWallet(); const wallet = useWallet();
@ -134,6 +141,48 @@ export default function SwapWormholeDialog({
swapCoinInfo.ticker, swapCoinInfo.ticker,
]); ]);
useEffect(() => {
const fetch = async () => {
if (
pool &&
pool.publicKey &&
wormholeMintAddr &&
balanceAmount > 0
) {
let poolClient;
try {
poolClient = await Pool.load(wallet.connection, pool.publicKey, SWAP_PROGRAM_ID);
} catch (e) {
console.log(`Received error loading pool: ${pool.publicKey}`);
setSwapPoolWormholeTokenHolding(0);
return;
}
const holdings = await poolClient.getHoldings(wallet.connection);
const whMint = await poolClient.getCachedMintAccount(wallet.connection, wormholeMintAddr, 100000);
const poolWhHoldingAccount = holdings.find(holding => holding.mint.toString() === wormholeMintAddr.toString());
if (!poolWhHoldingAccount) {
setSwapPoolWormholeTokenHolding(0);
return;
}
const poolWhTokenAmount = divideBnToNumber(
new BN(poolWhHoldingAccount.holding),
new BN(Math.pow(10, whMint.decimals))
);
setSwapPoolWormholeTokenHolding(poolWhTokenAmount);
} else {
setSwapPoolWormholeTokenHolding(0);
}
}
fetch();
}, [
pool,
wormholeMintAddr,
balanceAmount,
balanceInfo.mint,
swapCoinInfo.ticker,
wallet.connection
]);
// Converts the sollet wrapped token into the wormhole wrapped token // Converts the sollet wrapped token into the wormhole wrapped token
// by trading on the constant price pool. // by trading on the constant price pool.
async function convert() { async function convert() {
@ -232,7 +281,22 @@ export default function SwapWormholeDialog({
) : ( ) : (
<> <>
<DialogContentText> <DialogContentText>
{`Convert your tokens into wormhole-wrapped tokens.`} {`Convert your tokens into wormhole-wrapped tokens using a constant price swap pool.
Assets will be converted one-to-one with a 5 basis point fee paid to the swap program owner.`}
<br/>
<br/>
{`Swap pool: `}
<a
href={`https://explorer.solana.com/address/${pool.publicKey}`}
target="_blank"
rel="noreferrer"
>
<Chip
label={pool.publicKey.toString()}
/>
</a>
<br/>
{`Available wormhole balances in pool: ${swapPoolWormholeTokenHolding.toFixed(4)}`}
</DialogContentText> </DialogContentText>
<TextField <TextField
label="Amount" label="Amount"
@ -246,7 +310,7 @@ export default function SwapWormholeDialog({
<Button <Button
onClick={() => onClick={() =>
setTransferAmountString( setTransferAmountString(
balanceAmountToUserAmount(balanceAmount, decimals), Math.min(balanceAmountToUserAmount(balanceAmount, decimals), swapPoolWormholeTokenHolding),
) )
} }
> >
@ -265,11 +329,11 @@ export default function SwapWormholeDialog({
<span <span
onClick={() => onClick={() =>
setTransferAmountString( setTransferAmountString(
balanceAmountToUserAmount(balanceAmount, decimals), Math.min(balanceAmountToUserAmount(balanceAmount, decimals), swapPoolWormholeTokenHolding),
) )
} }
> >
Max: {balanceAmountToUserAmount(balanceAmount, decimals)} Max: {Math.min(balanceAmountToUserAmount(balanceAmount, decimals), swapPoolWormholeTokenHolding)}
</span> </span>
} }
/> />

View File

@ -1878,10 +1878,10 @@
bn.js "^5.1.2" bn.js "^5.1.2"
buffer-layout "^1.2.0" buffer-layout "^1.2.0"
"@project-serum/swap@^0.0.11": "@project-serum/swap@^0.0.13":
version "0.0.11" version "0.0.13"
resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.0.11.tgz#f606b73fbdcd152d7835068f168cb8ad172d8414" resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.0.13.tgz#a9fd66daecb4f5843c13abc870ba5ef673c7d449"
integrity sha512-DxvNL81LnBcE62oENr0LOqIAen9/LNb7gEcDbNlIHr02PckUCZTDIngIsbDY09ld1+V7STy+pLPnstt6ZZElsg== integrity sha512-VKwPrzkiKOEv4U85ZnxP29rUfyT3tvvKBDyYSEjAkpqOm3LkRhHvmcm6h/0gOS8waSFiYe2nC80t5tWZOrLbGg==
dependencies: dependencies:
"@project-serum/serum" "^0.13.21" "@project-serum/serum" "^0.13.21"
"@solana/spl-token" "^0.0.13" "@solana/spl-token" "^0.0.13"