destruct.spec.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import expect from 'expect';
  2. import { weight, createConnection, deploy, transaction, aliceKeypair, daveKeypair, query } from './index';
  3. import { ContractPromise } from '@polkadot/api-contract';
  4. import { ApiPromise } from '@polkadot/api';
  5. describe('Deploy destruct contract and test', () => {
  6. let conn: ApiPromise;
  7. before(async function () {
  8. conn = await createConnection();
  9. });
  10. after(async function () {
  11. await conn.disconnect();
  12. });
  13. it('destruct', async function () {
  14. this.timeout(50000);
  15. const alice = aliceKeypair();
  16. const dave = daveKeypair();
  17. // call the constructors
  18. let deploy_contract = await deploy(conn, alice, 'destruct.contract', BigInt(0));
  19. let contract = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  20. let hello = await query(conn, alice, contract, "hello");
  21. expect(hello.output?.toJSON()).toBe('Hello');
  22. let { data: { free: daveBalBefore } } = await conn.query.system.account(dave.address);
  23. let { data: { free: contractBalBefore } } = await conn.query.system.account(String(deploy_contract.address));
  24. let gasLimit = await weight(conn, contract, "selfterminate", [dave.address]);
  25. let tx = contract.tx.selfterminate({ gasLimit }, dave.address);
  26. await transaction(tx, alice);
  27. let { data: { free: daveBalAfter } } = await conn.query.system.account(dave.address);
  28. let { data: { free: contractBalAfter } } = await conn.query.system.account(String(deploy_contract.address));
  29. // The contact is gone and has no balance
  30. expect(contractBalAfter.toBigInt()).toBe(0n);
  31. // Dave now has the balance previously held by the contract
  32. expect(daveBalAfter.toBigInt()).toEqual(daveBalBefore.toBigInt() + contractBalBefore.toBigInt());
  33. });
  34. });