Pass through close to swap client

This commit is contained in:
armaniferrante 2021-05-15 12:58:05 -07:00
parent 2205d3f6c7
commit e17ddbe924
No known key found for this signature in database
GPG Key ID: 58BEF301E91F7828
2 changed files with 16 additions and 4 deletions

View File

@ -257,7 +257,14 @@ function TokenName({ mint }: { mint: PublicKey }) {
function SwapButton() {
const styles = useStyles();
const { fromMint, toMint, fromAmount, toAmount, slippage } = useSwapContext();
const {
fromMint,
toMint,
fromAmount,
toAmount,
slippage,
isClosingNewAccounts,
} = useSwapContext();
const { swapClient } = useDexContext();
const fromMintInfo = useMint(fromMint);
const toMintInfo = useMint(toMint);
@ -271,9 +278,9 @@ function SwapButton() {
throw new Error("Unable to calculate mint decimals");
}
const amount = new BN(fromAmount).muln(10 ** fromMintInfo.decimals);
const minExpectedSwapAmount = new BN(
(toAmount * (100 - slippage)) / 100
).muln(10 ** toMintInfo.decimals);
const minExpectedSwapAmount = new BN(toAmount * 10 ** toMintInfo.decimals)
.muln(100 - slippage)
.divn(100);
const fromOpenOrders = fromMarket
? openOrders.get(fromMarket?.address.toString())
: undefined;
@ -291,6 +298,8 @@ function SwapButton() {
toMarket,
fromOpenOrders: fromOpenOrders ? fromOpenOrders[0].address : undefined,
toOpenOrders: toOpenOrders ? toOpenOrders[0].address : undefined,
// Auto close newly created open orders accounts.
close: isClosingNewAccounts,
});
};
return (

View File

@ -1,4 +1,5 @@
import React, { useContext, useState } from "react";
import * as assert from "assert";
import { PublicKey } from "@solana/web3.js";
import { SRM_MINT, USDC_MINT } from "../../utils/pubkeys";
import { useFairRoute } from "./Dex";
@ -54,6 +55,8 @@ export function SwapContextProvider(props: any) {
const [fairOverride, setFairOverride] = useState<number | null>(null);
const fair = _useSwapFair(fromMint, toMint, fairOverride);
assert.ok(slippage >= 0);
const swapToFromMints = () => {
const oldFrom = fromMint;
const oldFromAmount = fromAmount;