destruct.spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 destruct 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('destruct', 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, 'destruct.contract', BigInt(0));
  20. let contract = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  21. let hello = await query(conn, alice, contract, "hello");
  22. expect(hello.output?.toJSON()).toBe('Hello');
  23. let { data: { free: daveBalBefore } } = await conn.query.system.account(dave.address);
  24. let { data: { free: contractBalBefore } } = await conn.query.system.account(String(deploy_contract.address));
  25. let gasLimit = await weight(conn, contract, "selfterminate", [dave.address]);
  26. let tx = contract.tx.selfterminate({ gasLimit }, dave.address);
  27. await transaction(tx, alice);
  28. let { data: { free: daveBalAfter } } = await conn.query.system.account(dave.address);
  29. let { data: { free: contractBalAfter } } = await conn.query.system.account(String(deploy_contract.address));
  30. // The contact is gone and has no balance
  31. expect(contractBalAfter.toBigInt()).toBe(0n);
  32. // Dave now has the balance previously held by the contract
  33. expect(daveBalAfter.toBigInt()).toEqual(daveBalBefore.toBigInt() + contractBalBefore.toBigInt());
  34. });
  35. });