Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amxx committed Oct 14, 2024
1 parent 1acd441 commit c5d6081
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
22 changes: 13 additions & 9 deletions test/helpers/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// The following listing does not pretend to be exhaustive or even accurate. It SHOULD NOT be used in production.

const { ethers } = require('hardhat');

const mapValues = (obj, fn) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, fn(v)]));
const format = (...args) => args.join(':');
const { mapValues } = require('./iterate');

// EVM (https://axelarscan.io/resources/chains?type=evm)
const ethereum = {
Expand Down Expand Up @@ -81,6 +79,15 @@ const cosmos = {
terra: 'columbus-5',
};

const makeCAIP = ({ namespace, reference, account }) => ({
namespace,
reference,
account,
caip2: `${namespace}:${reference}`,
caip10: `${namespace}:${reference}:${account}`,
toCaip10: other => `${namespace}:${reference}:${ethers.getAddress(other.target ?? other.address ?? other)}`,
});

module.exports = {
CHAINS: mapValues(
Object.assign(
Expand All @@ -95,11 +102,8 @@ module.exports = {
account: ethers.encodeBase58(ethers.randomBytes(32)),
})),
),
entry =>
Object.assign(entry, {
caip2: format(entry.namespace, entry.reference),
caip10: format(entry.namespace, entry.reference, entry.account),
}),
makeCAIP,
),
format,
getLocalCAIP: account =>
ethers.provider.getNetwork().then(({ chainId }) => makeCAIP({ namespace: 'eip155', reference: chainId, account })),
};
12 changes: 6 additions & 6 deletions test/utils/CAIP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');

const { CHAINS, format } = require('../helpers/chains');
const { CHAINS, getLocalCAIP } = require('../helpers/chains');

async function fixture() {
const caip2 = await ethers.deployContract('$CAIP2');
const caip10 = await ethers.deployContract('$CAIP10');
const { chainId } = await ethers.provider.getNetwork();
return { caip2, caip10, chainId };
return { caip2, caip10 };
}

describe('CAIP utilities', function () {
Expand All @@ -18,7 +17,8 @@ describe('CAIP utilities', function () {

describe('CAIP-2', function () {
it('local()', async function () {
expect(await this.caip2.$local()).to.equal(format('eip155', this.chainId));
const { caip2 } = await getLocalCAIP();
expect(await this.caip2.$local()).to.equal(caip2);
});

for (const { namespace, reference, caip2 } of Object.values(CHAINS))
Expand All @@ -36,8 +36,8 @@ describe('CAIP utilities', function () {
const { address: account } = ethers.Wallet.createRandom();

it(`local(${account})`, async function () {
// lowercase encoding for now
expect(await this.caip10.$local(ethers.Typed.address(account))).to.equal(format('eip155', this.chainId, account));
const { caip10 } = await getLocalCAIP(account);
expect(await this.caip10.$local(ethers.Typed.address(account))).to.equal(caip10);
});

for (const { account, caip2, caip10 } of Object.values(CHAINS))
Expand Down

0 comments on commit c5d6081

Please sign in to comment.