delegate_call.spec.ts 2.6 KB

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