delegate_call.spec.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { weight, createConnection, deploy, transaction, aliceKeypair, daveKeypair, debug_buffer, query, } from './index';
  4. import { ContractPromise } from '@polkadot/api-contract';
  5. import { ApiPromise } from '@polkadot/api';
  6. import { KeyringPair } from '@polkadot/keyring/types';
  7. describe('Deploy the delegator and the delegatee contracts; test the delegatecall to work correct', () => {
  8. let conn: ApiPromise;
  9. let delegatee: ContractPromise;
  10. let delegator: ContractPromise;
  11. let alice: KeyringPair;
  12. let dave: KeyringPair;
  13. before(async function () {
  14. alice = aliceKeypair();
  15. dave = daveKeypair();
  16. conn = await createConnection();
  17. const delegator_contract = await deploy(conn, alice, 'Delegator.contract', 0n);
  18. delegator = new ContractPromise(conn, delegator_contract.abi, delegator_contract.address);
  19. const delegatee_contract = await deploy(conn, alice, 'Delegatee.contract', 0n);
  20. delegatee = new ContractPromise(conn, delegatee_contract.abi, delegatee_contract.address);
  21. // Set delegatee storage to default values and alice address
  22. const gasLimit = await weight(conn, delegatee, 'setVars', [0n]);
  23. await transaction(delegatee.tx.setVars({ gasLimit }, [0n]), alice);
  24. });
  25. after(async function () {
  26. await conn.disconnect();
  27. });
  28. it('Executes the delegatee in the context of the delegator', async function () {
  29. const value = 1000000n;
  30. const arg = 123456789n;
  31. const parameters = [delegatee.address, arg];
  32. const gasLimit = await weight(conn, delegator, 'setVars', parameters);
  33. await transaction(delegator.tx.setVars({ gasLimit, value }, ...parameters), dave);
  34. // Storage of the delegatee must not change
  35. let num = await query(conn, alice, delegatee, "num");
  36. expect(BigInt(num.output?.toString() ?? "")).toStrictEqual(0n);
  37. let balance = await query(conn, alice, delegatee, "value");
  38. expect(BigInt(balance.output?.toString() ?? "")).toStrictEqual(0n);
  39. let sender = await query(conn, alice, delegatee, "sender");
  40. expect(sender.output?.toJSON()).toStrictEqual(alice.address);
  41. // Storage of the delegator must have changed
  42. num = await query(conn, alice, delegator, "num");
  43. expect(BigInt(num.output?.toString() ?? "")).toStrictEqual(arg);
  44. balance = await query(conn, alice, delegator, "value");
  45. expect(BigInt(balance.output?.toString() ?? "")).toStrictEqual(value);
  46. sender = await query(conn, alice, delegator, "sender");
  47. expect(sender.output?.toJSON()).toStrictEqual(dave.address);
  48. });
  49. });