|
| 1 | +import { DeployFunction } from "hardhat-deploy/types"; |
| 2 | +import { Address, encodeFunctionData, Hash, zeroAddress } from "viem"; |
| 3 | + |
| 4 | +import { logTransaction } from "../chainDeploy/helpers/logging"; |
| 5 | + |
| 6 | +const func: DeployFunction = async ({ viem, getNamedAccounts, deployments }) => { |
| 7 | + const { deployer, multisig } = await getNamedAccounts(); |
| 8 | + const publicClient = await viem.getPublicClient(); |
| 9 | + |
| 10 | + const oldComptroller = await deployments.getOrNull("Comptroller"); |
| 11 | + const fuseFeeDistributor = await viem.getContractAt( |
| 12 | + "FeeDistributor", |
| 13 | + (await deployments.get("FeeDistributor")).address as Address |
| 14 | + ); |
| 15 | + let tx: Hash; |
| 16 | + |
| 17 | + const comp = await deployments.deploy("Comptroller", { |
| 18 | + contract: "Comptroller.sol:Comptroller", |
| 19 | + from: deployer, |
| 20 | + args: [], |
| 21 | + log: true |
| 22 | + }); |
| 23 | + if (comp.transactionHash) await publicClient.waitForTransactionReceipt({ hash: comp.transactionHash as Hash }); |
| 24 | + console.log("Comptroller ", comp.address); |
| 25 | + |
| 26 | + const compFirstExtension = await deployments.deploy("ComptrollerFirstExtension", { |
| 27 | + contract: "ComptrollerFirstExtension", |
| 28 | + from: deployer, |
| 29 | + args: [], |
| 30 | + log: true |
| 31 | + }); |
| 32 | + if (compFirstExtension.transactionHash) |
| 33 | + await publicClient.waitForTransactionReceipt({ hash: compFirstExtension.transactionHash as Hash }); |
| 34 | + console.log("ComptrollerFirstExtension", compFirstExtension.address); |
| 35 | + |
| 36 | + const comptroller = await viem.getContractAt( |
| 37 | + "Comptroller", |
| 38 | + (await deployments.get("Comptroller")).address as Address |
| 39 | + ); |
| 40 | + |
| 41 | + /// LATEST IMPLEMENTATIONS |
| 42 | + // Comptroller |
| 43 | + if (oldComptroller) { |
| 44 | + const latestComptrollerImplementation = await fuseFeeDistributor.read.latestComptrollerImplementation([ |
| 45 | + oldComptroller.address as Address |
| 46 | + ]); |
| 47 | + if (latestComptrollerImplementation === zeroAddress || latestComptrollerImplementation !== comptroller.address) { |
| 48 | + if ((await fuseFeeDistributor.read.owner()).toLowerCase() !== deployer.toLowerCase()) { |
| 49 | + logTransaction( |
| 50 | + "Set Latest Comptroller Implementation", |
| 51 | + encodeFunctionData({ |
| 52 | + abi: fuseFeeDistributor.abi, |
| 53 | + functionName: "_setLatestComptrollerImplementation", |
| 54 | + args: [oldComptroller.address as Address, comptroller.address] |
| 55 | + }) |
| 56 | + ); |
| 57 | + } else { |
| 58 | + tx = await fuseFeeDistributor.write._setLatestComptrollerImplementation([ |
| 59 | + oldComptroller.address as Address, |
| 60 | + comptroller.address |
| 61 | + ]); |
| 62 | + await publicClient.waitForTransactionReceipt({ hash: tx }); |
| 63 | + console.log( |
| 64 | + `Set the latest Comptroller implementation for ${oldComptroller.address} to ${comptroller.address}` |
| 65 | + ); |
| 66 | + } |
| 67 | + } else { |
| 68 | + console.log( |
| 69 | + `No change in the latest Comptroller implementation ${latestComptrollerImplementation} for ${comptroller.address}` |
| 70 | + ); |
| 71 | + } |
| 72 | + } else { |
| 73 | + // on the first deploy to a chain |
| 74 | + if (multisig && (await fuseFeeDistributor.read.owner()).toLowerCase() !== deployer.toLowerCase()) { |
| 75 | + logTransaction( |
| 76 | + "Set Latest Comptroller Implementation", |
| 77 | + encodeFunctionData({ |
| 78 | + abi: fuseFeeDistributor.abi, |
| 79 | + functionName: "_setLatestComptrollerImplementation", |
| 80 | + args: [zeroAddress, comptroller.address] |
| 81 | + }) |
| 82 | + ); |
| 83 | + } else { |
| 84 | + tx = await fuseFeeDistributor.write._setLatestComptrollerImplementation([zeroAddress, comptroller.address]); |
| 85 | + await publicClient.waitForTransactionReceipt({ hash: tx }); |
| 86 | + console.log(`Set the latest Comptroller implementation for ${zeroAddress} to ${comptroller.address}`); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const comptrollerExtensions = await fuseFeeDistributor.read.getComptrollerExtensions([comptroller.address]); |
| 91 | + if (comptrollerExtensions.length == 0 || comptrollerExtensions[1] != compFirstExtension.address) { |
| 92 | + if (multisig && (await fuseFeeDistributor.read.owner()).toLowerCase() !== deployer.toLowerCase()) { |
| 93 | + logTransaction( |
| 94 | + "Set Comptroller Extensions", |
| 95 | + encodeFunctionData({ |
| 96 | + abi: fuseFeeDistributor.abi, |
| 97 | + functionName: "_setComptrollerExtensions", |
| 98 | + args: [comptroller.address, [comptroller.address, compFirstExtension.address as Address]] |
| 99 | + }) |
| 100 | + ); |
| 101 | + } else { |
| 102 | + tx = await fuseFeeDistributor.write._setComptrollerExtensions([ |
| 103 | + comptroller.address, |
| 104 | + [comptroller.address, compFirstExtension.address as Address] |
| 105 | + ]); |
| 106 | + await publicClient.waitForTransactionReceipt({ hash: tx }); |
| 107 | + console.log(`configured the extensions for comptroller ${comptroller.address}`); |
| 108 | + } |
| 109 | + } else { |
| 110 | + console.log(`comptroller extensions already configured`); |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +func.tags = ["prod", "comptroller-setup"]; |
| 115 | + |
| 116 | +export default func; |
0 commit comments