balances.spec.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { weight, createConnection, deploy, transaction, aliceKeypair, daveKeypair, query } from './index';
  4. import { ContractPromise } from '@polkadot/api-contract';
  5. import { ApiPromise } from '@polkadot/api';
  6. describe('Deploy balances contract and test', () => {
  7. let conn: ApiPromise;
  8. before(async function () {
  9. conn = await createConnection();
  10. });
  11. after(async function () {
  12. await conn.disconnect();
  13. });
  14. it('balances', async function () {
  15. this.timeout(50000);
  16. const alice = aliceKeypair();
  17. const dave = daveKeypair();
  18. // call the constructors
  19. let deploy_contract = await deploy(conn, alice, 'balances.contract', BigInt(1e7));
  20. let contract = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  21. let { output: contractRpcBal } = await query(conn, alice, contract, "getBalance");
  22. let { data: { free: contractQueryBalBefore } } = await conn.query.system.account(String(deploy_contract.address));
  23. // The "Existential Deposit" (aka. minimum balance) is part of the free balance;
  24. // to get the actual free balance from a contracts point of view we subtract it.
  25. const ED = 1000000000n;
  26. expect(contractRpcBal?.toString()).toBe((contractQueryBalBefore.toBigInt() - ED).toString());
  27. let gasLimit = await weight(conn, contract, "payMe", undefined, 1000000n);
  28. let tx = contract.tx.payMe({ gasLimit, value: 1000000n });
  29. await transaction(tx, alice);
  30. let { data: { free: contractQueryBalAfter } } = await conn.query.system.account(String(deploy_contract.address));
  31. expect(contractQueryBalAfter.toBigInt()).toEqual(contractQueryBalBefore.toBigInt() + 1000000n);
  32. let { data: { free: daveBal1 } } = await conn.query.system.account(dave.address);
  33. gasLimit = await weight(conn, contract, "transfer", [dave.address, 20000]);
  34. let tx1 = contract.tx.transfer({ gasLimit }, dave.address, 20000);
  35. await transaction(tx1, alice);
  36. let { data: { free: daveBal2 } } = await conn.query.system.account(dave.address);
  37. expect(daveBal2.toBigInt()).toEqual(daveBal1.toBigInt() + 20000n);
  38. gasLimit = await weight(conn, contract, "transfer", [dave.address, 10000]);
  39. let tx2 = contract.tx.send({ gasLimit }, dave.address, 10000);
  40. await transaction(tx2, alice);
  41. let { data: { free: daveBal3 } } = await conn.query.system.account(dave.address);
  42. expect(daveBal3.toBigInt()).toEqual(daveBal2.toBigInt() + 10000n);
  43. });
  44. });