add buyCollateral script and add setTargetReserves function for lending contract

This commit is contained in:
2026-01-08 11:30:31 +08:00
parent c8cb4dbecd
commit a18b9a42e4
19 changed files with 713 additions and 1221 deletions

View File

@@ -37,6 +37,13 @@ async function main() {
usdcPriceFeedAddress = "0x0153002d20B96532C639313c2d54c3dA09109309";
console.log("✅ USDC地址 (Arbitrum):", usdcAddress);
console.log("✅ Chainlink USDC/USD (Arbitrum):", usdcPriceFeedAddress);
} else if (chainId === 97n) {
// BNB 测试网
console.log("\n检测到 BNB 测试网");
usdcAddress = "0x939cf46F7A4d05da2a37213E7379a8b04528F590";
usdcPriceFeedAddress = "0x90c069C4538adAc136E051052E14c1cD799C41B7";
console.log("✅ USDC地址 (BSC Testnet):", usdcAddress);
console.log("✅ Chainlink USDC/USD (BSC Testnet):", usdcPriceFeedAddress);
} else {
throw new Error(`不支持的网络: ${chainId}`);
}

View File

@@ -33,6 +33,11 @@ async function main() {
// BSC 主网
USDC_ADDRESS = "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d";
USDC_PRICE_FEED = "0x51597f405303C4377E36123cBc172b13269EA163"; // USDC/USD
}
else if (chainId === "97") {
// BSC 测试网
USDC_ADDRESS = "0x939cf46F7A4d05da2a37213E7379a8b04528F590";
USDC_PRICE_FEED = "0x90c069C4538adAc136E051052E14c1cD799C41B7"; // USDC/USD
} else {
throw new Error(`不支持的网络: ${chainId}`);
}

View File

@@ -60,7 +60,7 @@ async function main() {
const USDC = {
address: deployments.usdcAddress,
decimals: 6 // todo bsc主网是 18 decimal
decimals: 18
};
// 选择要作为抵押品的 YT Vaults可以选择多个

View File

@@ -1,213 +0,0 @@
import { ethers, upgrades } from "hardhat";
import * as fs from "fs";
import * as path from "path";
/**
* 升级 Lending 或 Configurator 合约
* 使用 upgrades.upgradeProxy() 进行 UUPS 升级
*/
async function main() {
const [deployer] = await ethers.getSigners();
console.log("\n==========================================");
console.log("🔄 升级 Lending 借贷池系统");
console.log("==========================================");
console.log("升级账户:", deployer.address);
console.log("账户余额:", ethers.formatEther(await ethers.provider.getBalance(deployer.address)), "ETH\n");
// ========== 读取部署信息 ==========
const deploymentsPath = path.join(__dirname, "../../deployments-lending.json");
if (!fs.existsSync(deploymentsPath)) {
throw new Error("未找到部署信息文件,请先运行部署脚本");
}
const network = await ethers.provider.getNetwork();
const chainId = network.chainId.toString();
const deployments = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"))[chainId];
if (!deployments) {
throw new Error(`未找到网络 ${chainId} 的部署信息`);
}
console.log("📋 当前部署的合约:");
console.log(" LendingPriceFeed Proxy:", deployments.lendingPriceFeed);
if (deployments.lendingPriceFeedImpl) {
console.log(" LendingPriceFeed Impl:", deployments.lendingPriceFeedImpl);
}
console.log(" Configurator Proxy:", deployments.configuratorProxy);
console.log(" Configurator Impl:", deployments.configuratorImpl);
console.log(" Lending Proxy:", deployments.lendingProxy);
console.log(" Lending Impl:", deployments.lendingImpl, "\n");
// ========== 选择要升级的合约 ==========
// 修改这里来选择升级哪个合约
// 1 = LendingPriceFeed, 2 = Configurator, 3 = Lending
const UPGRADE_CONTRACT = 1; // 修改这个数字来选择要升级的合约
if (UPGRADE_CONTRACT === 1) {
// ========== 升级 LendingPriceFeed ==========
console.log("🔄 Phase 1: 升级 LendingPriceFeed 合约");
if (!deployments.lendingPriceFeed) {
throw new Error("未找到 LendingPriceFeed Proxy 地址,请先运行部署脚本");
}
console.log(" 当前 LendingPriceFeed Proxy:", deployments.lendingPriceFeed);
if (deployments.lendingPriceFeedImpl) {
console.log(" 当前 LendingPriceFeed Implementation:", deployments.lendingPriceFeedImpl);
}
// 获取新的 LendingPriceFeed 合约工厂
const LendingPriceFeedV2 = await ethers.getContractFactory("LendingPriceFeed");
console.log("\n 正在验证新实现合约...");
const upgradedPriceFeed = await upgrades.upgradeProxy(
deployments.lendingPriceFeed,
LendingPriceFeedV2,
{
kind: "uups"
}
);
await upgradedPriceFeed.waitForDeployment();
console.log(" ✅ LendingPriceFeed 已升级!");
// 获取新的实现合约地址
const upgradedPriceFeedAddress = await upgradedPriceFeed.getAddress();
const newPriceFeedImplAddress = await upgrades.erc1967.getImplementationAddress(upgradedPriceFeedAddress);
console.log(" 新 LendingPriceFeed Implementation:", newPriceFeedImplAddress);
// 验证升级
console.log("\n 验证升级结果:");
console.log(" LendingPriceFeed Proxy (不变):", upgradedPriceFeedAddress);
console.log(" Owner:", await upgradedPriceFeed.owner());
console.log(" USDC Address:", await upgradedPriceFeed.usdcAddress());
// 保存新的实现地址
deployments.lendingPriceFeedImpl = newPriceFeedImplAddress;
deployments.lastUpgradeTime = new Date().toISOString();
const allDeployments = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"));
allDeployments[chainId] = deployments;
fs.writeFileSync(deploymentsPath, JSON.stringify(allDeployments, null, 2));
console.log("\n✅ LendingPriceFeed 升级完成!");
console.log("=====================================");
console.log("旧实现:", deployments.lendingPriceFeedImpl || "未记录");
console.log("新实现:", newPriceFeedImplAddress);
console.log("=====================================\n");
} else if (UPGRADE_CONTRACT === 3) {
// ========== 升级 Lending ==========
console.log("🔄 Phase 1: 升级 Lending 合约");
if (!deployments.lendingProxy) {
throw new Error("未找到 Lending Proxy 地址,请先运行配置脚本");
}
console.log(" 当前 Lending Proxy:", deployments.lendingProxy);
console.log(" 当前 Lending Implementation:", deployments.lendingImpl);
// 获取新的 Lending 合约工厂
// 注意:如果你有 LendingV2请替换为 "LendingV2"
const LendingV2 = await ethers.getContractFactory("Lending");
console.log("\n 正在验证新实现合约...");
// upgrades.upgradeProxy 会自动验证存储布局兼容性
const upgradedLending = await upgrades.upgradeProxy(
deployments.lendingProxy,
LendingV2,
{
kind: "uups"
}
);
await upgradedLending.waitForDeployment();
console.log(" ✅ Lending 已升级!");
// 获取新的实现合约地址
const upgradedLendingAddress = await upgradedLending.getAddress();
const newLendingImplAddress = await upgrades.erc1967.getImplementationAddress(upgradedLendingAddress);
console.log(" 新 Lending Implementation:", newLendingImplAddress);
// 验证升级
console.log("\n 验证升级结果:");
console.log(" Lending Proxy (不变):", upgradedLendingAddress);
console.log(" Owner:", await upgradedLending.owner());
console.log(" Base Token:", await upgradedLending.baseToken());
// 保存新的实现地址
deployments.lendingImpl = newLendingImplAddress;
deployments.lendingUpgradeTimestamp = new Date().toISOString();
} else if (UPGRADE_CONTRACT === 2) {
// ========== 升级 Configurator ==========
console.log("🔄 Phase 1: 升级 Configurator 合约");
console.log(" 当前 Configurator Proxy:", deployments.configuratorProxy);
console.log(" 当前 Configurator Implementation:", deployments.configuratorImpl);
// 获取新的 Configurator 合约工厂
// 注意:如果你有 ConfiguratorV2请替换为 "ConfiguratorV2"
const ConfiguratorV2 = await ethers.getContractFactory("Configurator");
console.log("\n 正在验证新实现合约...");
const upgradedConfigurator = await upgrades.upgradeProxy(
deployments.configuratorProxy,
ConfiguratorV2,
{
kind: "uups"
}
);
await upgradedConfigurator.waitForDeployment();
console.log(" ✅ Configurator 已升级!");
// 获取新的实现合约地址
const upgradedConfiguratorAddress = await upgradedConfigurator.getAddress();
const newConfiguratorImplAddress = await upgrades.erc1967.getImplementationAddress(upgradedConfiguratorAddress);
console.log(" 新 Configurator Implementation:", newConfiguratorImplAddress);
// 验证升级
console.log("\n 验证升级结果:");
console.log(" Configurator Proxy (不变):", upgradedConfiguratorAddress);
console.log(" Owner:", await upgradedConfigurator.owner());
// 保存新的实现地址
deployments.configuratorImpl = newConfiguratorImplAddress;
deployments.configuratorUpgradeTimestamp = new Date().toISOString();
const allDeployments2 = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"));
allDeployments2[chainId] = deployments;
fs.writeFileSync(deploymentsPath, JSON.stringify(allDeployments2, null, 2));
console.log("\n✅ Configurator 升级完成!");
console.log("=====================================");
console.log("旧实现:", deployments.configuratorImpl);
console.log("新实现:", newConfiguratorImplAddress);
console.log("=====================================\n");
} else {
throw new Error(`无效的升级选项: ${UPGRADE_CONTRACT}。请设置 UPGRADE_CONTRACT 为 1 (LendingPriceFeed), 2 (Configurator), 或 3 (Lending)`);
}
// ========== 保存部署信息(最终)==========
const allDeployments = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"));
allDeployments[chainId] = deployments;
fs.writeFileSync(deploymentsPath, JSON.stringify(allDeployments, null, 2));
console.log("\n💾 升级信息已保存到:", deploymentsPath);
console.log("\n✅ 升级流程全部完成!");
console.log("⚠️ 重要提示:");
console.log(" 1. 代理地址保持不变,用户无需更改合约地址");
console.log(" 2. 所有状态数据已保留");
console.log(" 3. 建议在测试网充分测试后再升级主网");
console.log(" 4. 当前升级的合约:", UPGRADE_CONTRACT === 1 ? "LendingPriceFeed" : (UPGRADE_CONTRACT === 2 ? "Configurator" : "Lending"), "\n");
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});