caller_is_root.spec.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // SPDX-License-Identifier: Apache-2.0
  2. import expect from 'expect';
  3. import { createConnection, deploy, aliceKeypair, query, weight, transaction } 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 caller_is_root contract and test it', () => {
  8. let conn: ApiPromise;
  9. let contract: ContractPromise;
  10. let alice: KeyringPair;
  11. before(async function () {
  12. conn = await createConnection();
  13. alice = aliceKeypair();
  14. const instance = await deploy(conn, alice, 'CallerIsRoot.contract', 0n);
  15. contract = new ContractPromise(conn, instance.abi, instance.address);
  16. });
  17. after(async function () {
  18. await conn.disconnect();
  19. });
  20. it('is correct on a non-root caller', async function () {
  21. // Without sudo the caller should not be root
  22. const gasLimit = await weight(conn, contract, "covert");
  23. await transaction(contract.tx.covert({ gasLimit }), alice);
  24. // Calling `covert` as non-root sets the balance to 1
  25. const balance = await query(conn, alice, contract, "balance", []);
  26. expect(BigInt(balance.output?.toString() ?? "")).toStrictEqual(1n);
  27. });
  28. it('is correct on a root caller', async function () {
  29. // Alice has sudo rights on --dev nodes
  30. const gasLimit = await weight(conn, contract, "covert");
  31. await transaction(conn.tx.sudo.sudo(contract.tx.covert({ gasLimit })), alice);
  32. // Calling `covert` as root sets the balance to 0xdeadbeef
  33. const balance = await query(conn, alice, contract, "balance", []);
  34. expect(BigInt(balance.output?.toString() ?? "")).toStrictEqual(0xdeadbeefn);
  35. });
  36. });