ytLending supports USDC as the base token

This commit is contained in:
2025-12-26 13:23:50 +08:00
parent e21ee7a5df
commit 9a92c81aec
37 changed files with 4298 additions and 3064 deletions

View File

@@ -4,7 +4,7 @@ import * as path from "path";
/**
* 部署 Lending 借贷池系统
* 包含LendingFactory, Configurator, Lending 实现和代理
* 包含LendingFactory, Configurator, LendingPriceFeed, Lending 实现和代理
*/
async function main() {
const [deployer] = await ethers.getSigners();
@@ -16,6 +16,30 @@ async function main() {
const deployments: any = {};
// ========== 读取配置参数 ==========
console.log("📋 读取配置参数...");
const network = await ethers.provider.getNetwork();
const chainId = network.chainId.toString();
let USDC_ADDRESS: string;
let USDC_PRICE_FEED: string;
if (chainId === "421614") {
// Arbitrum 测试网
USDC_ADDRESS = "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d";
USDC_PRICE_FEED = "0x0153002d20B96532C639313c2d54c3dA09109309"; // USDC/USD
} else if (chainId === "56") {
// BSC 主网
USDC_ADDRESS = "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d";
USDC_PRICE_FEED = "0x51597f405303C4377E36123cBc172b13269EA163"; // USDC/USD
} else {
throw new Error(`不支持的网络: ${chainId}`);
}
console.log(" USDC 地址:", USDC_ADDRESS);
console.log(" USDC Price Feed:", USDC_PRICE_FEED, "\n");
// ========== 第一阶段:部署工厂合约 ==========
console.log("📦 Phase 1: 部署 LendingFactory...");
const LendingFactory = await ethers.getContractFactory("LendingFactory");
@@ -25,8 +49,38 @@ async function main() {
console.log("✅ LendingFactory 已部署:", lendingFactoryAddress);
deployments.lendingFactory = lendingFactoryAddress;
// ========== 第二阶段:部署 Configurator ==========
console.log("\n📦 Phase 2: 部署 Configurator (UUPS 代理)...");
// ========== 第二阶段:部署 LendingPriceFeed (UUPS 代理) ==========
console.log("\n📦 Phase 2: 部署 LendingPriceFeed (UUPS 代理)...");
const LendingPriceFeed = await ethers.getContractFactory("LendingPriceFeed");
// 使用 upgrades 插件部署 UUPS 代理
const lendingPriceFeed = await upgrades.deployProxy(
LendingPriceFeed,
[USDC_ADDRESS, USDC_PRICE_FEED],
{
kind: "uups",
initializer: "initialize"
}
);
await lendingPriceFeed.waitForDeployment();
const lendingPriceFeedProxyAddress = await lendingPriceFeed.getAddress();
console.log("✅ LendingPriceFeed Proxy:", lendingPriceFeedProxyAddress);
deployments.lendingPriceFeedProxy = lendingPriceFeedProxyAddress;
deployments.lendingPriceFeed = lendingPriceFeedProxyAddress; // 兼容旧字段
// 获取实现合约地址
const lendingPriceFeedImplAddress = await upgrades.erc1967.getImplementationAddress(lendingPriceFeedProxyAddress);
console.log("✅ LendingPriceFeed Implementation:", lendingPriceFeedImplAddress);
deployments.lendingPriceFeedImpl = lendingPriceFeedImplAddress;
// 验证价格获取
const usdcPrice = await lendingPriceFeed.getPrice(USDC_ADDRESS);
console.log("✅ USDC 价格 (1e30 精度):", usdcPrice.toString());
console.log("✅ LendingPriceFeed Owner:", await lendingPriceFeed.owner());
// ========== 第三阶段:部署 Configurator ==========
console.log("\n📦 Phase 3: 部署 Configurator (UUPS 代理)...");
const Configurator = await ethers.getContractFactory("Configurator");
// 使用 upgrades 插件部署 UUPS 代理
@@ -51,8 +105,8 @@ async function main() {
console.log("✅ Configurator Owner:", await configurator.owner());
// ========== 第阶段:部署 Lending 实现合约 ==========
console.log("\n📦 Phase 3: 通过工厂部署 Lending 实现合约...");
// ========== 第阶段:部署 Lending 实现合约 ==========
console.log("\n📦 Phase 4: 通过工厂部署 Lending 实现合约...");
const deployTx = await lendingFactory.deploy();
const deployReceipt = await deployTx.wait();
@@ -77,8 +131,8 @@ async function main() {
console.log("✅ Lending Implementation:", lendingImplAddress);
deployments.lendingImpl = lendingImplAddress;
// ========== 第阶段:准备部署 Lending 代理 ==========
console.log("\n📦 Phase 4: 准备部署 Lending 代理(需要先配置参数)");
// ========== 第阶段:准备部署 Lending 代理 ==========
console.log("\n📦 Phase 5: 准备部署 Lending 代理(需要先配置参数)");
console.log("⚠️ 请运行配置脚本 08-configureLending.ts 来完成配置和代理部署");
// ========== 保存部署信息 ==========
@@ -87,11 +141,11 @@ async function main() {
? JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"))
: {};
const network = await ethers.provider.getNetwork();
const chainId = network.chainId.toString();
existingDeployments[chainId] = {
...existingDeployments[chainId],
...deployments,
usdcAddress: USDC_ADDRESS,
usdcPriceFeed: USDC_PRICE_FEED,
deployTimestamp: new Date().toISOString(),
deployer: deployer.address
};
@@ -102,11 +156,23 @@ async function main() {
// ========== 部署总结 ==========
console.log("\n🎉 部署总结:");
console.log("=====================================");
console.log("LendingFactory: ", deployments.lendingFactory);
console.log("Configurator (Proxy): ", deployments.configuratorProxy);
console.log("Configurator (Impl): ", deployments.configuratorImpl);
console.log("Lending (Impl): ", deployments.lendingImpl);
console.log("=====================================\n");
console.log("📍 外部依赖:");
console.log(" USDC Address: ", USDC_ADDRESS);
console.log(" USDC Price Feed: ", USDC_PRICE_FEED);
console.log("\n📦 已部署合约:");
console.log(" LendingFactory: ", deployments.lendingFactory);
console.log("\n📊 LendingPriceFeed (UUPS):");
console.log(" Proxy: ", deployments.lendingPriceFeedProxy);
console.log(" Implementation: ", deployments.lendingPriceFeedImpl);
console.log("\n⚙ Configurator (UUPS):");
console.log(" Proxy: ", deployments.configuratorProxy);
console.log(" Implementation: ", deployments.configuratorImpl);
console.log("\n🏦 Lending:");
console.log(" Implementation: ", deployments.lendingImpl);
console.log(" Proxy: ", "待配置");
console.log("=====================================");
console.log("\n💡 下一步:");
console.log(" 运行 08-configureLending.ts 来创建 Lending 市场\n");
}
main()

View File

@@ -4,7 +4,7 @@ import * as path from "path";
/**
* 配置 Lending 借贷池参数并部署代理
* 包含:设置工厂、配置市场参数、添加抵押资产、部署 Lending 代理
* 包含:配置市场参数、添加 YT 抵押资产、部署 Lending 代理
*/
async function main() {
const [deployer] = await ethers.getSigners();
@@ -29,40 +29,66 @@ async function main() {
console.log("📋 使用已部署的合约:");
console.log(" LendingFactory:", deployments.lendingFactory);
console.log(" Configurator:", deployments.configuratorProxy);
console.log(" Lending Impl:", deployments.lendingImpl, "\n");
console.log(" LendingPriceFeed (Proxy):", deployments.lendingPriceFeed);
if (deployments.lendingPriceFeedImpl) {
console.log(" LendingPriceFeed (Impl):", deployments.lendingPriceFeedImpl);
}
console.log(" Configurator (Proxy):", deployments.configuratorProxy);
console.log(" Lending (Impl):", deployments.lendingImpl);
console.log(" USDC Address:", deployments.usdcAddress);
console.log(" USDC Price Feed:", deployments.usdcPriceFeed, "\n");
const configurator = await ethers.getContractAt("Configurator", deployments.configuratorProxy);
// ========== 读取 YT Vault 部署信息 ==========
const vaultDeploymentsPath = path.join(__dirname, "../../deployments-vault-system.json");
if (!fs.existsSync(vaultDeploymentsPath)) {
throw new Error("未找到 YT Vault 部署信息文件,请先部署 YT Vault 系统");
}
const vaultDeployments = JSON.parse(fs.readFileSync(vaultDeploymentsPath, "utf-8"));
if (!vaultDeployments.vaults || vaultDeployments.vaults.length === 0) {
throw new Error("未找到已部署的 YT Vault请先创建至少一个 YT Vault");
}
console.log("📋 找到 YT Vaults:", vaultDeployments.vaults.length);
vaultDeployments.vaults.forEach((vault: any, index: number) => {
console.log(` ${index + 1}. ${vault.name} (${vault.symbol}): ${vault.address}`);
});
console.log();
// ========== 第一阶段:配置外部代币和价格源 ==========
console.log("⚙️ Phase 1: 配置外部代币和价格源");
// ========== 第一阶段:配置参数 ==========
console.log("⚙️ Phase 1: 准备配置参数");
const USDC = {
address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
decimals: 6
address: deployments.usdcAddress,
decimals: 6 // todo bsc主网是 18 decimal
};
// 选择要作为抵押品的 YT Vaults可以选择多个
// todo: 根据需要修改这里,选择哪些 YT Vault 作为抵押品
const selectedVaults = vaultDeployments.vaults.slice(0, 3); // 默认选择前3个
const WETH = {
address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
decimals: 18
};
console.log(" 选择的抵押品 YT Vaults:");
selectedVaults.forEach((vault: any, index: number) => {
console.log(` ${index + 1}. ${vault.name}: ${vault.address}`);
});
console.log();
const usdcPriceFeed = "0x8fFfFfd4AfB6115b954Bd326cbe7B4BA576818f6";
const ethPriceFeed = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419";
console.log(" 基础资产 (USDC):", USDC.address);
console.log(" 抵押资产 (WETH):", WETH.address);
console.log(" USDC 价格源:", usdcPriceFeed);
console.log(" ETH 价格源:", ethPriceFeed, "\n");
// 准备抵押资产配置
const assetConfigs = selectedVaults.map((vault: any) => ({
asset: vault.address,
decimals: 18, // YT Token 都是 18 decimals
borrowCollateralFactor: ethers.parseUnits("0.80", 18), // 80% LTV
liquidateCollateralFactor: ethers.parseUnits("0.85", 18), // 85% 清算线
liquidationFactor: ethers.parseUnits("0.95", 18), // 95% (配合 storeFrontPriceFactor 产生折扣)
supplyCap: ethers.parseUnits("100000", 18) // 最多 10 万 YT
}));
// ========== 第二阶段:准备配置参数 ==========
console.log("⚙️ Phase 2: 准备市场配置参数");
// 使用占位符地址作为 Lending 代理地址
const LENDING_PROXY_PLACEHOLDER = ethers.ZeroAddress;
const configuration = {
baseToken: USDC.address,
baseTokenPriceFeed: usdcPriceFeed,
lendingPriceSource: deployments.lendingPriceFeed,
// 利率模型参数年化利率18位精度
// 注意:这些年化利率会在 initialize 时自动转换为每秒利率
@@ -82,47 +108,27 @@ async function main() {
baseBorrowMin: ethers.parseUnits("100", USDC.decimals), // 最小借 100 USDC
targetReserves: ethers.parseUnits("5000000", USDC.decimals), // 目标储备 500 万
assetConfigs: [
{
asset: WETH.address,
priceFeed: ethPriceFeed,
decimals: WETH.decimals,
borrowCollateralFactor: ethers.parseUnits("0.80", 18), // 80% LTV
liquidateCollateralFactor: ethers.parseUnits("0.85", 18), // 85% 清算线
liquidationFactor: ethers.parseUnits("0.95", 18), // 95% (配合 storeFrontPriceFactor 产生折扣)
supplyCap: ethers.parseUnits("100000", WETH.decimals) // 最多 10 万 ETH
}
]
assetConfigs: assetConfigs
};
console.log("✅ 配置参数已准备\n");
console.log("✅ 配置参数已准备");
console.log(" 基础资产: USDC (6 decimals)");
console.log(" 价格源: LendingPriceFeed");
console.log(" 抵押资产数量:", assetConfigs.length);
console.log(" Supply Kink: 80%");
console.log(" Borrow Kink: 80%");
console.log(" 最小借款: 100 USDC");
console.log(" 目标储备: 5,000,000 USDC\n");
// ========== 第三阶段:设置工厂和配置 ==========
console.log("⚙️ Phase 3: 设置工厂和配置参数");
// ========== 第三阶段:部署 Lending 代理 ==========
console.log("⚙️ Phase 3: 部署 Lending 代理");
console.log(" 设置工厂合约...");
const setFactoryTx = await configurator.setFactory(LENDING_PROXY_PLACEHOLDER, deployments.lendingFactory);
await setFactoryTx.wait();
console.log(" ✅ 工厂已设置");
console.log(" 设置市场配置...");
const setConfigTx = await configurator.setConfiguration(LENDING_PROXY_PLACEHOLDER, configuration);
await setConfigTx.wait();
console.log(" ✅ 配置已设置\n");
// ========== 第四阶段:部署 Lending 代理 ==========
console.log("⚙️ Phase 4: 部署 Lending 代理");
console.log(" 获取配置信息...");
const config = await configurator.getConfiguration(LENDING_PROXY_PLACEHOLDER);
console.log(" 部署 Lending 代理...");
const Lending = await ethers.getContractFactory("Lending");
// 使用 upgrades 插件部署 UUPS 代理
const lending = await upgrades.deployProxy(
Lending,
[config],
[configuration],
{
kind: "uups",
initializer: "initialize"
@@ -130,14 +136,32 @@ async function main() {
);
await lending.waitForDeployment();
const lendingProxyAddress = await lending.getAddress();
console.log(" ✅ Lending Proxy 已部署:", lendingProxyAddress);
console.log("✅ Lending Proxy 已部署:", lendingProxyAddress);
// 获取实现合约地址(验证)
const lendingImplAddress = await upgrades.erc1967.getImplementationAddress(lendingProxyAddress);
console.log(" ✅ Lending Implementation (验证):", lendingImplAddress, "\n");
console.log("✅ Lending Implementation (验证):", lendingImplAddress);
// 验证基本信息
console.log("\n📊 验证部署信息:");
const baseToken = await lending.baseToken();
const priceSource = await lending.lendingPriceSource();
const totalSupply = await lending.getTotalSupply();
const totalBorrow = await lending.getTotalBorrow();
console.log(" Base Token:", baseToken);
console.log(" Price Source:", priceSource);
console.log(" Total Supply:", totalSupply.toString());
console.log(" Total Borrow:", totalBorrow.toString());
console.log();
// ========== 保存部署信息 ==========
deployments.lendingProxy = lendingProxyAddress;
deployments.collateralAssets = selectedVaults.map((v: any) => ({
name: v.name,
symbol: v.symbol,
address: v.address
}));
deployments.configTimestamp = new Date().toISOString();
const allDeployments = JSON.parse(fs.readFileSync(deploymentsPath, "utf-8"));
@@ -147,14 +171,23 @@ async function main() {
console.log("💾 配置信息已保存到:", deploymentsPath);
// ========== 配置总结 ==========
console.log("\n🎉 配置总结:");
console.log("\n🎉 部署和配置完成!");
console.log("=====================================");
console.log("Lending Proxy: ", lendingProxyAddress);
console.log("Base Token: ", USDC.address);
console.log("Base Token (USDC): ", USDC.address);
console.log("Price Feed: ", deployments.lendingPriceFeed);
console.log("Collateral Assets: ", configuration.assetConfigs.length);
console.log("Supply Kink: ", "80%");
console.log("Borrow Kink: ", "80%");
console.log("Min Borrow: ", "100 USDC");
console.log("Target Reserves: ", "5,000,000 USDC");
console.log("=====================================");
console.log("\n📋 抵押资产列表:");
selectedVaults.forEach((vault: any, index: number) => {
console.log(` ${index + 1}. ${vault.name} (${vault.symbol})`);
console.log(` 地址: ${vault.address}`);
console.log(` LTV: 80%, 清算线: 85%`);
});
console.log("=====================================\n");
}

View File

@@ -29,16 +29,74 @@ async function main() {
}
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");
// ========== 选择要升级的合约 ==========
// 修改这里来选择升级 Lending 或 Configurator
const UPGRADE_LENDING = true; // true = 升级 Lending, false = 升级 Configurator
// 修改这里来选择升级哪个合约
// 1 = LendingPriceFeed, 2 = Configurator, 3 = Lending
const UPGRADE_CONTRACT = 1; // 修改这个数字来选择要升级的合约
if (UPGRADE_LENDING) {
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 合约");
@@ -81,7 +139,7 @@ async function main() {
deployments.lendingImpl = newLendingImplAddress;
deployments.lendingUpgradeTimestamp = new Date().toISOString();
} else {
} else if (UPGRADE_CONTRACT === 2) {
// ========== 升级 Configurator ==========
console.log("🔄 Phase 1: 升级 Configurator 合约");
@@ -117,34 +175,33 @@ async function main() {
// 保存新的实现地址
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("=====================================");
if (UPGRADE_LENDING) {
console.log("升级合约: Lending");
console.log("Lending Proxy: ", deployments.lendingProxy);
console.log("新 Lending Implementation:", deployments.lendingImpl);
} else {
console.log("升级合约: Configurator");
console.log("Configurator Proxy: ", deployments.configuratorProxy);
console.log("新 Configurator Impl: ", deployments.configuratorImpl);
}
console.log("=====================================\n");
console.log("✅ 升级完成!");
console.log("\n✅ 升级流程全部完成!");
console.log("⚠️ 重要提示:");
console.log(" 1. 代理地址保持不变,用户无需更改合约地址");
console.log(" 2. 所有状态数据已保留");
console.log(" 3. 建议在测试网充分测试后再升级主网\n");
console.log(" 3. 建议在测试网充分测试后再升级主网");
console.log(" 4. 当前升级的合约:", UPGRADE_CONTRACT === 1 ? "LendingPriceFeed" : (UPGRADE_CONTRACT === 2 ? "Configurator" : "Lending"), "\n");
}
main()