create_contract.spec.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import expect from 'expect';
  2. import { weight, createConnection, deploy, transaction, aliceKeypair, query, debug_buffer, dry_run, } from './index';
  3. import { ContractPromise } from '@polkadot/api-contract';
  4. import { ApiPromise } from '@polkadot/api';
  5. describe('Deploy create_contract contract and test', () => {
  6. let conn: ApiPromise;
  7. before(async function () {
  8. conn = await createConnection();
  9. });
  10. after(async function () {
  11. await conn.disconnect();
  12. });
  13. it('create_contract', async function () {
  14. this.timeout(50000);
  15. const alice = aliceKeypair();
  16. // call the constructors
  17. let deploy_contract = await deploy(conn, alice, 'creator.contract', BigInt(1e16));
  18. // we need to have upload the child code
  19. let _ = await deploy(conn, alice, 'child_create_contract.contract', BigInt(0));
  20. let contract = new ContractPromise(conn, deploy_contract.abi, deploy_contract.address);
  21. let dry = await dry_run(conn, contract, "createChild");
  22. // Expect the instantiation nonce to be present
  23. const current_nonce = dry.debugMessage.split('\n')[0].split('=');
  24. expect(current_nonce[0]).toEqual("call: instantiation_nonce");
  25. const gasLimit = dry.gasRequired;
  26. let tx = contract.tx.createChild({ gasLimit });
  27. await transaction(tx, alice);
  28. let res2 = await query(conn, alice, contract, "callChild");
  29. expect(res2.output?.toJSON()).toStrictEqual("child");
  30. // child was created with a balance of 1e15, verify
  31. res2 = await query(conn, alice, contract, "c");
  32. let child = res2.output!.toString();
  33. let { data: { free: childBalance } } = await conn.query.system.account(child);
  34. expect(BigInt(1e15) - childBalance.toBigInt()).toBeLessThan(1e11);
  35. // Expect the instantiation nonce to be present and to increase
  36. const next_nonce = (await debug_buffer(conn, contract, "createChild")).split('\n')[0].split('=');
  37. expect(next_nonce[0]).toEqual("call: instantiation_nonce");
  38. expect(parseInt(current_nonce[1])).toBeLessThan(parseInt(next_nonce[1]));
  39. });
  40. });