-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fixup Co-authored-by: Valentine Panchin <vpanchyn@markerr.com>
- Loading branch information
Showing
5 changed files
with
167 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Connection } from '../Connection'; | ||
import { Wallet } from '../wallet'; | ||
|
||
import { InitVault, Vault, VaultProgram } from '../programs/vault'; | ||
import { Keypair, PublicKey, SystemProgram } from '@solana/web3.js'; | ||
import { AccountLayout, MintLayout, NATIVE_MINT } from '@solana/spl-token'; | ||
import { CreateMint, CreateTokenAccount, Transaction } from '../programs'; | ||
import { sendTransaction } from '.'; | ||
import { TransactionsBatch } from '../utils/transactions-batch'; | ||
|
||
interface CreateVaultParams { | ||
connection: Connection; | ||
wallet: Wallet; | ||
priceMint: PublicKey; | ||
externalPriceAccount: PublicKey; | ||
} | ||
|
||
interface CreateVaultResponse { | ||
txId; | ||
vault: PublicKey; | ||
fractionMint: PublicKey; | ||
redeemTreasury: PublicKey; | ||
fractionTreasury: PublicKey; | ||
} | ||
|
||
// This command creates the external pricing oracle a vault | ||
// This gets the vault ready for adding the tokens. | ||
export const createVault = async ({ | ||
connection, | ||
wallet, | ||
priceMint = NATIVE_MINT, | ||
externalPriceAccount, | ||
}: CreateVaultParams): Promise<CreateVaultResponse> => { | ||
const accountRent = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); | ||
|
||
const mintRent = await connection.getMinimumBalanceForRentExemption(MintLayout.span); | ||
|
||
const vaultRent = await connection.getMinimumBalanceForRentExemption(Vault.MAX_VAULT_SIZE); | ||
|
||
const vault = Keypair.generate(); | ||
|
||
const vaultAuthority = await Vault.getPDA(vault.publicKey); | ||
|
||
const txBatch = new TransactionsBatch({ transactions: [] }); | ||
|
||
const fractionMint = Keypair.generate(); | ||
const fractionMintTx = new CreateMint( | ||
{ feePayer: wallet.publicKey }, | ||
{ | ||
newAccountPubkey: fractionMint.publicKey, | ||
lamports: mintRent, | ||
owner: vaultAuthority, | ||
freezeAuthority: vaultAuthority, | ||
}, | ||
); | ||
txBatch.addTransaction(fractionMintTx); | ||
txBatch.addSigner(fractionMint); | ||
|
||
const redeemTreasury = Keypair.generate(); | ||
const redeemTreasuryTx = new CreateTokenAccount( | ||
{ feePayer: wallet.publicKey }, | ||
{ | ||
newAccountPubkey: redeemTreasury.publicKey, | ||
lamports: accountRent, | ||
mint: priceMint, | ||
owner: vaultAuthority, | ||
}, | ||
); | ||
txBatch.addTransaction(redeemTreasuryTx); | ||
txBatch.addSigner(redeemTreasury); | ||
|
||
const fractionTreasury = Keypair.generate(); | ||
const fractionTreasuryTx = new CreateTokenAccount( | ||
{ feePayer: wallet.publicKey }, | ||
{ | ||
newAccountPubkey: fractionTreasury.publicKey, | ||
lamports: accountRent, | ||
mint: fractionMint.publicKey, | ||
owner: vaultAuthority, | ||
}, | ||
); | ||
txBatch.addTransaction(fractionTreasuryTx); | ||
txBatch.addSigner(fractionTreasury); | ||
|
||
const uninitializedVaultTx = new Transaction().add( | ||
SystemProgram.createAccount({ | ||
fromPubkey: wallet.publicKey, | ||
newAccountPubkey: vault.publicKey, | ||
lamports: vaultRent, | ||
space: Vault.MAX_VAULT_SIZE, | ||
programId: VaultProgram.PUBKEY, | ||
}), | ||
); | ||
txBatch.addTransaction(uninitializedVaultTx); | ||
txBatch.addSigner(vault); | ||
|
||
const initVaultTx = new InitVault( | ||
{ feePayer: wallet.publicKey }, | ||
{ | ||
vault: vault.publicKey, | ||
vaultAuthority: wallet.publicKey, | ||
fractionalTreasury: fractionTreasury.publicKey, | ||
pricingLookupAddress: externalPriceAccount, | ||
redeemTreasury: redeemTreasury.publicKey, | ||
fractionalMint: fractionMint.publicKey, | ||
allowFurtherShareCreation: true, | ||
}, | ||
); | ||
txBatch.addTransaction(initVaultTx); | ||
|
||
const txId = await sendTransaction({ | ||
connection, | ||
signers: txBatch.signers, | ||
txs: txBatch.transactions, | ||
wallet, | ||
}); | ||
|
||
return { | ||
txId, | ||
vault: vault.publicKey, | ||
fractionMint: fractionMint.publicKey, | ||
redeemTreasury: redeemTreasury.publicKey, | ||
fractionTreasury: fractionTreasury.publicKey, | ||
}; | ||
}; |
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
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,34 @@ | ||
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'); | ||
|
||
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, | ||
wallet, | ||
priceMint: NATIVE_MINT, | ||
externalPriceAccount: VAULT_EXTENRNAL_PRICE_ACCOUNT, | ||
}); | ||
|
||
await pause(20000); | ||
|
||
expect(await Vault.load(connection, vaultResponse.vault)).toHaveProperty('data'); | ||
}); | ||
}); | ||
}); |
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