Handle pools containing wrapped SOL

This commit is contained in:
Gary Wang 2020-12-11 03:50:09 +08:00
parent ce78c4c6b3
commit 1e59dacd84
2 changed files with 110 additions and 39 deletions

View File

@ -10,7 +10,12 @@ import {
} from '../../../utils/markets';
import { sendTransaction } from '../../../utils/send';
import { notify } from '../../../utils/notifications';
import { PublicKey, Transaction } from '@solana/web3.js';
import {
Account,
PublicKey,
SystemProgram,
Transaction,
} from '@solana/web3.js';
import { AutoComplete, Button, Input, Select, Tabs } from 'antd';
import {
createAssociatedTokenAccount,
@ -137,7 +142,7 @@ function AddAssetTab({ poolInfo }: TabParams) {
transaction.add(
AdminControlledPoolInstructions.addAsset(poolInfo, vaultAddress),
);
return transaction;
return [transaction, []];
},
);
@ -172,7 +177,7 @@ function RemoveAssetTab({ poolInfo }: TabParams) {
transaction.add(
AdminControlledPoolInstructions.removeAsset(poolInfo, vaultAddress),
);
return transaction;
return [transaction, []];
},
);
@ -228,16 +233,52 @@ function DepositTab({ poolInfo }: TabParams) {
parseFloat(quantity) * 10 ** mintDecimals,
);
const wrappedSolAccount =
mintAddress.equals(TokenInstructions.WRAPPED_SOL_MINT) &&
walletTokenAccount.pubkey.equals(wallet.publicKey)
? new Account()
: null;
const transaction = new Transaction();
transaction.add(
TokenInstructions.transfer({
source: walletTokenAccount.pubkey,
destination: vaultAddress,
amount: parsedQuantity,
owner: wallet.publicKey,
}),
);
return transaction;
const signers: Account[] = [];
if (wrappedSolAccount) {
transaction.add(
SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
lamports: parsedQuantity + 2.04e6,
newAccountPubkey: wrappedSolAccount.publicKey,
programId: TokenInstructions.TOKEN_PROGRAM_ID,
space: 165,
}),
TokenInstructions.initializeAccount({
account: wrappedSolAccount.publicKey,
mint: TokenInstructions.WRAPPED_SOL_MINT,
owner: wallet.publicKey,
}),
TokenInstructions.transfer({
source: wrappedSolAccount.publicKey,
destination: vaultAddress,
amount: parsedQuantity,
owner: wallet.publicKey,
}),
TokenInstructions.closeAccount({
source: wrappedSolAccount.publicKey,
destination: walletTokenAccount.pubkey,
owner: wallet.publicKey,
}),
);
signers.push(wrappedSolAccount);
} else {
transaction.add(
TokenInstructions.transfer({
source: walletTokenAccount.pubkey,
destination: vaultAddress,
amount: parsedQuantity,
owner: wallet.publicKey,
}),
);
}
return [transaction, signers];
},
true,
);
@ -300,7 +341,31 @@ function WithdrawTab({ poolInfo }: TabParams) {
parseFloat(quantity) * 10 ** mintDecimals,
);
const wrappedSolAccount =
mintAddress.equals(TokenInstructions.WRAPPED_SOL_MINT) &&
walletTokenAccount.pubkey.equals(wallet.publicKey)
? new Account()
: null;
const transaction = new Transaction();
const signers: Account[] = [];
if (wrappedSolAccount) {
transaction.add(
SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
lamports: 2.04e6,
newAccountPubkey: wrappedSolAccount.publicKey,
programId: TokenInstructions.TOKEN_PROGRAM_ID,
space: 165,
}),
TokenInstructions.initializeAccount({
account: wrappedSolAccount.publicKey,
mint: TokenInstructions.WRAPPED_SOL_MINT,
owner: wallet.publicKey,
}),
);
signers.push(wrappedSolAccount);
}
transaction.add(
AdminControlledPoolInstructions.approveDelegate(
poolInfo,
@ -308,14 +373,32 @@ function WithdrawTab({ poolInfo }: TabParams) {
wallet.publicKey,
new BN(parsedQuantity),
),
TokenInstructions.transfer({
source: vaultAddress,
destination: walletTokenAccount.pubkey,
amount: parsedQuantity,
owner: wallet.publicKey,
}),
);
return transaction;
if (wrappedSolAccount) {
transaction.add(
TokenInstructions.transfer({
source: vaultAddress,
destination: wrappedSolAccount.publicKey,
amount: parsedQuantity,
owner: wallet.publicKey,
}),
TokenInstructions.closeAccount({
source: wrappedSolAccount.publicKey,
destination: walletTokenAccount.pubkey,
owner: wallet.publicKey,
}),
);
} else {
transaction.add(
TokenInstructions.transfer({
source: vaultAddress,
destination: walletTokenAccount.pubkey,
amount: parsedQuantity,
owner: wallet.publicKey,
}),
);
}
return [transaction, signers];
},
);
@ -356,7 +439,7 @@ function UpdateFeeTab({ poolInfo }: TabParams) {
Math.round(parseFloat(feeRate) * 1_000_000),
),
);
return transaction;
return [transaction, []];
},
);
@ -375,7 +458,7 @@ function UpdateFeeTab({ poolInfo }: TabParams) {
function useOnSubmitHandler(
description: string,
makeTransaction: () => Promise<Transaction | null | undefined>,
makeTransaction: () => Promise<[Transaction, Account[]]>,
refresh = false,
): [(FormEvent) => void, boolean] {
const connection = useConnection();
@ -392,11 +475,8 @@ function useOnSubmitHandler(
if (!connected) {
throw new Error('Wallet not connected');
}
const transaction = await makeTransaction();
if (!transaction) {
return;
}
await sendTransaction({ connection, wallet, transaction });
const [transaction, signers] = await makeTransaction();
await sendTransaction({ connection, wallet, transaction, signers });
if (refresh) {
refreshAllCaches();
}

View File

@ -1549,9 +1549,9 @@
buffer-layout "^1.2.0"
"@project-serum/pool@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@project-serum/pool/-/pool-0.2.0.tgz#ce79b63a58e2dc7f5ae1387948ee1d91cca64b01"
integrity sha512-Gsd1G+S/7qx/2c49zIG1WF7xjElghRsVXUQewi10Rs7zY0ryS0jmIKHf6pPxn4Bpc0GKZmENrvwe2tChgY3+CA==
version "0.2.1"
resolved "https://registry.yarnpkg.com/@project-serum/pool/-/pool-0.2.1.tgz#a4c43cdc22f31924343c2aacff7c966111e95e45"
integrity sha512-dpxJCS18uFsFTejtKQDBZSn6/1xRHfN3Gm0TniL3T68EKhEr1KMYbB7oWKoS4aMGiFMxHM4xyYivfcQQFoRIeQ==
dependencies:
"@project-serum/associated-token" "^0.1.0"
"@project-serum/borsh" "^0.0.1-alpha.0"
@ -1559,7 +1559,7 @@
bn.js "^5.1.2"
buffer-layout "^1.2.0"
"@project-serum/serum@^0.13.14":
"@project-serum/serum@^0.13.14", "@project-serum/serum@^0.13.8":
version "0.13.14"
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.14.tgz#f39e6d3d1aaab811fbec17a304c926522e477299"
integrity sha512-a/Irc8L+K2rOsJcl2xRKWJtc8WCqf6O4iWhlOOKhI+e0mTEvRUmecXn0yzqnUIUwgRmtavmirJAzXFnjCpbKww==
@ -1568,15 +1568,6 @@
bn.js "^5.1.2"
buffer-layout "^1.2.0"
"@project-serum/serum@^0.13.8":
version "0.13.10"
resolved "https://registry.yarnpkg.com/@project-serum/serum/-/serum-0.13.10.tgz#0c4fdcad34e5dcb11561b8dc650903d2420f91c4"
integrity sha512-60Vl8pMiKSjdjRky4O9aajUMr0BIdYzHZyAX16Lr+gu03x1iMgL7bih23ywuNONo3vBhA1PjHpwA9J/fbrE2vQ==
dependencies:
"@solana/web3.js" "0.86.1"
bn.js "^5.1.2"
buffer-layout "^1.2.0"
"@project-serum/sol-wallet-adapter@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@project-serum/sol-wallet-adapter/-/sol-wallet-adapter-0.1.1.tgz#f4358e5210c2a059a28e4c9972b25b4345412f97"