# Contract deployment order
### Boilerplate contracts
1. Deploy 2 virtual tokens -> BaseToken.sol & QuoteToken.sol
2. Deploy ERC20 Token `const USDC = (await tokenFactory.deploy()) as TestERC20``
3. Deploy UniswapV3Factory then create a pool
4. `await uniV3Factory.createPool(baseToken.address, quoteToken.address, uniFeeTier)`` /* uniFeeTier = 10000, // 1% */
### Protocol contracts
1. ClearingHouseConfig -> no arguments
2. MarketRegistry -> uniV3Factory.address & quoteToken.address
2.1. For each market, we deploy a pair of two virtual tokens (with no real value) and initiate a new Uniswap V3 pool to provide liquidity to.
2.1.1. Base token: the virtual underlying asset users are trading for, such as vETH, vBTC
2.1.2. Quote token: the counter currency of base token, which is always vUSDC for any base token
3. OrderBook -> (marketRegistry.address)
4. InsuranceFund -> (USDC.address)
5. Exchange -> (
marketRegistry.address,
orderBook.address,
clearingHouseConfig.address
)
6. AccountBalance -> (clearingHouseConfig.address, orderbook.address)
7. Vault -> (
insuranceFund.address,
clearingHouseConfig.address,
accountBalance.address,
exchange.address,
)
8. CollateralManager -> (
clearingHouseConfig.address,
vault.address,
5, // maxCollateralTokensPerAccount
"750000", // debtNonSettlementTokenValueRatio
"500000", // liquidationRatio
"2000", // mmRatioBuffer
"30000", // clInsuranceFundFeeRatio
parseUnits("10000", usdcDecimals), // debtThreshold
parseUnits("500", usdcDecimals), // collateralValueDust
)
9. ClearingHouse -> (
clearingHouseConfig.address,
vault.address,
quoteToken.address,
uniV3Factory.address,
exchange.address,
accountBalance.address,
insuranceFund.address
)
```javascript
const { ethers } = require("hardhat");
async function main() {
// Boilerplate contracts
const [deployer] = await ethers.getSigners();
// 1. Deploy BaseToken and QuoteToken
const BaseToken = await ethers.getContractFactory("BaseToken");
const baseToken = await BaseToken.deploy();
await baseToken.deployed();
const QuoteToken = await ethers.getContractFactory("QuoteToken");
const quoteToken = await QuoteToken.deploy();
await quoteToken.deployed();
// 2. Deploy USDC Token
const TestERC20 = await ethers.getContractFactory("TestERC20");
const USDC = await TestERC20.deploy();
await USDC.deployed();
// 3. Deploy UniswapV3Factory and create a pool
const UniswapV3Factory = await ethers.getContractFactory("UniswapV3Factory");
const uniV3Factory = await UniswapV3Factory.deploy();
await uniV3Factory.deployed();
const uniFeeTier = 10000; // 1%
await uniV3Factory.createPool(baseToken.address, quoteToken.address, uniFeeTier);
// Protocol contracts
// 1. ClearingHouseConfig
const ClearingHouseConfig = await ethers.getContractFactory("ClearingHouseConfig");
const clearingHouseConfig = await ClearingHouseConfig.deploy();
await clearingHouseConfig.deployed();
// 2. MarketRegistry
const MarketRegistry = await ethers.getContractFactory("MarketRegistry");
const marketRegistry = await MarketRegistry.deploy(uniV3Factory.address, quoteToken.address);
await marketRegistry.deployed();
// 3. OrderBook
const OrderBook = await ethers.getContractFactory("OrderBook");
const orderBook = await OrderBook.deploy(marketRegistry.address);
await orderBook.deployed();
// 4. InsuranceFund
const InsuranceFund = await ethers.getContractFactory("InsuranceFund");
const insuranceFund = await InsuranceFund.deploy(USDC.address);
await insuranceFund.deployed();
// 5. Exchange
const Exchange = await ethers.getContractFactory("Exchange");
const exchange = await Exchange.deploy(
marketRegistry.address,
orderBook.address,
clearingHouseConfig.address
);
await exchange.deployed();
// 6. AccountBalance
const AccountBalance = await ethers.getContractFactory("AccountBalance");
const accountBalance = await AccountBalance.deploy(
clearingHouseConfig.address,
orderBook.address
);
await accountBalance.deployed();
// 7. Vault
const Vault = await ethers.getContractFactory("Vault");
const vault = await Vault.deploy(
insuranceFund.address,
clearingHouseConfig.address,
accountBalance.address,
exchange.address,
);
await vault.deployed();
// 8. CollateralManager
const CollateralManager = await ethers.getContractFactory("CollateralManager");
const collateralManager = await CollateralManager.deploy(
clearingHouseConfig.address,
vault.address,
5, // maxCollateralTokensPerAccount
"750000", // debtNonSettlementTokenValueRatio
"500000", // liquidationRatio
"2000", // mmRatioBuffer
"30000", // clInsuranceFundFeeRatio
ethers.utils.parseUnits("10000", USDC_DECIMALS), // debtThreshold
ethers.utils.parseUnits("500", USDC_DECIMALS), // collateralValueDust
);
await collateralManager.deployed();
// 9. ClearingHouse
const ClearingHouse = await ethers.getContractFactory("ClearingHouse");
const clearingHouse = await ClearingHouse.deploy(
clearingHouseConfig.address,
vault.address,
quoteToken.address,
uniV3Factory.address,
exchange.address,
accountBalance.address,
insuranceFund.address
);
await clearingHouse.deployed();
console.log("Deployment completed successfully.");
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
});
```