constructor_dispatch.spec.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { createConnection, deploy, aliceKeypair, query, debug_buffer, weight, transaction, daveKeypair, } from './index';
  2. import { ContractPromise } from '@polkadot/api-contract';
  3. import { ApiPromise } from '@polkadot/api';
  4. import { KeyringPair } from '@polkadot/keyring/types';
  5. import expect from 'expect';
  6. import { error } from 'console';
  7. describe('Test that the constructor can not be reached from the call function', () => {
  8. let conn: ApiPromise;
  9. let contract: ContractPromise;
  10. let caller: ContractPromise;
  11. let alice: KeyringPair;
  12. before(async function () {
  13. alice = aliceKeypair();
  14. conn = await createConnection();
  15. const contract_deployment = await deploy(conn, alice, 'ConstructorDispatch.contract', 0n);
  16. contract = new ContractPromise(conn, contract_deployment.abi, contract_deployment.address);
  17. const caller_deployment = await deploy(conn, alice, 'HappyCaller.contract', 0n);
  18. caller = new ContractPromise(conn, caller_deployment.abi, caller_deployment.address);
  19. });
  20. after(async function () {
  21. await conn.disconnect();
  22. });
  23. it('Should fail to overwrite the admin account of the target contract', async function () {
  24. // Expect the caller to succeed on normal function
  25. let input = contract.abi.messages[0].selector;
  26. let attempt = await query(conn, alice, caller, "call", [contract.address, input]);
  27. expect(attempt.result.isOk).toStrictEqual(true);
  28. // "Calling" the constructor should fail
  29. input = contract.abi.constructors[0].selector;
  30. attempt = await query(conn, alice, caller, "call", [contract.address, input]);
  31. expect(attempt.result.asOk.flags.isRevert).toStrictEqual(true);
  32. const gasLimit = await weight(conn, caller, 'call', [contract.address, input]);
  33. await expect(transaction(caller.tx.call({ gasLimit }, contract.address, input), alice)).rejects.toBeDefined();
  34. // Alice must still be admin
  35. let admin = await query(conn, alice, contract, "boss");
  36. expect(admin.output?.toString()).toStrictEqual(alice.address.toString());
  37. });
  38. });