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

@@ -209,6 +209,19 @@
"name": "SupplyCollateral", "name": "SupplyCollateral",
"type": "event" "type": "event"
}, },
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint104",
"name": "targetReserves",
"type": "uint104"
}
],
"name": "TargetReservesUpdated",
"type": "event"
},
{ {
"anonymous": false, "anonymous": false,
"inputs": [ "inputs": [

View File

@@ -365,6 +365,19 @@
"name": "SupplyCollateral", "name": "SupplyCollateral",
"type": "event" "type": "event"
}, },
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint104",
"name": "targetReserves",
"type": "uint104"
}
],
"name": "TargetReservesUpdated",
"type": "event"
},
{ {
"anonymous": false, "anonymous": false,
"inputs": [ "inputs": [
@@ -1116,6 +1129,19 @@
"stateMutability": "nonpayable", "stateMutability": "nonpayable",
"type": "function" "type": "function"
}, },
{
"inputs": [
{
"internalType": "uint104",
"name": "newTargetReserves",
"type": "uint104"
}
],
"name": "setTargetReserves",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{ {
"inputs": [], "inputs": [],
"name": "storeFrontPriceFactor", "name": "storeFrontPriceFactor",

View File

@@ -14,6 +14,7 @@ interface ILending {
event AbsorbCollateral(address indexed absorber, address indexed borrower, address indexed asset, uint256 collateralAbsorbed, uint256 usdValue); event AbsorbCollateral(address indexed absorber, address indexed borrower, address indexed asset, uint256 collateralAbsorbed, uint256 usdValue);
event BuyCollateral(address indexed buyer, address indexed asset, uint256 baseAmount, uint256 collateralAmount); event BuyCollateral(address indexed buyer, address indexed asset, uint256 baseAmount, uint256 collateralAmount);
event WithdrawReserves(address indexed to, uint256 amount); event WithdrawReserves(address indexed to, uint256 amount);
event TargetReservesUpdated(uint104 targetReserves);
error Unauthorized(); error Unauthorized();
error InsufficientBalance(); error InsufficientBalance();

View File

@@ -95,6 +95,11 @@ contract Lending is
_unpause(); _unpause();
} }
function setTargetReserves(uint104 newTargetReserves) external onlyOwner {
targetReserves = newTargetReserves;
emit TargetReservesUpdated(targetReserves);
}
/** /**
* @notice 计算累计利息后的索引(不修改状态) * @notice 计算累计利息后的索引(不修改状态)
* @param timeElapsed 经过的时间 * @param timeElapsed 经过的时间

View File

@@ -1,33 +1,44 @@
{ {
"421614": { "97": {
"lendingFactory": "0xB69Dcea8F67d166Ad44650A2281f132689E524f2", "lendingFactory": "0x41857cc92a74fa5FB776e5D7091dD79faaaA973C",
"lendingPriceFeedProxy": "0xE82c7cB9CfA42D6eb7e443956b78f8290249c316", "lendingPriceFeedProxy": "0x13bD017E5837b5451447508ebd4Fe65A2B1d4f30",
"lendingPriceFeed": "0xE82c7cB9CfA42D6eb7e443956b78f8290249c316", "lendingPriceFeed": "0x13bD017E5837b5451447508ebd4Fe65A2B1d4f30",
"lendingPriceFeedImpl": "0xa51409ad5B8Fa77aB7ab8221a5bD76fdF7077E08", "lendingPriceFeedImpl": "0xff09A4EBC7F871b8B2CdCfE4c94e30E46DE559d5",
"configuratorProxy": "0x488409CE9A3Fcd8EbD373dCb7e025cF8AB96fcdc", "configuratorProxy": "0xB9676f3482D332210C5c630Fa265c92171958F92",
"configuratorImpl": "0xB6c6A171C63Bd15B42f28C3207eb697F3c4d4606", "configuratorImpl": "0xF553843cb7F35378Fd4020d2DFceb90BfD760552",
"lendingImpl": "0xdE3ddDBB2fee645EEc14f90cbe6890eBaa249b6e", "lendingImpl": "0x5336FE1bC5c7c1dE35CB053f2f649EA9D4dA3E5F",
"usdcAddress": "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d", "usdcAddress": "0x939cf46F7A4d05da2a37213E7379a8b04528F590",
"usdcPriceFeed": "0x0153002d20B96532C639313c2d54c3dA09109309", "usdcPriceFeed": "0x90c069C4538adAc136E051052E14c1cD799C41B7",
"deployTimestamp": "2025-12-26T04:16:38.113Z", "deployTimestamp": "2026-01-07T03:58:01.816Z",
"deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"lendingProxy": "0xCb4E7B1069F6C26A1c27523ce4c8dfD884552d1D", "lendingProxy": "0x1E60013A6eb8966a1b59BC31cE0D07054E591eE7",
"collateralAssets": [ "collateralAssets": [
{ {
"name": "YT Token A", "name": "YT Token A",
"symbol": "YT-A", "symbol": "YT-A",
"address": "0x97204190B35D9895a7a47aa7BaC61ac08De3cF05" "address": "0x0cA35994F033685E7a57ef9bc5d00dd3cf927330"
}, },
{ {
"name": "YT Token B", "name": "YT Token B",
"symbol": "YT-B", "symbol": "YT-B",
"address": "0x181ef4011c35C4a2Fda08eBC5Cf509Ef58E553fF" "address": "0x333805C9EE75f59Aa2Cc79DfDe2499F920c7b408"
}, },
{ {
"name": "YT Token C", "name": "YT Token C",
"symbol": "YT-C", "symbol": "YT-C",
"address": "0xE9A5b9f3a2Eda4358f81d4E2eF4f3280A664e5B0" "address": "0x6DF0ED6f0345F601A206974973dE9fC970598587"
} }
] ],
"configTimestamp": "2026-01-07T03:58:41.420Z",
"upgradeHistory": [
{
"timestamp": "2026-01-08T03:26:13.317Z",
"contract": "Lending",
"oldImplementation": "0xe443B92e052e72C39a7833cD723276f8e337144A",
"newImplementation": "0x5336FE1bC5c7c1dE35CB053f2f649EA9D4dA3E5F",
"upgrader": "0xa013422A5918CD099C63c8CC35283EACa99a705d"
}
],
"lendingUpgradeTimestamp": "2026-01-08T03:26:13.317Z"
} }
} }

View File

@@ -1,15 +1,15 @@
{ {
"network": "arbSepolia", "network": "bscTestnet",
"chainId": "421614", "chainId": "97",
"deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"timestamp": "2025-12-24T08:07:32.332Z", "timestamp": "2026-01-07T03:50:12.838Z",
"contracts": { "contracts": {
"USDC": { "USDC": {
"address": "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d", "address": "0x939cf46F7A4d05da2a37213E7379a8b04528F590",
"description": "USDC代币地址已存在的合约" "description": "USDC代币地址已存在的合约"
}, },
"ChainlinkUSDCPriceFeed": { "ChainlinkUSDCPriceFeed": {
"address": "0x0153002d20B96532C639313c2d54c3dA09109309", "address": "0x90c069C4538adAc136E051052E14c1cD799C41B7",
"description": "Chainlink USDC/USD 价格预言机", "description": "Chainlink USDC/USD 价格预言机",
"precision": "1e8" "precision": "1e8"
} }

View File

@@ -1,51 +1,51 @@
{ {
"network": "arbSepolia", "network": "bscTestnet",
"chainId": "421614", "chainId": "97",
"deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"timestamp": "2025-12-24T08:11:26.455Z", "timestamp": "2026-01-07T03:53:55.098Z",
"usdcAddress": "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d", "usdcAddress": "0x939cf46F7A4d05da2a37213E7379a8b04528F590",
"usdcPriceFeedAddress": "0x0153002d20B96532C639313c2d54c3dA09109309", "usdcPriceFeedAddress": "0x90c069C4538adAc136E051052E14c1cD799C41B7",
"defaultHardCap": "10000000000000000000000000", "defaultHardCap": "10000000000000000000000000",
"contracts": { "contracts": {
"YTAssetVault": { "YTAssetVault": {
"implementation": "0x8097a7B04989c4D8B155Fd5DaF396d014808D7F7" "implementation": "0x6cBD32731742004471ce16FcB80a6db0844E8b13"
}, },
"YTAssetFactory": { "YTAssetFactory": {
"proxy": "0xb5Ddb2C45874f04aD0d48F3bB6b0748b1D06814C", "proxy": "0x6DaB73519DbaFf23F36FEd24110e2ef5Cfc8aAC9",
"implementation": "0xcD175992dE5EfF46673dBaAb12979bc4fcC0f0f6" "implementation": "0xc22a07Cf4bbDc323bC3288a82E85d1367a470b75"
} }
}, },
"vaults": [ "vaults": [
{ {
"name": "YT Token A", "name": "YT Token A",
"symbol": "YT-A", "symbol": "YT-A",
"address": "0x97204190B35D9895a7a47aa7BaC61ac08De3cF05", "address": "0x0cA35994F033685E7a57ef9bc5d00dd3cf927330",
"index": "0", "index": "0",
"manager": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "manager": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"hardCap": "10000000000000000000000000", "hardCap": "10000000000000000000000000",
"redemptionTime": 1798099928, "redemptionTime": 1799294058,
"ytPrice": "1000000000000000000000000000000" "ytPrice": "1000000000000000000000000000000"
}, },
{ {
"name": "YT Token B", "name": "YT Token B",
"symbol": "YT-B", "symbol": "YT-B",
"address": "0x181ef4011c35C4a2Fda08eBC5Cf509Ef58E553fF", "address": "0x333805C9EE75f59Aa2Cc79DfDe2499F920c7b408",
"index": "1", "index": "1",
"manager": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "manager": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"hardCap": "10000000000000000000000000", "hardCap": "10000000000000000000000000",
"redemptionTime": 1798099928, "redemptionTime": 1799294058,
"ytPrice": "1000000000000000000000000000000" "ytPrice": "1000000000000000000000000000000"
}, },
{ {
"name": "YT Token C", "name": "YT Token C",
"symbol": "YT-C", "symbol": "YT-C",
"address": "0xE9A5b9f3a2Eda4358f81d4E2eF4f3280A664e5B0", "address": "0x6DF0ED6f0345F601A206974973dE9fC970598587",
"index": "2", "index": "2",
"manager": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "manager": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"hardCap": "10000000000000000000000000", "hardCap": "10000000000000000000000000",
"redemptionTime": 1798099928, "redemptionTime": 1799294058,
"ytPrice": "1000000000000000000000000000000" "ytPrice": "1000000000000000000000000000000"
} }
], ],
"lastUpdate": "2025-12-24T08:12:25.131Z" "lastUpdate": "2026-01-07T03:54:28.113Z"
} }

View File

@@ -1,12 +1,12 @@
{ {
"timestamp": "2025-12-24T08:20:12.674Z", "timestamp": "2026-01-07T03:56:07.469Z",
"operator": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "operator": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"whitelistedTokens": { "whitelistedTokens": {
"ytTokens": [ "ytTokens": [
{ {
"name": "YT Token A", "name": "YT Token A",
"symbol": "YT-A", "symbol": "YT-A",
"address": "0x97204190B35D9895a7a47aa7BaC61ac08De3cF05", "address": "0x0cA35994F033685E7a57ef9bc5d00dd3cf927330",
"weight": 4000, "weight": 4000,
"maxUsdyAmount": "45000000000000000000000000", "maxUsdyAmount": "45000000000000000000000000",
"price": "1000000000000000000000000000000", "price": "1000000000000000000000000000000",
@@ -15,7 +15,7 @@
{ {
"name": "YT Token B", "name": "YT Token B",
"symbol": "YT-B", "symbol": "YT-B",
"address": "0x181ef4011c35C4a2Fda08eBC5Cf509Ef58E553fF", "address": "0x333805C9EE75f59Aa2Cc79DfDe2499F920c7b408",
"weight": 3000, "weight": 3000,
"maxUsdyAmount": "35000000000000000000000000", "maxUsdyAmount": "35000000000000000000000000",
"price": "1000000000000000000000000000000", "price": "1000000000000000000000000000000",
@@ -24,7 +24,7 @@
{ {
"name": "YT Token C", "name": "YT Token C",
"symbol": "YT-C", "symbol": "YT-C",
"address": "0xE9A5b9f3a2Eda4358f81d4E2eF4f3280A664e5B0", "address": "0x6DF0ED6f0345F601A206974973dE9fC970598587",
"weight": 2000, "weight": 2000,
"maxUsdyAmount": "25000000000000000000000000", "maxUsdyAmount": "25000000000000000000000000",
"price": "1000000000000000000000000000000", "price": "1000000000000000000000000000000",
@@ -34,7 +34,7 @@
"usdc": { "usdc": {
"name": "USDC", "name": "USDC",
"symbol": "USDC", "symbol": "USDC",
"address": "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d", "address": "0x939cf46F7A4d05da2a37213E7379a8b04528F590",
"weight": 1000, "weight": 1000,
"maxUsdyAmount": "30000000000000", "maxUsdyAmount": "30000000000000",
"priceSource": "Chainlink (自动)", "priceSource": "Chainlink (自动)",

View File

@@ -1,23 +1,23 @@
{ {
"network": "arbSepolia", "network": "bscTestnet",
"chainId": "421614", "chainId": "97",
"configurer": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "configurer": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"timestamp": "2025-12-24T08:14:12.730Z", "timestamp": "2026-01-07T03:55:26.868Z",
"configuration": { "configuration": {
"permissions": { "permissions": {
"usdyVaults": [ "usdyVaults": [
"0xc110C84b107126c4E5b1CE598d3602ec0260D98B", "0x19982e5145ca5401A1084c0BF916c0E0bB343Af9",
"0x691Aa0fF71a330454f50452925A3005Ae8412902" "0x14246886a1E1202cb6b5a2db793eF3359d536302"
], ],
"ytlpMinters": [ "ytlpMinters": [
"0x691Aa0fF71a330454f50452925A3005Ae8412902" "0x14246886a1E1202cb6b5a2db793eF3359d536302"
], ],
"vaultPoolManager": "0x691Aa0fF71a330454f50452925A3005Ae8412902", "vaultPoolManager": "0x14246886a1E1202cb6b5a2db793eF3359d536302",
"vaultSwappers": [ "vaultSwappers": [
"0x15dA695F8ad005c2Ccd0AEC57C902c404E510Aab" "0x51eEF57eC57c867AC23945f0ce21aA5A9a2C246c"
], ],
"poolManagerHandlers": [ "poolManagerHandlers": [
"0x15dA695F8ad005c2Ccd0AEC57C902c404E510Aab" "0x51eEF57eC57c867AC23945f0ce21aA5A9a2C246c"
], ],
"priceFeedKeepers": [ "priceFeedKeepers": [
"0xa013422A5918CD099C63c8CC35283EACa99a705d" "0xa013422A5918CD099C63c8CC35283EACa99a705d"

View File

@@ -1,32 +1,32 @@
{ {
"network": "arbSepolia", "network": "bscTestnet",
"chainId": "421614", "chainId": "97",
"deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d", "deployer": "0xa013422A5918CD099C63c8CC35283EACa99a705d",
"timestamp": "2025-12-24T08:10:20.679Z", "timestamp": "2026-01-07T03:52:50.659Z",
"contracts": { "contracts": {
"USDY": { "USDY": {
"proxy": "0x664dF9c24b184f8D2533BfFF1E8cbff939978879", "proxy": "0x631Bd6834C50f6d2B07035c9253b4a19132E888c",
"implementation": "0x88b8E9aE3789A2c06C5df536C71e691cD6780a65" "implementation": "0xb14d186d4EAcE8131a449126c6208165a3F5FC5b"
}, },
"YTLPToken": { "YTLPToken": {
"proxy": "0x102e3F25Ef0ad9b0695C8F2daF8A1262437eEfc3", "proxy": "0x1b96F219E8aeE557DD8bD905a6c72cc64eA5BD7B",
"implementation": "0xE071419995aE63079af74E7f8eB1643B6F6fb2d7" "implementation": "0x0C3fa01b2D0596B4190edEF1B77534237231C77e"
}, },
"YTPriceFeed": { "YTPriceFeed": {
"proxy": "0xdC18de7D5A439cb90F149Eb62bAace55557d20AA", "proxy": "0x0f2d930EE73972132E3a36b7eD6F709Af6E5B879",
"implementation": "0x7088891AeAA1d6795bA49C1871199EbAc3892599" "implementation": "0x2201c2B382E1decD933fc8d3503bEcE221B6C46c"
}, },
"YTVault": { "YTVault": {
"proxy": "0xc110C84b107126c4E5b1CE598d3602ec0260D98B", "proxy": "0x19982e5145ca5401A1084c0BF916c0E0bB343Af9",
"implementation": "0xaF332cd890A394501E191Aa683Fe6aF4227C2623" "implementation": "0x61278a2EBFC07eF0F7f84407291aAD07DA596AB2"
}, },
"YTPoolManager": { "YTPoolManager": {
"proxy": "0x691Aa0fF71a330454f50452925A3005Ae8412902", "proxy": "0x14246886a1E1202cb6b5a2db793eF3359d536302",
"implementation": "0x0D4625A5d3b696684ECf00b49F1B68297A8b3154" "implementation": "0x96Fe19188c3c7d0EDA441dafC7976fBB3526d28c"
}, },
"YTRewardRouter": { "YTRewardRouter": {
"proxy": "0x15dA695F8ad005c2Ccd0AEC57C902c404E510Aab", "proxy": "0x51eEF57eC57c867AC23945f0ce21aA5A9a2C246c",
"implementation": "0xa77E96720924c7CBc70D4B0E3842a962f94931dc" "implementation": "0x3688CDd7A25613E7b1E7E0ee1aA46c21F66D27F3"
} }
} }
} }

View File

@@ -145,6 +145,11 @@ const config: HardhatUserConfig = {
accounts: accounts, accounts: accounts,
chainId: 421614, chainId: 421614,
}, },
bscTestnet: {
url: "https://api.zan.top/node/v1/bsc/testnet/baf84c429d284bb5b676cb8c9ca21c07",
accounts: accounts,
chainId: 97,
},
}, },
// gas报告配置 // gas报告配置

View File

@@ -37,6 +37,13 @@ async function main() {
usdcPriceFeedAddress = "0x0153002d20B96532C639313c2d54c3dA09109309"; usdcPriceFeedAddress = "0x0153002d20B96532C639313c2d54c3dA09109309";
console.log("✅ USDC地址 (Arbitrum):", usdcAddress); console.log("✅ USDC地址 (Arbitrum):", usdcAddress);
console.log("✅ Chainlink USDC/USD (Arbitrum):", usdcPriceFeedAddress); 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 { } else {
throw new Error(`不支持的网络: ${chainId}`); throw new Error(`不支持的网络: ${chainId}`);
} }

View File

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

View File

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

View File

@@ -1,8 +1,8 @@
import hre from 'hardhat'; import hre from 'hardhat';
import { Signer } from 'ethers'; import { Signer } from 'ethers';
const LOOKBACK_BLOCKS = 50000; // 查询最近 50000 个区块 const LOOKBACK_BLOCKS = 10000; // 查询最近 50000 个区块
const LIQUIDATION_THRESHOLD = 10; // $10 的最小清算阈值(美元单位) const LIQUIDATION_THRESHOLD = 1; // $10 的最小清算阈值(美元单位)
/** /**
* 获取最近活跃的地址(通过多个事件) * 获取最近活跃的地址(通过多个事件)

View File

@@ -98,12 +98,6 @@ async function main() {
console.log("=====================================\n"); console.log("=====================================\n");
console.log("💾 升级信息已保存到:", deploymentsPath); console.log("💾 升级信息已保存到:", deploymentsPath);
console.log("");
console.log("📌 重要提示:");
console.log(" 1. 代理地址保持不变,用户无需更改合约地址");
console.log(" 2. 所有状态数据已保留");
console.log(" 3. 建议运行验证脚本确认升级成功");
console.log(" 4. 建议在测试网充分测试后再升级主网\n");
} }
main() main()

View File

@@ -0,0 +1,212 @@
import { ethers } from "hardhat";
import { Lending } from "../../typechain-types";
/**
* 购买清算抵押品脚本
*
* 环境变量:
* - LENDING_ADDRESS: Lending 合约地址
* - ASSET_ADDRESS: 抵押品资产地址
* - BASE_AMOUNT (可选): 愿意支付的最大金额,默认 100
* - SLIPPAGE (可选): 滑点容忍度 (1-5),默认 2
*/
async function main() {
// ==================== 配置 ====================
const LENDING_ADDRESS = process.env.LENDING_ADDRESS;
const ASSET_ADDRESS = process.env.ASSET_ADDRESS;
const BASE_AMOUNT_INPUT = process.env.BASE_AMOUNT || "100";
const SLIPPAGE_PERCENT = parseInt(process.env.SLIPPAGE || "2");
// 参数验证
if (!LENDING_ADDRESS || LENDING_ADDRESS === "0x...") {
throw new Error("❌ 请设置 LENDING_ADDRESS 环境变量");
}
if (!ASSET_ADDRESS || ASSET_ADDRESS === "0x...") {
throw new Error("❌ 请设置 ASSET_ADDRESS 环境变量");
}
const SLIPPAGE = SLIPPAGE_PERCENT / 100; // 转换为小数
console.log("==================== 购买清算抵押品 ====================");
console.log(`Lending 合约: ${LENDING_ADDRESS}`);
console.log(`抵押品地址: ${ASSET_ADDRESS}`);
console.log(`最大支付金额: ${BASE_AMOUNT_INPUT} USDC`);
console.log(`滑点容忍度: ${SLIPPAGE_PERCENT}%\n`);
// ==================== 初始化 ====================
const lending = await ethers.getContractAt("Lending", LENDING_ADDRESS) as unknown as Lending;
const [buyer] = await ethers.getSigners();
const baseToken = await lending.baseToken();
const base = await ethers.getContractAt("IERC20Metadata", baseToken);
const baseDecimals = await base.decimals();
const BASE_AMOUNT = ethers.parseUnits(BASE_AMOUNT_INPUT, baseDecimals);
console.log(`买家地址: ${buyer.address}\n`);
// ==================== 前置检查 ====================
console.log("📊 检查系统状态...");
// 1. 检查储备金状态
const reserves = await lending.getReserves();
const targetReserves = await lending.targetReserves();
console.log(`✓ 当前储备金: ${ethers.formatUnits(reserves, baseDecimals)} USDC`);
console.log(`✓ 目标储备金: ${ethers.formatUnits(targetReserves, baseDecimals)} USDC`);
if (reserves >= targetReserves) {
throw new Error("❌ 储备金充足,当前无法购买抵押品");
}
// 2. 检查抵押品储备
const collateralReserve = await lending.getCollateralReserves(ASSET_ADDRESS);
if (collateralReserve === 0n) {
throw new Error("❌ 该抵押品储备为空,无法购买");
}
const asset = await ethers.getContractAt("IERC20Metadata", ASSET_ADDRESS);
const assetDecimals = await asset.decimals();
console.log(`✓ 抵押品储备: ${ethers.formatUnits(collateralReserve, assetDecimals)} 代币\n`);
// 3. 检查买家余额
const buyerBalance = await base.balanceOf(buyer.address);
console.log(`💰 买家余额: ${ethers.formatUnits(buyerBalance, baseDecimals)} USDC`);
if (buyerBalance < BASE_AMOUNT) {
throw new Error(`❌ 余额不足。需要: ${ethers.formatUnits(BASE_AMOUNT, baseDecimals)} USDC当前: ${ethers.formatUnits(buyerBalance, baseDecimals)} USDC`);
}
// ==================== 获取购买信息 ====================
console.log("\n📋 计算购买详情...");
const info = await getBuyCollateralInfo(lending, ASSET_ADDRESS, BASE_AMOUNT, SLIPPAGE);
console.log(`✓ 预期购买: ${ethers.formatUnits(info.expectedAmount, assetDecimals)} 代币`);
console.log(`✓ 实际购买: ${ethers.formatUnits(info.actualAmount, assetDecimals)} 代币`);
console.log(`✓ 最小接受: ${ethers.formatUnits(info.minAmount, assetDecimals)} 代币`);
if (info.isLimited) {
console.log(`\n⚠ 储备不足,购买量已调整:`);
console.log(` 原计划支付: ${ethers.formatUnits(info.baseAmount, baseDecimals)} USDC`);
console.log(` 实际约支付: ${ethers.formatUnits(info.actualBaseAmount, baseDecimals)} USDC`);
console.log(` 节省约: ${ethers.formatUnits(info.baseAmount - info.actualBaseAmount, baseDecimals)} USDC`);
}
// ==================== 授权检查 ====================
console.log("\n🔐 检查授权...");
const allowance = await base.allowance(buyer.address, LENDING_ADDRESS);
if (allowance < BASE_AMOUNT) {
console.log(`当前授权: ${ethers.formatUnits(allowance, baseDecimals)} USDC (不足)`);
console.log("正在授权...");
const approveTx = await base.approve(LENDING_ADDRESS, ethers.MaxUint256);
await approveTx.wait();
console.log("✅ 授权成功");
} else {
console.log("✓ 授权充足");
}
// ==================== 执行购买 ====================
console.log("\n💸 执行购买交易...");
const tx = await lending.buyCollateral(
ASSET_ADDRESS,
info.minAmount,
BASE_AMOUNT,
buyer.address
);
console.log(`交易已提交: ${tx.hash}`);
console.log("等待确认...");
const receipt = await tx.wait();
console.log(`✅ 交易确认! Gas 消耗: ${receipt?.gasUsed.toString()}\n`);
// ==================== 解析结果 ====================
const buyEvent = receipt?.logs.find((log: any) => {
try {
return lending.interface.parseLog(log)?.name === "BuyCollateral";
} catch {
return false;
}
});
if (buyEvent) {
const parsedEvent = lending.interface.parseLog(buyEvent);
const paidAmount = parsedEvent?.args.baseAmount;
const receivedAmount = parsedEvent?.args.collateralAmount;
console.log("==================== 交易结果 ====================");
console.log(`✓ 实际支付: ${ethers.formatUnits(paidAmount, baseDecimals)} USDC`);
console.log(`✓ 实际获得: ${ethers.formatUnits(receivedAmount, assetDecimals)} 代币`);
console.log(`✓ 购买者: ${parsedEvent?.args.buyer}`);
console.log(`✓ 接收地址: ${buyer.address}`);
// 计算实际单价和折扣信息
// 实际单价 = 支付金额 / 获得数量
const actualPricePerToken = (paidAmount * ethers.parseUnits("1", assetDecimals)) / receivedAmount;
// 计算正常市场价(使用 quoteCollateral 反推)
// 如果用相同的 baseAmount 在市场价购买,能买到多少代币
const marketAmount = await lending.quoteCollateral(ASSET_ADDRESS, paidAmount);
console.log(`\n💰 折扣购买说明:`);
console.log(`✓ 实际单价: ${ethers.formatUnits(actualPricePerToken, baseDecimals)} USDC/代币`);
// 只有当实际购买量大于市场价购买量时才显示折扣
if (receivedAmount > marketAmount) {
const discount = ((receivedAmount - marketAmount) * 10000n) / marketAmount;
const saved = actualPricePerToken * marketAmount / ethers.parseUnits("1", assetDecimals) - paidAmount;
console.log(`✓ 市场价可购买: ${ethers.formatUnits(marketAmount, assetDecimals)} 代币`);
console.log(`✓ 折扣多得: ${ethers.formatUnits(receivedAmount - marketAmount, assetDecimals)} 代币 (${Number(discount) / 100}%)`);
console.log(`✓ 相当于节省: ${ethers.formatUnits(saved, baseDecimals)} USDC`);
} else {
console.log(` 这是清算抵押品的折扣购买,价格低于市场价`);
}
console.log("===================================================");
}
// ==================== 更新后状态 ====================
console.log("\n📊 购买后状态:");
const newBalance = await base.balanceOf(buyer.address);
const newAssetBalance = await asset.balanceOf(buyer.address);
console.log(`买家 USDC 余额: ${ethers.formatUnits(newBalance, baseDecimals)} USDC`);
console.log(`买家抵押品余额: ${ethers.formatUnits(newAssetBalance, assetDecimals)} 代币`);
console.log("\n✅ 购买完成!");
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error("\n❌ 执行失败:", error.message || error);
process.exit(1);
});
/**
* 获取详细的购买信息
*/
export async function getBuyCollateralInfo(
lendingContract: Lending,
asset: string,
baseAmount: bigint,
slippageTolerance: number = 0.01
) {
const expectedAmount = await lendingContract.quoteCollateral(asset, baseAmount);
const availableReserve = await lendingContract.getCollateralReserves(asset);
const actualAmount = expectedAmount < availableReserve ? expectedAmount : availableReserve;
const slippageMultiplier = BigInt(Math.floor((1 - slippageTolerance) * 1e18));
const minAmount = (actualAmount * slippageMultiplier) / BigInt(1e18);
// 估算实际支付金额(基于实际购买量)
const actualBaseAmount = actualAmount < expectedAmount
? (baseAmount * actualAmount) / expectedAmount // 按比例计算
: baseAmount;
return {
expectedAmount, // 理想情况下可购买的数量
availableReserve, // 协议可用储备
actualAmount, // 实际可购买的数量(限制后)
minAmount, // 应用滑点保护后的最小值
baseAmount, // 用户愿意支付的最大金额
actualBaseAmount, // 实际需要支付的金额(可能更少)
isLimited: actualAmount < expectedAmount, // 是否受储备限制
};
}