balances.spec.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { Transaction, SystemProgram, sendAndConfirmTransaction } from '@solana/web3.js';
  4. import { loadContractAndCallConstructor } 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 loadContractAndCallConstructor('balances', []);
  10. let res = await program.methods.getBalance(payer.publicKey)
  11. .remainingAccounts([{ pubkey: payer.publicKey, isSigner: false, isWritable: false }])
  12. .view();
  13. let bal = Number(res);
  14. let rpc_bal = await provider.connection.getBalance(payer.publicKey);
  15. expect(bal + 5000).toBe(rpc_bal);
  16. // we wish to test the `.send()` function, so first top up the storage balance
  17. let before_bal = await provider.connection.getBalance(storage.publicKey);
  18. /// transfer some lamports to the storage account
  19. const transaction = new Transaction().add(
  20. SystemProgram.transfer({
  21. fromPubkey: payer.publicKey,
  22. toPubkey: storage.publicKey,
  23. lamports: 1500,
  24. }),
  25. );
  26. // Sign transaction, broadcast, and confirm
  27. await sendAndConfirmTransaction(provider.connection, transaction, [payer]);
  28. await program.methods.send(payer.publicKey, new BN(500))
  29. .remainingAccounts([
  30. { pubkey: storage.publicKey, isSigner: true, isWritable: true },
  31. { pubkey: payer.publicKey, isSigner: false, isWritable: true }
  32. ])
  33. .signers([storage])
  34. .rpc();
  35. expect(await provider.connection.getBalance(storage.publicKey)).toBe(before_bal + 1000);
  36. });
  37. });