bankrun.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import type { Program } from '@coral-xyz/anchor';
  4. import type NodeWallet from '@coral-xyz/anchor/dist/cjs/nodewallet';
  5. import {
  6. ASSOCIATED_TOKEN_PROGRAM_ID,
  7. TOKEN_PROGRAM_ID,
  8. createMint,
  9. getAssociatedTokenAddressSync,
  10. getOrCreateAssociatedTokenAccount,
  11. mintTo,
  12. } from '@solana/spl-token';
  13. import { PublicKey } from '@solana/web3.js';
  14. import { BankrunProvider } from 'anchor-bankrun';
  15. import { startAnchor } from 'solana-bankrun';
  16. import type { Fundraiser } from '../target/types/fundraiser';
  17. const IDL = require('../target/idl/fundraiser.json');
  18. const PROGRAM_ID = new PublicKey(IDL.address);
  19. describe('fundraiser bankrun', async () => {
  20. const context = await startAnchor('', [{ name: 'fundraiser', programId: PROGRAM_ID }], []);
  21. const provider = new BankrunProvider(context);
  22. anchor.setProvider(provider);
  23. const wallet = provider.wallet as anchor.Wallet;
  24. const program = new anchor.Program<Fundraiser>(IDL, provider);
  25. const maker = anchor.web3.Keypair.generate();
  26. let mint: anchor.web3.PublicKey;
  27. let contributorATA: anchor.web3.PublicKey;
  28. let makerATA: anchor.web3.PublicKey;
  29. const fundraiser = anchor.web3.PublicKey.findProgramAddressSync([Buffer.from('fundraiser'), maker.publicKey.toBuffer()], program.programId)[0];
  30. const contributor = anchor.web3.PublicKey.findProgramAddressSync(
  31. [Buffer.from('contributor'), fundraiser.toBuffer(), provider.publicKey.toBuffer()],
  32. program.programId,
  33. )[0];
  34. const confirm = async (signature: string): Promise<string> => {
  35. const block = await provider.connection.getLatestBlockhash();
  36. await provider.connection.confirmTransaction({
  37. signature,
  38. ...block,
  39. });
  40. return signature;
  41. };
  42. it('Test Preparation', async () => {
  43. const airdrop = await provider.connection.requestAirdrop(maker.publicKey, 1 * anchor.web3.LAMPORTS_PER_SOL).then(confirm);
  44. console.log('\nAirdropped 1 SOL to maker', airdrop);
  45. mint = await createMint(provider.connection, wallet.payer, provider.publicKey, provider.publicKey, 6);
  46. console.log('Mint created', mint.toBase58());
  47. contributorATA = (await getOrCreateAssociatedTokenAccount(provider.connection, wallet.payer, mint, wallet.publicKey)).address;
  48. makerATA = (await getOrCreateAssociatedTokenAccount(provider.connection, wallet.payer, mint, maker.publicKey)).address;
  49. const mintTx = await mintTo(provider.connection, wallet.payer, mint, contributorATA, provider.publicKey, 1_000_000_0);
  50. console.log('Minted 10 tokens to contributor', mintTx);
  51. });
  52. it('Initialize Fundaraiser', async () => {
  53. const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);
  54. const tx = await program.methods
  55. .initialize(new anchor.BN(30000000), 0)
  56. .accountsPartial({
  57. maker: maker.publicKey,
  58. fundraiser,
  59. mintToRaise: mint,
  60. vault,
  61. systemProgram: anchor.web3.SystemProgram.programId,
  62. tokenProgram: TOKEN_PROGRAM_ID,
  63. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  64. })
  65. .signers([maker])
  66. .rpc()
  67. .then(confirm);
  68. console.log('\nInitialized fundraiser Account');
  69. console.log('Your transaction signature', tx);
  70. });
  71. it('Contribute to Fundraiser', async () => {
  72. const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);
  73. const tx = await program.methods
  74. .contribute(new anchor.BN(1000000))
  75. .accountsPartial({
  76. contributor: provider.publicKey,
  77. fundraiser,
  78. contributorAccount: contributor,
  79. contributorAta: contributorATA,
  80. vault,
  81. tokenProgram: TOKEN_PROGRAM_ID,
  82. })
  83. .rpc()
  84. .then(confirm);
  85. console.log('\nContributed to fundraiser', tx);
  86. console.log('Your transaction signature', tx);
  87. console.log('Vault balance', (await provider.connection.getTokenAccountBalance(vault)).value.amount);
  88. const contributorAccount = await program.account.contributor.fetch(contributor);
  89. console.log('Contributor balance', contributorAccount.amount.toString());
  90. });
  91. it('Contribute to Fundraiser', async () => {
  92. const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);
  93. const tx = await program.methods
  94. .contribute(new anchor.BN(1000000))
  95. .accountsPartial({
  96. contributor: provider.publicKey,
  97. fundraiser,
  98. contributorAccount: contributor,
  99. contributorAta: contributorATA,
  100. vault,
  101. tokenProgram: TOKEN_PROGRAM_ID,
  102. })
  103. .rpc()
  104. .then(confirm);
  105. console.log('\nContributed to fundraiser', tx);
  106. console.log('Your transaction signature', tx);
  107. console.log('Vault balance', (await provider.connection.getTokenAccountBalance(vault)).value.amount);
  108. const contributorAccount = await program.account.contributor.fetch(contributor);
  109. console.log('Contributor balance', contributorAccount.amount.toString());
  110. });
  111. it('Contribute to Fundraiser - Robustness Test', async () => {
  112. try {
  113. const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);
  114. const tx = await program.methods
  115. .contribute(new anchor.BN(2000000))
  116. .accountsPartial({
  117. contributor: provider.publicKey,
  118. fundraiser,
  119. contributorAccount: contributor,
  120. contributorAta: contributorATA,
  121. vault,
  122. tokenProgram: TOKEN_PROGRAM_ID,
  123. })
  124. .rpc()
  125. .then(confirm);
  126. console.log('\nContributed to fundraiser', tx);
  127. console.log('Your transaction signature', tx);
  128. console.log('Vault balance', (await provider.connection.getTokenAccountBalance(vault)).value.amount);
  129. } catch (error) {
  130. console.log('\nError contributing to fundraiser');
  131. console.log(error.msg);
  132. }
  133. });
  134. it('Check contributions - Robustness Test', async () => {
  135. try {
  136. const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);
  137. const tx = await program.methods
  138. .checkContributions()
  139. .accountsPartial({
  140. maker: maker.publicKey,
  141. mintToRaise: mint,
  142. fundraiser,
  143. makerAta: makerATA,
  144. vault,
  145. tokenProgram: TOKEN_PROGRAM_ID,
  146. })
  147. .signers([maker])
  148. .rpc()
  149. .then(confirm);
  150. console.log('\nChecked contributions');
  151. console.log('Your transaction signature', tx);
  152. console.log('Vault balance', (await provider.connection.getTokenAccountBalance(vault)).value.amount);
  153. } catch (error) {
  154. console.log('\nError checking contributions');
  155. console.log(error.msg);
  156. }
  157. });
  158. it('Refund Contributions', async () => {
  159. const vault = getAssociatedTokenAddressSync(mint, fundraiser, true);
  160. const contributorAccount = await program.account.contributor.fetch(contributor);
  161. console.log('\nContributor balance', contributorAccount.amount.toString());
  162. const tx = await program.methods
  163. .refund()
  164. .accountsPartial({
  165. contributor: provider.publicKey,
  166. maker: maker.publicKey,
  167. mintToRaise: mint,
  168. fundraiser,
  169. contributorAccount: contributor,
  170. contributorAta: contributorATA,
  171. vault,
  172. tokenProgram: TOKEN_PROGRAM_ID,
  173. systemProgram: anchor.web3.SystemProgram.programId,
  174. })
  175. .rpc()
  176. .then(confirm);
  177. console.log('\nRefunded contributions', tx);
  178. console.log('Your transaction signature', tx);
  179. console.log('Vault balance', (await provider.connection.getTokenAccountBalance(vault)).value.amount);
  180. });
  181. });