builtins.spec.ts 1.7 KB

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