Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close vault action #86

Merged
merged 4 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,24 @@ const rates = await new Coingecko().getRate([Currency.AR, Currency.SOL], Currenc
- [x] Safety Deposit Box
- [x] Vault
- [x] External Price
- [ ] Instructions
- [x] Instructions
- [x] InitVault
- [x] AddTokenToInactiveVault
- [x] ActivateVault
- [x] CombineVault
- [ ] RedeemShares
- [x] RedeemShares
- [x] WithdrawTokenFromSafetyDepositBox
- [ ] MintFractionalShares
- [ ] WithdrawSharesFromTreasury
- [ ] AddSharesToTreasury
- [x] MintFractionalShares
- [x] WithdrawSharesFromTreasury
- [x] AddSharesToTreasury
- [x] UpdateExternalPriceAccount
- [x] SetAuthority
- [ ] Actions
- [x] CreateVault
- [x] CloseVault
- [ ] AddTokensToVault
- [ ] SetVaultAndAuctionAuthorities
- [ ] UnwindVault
- [ ] Candy Machine
- [ ] Accounts
- [ ] Candy Machine
Expand Down
121 changes: 121 additions & 0 deletions src/actions/closeVault.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Connection } from '../Connection';
import { Wallet } from '../wallet';

import { ActivateVault, CombineVault, Vault } from '../programs/vault';
import { Keypair, PublicKey } from '@solana/web3.js';
import { AccountLayout, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { CreateTokenAccount, Transaction } from '../programs';
import { sendTransaction } from '../actions/transactions';
import BN from 'bn.js';
import { TransactionsBatch } from '../utils/transactions-batch';

interface CloseVaultParams {
connection: Connection;
wallet: Wallet;
vault: PublicKey;
priceMint: PublicKey;
}

interface CloseVaultResponse {
txId;
}

// This command "closes" the vault, by activating & combining it in one go, handing it over to the auction manager
// authority (that may or may not exist yet.)
export const closeVault = async ({
connection,
wallet,
vault,
priceMint,
}: CloseVaultParams): Promise<CloseVaultResponse> => {
const accountRent = await connection.getMinimumBalanceForRentExemption(AccountLayout.span);

const fractionMintAuthority = await Vault.getPDA(vault);

const txBatch = new TransactionsBatch({ transactions: [] });

const txOptions = { feePayer: wallet.publicKey };

const {
data: { fractionMint, fractionTreasury, redeemTreasury, pricingLookupAddress },
} = await Vault.load(connection, vault);

const fractionMintKey = new PublicKey(fractionMint);
const fractionTreasuryKey = new PublicKey(fractionTreasury);
const redeemTreasuryKey = new PublicKey(redeemTreasury);
const pricingLookupAddressKey = new PublicKey(pricingLookupAddress);

const activateVaultTx = new ActivateVault(txOptions, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you activate in the close command?

vault,
numberOfShares: new BN(0),
fractionMint: fractionMintKey,
fractionTreasury: fractionTreasuryKey,
fractionMintAuthority,
vaultAuthority: wallet.publicKey,
});
txBatch.addTransaction(activateVaultTx);

const outstandingShareAccount = Keypair.generate();
const outstandingShareAccountTx = new CreateTokenAccount(txOptions, {
newAccountPubkey: outstandingShareAccount.publicKey,
lamports: accountRent,
mint: fractionMintKey,
owner: wallet.publicKey,
});
txBatch.addTransaction(outstandingShareAccountTx);
txBatch.addSigner(outstandingShareAccount);

const payingTokenAccount = Keypair.generate();
const payingTokenAccountTx = new CreateTokenAccount(txOptions, {
newAccountPubkey: payingTokenAccount.publicKey,
lamports: accountRent,
mint: priceMint,
owner: wallet.publicKey,
});
txBatch.addTransaction(payingTokenAccountTx);
txBatch.addSigner(payingTokenAccount);

const transferAuthority = Keypair.generate();

const createApproveTx = (account: Keypair) =>
new Transaction().add(
Token.createApproveInstruction(
TOKEN_PROGRAM_ID,
account.publicKey,
transferAuthority.publicKey,
wallet.publicKey,
[],
0,
),
);

txBatch.addTransaction(createApproveTx(payingTokenAccount));
txBatch.addTransaction(createApproveTx(outstandingShareAccount));
txBatch.addSigner(transferAuthority);

const combineVaultTx = new CombineVault(txOptions, {
vault,
outstandingShareTokenAccount: outstandingShareAccount.publicKey,
payingTokenAccount: payingTokenAccount.publicKey,
fractionMint: fractionMintKey,
fractionTreasury: fractionTreasuryKey,
redeemTreasury: redeemTreasuryKey,
burnAuthority: fractionMintAuthority,
externalPriceAccount: pricingLookupAddressKey,
transferAuthority: transferAuthority.publicKey,
vaultAuthority: wallet.publicKey,
newVaultAuthority: wallet.publicKey,
});
txBatch.addTransaction(combineVaultTx);

const txId = await sendTransaction({
connection,
signers: txBatch.signers,
txs: txBatch.transactions,
wallet,
});

return {
txId,
};
};
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './transactions';
export * from './initStore';
export * from './mintNFT';
export * from './mintEditionFromMaster';
export * from './closeVault';
export * from './createVault';
export * from './createMetadata';
export * from './createMasterEdition';
Expand Down
40 changes: 40 additions & 0 deletions test/actions/closeVault.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { NATIVE_MINT } from '@solana/spl-token';
import { Connection, NodeWallet } from '../../src';
import { createVault, closeVault } from '../../src/actions';
import { FEE_PAYER, pause, VAULT_EXTENRNAL_PRICE_ACCOUNT } from '../utils';
import { Vault, VaultState } from '../../src/programs/vault';

describe('closing a Vault', () => {
const connection = new Connection('devnet');
const wallet = new NodeWallet(FEE_PAYER);

describe('success', () => {
test('closes vault', async () => {
let vault;
const vaultResponse = await createVault({
connection,
wallet,
priceMint: NATIVE_MINT,
externalPriceAccount: VAULT_EXTENRNAL_PRICE_ACCOUNT,
});

await pause(20000);
vault = await Vault.load(connection, vaultResponse.vault);
expect(vault).toHaveProperty('data');
expect(vault.data.state).toEqual(VaultState.Inactive);

await closeVault({
connection,
wallet,
vault: vaultResponse.vault,
priceMint: NATIVE_MINT,
});

await pause(20000);

vault = await Vault.load(connection, vaultResponse.vault);
expect(vault).toHaveProperty('data');
expect(vault.data.state).toEqual(VaultState.Combined);
}, 60000);
});
});
18 changes: 5 additions & 13 deletions test/actions/createVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ import { NATIVE_MINT } from '@solana/spl-token';
import { Connection, NodeWallet } from '../../src';
import { createVault } from '../../src/actions';
import { FEE_PAYER, pause, VAULT_EXTENRNAL_PRICE_ACCOUNT } from '../utils';
import { mockAxios200 } from './shared';
import { Vault } from '../../src/programs/vault';

jest.mock('axios');
import { Vault, VaultState } from '../../src/programs/vault';

describe('creating a Vault', () => {
const connection = new Connection('devnet');
const wallet = new NodeWallet(FEE_PAYER);

jest.setTimeout(30000);

describe('success', () => {
beforeEach(() => {
mockAxios200(wallet);
});

test('generates vault', async () => {
const vaultResponse = await createVault({
connection,
Expand All @@ -27,8 +18,9 @@ describe('creating a Vault', () => {
});

await pause(20000);

expect(await Vault.load(connection, vaultResponse.vault)).toHaveProperty('data');
});
const vault = await Vault.load(connection, vaultResponse.vault);
expect(vault).toHaveProperty('data');
expect(vault.data.state).toEqual(VaultState.Inactive);
}, 30000);
});
});
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const SOURCE_PUBKEY = new PublicKey('4CkQJBxhU8EZ2UjhigbtdaPbpTe6mqf811fi
export const DESTINATION_PUBKEY = new PublicKey('CZXESU6tu9m4YDs2wfQFbXmjbaDtJKBgurgYzGmeoArh');

export const VAULT_EXTENRNAL_PRICE_ACCOUNT = new PublicKey(
'4FB1P1tewZsXq7FbrSDqkQw319FfWMA4Go2DG3Rdt39K',
'58S2MNcuS79ncBc5xi1T8jdS98jcXJbXqM5UvGvgmwcr',
);

export const mockTransaction: TransactionCtorFields = {
Expand Down