upgradeable_proxy.spec.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. console.log(events);
  20. expect(events.length).toEqual(1);
  21. expect(events[0].event.identifier).toBe("UpgradeableProxy::Upgraded");
  22. expect(events[0].args.map(a => a.toJSON())[0]).toEqual(params[0].toJSON());
  23. }
  24. let conn: ApiPromise;
  25. let alice: KeyringPair;
  26. let proxy: ContractPromise;
  27. let counter: ContractPromise;
  28. before(async function () {
  29. alice = aliceKeypair();
  30. conn = await createConnection();
  31. const proxy_deployment = await deploy(conn, alice, 'UpgradeableProxy.contract', 0n);
  32. proxy = new ContractPromise(conn, proxy_deployment.abi, proxy_deployment.address);
  33. // Pretend the proxy contract to be implementation V1
  34. const implV1 = await deploy(conn, alice, 'UpgradeableImplV1.contract', 0n);
  35. const selector = implV1.abi.messages.find((m: IMsg) => m.identifier === "inc").selector;
  36. await upgrade_version(implV1.address, selector);
  37. counter = new ContractPromise(conn, implV1.abi, proxy_deployment.address);
  38. const count = await query(conn, alice, counter, "count");
  39. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(1n);
  40. });
  41. after(async function () {
  42. await conn.disconnect();
  43. });
  44. it('Tests implementation and upgrading', async function () {
  45. // Test implementation V1
  46. let gasLimit = await weight(conn, counter, 'inc', []);
  47. await transaction(counter.tx.inc({ gasLimit }), alice);
  48. await transaction(counter.tx.inc({ gasLimit }), alice);
  49. let count = await query(conn, alice, counter, "count");
  50. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(3n);
  51. // Upgrade to implementation V2
  52. const implV2 = await deploy(conn, alice, 'UpgradeableImplV2.contract', 0n);
  53. const selector = implV2.abi.messages.find((m: IMsg) => m.identifier === "setVersion").selector;
  54. await upgrade_version(implV2.address, selector);
  55. counter = new ContractPromise(conn, implV2.abi, proxy.address);
  56. // Test implementation V2
  57. count = await query(conn, alice, counter, "count");
  58. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(3n);
  59. gasLimit = await weight(conn, counter, 'dec', []);
  60. await transaction(counter.tx.dec({ gasLimit }), alice);
  61. count = await query(conn, alice, counter, "count");
  62. expect(BigInt(count.output?.toString() ?? "")).toStrictEqual(2n);
  63. const version = await query(conn, alice, counter, "version");
  64. expect(version.output?.toString()).toStrictEqual("v2");
  65. });
  66. });