diff --git a/package.json b/package.json index d3d10b4..25fa280 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "dependencies": { "@fontsource/roboto": "^4.3.0", "@project-serum/serum": "^0.13.34", - "@project-serum/swap": "^0.1.0-alpha.15", + "@project-serum/swap": "^0.1.0-alpha.17", "@solana/spl-token": "^0.1.4" }, "peerDependencies": { @@ -41,7 +41,7 @@ "@project-serum/anchor": "^0.7.0", "@project-serum/serum": "^0.13.34", "@project-serum/sol-wallet-adapter": "^0.2.0", - "@project-serum/swap": "^0.1.0-alpha.15", + "@project-serum/swap": "^0.1.0-alpha.17", "@solana/spl-token": "^0.1.4", "@solana/spl-token-registry": "^0.2.86", "@solana/web3.js": "^1.10.1", diff --git a/src/components/OpenOrdersDialog.tsx b/src/components/OpenOrdersDialog.tsx index 6518e20..c833232 100644 --- a/src/components/OpenOrdersDialog.tsx +++ b/src/components/OpenOrdersDialog.tsx @@ -25,6 +25,7 @@ import { Close } from "@material-ui/icons"; import { useMarket, useOpenOrders, useDexContext } from "../context/Dex"; import { useTokenMap } from "../context/TokenList"; import { useMint, useOwnedTokenAccount } from "../context/Token"; +import { DEX_PID } from '../utils/pubkeys'; const useStyles = makeStyles((theme) => ({ table: {}, @@ -170,9 +171,15 @@ function OpenOrdersRow({ }; const closeOpenOrders = async () => { - // TODO. - // - // Blocked by https://github.com/project-serum/serum-dex/pull/112. + await swapClient.program.rpc.closeAccount({ + accounts: { + openOrders: ooAccount.address, + authority: swapClient.program.provider.wallet.publicKey, + destination: swapClient.program.provider.wallet.publicKey, + market: marketClient!.address, + dexProgram: DEX_PID, + }, + }); }; return ( diff --git a/src/components/Swap.tsx b/src/components/Swap.tsx index d0f710d..67107a3 100644 --- a/src/components/Swap.tsx +++ b/src/components/Swap.tsx @@ -303,7 +303,7 @@ function TokenName({ mint, style }: { mint: PublicKey; style: any }) { function SwapButton() { const styles = useStyles(); - const { fromMint, toMint, fromAmount, slippage, isClosingNewAccounts } = + const { fromMint, toMint, fromAmount, slippage, isClosingNewAccounts, isStrict } = useSwapContext(); const { swapClient } = useDexContext(); const fromMintInfo = useMint(fromMint); @@ -330,15 +330,18 @@ function SwapButton() { if (!fair) { throw new Error("Invalid fair"); } - const amount = new BN(fromAmount).mul( - new BN(10).pow(new BN(fromMintInfo.decimals)) - ); + if (!route || route.markets.length === 0) { + throw new Error('Invalid route'); + } + const amount = new BN(fromAmount*10**fromMintInfo.decimals); + console.log('amount', route, fair.toString(), fromAmount.toString(), amount.toString()); const minExchangeRate = { - rate: new BN(10 ** toMintInfo.decimals * (1 - BASE_TAKER_FEE_BPS)) + rate: new BN(10 ** toMintInfo.decimals * (1 - BASE_TAKER_FEE_BPS)**route.markets.length) .divn(fair) .muln(100 - slippage) .divn(100), decimals: fromMintInfo.decimals, + strict: isStrict, }; const fromOpenOrders = fromMarket ? openOrders.get(fromMarket?.address.toString()) @@ -353,7 +356,7 @@ function SwapButton() { toWallet: toWallet ? toWallet.publicKey : undefined, amount, minExchangeRate, - referral, + referral, // Pass in the below parameters so that the client doesn't perform // wasteful network requests when we already have the data. fromMarket, diff --git a/src/context/Swap.tsx b/src/context/Swap.tsx index dc429a7..9505f86 100644 --- a/src/context/Swap.tsx +++ b/src/context/Swap.tsx @@ -56,6 +56,13 @@ export type SwapContext = { // True if all newly created market accounts should be closed in the // same user flow (ideally in the same transaction). isClosingNewAccounts: boolean; + + // True if the swap exchange rate should be a function of nothing but the + // from and to tokens, ignoring any quote tokens that may have been + // accumulated by performing the swap. + isStrict: boolean; + setIsStrict: (isStrict: boolean) => void; + setIsClosingNewAccounts: (b: boolean) => void; }; const _SwapContext = React.createContext(null); @@ -66,6 +73,7 @@ export function SwapContextProvider(props: any) { const [fromAmount, _setFromAmount] = useState(props.fromAmount ?? 0); const [toAmount, _setToAmount] = useState(props.toAmount ?? 0); const [isClosingNewAccounts, setIsClosingNewAccounts] = useState(false); + const [isStrict, setIsStrict] = useState(false); const [slippage, setSlippage] = useState(DEFAULT_SLIPPAGE_PERCENT); const [fairOverride, setFairOverride] = useState(null); const fair = _useSwapFair(fromMint, toMint, fairOverride); @@ -117,6 +125,8 @@ export function SwapContextProvider(props: any) { fairOverride, setFairOverride, isClosingNewAccounts, + isStrict, + setIsStrict, setIsClosingNewAccounts, referral, }} diff --git a/yarn.lock b/yarn.lock index 9685b21..c2a5e7b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1654,10 +1654,10 @@ bs58 "^4.0.1" eventemitter3 "^4.0.4" -"@project-serum/swap@^0.1.0-alpha.15": - version "0.1.0-alpha.15" - resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.1.0-alpha.15.tgz#3650294c579e99c60dd502ce6bdea5ac55a90f3d" - integrity sha512-/388h8vXzhZ5koeXjeXIJk8gErbGdDqVzhH/W09XLKl7p//eiJ7DWdFUKVPZxUaHe8XhYrWoRzbWdBOvUXmdYA== +"@project-serum/swap@^0.1.0-alpha.17": + version "0.1.0-alpha.17" + resolved "https://registry.yarnpkg.com/@project-serum/swap/-/swap-0.1.0-alpha.17.tgz#06532c7ccbd2c2caf62b1b179bf39697b7f0aa17" + integrity sha512-9SxUqtNzbbPo9ywxaTQEtbkalhQFfUUXiOmuQs2N5rLHl9gPm0u/SKrMi35ZtJ49f+v86ZVL4w4Gbqdclc6niA== dependencies: "@project-serum/serum" "^0.13.34" "@solana/spl-token" "^0.1.3"