CAIP.test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { CHAINS, getLocalCAIP } = require('../helpers/chains');
  5. async function fixture() {
  6. const caip2 = await ethers.deployContract('$CAIP2');
  7. const caip10 = await ethers.deployContract('$CAIP10');
  8. return { caip2, caip10 };
  9. }
  10. describe('CAIP utilities', function () {
  11. beforeEach(async function () {
  12. Object.assign(this, await loadFixture(fixture));
  13. });
  14. describe('CAIP-2', function () {
  15. it('local()', async function () {
  16. const { caip2 } = await getLocalCAIP();
  17. expect(await this.caip2.$local()).to.equal(caip2);
  18. });
  19. for (const { namespace, reference, caip2 } of Object.values(CHAINS))
  20. it(`format(${namespace}, ${reference})`, async function () {
  21. expect(await this.caip2.$format(namespace, reference)).to.equal(caip2);
  22. });
  23. for (const { namespace, reference, caip2 } of Object.values(CHAINS))
  24. it(`parse(${caip2})`, async function () {
  25. expect(await this.caip2.$parse(caip2)).to.deep.equal([namespace, reference]);
  26. });
  27. });
  28. describe('CAIP-10', function () {
  29. const { address: account } = ethers.Wallet.createRandom();
  30. it(`local(${account})`, async function () {
  31. const { caip10 } = await getLocalCAIP(account);
  32. expect(await this.caip10.$local(ethers.Typed.address(account))).to.equal(caip10);
  33. });
  34. for (const { account, caip2, caip10 } of Object.values(CHAINS))
  35. it(`format(${caip2}, ${account})`, async function () {
  36. expect(await this.caip10.$format(ethers.Typed.string(caip2), ethers.Typed.string(account))).to.equal(caip10);
  37. });
  38. for (const { account, caip2, caip10 } of Object.values(CHAINS))
  39. it(`parse(${caip10})`, async function () {
  40. expect(await this.caip10.$parse(caip10)).to.deep.equal([caip2, account]);
  41. });
  42. });
  43. });