From 02e0ee28c4582068fc128d647daccd23d33e21e4 Mon Sep 17 00:00:00 2001 From: zhoujia6139 Date: Wed, 13 Sep 2023 18:28:45 +0800 Subject: [PATCH] chore: small optimization --- contracts/interfaces/IPoolApeStaking.sol | 2 +- contracts/protocol/pool/PoolApeStaking.sol | 131 ++++++++------------- test/_ape_staking_migration.spec.ts | 40 +++---- 3 files changed, 69 insertions(+), 104 deletions(-) diff --git a/contracts/interfaces/IPoolApeStaking.sol b/contracts/interfaces/IPoolApeStaking.sol index 7fc78b07..3c26ec3b 100644 --- a/contracts/interfaces/IPoolApeStaking.sol +++ b/contracts/interfaces/IPoolApeStaking.sol @@ -65,7 +65,7 @@ interface IPoolApeStaking { struct ApeCoinInfo { address asset; - uint256 cashAmount; + uint256 totalAmount; uint256 borrowAmount; bool openSApeCollateralFlag; } diff --git a/contracts/protocol/pool/PoolApeStaking.sol b/contracts/protocol/pool/PoolApeStaking.sol index a4466dda..8fe1d8d1 100644 --- a/contracts/protocol/pool/PoolApeStaking.sol +++ b/contracts/protocol/pool/PoolApeStaking.sol @@ -272,7 +272,7 @@ contract PoolApeStaking is if (nBakc == address(0)) { nBakc = ps._reserves[BAKC].xTokenAddress; } - //transfer bakc from nBakc to ape + //transfer bakc from nBakc to nApe for (uint256 j = 0; j < pairLength; j++) { IERC721(BAKC).safeTransferFrom( nBakc, @@ -304,11 +304,13 @@ contract PoolApeStaking is Errors.INVALID_ASSET_TYPE ); // 1, prepare cash part. - if (apeCoinInfo.cashAmount > 0) { + uint256 cashAmount = apeCoinInfo.totalAmount - + apeCoinInfo.borrowAmount; + if (cashAmount > 0) { IERC20(apeCoinInfo.asset).transferFrom( onBehalf, address(this), - apeCoinInfo.cashAmount + cashAmount ); } @@ -326,132 +328,99 @@ contract PoolApeStaking is ); } - uint256 totalAmount = apeCoinInfo.cashAmount + - apeCoinInfo.borrowAmount; - if (apeCoinInfo.asset == address(APE_COMPOUND) && totalAmount > 0) { - APE_COMPOUND.withdraw(totalAmount); + if ( + apeCoinInfo.asset == address(APE_COMPOUND) && + apeCoinInfo.totalAmount > 0 + ) { + APE_COMPOUND.withdraw(apeCoinInfo.totalAmount); } } //staking in paraApeStaking { uint256 stakingLength = stakingInfos.length; + uint256 bakcPairCap; for (uint256 index = 0; index < stakingLength; index++) { ParaStakingInfo calldata stakingInfo = stakingInfos[index]; if ( stakingInfo.PoolId == - ApeStakingCommonLogic.BAYC_BAKC_PAIR_POOL_ID - ) { - IParaApeStaking(PARA_APE_STAKING).depositPairNFT( - onBehalf, - true, - stakingInfo.apeTokenIds, - stakingInfo.bakcTokenIds - ); - } else if ( + ApeStakingCommonLogic.BAYC_BAKC_PAIR_POOL_ID || stakingInfo.PoolId == ApeStakingCommonLogic.MAYC_BAKC_PAIR_POOL_ID ) { + bool isBayc = (stakingInfo.PoolId == + ApeStakingCommonLogic.BAYC_BAKC_PAIR_POOL_ID); IParaApeStaking(PARA_APE_STAKING).depositPairNFT( onBehalf, - false, + isBayc, stakingInfo.apeTokenIds, stakingInfo.bakcTokenIds ); } else if ( stakingInfo.PoolId == - ApeStakingCommonLogic.BAYC_SINGLE_POOL_ID - ) { - IParaApeStaking(PARA_APE_STAKING).depositNFT( - onBehalf, - BAYC, - stakingInfo.apeTokenIds - ); - } else if ( + ApeStakingCommonLogic.BAYC_SINGLE_POOL_ID || stakingInfo.PoolId == - ApeStakingCommonLogic.MAYC_SINGLE_POOL_ID - ) { - IParaApeStaking(PARA_APE_STAKING).depositNFT( - onBehalf, - MAYC, - stakingInfo.apeTokenIds - ); - } else if ( + ApeStakingCommonLogic.MAYC_SINGLE_POOL_ID || stakingInfo.PoolId == ApeStakingCommonLogic.BAKC_SINGLE_POOL_ID ) { + address nft = (stakingInfo.PoolId == + ApeStakingCommonLogic.BAYC_SINGLE_POOL_ID) + ? BAYC + : (stakingInfo.PoolId == + ApeStakingCommonLogic.MAYC_SINGLE_POOL_ID) + ? MAYC + : BAKC; IParaApeStaking(PARA_APE_STAKING).depositNFT( onBehalf, - BAKC, - stakingInfo.bakcTokenIds + nft, + stakingInfo.apeTokenIds ); } else if ( stakingInfo.PoolId == - ApeStakingCommonLogic.BAYC_APECOIN_POOL_ID - ) { - uint256 cap = IParaApeStaking(PARA_APE_STAKING) - .getApeCoinStakingCap( - IApeStakingP2P.StakingType.BAYCStaking - ); - IParaApeStaking(PARA_APE_STAKING).depositApeCoinPool( - IApeCoinPool.ApeCoinDepositInfo({ - onBehalf: onBehalf, - cashToken: address(APE_COIN), - cashAmount: cap * stakingInfo.apeTokenIds.length, - isBAYC: true, - tokenIds: stakingInfo.apeTokenIds - }) - ); - } else if ( + ApeStakingCommonLogic.BAYC_APECOIN_POOL_ID || stakingInfo.PoolId == ApeStakingCommonLogic.MAYC_APECOIN_POOL_ID ) { + bool isBayc = (stakingInfo.PoolId == + ApeStakingCommonLogic.BAYC_APECOIN_POOL_ID); + IApeStakingP2P.StakingType stakingType = isBayc + ? IApeStakingP2P.StakingType.BAYCStaking + : IApeStakingP2P.StakingType.MAYCStaking; uint256 cap = IParaApeStaking(PARA_APE_STAKING) - .getApeCoinStakingCap( - IApeStakingP2P.StakingType.MAYCStaking - ); + .getApeCoinStakingCap(stakingType); IParaApeStaking(PARA_APE_STAKING).depositApeCoinPool( IApeCoinPool.ApeCoinDepositInfo({ onBehalf: onBehalf, cashToken: address(APE_COIN), cashAmount: cap * stakingInfo.apeTokenIds.length, - isBAYC: false, + isBAYC: isBayc, tokenIds: stakingInfo.apeTokenIds }) ); } else if ( stakingInfo.PoolId == - ApeStakingCommonLogic.BAYC_BAKC_APECOIN_POOL_ID - ) { - uint256 cap = IParaApeStaking(PARA_APE_STAKING) - .getApeCoinStakingCap( - IApeStakingP2P.StakingType.BAKCPairStaking - ); - IParaApeStaking(PARA_APE_STAKING).depositApeCoinPairPool( - IApeCoinPool.ApeCoinPairDepositInfo({ - onBehalf: onBehalf, - cashToken: address(APE_COIN), - cashAmount: cap * stakingInfo.apeTokenIds.length, - isBAYC: true, - apeTokenIds: stakingInfo.apeTokenIds, - bakcTokenIds: stakingInfo.bakcTokenIds - }) - ); - } else if ( + ApeStakingCommonLogic.BAYC_BAKC_APECOIN_POOL_ID || stakingInfo.PoolId == ApeStakingCommonLogic.MAYC_BAKC_APECOIN_POOL_ID ) { - uint256 cap = IParaApeStaking(PARA_APE_STAKING) - .getApeCoinStakingCap( - IApeStakingP2P.StakingType.BAKCPairStaking - ); + if (bakcPairCap == 0) { + bakcPairCap = IParaApeStaking(PARA_APE_STAKING) + .getApeCoinStakingCap( + IApeStakingP2P.StakingType.BAKCPairStaking + ); + } + + bool isBayc = (stakingInfo.PoolId == + ApeStakingCommonLogic.BAYC_BAKC_APECOIN_POOL_ID); IParaApeStaking(PARA_APE_STAKING).depositApeCoinPairPool( IApeCoinPool.ApeCoinPairDepositInfo({ onBehalf: onBehalf, cashToken: address(APE_COIN), - cashAmount: cap * stakingInfo.apeTokenIds.length, - isBAYC: false, + cashAmount: bakcPairCap * + stakingInfo.apeTokenIds.length, + isBAYC: isBayc, apeTokenIds: stakingInfo.apeTokenIds, bakcTokenIds: stakingInfo.bakcTokenIds }) @@ -463,11 +432,7 @@ contract PoolApeStaking is // repay and supply remaining apecoin uint256 diffBalance = APE_COIN.balanceOf(address(this)) - beforeBalance; if (diffBalance > 0) { - //wrong cashAmount or borrowAmount - require( - apeCoinInfo.cashAmount + apeCoinInfo.borrowAmount == 0, - Errors.INVALID_PARAMETER - ); + require(apeCoinInfo.totalAmount == 0, Errors.INVALID_PARAMETER); APE_COMPOUND.deposit(address(this), diffBalance); _repayAndSupplyForUser( ps, diff --git a/test/_ape_staking_migration.spec.ts b/test/_ape_staking_migration.spec.ts index 5b217e6f..7d8c50ed 100644 --- a/test/_ape_staking_migration.spec.ts +++ b/test/_ape_staking_migration.spec.ts @@ -202,7 +202,7 @@ describe("Para Ape Staking Test", () => { ], { asset: ape.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -284,7 +284,7 @@ describe("Para Ape Staking Test", () => { ], { asset: ape.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -365,7 +365,7 @@ describe("Para Ape Staking Test", () => { ], { asset: cApe.address, - cashAmount: 0, + totalAmount: parseEther("85600"), borrowAmount: parseEther("85600"), openSApeCollateralFlag: true, } @@ -448,7 +448,7 @@ describe("Para Ape Staking Test", () => { ], { asset: cApe.address, - cashAmount: 0, + totalAmount: parseEther("85600"), borrowAmount: parseEther("85600"), openSApeCollateralFlag: true, } @@ -523,13 +523,13 @@ describe("Para Ape Staking Test", () => { }, { PoolId: 5, - apeTokenIds: [], - bakcTokenIds: [0], + apeTokenIds: [0], + bakcTokenIds: [], }, ], { asset: ape.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -603,13 +603,13 @@ describe("Para Ape Staking Test", () => { }, { PoolId: 5, - apeTokenIds: [], - bakcTokenIds: [0], + apeTokenIds: [0], + bakcTokenIds: [], }, ], { asset: ape.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -682,13 +682,13 @@ describe("Para Ape Staking Test", () => { }, { PoolId: 5, - apeTokenIds: [], - bakcTokenIds: [0], + apeTokenIds: [0], + bakcTokenIds: [], }, ], { asset: cApe.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -763,13 +763,13 @@ describe("Para Ape Staking Test", () => { }, { PoolId: 5, - apeTokenIds: [], - bakcTokenIds: [0], + apeTokenIds: [0], + bakcTokenIds: [], }, ], { asset: cApe.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -843,7 +843,7 @@ describe("Para Ape Staking Test", () => { ], { asset: ape.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -918,7 +918,7 @@ describe("Para Ape Staking Test", () => { ], { asset: ape.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -992,7 +992,7 @@ describe("Para Ape Staking Test", () => { ], { asset: cApe.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, } @@ -1068,7 +1068,7 @@ describe("Para Ape Staking Test", () => { ], { asset: cApe.address, - cashAmount: 0, + totalAmount: 0, borrowAmount: 0, openSApeCollateralFlag: true, }