constructor_dispatch.spec.ts 2.1 KB

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