-
Notifications
You must be signed in to change notification settings - Fork 77
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, { | ||
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?