call_flags.spec.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import expect from 'expect';
  2. import { createConnection, deploy, aliceKeypair, query, debug_buffer, } from './index';
  3. import { ContractPromise } from '@polkadot/api-contract';
  4. import { ApiPromise } from '@polkadot/api';
  5. import { KeyringPair } from '@polkadot/keyring/types';
  6. enum CallFlags {
  7. FORWARD_INPUT, CLONE_INPUT, TAIL_CALL, ALLOW_REENTRY
  8. }
  9. describe('Deploy the CallFlags contract and tests for various call flag combinations', () => {
  10. let conn: ApiPromise;
  11. let contract: ContractPromise;
  12. let alice: KeyringPair;
  13. const voyager = 987654321;
  14. const foo = [0, 0, 0, 0];
  15. before(async function () {
  16. alice = aliceKeypair();
  17. conn = await createConnection();
  18. const deployment = await deploy(conn, alice, 'CallFlags.contract', 0n);
  19. contract = new ContractPromise(conn, deployment.abi, deployment.address);
  20. });
  21. after(async function () {
  22. await conn.disconnect();
  23. });
  24. it('works with the reentry flag', async function () {
  25. const flags = [CallFlags.ALLOW_REENTRY];
  26. const answer = await query(conn, alice, contract, "echo", [contract.address, foo, voyager, flags]);
  27. expect(answer.output?.toJSON()).toStrictEqual(voyager);
  28. });
  29. it('works with the reentry and tail call flags', async function () {
  30. const flags = [CallFlags.ALLOW_REENTRY, CallFlags.TAIL_CALL];
  31. const answer = await query(conn, alice, contract, "echo", [contract.address, foo, voyager, flags]);
  32. expect(answer.output?.toJSON()).toStrictEqual(voyager);
  33. });
  34. it('works with the reentry and clone input flags', async function () {
  35. const flags = [CallFlags.ALLOW_REENTRY, CallFlags.CLONE_INPUT];
  36. const answer = await query(conn, alice, contract, "echo", [contract.address, foo, voyager, flags]);
  37. expect(answer.output?.toJSON()).toStrictEqual(voyager);
  38. });
  39. it('works with the reentry, tail call and clone input flags', async function () {
  40. const flags = [CallFlags.ALLOW_REENTRY, CallFlags.TAIL_CALL, CallFlags.CLONE_INPUT];
  41. const answer = await query(conn, alice, contract, "echo", [contract.address, foo, voyager, flags]);
  42. expect(answer.output?.toJSON()).toStrictEqual(voyager);
  43. });
  44. it('fails without the reentry flag', async function () {
  45. const flags = [CallFlags.TAIL_CALL];
  46. const answer = await query(conn, alice, contract, "echo", [contract.address, foo, voyager, flags]);
  47. const { index, error } = answer.result.asErr.asModule;
  48. // Module 8 error 0x16 is ReentranceDenied in the contracts pallet
  49. expect(index.toJSON()).toStrictEqual(8);
  50. expect(error.toJSON()).toStrictEqual("0x16000000");
  51. });
  52. it('fails with the input forwarding flag', async function () {
  53. const flags = [CallFlags.ALLOW_REENTRY, CallFlags.FORWARD_INPUT];
  54. const answer = await query(conn, alice, contract, "echo", [contract.address, foo, voyager, flags]);
  55. expect(answer.result.asOk.flags.isRevert).toStrictEqual(true);
  56. });
  57. it('test for the tail call flag to work correctly', async function () {
  58. let flags = [CallFlags.ALLOW_REENTRY];
  59. let answer = await query(conn, alice, contract, "tail_call_it", [contract.address, foo, voyager, flags]);
  60. expect(answer.output?.toJSON()).toStrictEqual(voyager + 1);
  61. flags = [CallFlags.ALLOW_REENTRY, CallFlags.TAIL_CALL];
  62. answer = await query(conn, alice, contract, "tail_call_it", [contract.address, foo, voyager, flags]);
  63. expect(answer.output?.toJSON()).toStrictEqual(voyager);
  64. });
  65. it('works on calls on "this"', async function () {
  66. const answer = await query(conn, alice, contract, "call_this", [voyager]);
  67. expect(answer.output?.toJSON()).toStrictEqual(voyager);
  68. });
  69. });