create_contract.spec.ts 2.3 KB

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