# DeFi应用对接Curve协议教程

+关注继续查看

Curve.Fi是一组规模庞大的DEFi协议，这个教程的目的是帮助你关注智能合约开发的现代方法，以及如何在自己的Defi应用中集成Curve.Fi这种大型协议的关键点。

## 2、从Curve.Fi用户交互界面开始

contract ICurveFi_DepositY {
function add_liquidity(uint256[4] calldata uamounts, uint256 min_mint_amount) external;
function remove_liquidity(uint256 _amount, uint256[4] calldata min_uamounts) external;
function remove_liquidity_imbalance(uint256[4] calldata uamounts, uint256 max_burn_amount) external;

function coins(int128 i) external view returns (address);
function underlying_coins(int128 i) external view returns (address);
function underlying_coins() external view returns (address[4] memory);
function curve() external view returns (address);
function token() external view returns (address);
}

contract ICurveFi_Gauge {

function deposit(uint256 _value) external;
function withdraw(uint256 _value) external;

function integrate_fraction(address _for) external view returns(uint256);
}

contract ICurveFi_Gauge {

function deposit(uint256 _value) external;
function withdraw(uint256 _value) external;

function integrate_fraction(address _for) external view returns(uint256);
}

contract ICurveFi_Minter {

}

contract IYERC20 {
//ERC20 functions
//
//

//Y-token functions
function deposit(uint256 amount) external;
function withdraw(uint256 shares) external;
function getPricePerFullShare() external view returns (uint256);

}

## 3、基于Curve.Fi以正确的顺序转移资金

function multiStepDeposit(uint256[4] memory _amounts) public {

for (uint256 i = 0; i < stablecoins.length; i++) {
IERC20(stablecoins[i]).safeApprove(curveFi_Deposit, _amounts[i]);
}

//Step 1 - deposit stablecoins and get Curve.Fi LP tokens
ICurveFi_DepositY(curveFi_Deposit).add_liquidity(_amounts, 0); //0 to mint all Curve has to

//Step 2 - stake Curve LP tokens into Gauge and get CRV rewards

IERC20(curveFi_LPToken).safeApprove(curveFi_LPGauge, curveLPBalance);
ICurveFi_Gauge(curveFi_LPGauge).deposit(curveLPBalance);

//Step 3 - get all the rewards (and make whatever you need with them)
crvTokenClaim();
IERC20(curveFi_CRVToken).safeTransfer(_msgSender(), crvAmount);
}

function multiStepWithdraw(uint256[4] memory _amounts) public {

//Step 1 - Calculate amount of Curve LP-tokens to unstake
uint256 nWithdraw;
uint256 i;
for (i = 0; i < stablecoins.length; i++) {
}
uint256 withdrawShares = calculateShares(nWithdraw);

//Check if you can re-use unstaked LP tokens
uint256 notStaked = curveLPTokenUnstaked();
if (notStaked > 0) {
withdrawShares = withdrawShares.sub(notStaked);
}

//Step 2 - Unstake Curve LP tokens from Gauge
ICurveFi_Gauge(curveFi_LPGauge).withdraw(withdrawShares);

//Step 3 - Withdraw stablecoins from CurveDeposit
IERC20(curveFi_LPToken).safeApprove(curveFi_Deposit, withdrawShares);
ICurveFi_DepositY(curveFi_Deposit).remove_liquidity_imbalance(_amounts, withdrawShares);

//Step 4 - Send stablecoins to the requestor
//
}

## 4、测试Curve.Fi解决方案

573 0

8946 0

7月10日，继阿里巴巴集团与宁波市人民政府签署战略合作框架协议仅12天后，在阿里云宁波市工业互联网中心应用推广论坛上，阿里云与宁波市江北区人民政府联合宣布，阿里云宁波市工业互联网中心正式落户宁波文创港。
89 0

24728 0

19588 0
+关注
ezpod

213

0

OceanBase 入门到实战教程