builtins.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import expect from 'expect';
  2. import { establishConnection } from './index';
  3. describe('Deploy solang contract and test', () => {
  4. it('builtins', async function () {
  5. this.timeout(50000);
  6. let conn = await establishConnection();
  7. let hash_functions = await conn.loadProgram("bundle.so", "builtins.abi");
  8. // call the constructor
  9. await hash_functions.call_constructor(conn, 'builtins', []);
  10. console.log("calling ripemd160");
  11. let res = await hash_functions.call_function(conn, "hash_ripemd160", ['0x' + Buffer.from('Call me Ishmael.', 'utf8').toString('hex')]);
  12. expect(res["0"]).toBe("0x0c8b641c461e3c7abbdabd7f12a8905ee480dadf");
  13. console.log("calling sha256");
  14. res = await hash_functions.call_function(conn, "hash_sha256", ['0x' + Buffer.from('Call me Ishmael.', 'utf8').toString('hex')]);
  15. expect(res["0"]).toBe("0x458f3ceeeec730139693560ecf66c9c22d9c7bc7dcb0599e8e10b667dfeac043");
  16. console.log("calling keccak256");
  17. res = await hash_functions.call_function(conn, "hash_kecccak256", ['0x' + Buffer.from('Call me Ishmael.', 'utf8').toString('hex')]);
  18. expect(res["0"]).toBe("0x823ad8e1757b879aac338f9a18542928c668e479b37e4a56f024016215c5928c");
  19. console.log("calling timestamp");
  20. res = await hash_functions.call_function(conn, "mr_now", []);
  21. let now = Math.floor(+new Date() / 1000);
  22. let ts = Number(res[0]);
  23. expect(ts).toBeLessThanOrEqual(now);
  24. expect(ts).toBeGreaterThan(now - 120);
  25. console.log("calling slot");
  26. res = await hash_functions.call_function(conn, "mr_slot", []);
  27. let sol_slot = Number(res[0]);
  28. let rpc_slot = await conn.connection.getSlot();
  29. console.log("slot from rpc " + rpc_slot);
  30. expect(sol_slot).toBeGreaterThan(rpc_slot - 10);
  31. expect(sol_slot).toBeLessThan(rpc_slot + 10);
  32. });
  33. });