upgradeable_proxy.spec.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import expect from 'expect';
  2. import { weight, createConnection, deploy, transaction, aliceKeypair, query, } from './index';
  3. import { ContractPromise } from '@polkadot/api-contract';
  4. import { ApiPromise } from '@polkadot/api';
  5. import { KeyringPair } from '@polkadot/keyring/types';
  6. import { DecodedEvent } from '@polkadot/api-contract/types';
  7. import { AccountId, ContractSelector } from '@polkadot/types/interfaces';
  8. interface IMsg {
  9. identifier: string,
  10. selector: any,
  11. };
  12. describe('Deploy the upgradable proxy and implementations; expect the upgrade mechanism to work', () => {
  13. // Helper: Upgrade implementation and execute a constructor that takes no arguments
  14. async function upgrade_version(impl: AccountId, input: any) {
  15. const params = [impl, input];
  16. const gasLimit = await weight(conn, proxy, 'upgradeToAndCall', params);
  17. let result: any = await transaction(proxy.tx.upgradeToAndCall({ gasLimit }, ...params), aliceKeypair());
  18. let events: DecodedEvent[] = result.contractEvents;
  19. expect(events.length).toEqual(1);
  20. expect(events[0].event.identifier).toBe("Upgraded");
  21. expect(events[0].args.map(a => a.toJSON())[0]).toEqual(params[0].toJSON());
  22. }
  23. let conn: ApiPromise;
  24. let alice: KeyringPair;
  25. let proxy: ContractPromise;
  26. let counter: ContractPromise;
  27. before(async function () {
  28. alice = aliceKeypair();
  29. conn = await createConnection();
  30. const proxy_deployment = await deploy(conn, alice, 'UpgradeableProxy.contract', 0n);
  31. proxy = new ContractPromise(conn, proxy_deployment.abi, proxy_deployment.address);
  32. // Pretend the proxy contract to be implementation V1
  33. const implV1 = await deploy(conn, alice, 'UpgradeableImplV1.contract', 0n);
  34. const selector = implV1.abi.messages.find((m: IMsg) => m.identifier === "inc").selector;
  35. await upgrade_version(implV1.address, selector);
  36. counter = new ContractPromise(conn, implV1.abi, proxy_deployment.address);
  37. const count = await query(conn, alice, counter, "count");
  38. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(1n);
  39. });
  40. after(async function () {
  41. await conn.disconnect();
  42. });
  43. it('Tests implementation and upgrading', async function () {
  44. // Test implementation V1
  45. let gasLimit = await weight(conn, counter, 'inc', []);
  46. await transaction(counter.tx.inc({ gasLimit }), alice);
  47. await transaction(counter.tx.inc({ gasLimit }), alice);
  48. let count = await query(conn, alice, counter, "count");
  49. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(3n);
  50. // Upgrade to implementation V2
  51. const implV2 = await deploy(conn, alice, 'UpgradeableImplV2.contract', 0n);
  52. const selector = implV2.abi.messages.find((m: IMsg) => m.identifier === "setVersion").selector;
  53. await upgrade_version(implV2.address, selector);
  54. counter = new ContractPromise(conn, implV2.abi, proxy.address);
  55. // Test implementation V2
  56. count = await query(conn, alice, counter, "count");
  57. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(3n);
  58. gasLimit = await weight(conn, counter, 'dec', []);
  59. await transaction(counter.tx.dec({ gasLimit }), alice);
  60. count = await query(conn, alice, counter, "count");
  61. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(2n);
  62. const version = await query(conn, alice, counter, "version");
  63. expect(version.output?.toString()).toStrictEqual("v2");
  64. });
  65. });