diff --git a/package.json b/package.json index 35cca3c..f393a57 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "@material-ui/core": "^4.11.2", "@material-ui/icons": "^4.11.2", "@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/web3.js": "^0.87.2", "@testing-library/jest-dom": "^5.11.6", diff --git a/src/components/SwapWormholeDialog.js b/src/components/SwapWormholeDialog.js index 630bf3d..01ed36b 100644 --- a/src/components/SwapWormholeDialog.js +++ b/src/components/SwapWormholeDialog.js @@ -14,12 +14,18 @@ import { swapApiRequest } from '../utils/swap/api'; import { getErc20Decimals } from '../utils/swap/eth.js'; import { useSendTransaction } from '../utils/notifications'; 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( 'SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8', ); +// const POOL_BASE = new PublicKey( +// 'CAXLccDUeS6egtNNEBLrxAqxSvuL6SwspqYX14JdKaiK', +// ); const POOL_BASE = new PublicKey( - 'CAXLccDUeS6egtNNEBLrxAqxSvuL6SwspqYX14JdKaiK', + 'BX4Y3jfmWwERaoNSonNpS4tRnBvUTgtRbbQra7D3DZSU', ); export default function SwapWormholeDialog({ @@ -36,6 +42,7 @@ export default function SwapWormholeDialog({ // * pool.accountInfo !== null => pool exists. const [pool, setPool] = useState(undefined); const [wormholeMintAddr, setWormholeMintAddr] = useState(null); + const [swapPoolWormholeTokenHolding, setSwapPoolWormholeTokenHolding] = useState(0) const [transferAmountString, setTransferAmountString] = useState(''); const [isLoading, setIsLoading] = useState(false); const wallet = useWallet(); @@ -134,6 +141,48 @@ export default function SwapWormholeDialog({ 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 // by trading on the constant price pool. async function convert() { @@ -232,7 +281,22 @@ export default function SwapWormholeDialog({ ) : ( <> - {`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.`} +
+
+ {`Swap pool: `} + + + +
+ {`Available wormhole balances in pool: ${swapPoolWormholeTokenHolding.toFixed(4)}`}
setTransferAmountString( - balanceAmountToUserAmount(balanceAmount, decimals), + Math.min(balanceAmountToUserAmount(balanceAmount, decimals), swapPoolWormholeTokenHolding), ) } > @@ -265,11 +329,11 @@ export default function SwapWormholeDialog({ setTransferAmountString( - balanceAmountToUserAmount(balanceAmount, decimals), + Math.min(balanceAmountToUserAmount(balanceAmount, decimals), swapPoolWormholeTokenHolding), ) } > - Max: {balanceAmountToUserAmount(balanceAmount, decimals)} + Max: {Math.min(balanceAmountToUserAmount(balanceAmount, decimals), swapPoolWormholeTokenHolding)} } /> diff --git a/yarn.lock b/yarn.lock index fa233be..6abc00b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1878,10 +1878,10 @@ bn.js "^5.1.2" buffer-layout "^1.2.0" -"@project-serum/swap@^0.0.11": - version "0.0.11" - resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.0.11.tgz#f606b73fbdcd152d7835068f168cb8ad172d8414" - integrity sha512-DxvNL81LnBcE62oENr0LOqIAen9/LNb7gEcDbNlIHr02PckUCZTDIngIsbDY09ld1+V7STy+pLPnstt6ZZElsg== +"@project-serum/swap@^0.0.13": + version "0.0.13" + resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.0.13.tgz#a9fd66daecb4f5843c13abc870ba5ef673c7d449" + integrity sha512-VKwPrzkiKOEv4U85ZnxP29rUfyT3tvvKBDyYSEjAkpqOm3LkRhHvmcm6h/0gOS8waSFiYe2nC80t5tWZOrLbGg== dependencies: "@project-serum/serum" "^0.13.21" "@solana/spl-token" "^0.0.13"