balances.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { Transaction, SystemProgram, sendAndConfirmTransaction } from '@solana/web3.js';
  4. import { loadContract } from './setup';
  5. import { BN } from '@coral-xyz/anchor';
  6. describe('Deploy solang contract and test', function () {
  7. this.timeout(500000);
  8. it('balances', async function () {
  9. let { program, storage, payer, provider } = await loadContract('balances', []);
  10. let res = await program.methods.getBalance(payer.publicKey)
  11. .accounts({ dataAccount: storage.publicKey })
  12. .remainingAccounts([{ pubkey: payer.publicKey, isSigner: false, isWritable: false }])
  13. .view();
  14. let bal = Number(res);
  15. let rpc_bal = await provider.connection.getBalance(payer.publicKey);
  16. expect(bal + 5000).toBe(rpc_bal);
  17. // we wish to test the `.send()` function, so first top up the storage balance
  18. let before_bal = await provider.connection.getBalance(storage.publicKey);
  19. /// transfer some lamports to the storage account
  20. const transaction = new Transaction().add(
  21. SystemProgram.transfer({
  22. fromPubkey: payer.publicKey,
  23. toPubkey: storage.publicKey,
  24. lamports: 1500,
  25. }),
  26. );
  27. // Sign transaction, broadcast, and confirm
  28. await sendAndConfirmTransaction(provider.connection, transaction, [payer]);
  29. await program.methods.send(payer.publicKey, new BN(500))
  30. .accounts({ dataAccount: storage.publicKey })
  31. .remainingAccounts([
  32. { pubkey: storage.publicKey, isSigner: true, isWritable: true },
  33. { pubkey: payer.publicKey, isSigner: false, isWritable: true }
  34. ])
  35. .signers([storage])
  36. .rpc();
  37. expect(await provider.connection.getBalance(storage.publicKey)).toBe(before_bal + 1000);
  38. });
  39. });