Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Amiel committed Oct 11, 2023
1 parent 7d26c10 commit 4e47d29
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
42 changes: 41 additions & 1 deletion test/UtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { ethers } = require('hardhat');
const { time } = require('@nomicfoundation/hardhat-network-helpers');
const { Wallet, Contract } = ethers;
const { AddressZero } = ethers.constants;
const { defaultAbiCoder } = ethers.utils;
const { defaultAbiCoder, arrayify, toUtf8Bytes, hexlify } = ethers.utils;
const { expect } = chai;
const { getRandomBytes32, expectRevert } = require('./utils');
const { deployContract } = require('../scripts/deploy');
Expand Down Expand Up @@ -392,4 +392,44 @@ describe('StandardizedTokenDeployer', () => {
'AlreadyDeployed',
);
});

describe('AddressBytesUtils', () => {
let addressBytesUtils;

before(async () => {
addressBytesUtils = await deployContract(ownerWallet, 'AddressBytesUtilsTest');
});

it('Should convert bytes address to address', async () => {
const bytesAddress = arrayify(ownerWallet.address);
const convertedAddress = await addressBytesUtils.toAddress(bytesAddress);
expect(convertedAddress).to.eq(ownerWallet.address);
});

it('Should revert on invalid bytes length', async () => {
const bytesAddress = defaultAbiCoder.encode(['bytes'], [toUtf8Bytes(ownerWallet.address)]);
await expectRevert(
(gasOptions) => addressBytesUtils.toAddress(bytesAddress, gasOptions),
addressBytesUtils,
'InvalidBytesLength',
);
});

it('Should convert address to bytes address', async () => {
const convertedAddress = await addressBytesUtils.toBytes(ownerWallet.address);
expect(convertedAddress).to.eq(hexlify(ownerWallet.address));
});
});

describe('NoReEntrancy', () => {
let noReEntrancy;

before(async () => {
noReEntrancy = await deployContract(ownerWallet, 'NoReEntrancyTest');
});

it('Should revert on reentrancy', async function () {
await expect(noReEntrancy.testFunction()).to.be.revertedWithCustomError(noReEntrancy, 'ReEntrancy');
});
});
});
96 changes: 96 additions & 0 deletions test/tokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ describe('Interchain Token Service', () => {
);
});

it('Should revert on invalid gateway', async () => {
await expectRevert(
(gasOptions) =>
deployInterchainTokenService(
wallet,
create3Deployer.address,
tokenManagerDeployer.address,
standardizedTokenDeployer.address,
AddressZero,
gasService.address,
remoteAddressValidator.address,
tokenManagerImplementations.map((impl) => impl.address),
deploymentKey,
gasOptions,
),
service,
'ZeroAddress',
);
});

it('Should revert on invalid token manager implementation length', async () => {
tokenManagerImplementations.push(wallet);

Expand Down Expand Up @@ -743,6 +763,35 @@ describe('Interchain Token Service', () => {
await txPaused.wait();
});

it('Should revert on receiving a remote standardized token depoloyment if not approved by the gateway', async () => {
const tokenId = getRandomBytes32();
const distributor = wallet.address;
const operator = wallet.address;
const mintTo = '0x';
const mintAmount = 1234;
const commandId = getRandomBytes32();
const payload = defaultAbiCoder.encode(
['uint256', 'bytes32', 'string', 'string', 'uint8', 'bytes', 'bytes', 'uint256', 'bytes'],
[
SELECTOR_DEPLOY_AND_REGISTER_STANDARDIZED_TOKEN,
tokenId,
tokenName,
tokenSymbol,
tokenDecimals,
distributor,
mintTo,
mintAmount,
operator,
],
);

await expectRevert(
(gasOptions) => service.execute(commandId, sourceChain, sourceAddress, payload, gasOptions),
service,
'NotApprovedByGateway',
);
});

it('Should be able to receive a remote standardized token depoloyment with a lock/unlock token manager', async () => {
const tokenId = getRandomBytes32();
const distributor = wallet.address;
Expand Down Expand Up @@ -891,6 +940,19 @@ describe('Interchain Token Service', () => {
expect(await tokenManager.tokenAddress()).to.equal(tokenAddress);
expect(await tokenManager.operator()).to.equal(service.address);
});

it('Should revert on execute with token', async () => {
const commandId = getRandomBytes32();
const sourceAddress = 'Source Address';
const payload = '0x';
const amount = 123;

await expectRevert(
(gasOptions) => service.executeWithToken(commandId, sourceChain, sourceAddress, payload, tokenSymbol, amount, gasOptions),
service,
'ExecuteWithTokenNotSupported',
);
});
});

describe('Custom Token Manager Deployment', () => {
Expand Down Expand Up @@ -918,6 +980,9 @@ describe('Interchain Token Service', () => {
const tokenManager = new Contract(tokenManagerAddress, TokenManager.abi, wallet);

expect(await tokenManager.operator()).to.equal(wallet.address);

const tokenAddress = await service.getTokenAddress(tokenId);
expect(tokenAddress).to.eq(token.address);
});

it('Should deploy a mint/burn token manager', async () => {
Expand All @@ -937,6 +1002,31 @@ describe('Interchain Token Service', () => {
const tokenManager = new Contract(tokenManagerAddress, TokenManager.abi, wallet);

expect(await tokenManager.operator()).to.equal(wallet.address);

const tokenAddress = await service.getTokenAddress(tokenId);
expect(tokenAddress).to.eq(token.address);
});

it('Should deploy a mint/burn_from token manager', async () => {
const tokenName = 'Token Name';
const tokenSymbol = 'TN';
const tokenDecimals = 13;
const salt = getRandomBytes32();
const tokenId = await service.getCustomTokenId(wallet.address, salt);
const tokenManagerAddress = await service.getTokenManagerAddress(tokenId);
const token = await deployContract(wallet, 'InterchainTokenTest', [tokenName, tokenSymbol, tokenDecimals, tokenManagerAddress]);
const params = defaultAbiCoder.encode(['bytes', 'address'], [wallet.address, token.address]);

const tx = service.deployCustomTokenManager(salt, MINT_BURN_FROM, params);
await expect(tx).to.emit(service, 'TokenManagerDeployed').withArgs(tokenId, MINT_BURN_FROM, params);

expect(tokenManagerAddress).to.not.equal(AddressZero);
const tokenManager = new Contract(tokenManagerAddress, TokenManager.abi, wallet);

expect(await tokenManager.operator()).to.equal(wallet.address);

const tokenAddress = await service.getTokenAddress(tokenId);
expect(tokenAddress).to.eq(token.address);
});

it('Should deploy a lock/unlock with fee on transfer token manager', async () => {
Expand All @@ -961,6 +1051,9 @@ describe('Interchain Token Service', () => {
const tokenManager = new Contract(tokenManagerAddress, TokenManager.abi, wallet);

expect(await tokenManager.operator()).to.equal(wallet.address);

const tokenAddress = await service.getTokenAddress(tokenId);
expect(tokenAddress).to.eq(token.address);
});

it('Should deploy a liquidity pool token manager', async () => {
Expand All @@ -980,6 +1073,9 @@ describe('Interchain Token Service', () => {
const tokenManager = new Contract(tokenManagerAddress, TokenManager.abi, wallet);

expect(await tokenManager.operator()).to.equal(wallet.address);

const tokenAddress = await service.getTokenAddress(tokenId);
expect(tokenAddress).to.eq(token.address);
});

it('Should allow operator to change the liquidity pool address', async () => {
Expand Down

0 comments on commit 4e47d29

Please sign in to comment.