简化此代码// SPDX-License-Identifier: MIT pragma solidity 0.8.16; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; contract CSAMM { IERC20 immutable token0; IERC20 immutable token1; uint public reserve0; uint public reserve1; uint public totalSupply; mapping(address => uint) public balanceOf; constructor(address _token0, address _token1) { token0 = IERC20(_token0); token1 = IERC20(_token1); } function _mint(address _to, uint _amount) private { // 此处补全 balanceOf[_to]=_amount; totalSupply+=_amount; } function _burn(address _from, uint _amount) private { // 此处补全 require(balanceOf[_from]>=_amount, '_amount>balance'); balanceOf[_from]-=_amount; totalSupply-=_amount; } function swap( address _tokenIn, uint _amountIn ) external returns (uint amountOut) { // 此处补全 amountOut=_amountIn; if(IERC20(_tokenIn)==token0){ token0.transferFrom(msg.sender, address(this), _amountIn); token1.transfer(msg.sender, _amountIn); _update(_amountIn+reserve0, reserve1-_amountIn); }else{ token1.transferFrom(msg.sender, address(this), _amountIn); token0.transfer(msg.sender, _amountIn); _update(reserve0-_amountIn, reserve1+_amountIn); } return amountOut; } function addLiquidity( uint _amount0, uint _amount1 ) external returns (uint shares) { if(totalSupply==0){ shares=_amount0+_amount1; token0.transferFrom(msg.sender, address(this), _amount0); token1.transferFrom(msg.sender, address(this), _amount1); _mint(msg.sender,shares); }else{ token0.transferFrom(msg.sender, address(this), _amount0); token1.transferFrom(msg.sender, address(this), _amount1); shares=(_amount0+_amount1)*totalSupply/(reserve0+reserve1); _mint(msg.sender,shares); } _update(_amount0+reserve0, _amount1+reserve1); } function removeLiquidity(uint _shares) external returns (uint d0, uint d1) { // 此处补全 d0=reserve0*_shares/totalSupply; d1=reserve1*_shares/totalSupply; token0.transfer(msg.sender, d0); token1.transfer(msg.sender, d1); _burn(msg.sender, _shares); _update(reserve0-d0,reserve1-d1); } function _update(uint _res0, uint _res1) private { reserve0 = _res0; reserve1 = _res1; } }
时间: 2023-12-06 20:44:01 浏览: 75
_SPDX-License-Identifier:MIT_pragma_Solidity_^0._0x8a47f3
// SPDX-License-Identifier: MIT
pragma solidity 0.8.16;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract CSAMM {
IERC20 immutable token0;
IERC20 immutable token1;
uint public reserve0;
uint public reserve1;
uint public totalSupply;
mapping(address => uint) public balanceOf;
constructor(address _token0, address _token1) {
token0 = IERC20(_token0);
token1 = IERC20(_token1);
}
function _mint(address _to, uint _amount) private {
balanceOf[_to] += _amount;
totalSupply += _amount;
}
function _burn(address _from, uint _amount) private {
require(balanceOf[_from] >= _amount, '_amount > balance');
balanceOf[_from] -= _amount;
totalSupply -= _amount;
}
function swap(address _tokenIn, uint _amountIn) external returns (uint amountOut) {
amountOut = _amountIn;
if (IERC20(_tokenIn) == token0) {
token0.transferFrom(msg.sender, address(this), _amountIn);
token1.transfer(msg.sender, _amountIn);
_update(_amountIn + reserve0, reserve1 - _amountIn);
} else {
token1.transferFrom(msg.sender, address(this), _amountIn);
token0.transfer(msg.sender, _amountIn);
_update(reserve0 - _amountIn, reserve1 + _amountIn);
}
return amountOut;
}
function addLiquidity(uint _amount0, uint _amount1) external returns (uint shares) {
if (totalSupply == 0) {
shares = _amount0 + _amount1;
token0.transferFrom(msg.sender, address(this), _amount0);
token1.transferFrom(msg.sender, address(this), _amount1);
_mint(msg.sender, shares);
} else {
token0.transferFrom(msg.sender, address(this), _amount0);
token1.transferFrom(msg.sender, address(this), _amount1);
shares = (_amount0 + _amount1) * totalSupply / (reserve0 + reserve1);
_mint(msg.sender, shares);
}
_update(_amount0 + reserve0, _amount1 + reserve1);
}
function removeLiquidity(uint _shares) external returns (uint d0, uint d1) {
d0 = reserve0 * _shares / totalSupply;
d1 = reserve1 * _shares / totalSupply;
token0.transfer(msg.sender, d0);
token1.transfer(msg.sender, d1);
_burn(msg.sender, _shares);
_update(reserve0 - d0, reserve1 - d1);
}
function _update(uint _res0, uint _res1) private {
reserve0 = _res0;
reserve1 = _res1;
}
}
阅读全文