Skip to content

Commit

Permalink
add: test to check contract size donot exceeds 24kb
Browse files Browse the repository at this point in the history
  • Loading branch information
blockchainguyy committed Oct 10, 2023
1 parent 714121e commit 2001c7c
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/tokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const chai = require('chai');
const { expect } = chai;
require('dotenv').config();
const fs = require('fs');
const { ethers } = require('hardhat');
const { AddressZero, MaxUint256 } = ethers.constants;
const { defaultAbiCoder, solidityPack, keccak256 } = ethers.utils;
const { Contract, Wallet } = ethers;
const path = require('path');

const TokenManager = require('../artifacts/contracts/token-manager/TokenManager.sol/TokenManager.json');
const Token = require('../artifacts/contracts/interfaces/IStandardizedToken.sol/IStandardizedToken.json');
Expand Down Expand Up @@ -1460,3 +1462,36 @@ describe('Interchain Token Service', () => {
});
});
});

describe('Contract Size Check', function () {
const maxContractSize = 24 * 1024; // 24KB

function checkContractSize(filePath) {
const stats = fs.statSync(filePath);

if (stats.isFile()) {
const contractName = path.basename(filePath, '.json');

if (contractName.endsWith('.dbg')) {
return;
}

const contractArtifact = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const bytecode = contractArtifact.bytecode.slice(2);
const contractSize = bytecode.length / 2;
expect(parseInt(contractSize), `${contractName} exceeds 24KB (Size: ${contractSize} bytes)`).to.be.at.most(
parseInt(maxContractSize),
);
} else if (stats.isDirectory()) {
const files = fs.readdirSync(filePath);
files.forEach((file) => {
checkContractSize(path.join(filePath, file));
});
}
}

it('should not exceed 24KB', () => {
const contractsDir = path.join(__dirname, '..', 'artifacts', 'contracts');
checkContractSize(contractsDir);
});
});

0 comments on commit 2001c7c

Please sign in to comment.