import { DeploymentsKey, TokenContractFullyQualifiedName, TokenDeploymentsKey } from "@lugia/common/constant" import { ContractDeployManager } from "@lugia/common/script/contract-deploy-manager" import { DeployFunction } from "hardhat-deploy/types" import { HardhatRuntimeEnvironment } from "hardhat/types" import { lugiaConfig } from "../deploy.config" import { AddressManagerService } from "../scripts/address-manager-service" import { AddressManager, CircuitBreaker, Vault, NekoCoin } from "../typechain-types" const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { console.log(`\nRunning: ${__filename}`) const { isHardhatFork } = hre const { getNamedAccounts } = isHardhatFork ? hre.companionNetworks.fork : hre const { gnosisSafeAddress } = await getNamedAccounts() const deployManager = new ContractDeployManager(hre) const addressManager = await deployManager.getContract(DeploymentsKey.AddressManager) const vault = await deployManager.getContract(DeploymentsKey.Vault) const collateralToken = await vault.getCollateralToken() const deploymentKey = DeploymentsKey.CircuitBreaker // initially admin is deployer, so we can run following config updates // will set admin to gnosisSafeAddress in the end const args = [ lugiaConfig.circuitBreakerConfig.rateLimitCooldownPeriod, lugiaConfig.circuitBreakerConfig.withdrawalPeriod, lugiaConfig.circuitBreakerConfig.liquidityTickLength, ] await deployManager.deployContract(deploymentKey, args) const addressManagerService = new AddressManagerService(hre, addressManager) await addressManagerService.register(deploymentKey) const circuitBreaker = await deployManager.getContract(deploymentKey) // set protected contracts if (!(await circuitBreaker.isProtectedContract(vault.address))) { await deployManager.execute(DeploymentsKey.CircuitBreaker, "addProtectedContracts", [[vault.address]]) } // registerAssets const limiter = await circuitBreaker.tokenLimiters(collateralToken) if (limiter.minLiqRetainedBps.eq(0)) { await deployManager.execute(DeploymentsKey.CircuitBreaker, "registerAsset", [ collateralToken, lugiaConfig.circuitBreakerConfig.minLiqRetainedBps, lugiaConfig.circuitBreakerConfig.limitBeginThreshold, ]) } // updateAssetParams const limiterAfter = await circuitBreaker.tokenLimiters(collateralToken) if ( !limiterAfter.minLiqRetainedBps.eq(lugiaConfig.circuitBreakerConfig.minLiqRetainedBps) || !limiterAfter.limitBeginThreshold.eq(lugiaConfig.circuitBreakerConfig.limitBeginThreshold) ) { await deployManager.execute(DeploymentsKey.CircuitBreaker, "updateAssetParams", [ collateralToken, lugiaConfig.circuitBreakerConfig.minLiqRetainedBps, lugiaConfig.circuitBreakerConfig.limitBeginThreshold, ]) } // setAdmin if ((await circuitBreaker.admin()) !== gnosisSafeAddress) { await deployManager.execute(DeploymentsKey.CircuitBreaker, "setAdmin", [gnosisSafeAddress]) } // add circuitBreaker as whitelist of NekoCoin const nekoCoin = await deployManager.getContractWithName( TokenDeploymentsKey.NekoCoin, TokenContractFullyQualifiedName[TokenDeploymentsKey.NekoCoin], ) if (!(await nekoCoin.isInWhitelist(circuitBreaker.address))) { await deployManager.execute(TokenDeploymentsKey.NekoCoin, "addWhitelist", [circuitBreaker.address]) } } func.tags = ["CircuitBreaker"] func.dependencies = ["AddressManager", "Vault"] export default func