I am getting this error whenever I run my hardhat deploy script for uniswap pool contract :
TypeError: Cannot read properties of undefined (reading 'format')
at Function.from (/home/abdulaleem/token/node_modules/ethers/src.ts/abi/interface.ts:1259:33)
at new ContractFactory (/home/abdulaleem/token/node_modules/ethers/src.ts/contract/factory.ts:53:33)
at main (/home/abdulaleem/token/scripts/01-deploy-uniswap-contracts.js:39:17)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
Command:
npx hardhat run --network mumbai scripts/01-deploy-uniswap-contracts.js
Here error occurs while at const MATIC = new ContractFactory():
const [deployer] = await ethers.getSigners();
const Matic = new ContractFactory(
artifacts.MATIC.abi,
artifacts.MATIC.bytecode,
deployer
);
const matic = await Matic.deploy();
Here's the full code:
const { Contract, ContractFactory, utils, BigNumber } = require("ethers");
const MATIC = require("../matic.json");
const artifacts = {
UniswapV3Factory: require("@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json"),
SwapRouter: require("@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json"),
NFTDescriptor: require("@uniswap/v3-periphery/artifacts/contracts/libraries/NFTDescriptor.sol/NFTDescriptor.json"),
NonfungibleTokenPositionDescriptor: require("@uniswap/v3-periphery/artifacts/contracts/NonfungibleTokenPositionDescriptor.sol/NonfungibleTokenPositionDescriptor.json"),
NonfungiblePositionManager: require("@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json"),
MATIC,
};
const linkLibraries = ({ bytecode, linkReferences }, libraries) => {
Object.keys(linkReferences).forEach((fileName) => {
Object.keys(linkReferences[fileName]).forEach((contractName) => {
if (!libraries.hasOwnProperty(contractName)) {
throw new Error(`Missing link library name ${contractName}`);
}
const address = utils
.getAddress(libraries[contractName])
.toLowerCase()
.slice(2);
linkReferences[fileName][contractName].forEach(({ start, length }) => {
const start2 = 2 + start * 2;
const length2 = length * 2;
bytecode = bytecode
.slice(0, start2)
.concat(address)
.concat(bytecode.slice(start2 + length2, bytecode.length));
});
});
});
return bytecode;
};
async function main() {
const [deployer] = await ethers.getSigners();
const Matic = new ContractFactory(
artifacts.MATIC.abi,
artifacts.MATIC.bytecode,
deployer
);
const matic = await Matic.deploy();
Factory = new ContractFactory(
artifacts.UniswapV3Factory.abi,
artifacts.UniswapV3Factory.bytecode,
deployer
);
factory = await Factory.deploy();
SwapRouter = new ContractFactory(
artifacts.SwapRouter.abi,
artifacts.SwapRouter.bytecode,
deployer
);
swapRouter = await SwapRouter.deploy(factory.address, matic.address);
NFTDescriptor = new ContractFactory(
artifacts.NFTDescriptor.abi,
artifacts.NFTDescriptor.bytecode,
deployer
);
nftDescriptor = await NFTDescriptor.deploy();
const linkedBytecode = linkLibraries(
{
bytecode: artifacts.NonfungibleTokenPositionDescriptor.bytecode,
linkReferences: {
"NFTDescriptor.sol": {
NFTDescriptor: [
{
length: 20,
start: 1261,
},
],
},
},
},
{
NFTDescriptor: nftDescriptor.address,
}
);
NonfungibleTokenPositionDescriptor = new ContractFactory(
artifacts.NonfungibleTokenPositionDescriptor.abi,
linkedBytecode,
deployer
);
nonfungibleTokenPositionDescriptor =
await NonfungibleTokenPositionDescriptor.deploy(matic.address);
NonfungiblePositionManager = new ContractFactory(
artifacts.NonfungiblePositionManager.abi,
artifacts.NonfungiblePositionManager.bytecode,
deployer
);
nonfungiblePositionManager = await NonfungiblePositionManager.deploy(
factory.address,
matic.address,
nonfungibleTokenPositionDescriptor.address
);
console.log("Martic_ADDRESS=", `'${matic.address}'`);
console.log("FACTORY_ADDRESS=", `'${factory.address}'`);
console.log("SWAP_ROUTER_ADDRESS=", `'${swapRouter.address}'`);
console.log("NFT_DESCRIPTOR_ADDRESS=", `'${nftDescriptor.address}'`);
console.log(
"POSITION_DESCRIPTOR_ADDRESS=",
`'${nonfungibleTokenPositionDescriptor.address}'`
);
console.log(
"POSITION_MANAGER_ADDRESS=",
`'${nonfungiblePositionManager.address}'`
);
}
/*
npx hardhat run --network localhost scripts/01_deployContracts.js
*/
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Here's hardhat.config.js file:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
require("hardhat-deploy");
/** @type import('hardhat/config').HardhatUserConfig */
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL;
const MUMBAI_RPC_URL = process.env.MUMBAI_RPC_URL;
const Private_KEY = process.env.PRIVATE_KEY;
const Private_KEY_USER = process.env.PRIVATE_KEY_USER;
const Etherscan_API_KEY = process.env.ETHERSCAN_API_KEY;
const POLYGON_SCAN_API_KEY = process.env.POLYGONSCAN_API_KEY;
module.exports = {
defaultNetworks: "hardhat",
networks: {
sepolia: {
url: SEPOLIA_RPC_URL,
accounts: [Private_KEY],
chainId: 11155111,
blockConfirmations: 6,
},
mumbai: {
url: MUMBAI_RPC_URL,
accounts: [Private_KEY, Private_KEY_USER],
chainId: 80001,
blockConfirmations: 6,
},
localhost: {
url: "http://127.0.0.1:8545/",
chainId: 31337,
},
},
etherscan: {
apiKey: {
polygonMumbai: POLYGON_SCAN_API_KEY,
},
},
solidity: {
compilers: [{ version: "0.8.20" }, { version: "0.7.6" }],
},
namedAccounts: {
//by default 0th account is deployer
deployer: {
default: 0,
},
user: {
default: 1,
},
},
settings: {
optimizer: {
enabled: true,
runs: 5000,
details: { yul: false },
},
},
};