Wrap/unwrap sol in the same tx
This commit is contained in:
parent
958f809be5
commit
fd035017a5
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@project-serum/swap-ui",
|
"name": "@project-serum/swap-ui",
|
||||||
"version": "0.1.10",
|
"version": "0.1.11",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"homepage": "https://github.com/project-serum/swap-ui",
|
"homepage": "https://github.com/project-serum/swap-ui",
|
||||||
|
|
|
@ -360,85 +360,83 @@ export function SwapButton() {
|
||||||
throw new Error("Quote mint not found");
|
throw new Error("Quote mint not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
// All transactions to send for the swap.
|
|
||||||
let txs: { tx: Transaction; signers: Array<Signer | undefined> }[] = [];
|
|
||||||
const amount = new BN(fromAmount * 10 ** fromMintInfo.decimals);
|
const amount = new BN(fromAmount * 10 ** fromMintInfo.decimals);
|
||||||
|
|
||||||
const isSol = fromMint.equals(SOL_MINT) || toMint.equals(SOL_MINT);
|
const isSol = fromMint.equals(SOL_MINT) || toMint.equals(SOL_MINT);
|
||||||
const wrappedSolAccount = isSol ? Keypair.generate() : undefined;
|
const wrappedSolAccount = isSol ? Keypair.generate() : undefined;
|
||||||
|
|
||||||
// Wrap the SOL into wrapped SOL.
|
|
||||||
if (isSol) {
|
|
||||||
txs.push(
|
|
||||||
await wrapSol(
|
|
||||||
swapClient.program.provider,
|
|
||||||
wrappedSolAccount as Keypair,
|
|
||||||
fromMint,
|
|
||||||
amount
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build the swap.
|
// Build the swap.
|
||||||
txs.push(
|
let txs = await (async () => {
|
||||||
...(await (async () => {
|
if (!fromMarket) {
|
||||||
if (!fromMarket) {
|
throw new Error("Market undefined");
|
||||||
throw new Error("Market undefined");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const minExchangeRate = {
|
const minExchangeRate = {
|
||||||
rate: new BN((10 ** toMintInfo.decimals * FEE_MULTIPLIER) / fair)
|
rate: new BN((10 ** toMintInfo.decimals * FEE_MULTIPLIER) / fair)
|
||||||
.muln(100 - slippage)
|
.muln(100 - slippage)
|
||||||
.divn(100),
|
.divn(100),
|
||||||
fromDecimals: fromMintInfo.decimals,
|
fromDecimals: fromMintInfo.decimals,
|
||||||
quoteDecimals: quoteMintInfo.decimals,
|
quoteDecimals: quoteMintInfo.decimals,
|
||||||
strict: isStrict,
|
strict: isStrict,
|
||||||
};
|
};
|
||||||
const fromOpenOrders = fromMarket
|
const fromOpenOrders = fromMarket
|
||||||
? openOrders.get(fromMarket?.address.toString())
|
? openOrders.get(fromMarket?.address.toString())
|
||||||
: undefined;
|
: undefined;
|
||||||
const toOpenOrders = toMarket
|
const toOpenOrders = toMarket
|
||||||
? openOrders.get(toMarket?.address.toString())
|
? openOrders.get(toMarket?.address.toString())
|
||||||
: undefined;
|
: undefined;
|
||||||
const fromWalletAddr = fromMint.equals(SOL_MINT)
|
const fromWalletAddr = fromMint.equals(SOL_MINT)
|
||||||
? wrappedSolAccount!.publicKey
|
? wrappedSolAccount!.publicKey
|
||||||
: fromWallet
|
: fromWallet
|
||||||
? fromWallet.publicKey
|
? fromWallet.publicKey
|
||||||
: undefined;
|
: undefined;
|
||||||
const toWalletAddr = toMint.equals(SOL_MINT)
|
const toWalletAddr = toMint.equals(SOL_MINT)
|
||||||
? wrappedSolAccount!.publicKey
|
? wrappedSolAccount!.publicKey
|
||||||
: toWallet
|
: toWallet
|
||||||
? toWallet.publicKey
|
? toWallet.publicKey
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
return await swapClient.swapTxs({
|
return await swapClient.swapTxs({
|
||||||
fromMint,
|
fromMint,
|
||||||
toMint,
|
toMint,
|
||||||
quoteMint,
|
quoteMint,
|
||||||
amount,
|
amount,
|
||||||
minExchangeRate,
|
minExchangeRate,
|
||||||
referral,
|
referral,
|
||||||
fromMarket,
|
fromMarket,
|
||||||
toMarket,
|
toMarket,
|
||||||
// Automatically created if undefined.
|
// Automatically created if undefined.
|
||||||
fromOpenOrders: fromOpenOrders
|
fromOpenOrders: fromOpenOrders ? fromOpenOrders[0].address : undefined,
|
||||||
? fromOpenOrders[0].address
|
toOpenOrders: toOpenOrders ? toOpenOrders[0].address : undefined,
|
||||||
: undefined,
|
fromWallet: fromWalletAddr,
|
||||||
toOpenOrders: toOpenOrders ? toOpenOrders[0].address : undefined,
|
toWallet: toWalletAddr,
|
||||||
fromWallet: fromWalletAddr,
|
quoteWallet: quoteWallet ? quoteWallet.publicKey : undefined,
|
||||||
toWallet: toWalletAddr,
|
// Auto close newly created open orders accounts.
|
||||||
quoteWallet: quoteWallet ? quoteWallet.publicKey : undefined,
|
close: isClosingNewAccounts,
|
||||||
// Auto close newly created open orders accounts.
|
});
|
||||||
close: isClosingNewAccounts,
|
})();
|
||||||
});
|
|
||||||
})())
|
|
||||||
);
|
|
||||||
|
|
||||||
// Unwrap the SOL.
|
// If swapping SOL, then insert a wrap/unwrap instruction.
|
||||||
if (isSol) {
|
if (isSol) {
|
||||||
txs.push(
|
if (txs.length > 1) {
|
||||||
unwrapSol(swapClient.program.provider, wrappedSolAccount as Keypair)
|
throw new Error("SOL must be swapped in a single transaction");
|
||||||
|
}
|
||||||
|
const { tx: wrapTx, signers: wrapSigners } = await wrapSol(
|
||||||
|
swapClient.program.provider,
|
||||||
|
wrappedSolAccount as Keypair,
|
||||||
|
fromMint,
|
||||||
|
amount
|
||||||
);
|
);
|
||||||
|
const { tx: unwrapTx, signers: unwrapSigners } = unwrapSol(
|
||||||
|
swapClient.program.provider,
|
||||||
|
wrappedSolAccount as Keypair
|
||||||
|
);
|
||||||
|
const tx = new Transaction();
|
||||||
|
tx.add(wrapTx);
|
||||||
|
tx.add(txs[0].tx);
|
||||||
|
tx.add(unwrapTx);
|
||||||
|
txs[0].tx = tx;
|
||||||
|
txs[0].signers.push(...wrapSigners);
|
||||||
|
txs[0].signers.push(...unwrapSigners);
|
||||||
}
|
}
|
||||||
|
|
||||||
await swapClient.program.provider.sendAll(txs);
|
await swapClient.program.provider.sendAll(txs);
|
||||||
|
|
Loading…
Reference in New Issue