Skip to content

Commit

Permalink
Merge pull request #36 from platypus-finance/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MrBeaverTail authored Nov 9, 2023
2 parents 2afc1cb + e75c92f commit 65e5d16
Show file tree
Hide file tree
Showing 20 changed files with 2,954 additions and 160 deletions.
6 changes: 6 additions & 0 deletions contracts/interfaces/IAnkrETHOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

interface IAnkrETHOracle {
function getRatioFor(address ankrETHToken) external view returns (uint256);
}
14 changes: 13 additions & 1 deletion contracts/pool/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,18 @@ contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable,
emit Withdraw(msg.sender, token, amount, liquidity, to);
}

/// @notice Converts amount of lp1 to lp2 taking into account the value of the lp.
/// Takes into accounts decimals and LP values
function convertLp(
Asset lp1,
Asset lp2,
uint256 amountLp1
) public view returns (uint256) {
return
(((amountLp1 * (10**lp2.decimals())) / (10**lp1.decimals())) * lp1.liability() * lp2.totalSupply()) /
(lp2.liability() * lp1.totalSupply());
}

/**
* @notice Enables withdrawing liquidity from an asset using LP from a different asset in the same aggregate
* @param initialToken The corresponding token user holds the LP (Asset) from
Expand Down Expand Up @@ -698,7 +710,7 @@ contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable,
_checkPriceDeviation(initialToken, wantedToken);

// Convert liquidity to d.p of initial asset
uint256 liquidityInInitialAssetDP = (liquidity * 10**initialAsset.decimals()) / (10**wantedAsset.decimals());
uint256 liquidityInInitialAssetDP = convertLp(wantedAsset, initialAsset, liquidity);

// require liquidity in initial asset dp to be > 0
require(liquidityInInitialAssetDP > 0, 'DUST?');
Expand Down
99 changes: 51 additions & 48 deletions contracts/pool/PoolAAvaxC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
* @notice Initializes pool. Dev is set to be the account calling this function.
*/
function initialize(address weth_) external initializer {
require(weth_ != address(0), 'PTL:WETH address cannot be zero');
require(weth_ != address(0), 'WETH address cannot be Z');
__Ownable_init();
__ReentrancyGuard_init_unchained();
__Pausable_init_unchained();
Expand Down Expand Up @@ -267,7 +267,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
* @param dev new contract dev address
*/
function setDev(address dev) external onlyOwner {
require(dev != address(0), 'ZERO');
require(dev != address(0), 'Z');
emit DevUpdated(_dev, dev);
_dev = dev;
}
Expand All @@ -277,7 +277,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
* @param aAvaxc_ the new aAvaxc address
*/
function setAAvaxc(address aAvaxc_) external onlyOwner {
require(aAvaxc_ != address(0), 'ZERO');
require(aAvaxc_ != address(0), 'Z');
emit aAvaxcUpdated(aAvaxc, aAvaxc_);
aAvaxc = aAvaxc_;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
* @param priceOracle new pool's priceOracle addres
*/
function setPriceOracle(address priceOracle) external onlyOwner {
require(priceOracle != address(0), 'ZERO');
require(priceOracle != address(0), 'Z');
emit OracleUpdated(address(_priceOracle), priceOracle);
_priceOracle = IaAvaxc(priceOracle);
}
Expand Down Expand Up @@ -438,8 +438,8 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
* @param asset The address of the platypus Asset contract
*/
function addAsset(address token, address asset) external onlyOwner {
require(token != address(0), 'ZERO');
require(asset != address(0), 'ZERO');
require(token != address(0), 'Z');
require(asset != address(0), 'Z');
require(!_containsAsset(token), 'ASSET_EXISTS');

_addAsset(token, Asset(asset));
Expand Down Expand Up @@ -511,9 +511,9 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address to,
uint256 deadline
) external override ensure(deadline) nonReentrant whenNotPaused returns (uint256 liquidity) {
require(amount > 0, 'ZERO_AMOUNT');
require(token != address(0), 'ZERO');
require(to != address(0), 'ZERO');
require(amount > 0, 'Z_AM');
require(token != address(0), 'Z');
require(to != address(0), 'Z');

IERC20 erc20 = IERC20(token);
Asset asset = _assetOf(token);
Expand All @@ -533,7 +533,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
returns (uint256 liquidity)
{
uint256 amount = msg.value;
require(amount > 0, 'PTP:ZERO_VALUE');
require(amount > 0, 'Z_VALUE');

IERC20 erc20 = IERC20(weth);
Asset asset = _assetOf(weth);
Expand Down Expand Up @@ -614,7 +614,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
uint256 liabilityToBurn;
(amount, liabilityToBurn, , ) = _withdrawFrom(asset, liquidity);

require(minimumAmount <= amount, 'AMOUNT_TOO_LOW');
require(minimumAmount <= amount, 'AM_TOO_LOW');

asset.burn(msg.sender, liquidity);
asset.removeCash(amount);
Expand All @@ -638,9 +638,9 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address to,
uint256 deadline
) external override ensure(deadline) nonReentrant whenNotPaused returns (uint256 amount) {
require(liquidity > 0, 'ZERO_ASSET_AMOUNT');
require(token != address(0), 'ZERO');
require(to != address(0), 'ZERO');
require(liquidity > 0, 'Z_ASSET_AM');
require(token != address(0), 'Z');
require(to != address(0), 'Z');

Asset asset = _assetOf(token);

Expand Down Expand Up @@ -692,10 +692,10 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address to,
uint256 deadline
) external override ensure(deadline) nonReentrant whenNotPaused returns (uint256 amount) {
require(liquidity > 0, 'ZERO_ASSET_AMOUNT');
require(wantedToken != address(0), 'ZERO');
require(initialToken != address(0), 'ZERO');
require(to != address(0), 'ZERO');
require(liquidity > 0, 'Z_ASSET_AM');
require(wantedToken != address(0), 'Z');
require(initialToken != address(0), 'Z');
require(to != address(0), 'Z');

// get corresponding assets
Asset initialAsset = _assetOf(initialToken);
Expand All @@ -705,9 +705,12 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
require(wantedAsset.aggregateAccount() == initialAsset.aggregateAccount(), 'DIFF_AGG_ACC');

// Convert liquidity to d.p of initial asset
uint256 liquidityInInitialAssetDP = (liquidity * 10**initialAsset.decimals()) / (10**wantedAsset.decimals());
uint256 liquidityInInitialAssetDP = (((liquidity * (10**initialAsset.decimals())) /
(10**wantedAsset.decimals())) *
wantedAsset.liability() *
initialAsset.totalSupply()) / (initialAsset.liability() * wantedAsset.totalSupply());

require(aAvaxc != address(0), 'aAvaxc_NOT_SET');
require(aAvaxc != address(0), 'aAvaxc_N_SET');
uint256 aAvaxcRate = ETH_UNIT.wdiv(_priceOracle.ratio());
require(aAvaxcRate > 0, 'INVALID_PRICE');

Expand All @@ -719,7 +722,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
liquidityInInitialAssetDP = liquidityInInitialAssetDP.wmul(aAvaxcRate);
} else {
// we assume only these two tokens can be added to the pool
revert('UNSUPPORTED_WITHDRAW_IN_OTHER_ASSET');
revert('UNSUPPOR');
}

// require liquidity in initial asset dp to be > 0
Expand All @@ -743,7 +746,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
require((wantedAsset.cash() - amount).wdiv(wantedAsset.liability()) >= ETH_UNIT, 'COV_RATIO_LOW');

// require amount to be higher than the amount specified
require(minimumAmount <= amount, 'AMOUNT_TOO_LOW');
require(minimumAmount <= amount, 'AM_TOO_LOW');

// calculate liability to burn in initialAsset
uint256 liabilityToBurn = (initialAsset.liability() * liquidityInInitialAssetDP) / initialAsset.totalSupply();
Expand Down Expand Up @@ -782,11 +785,11 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address to,
uint256 deadline
) external override ensure(deadline) nonReentrant whenNotPaused returns (uint256 actualToAmount, uint256 haircut) {
require(fromToken != address(0), 'ZERO');
require(toToken != address(0), 'ZERO');
require(fromToken != toToken, 'SAME_ADDRESS');
require(fromAmount > 0, 'ZERO_FROM_AMOUNT');
require(to != address(0), 'ZERO');
require(fromToken != address(0), 'Z');
require(toToken != address(0), 'Z');
require(fromToken != toToken, 'SAME_ADDR');
require(fromAmount > 0, 'Z_FROM_AM');
require(to != address(0), 'Z');

IERC20 fromERC20 = IERC20(fromToken);
Asset fromAsset = _assetOf(fromToken);
Expand All @@ -796,7 +799,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
require(toAsset.aggregateAccount() == fromAsset.aggregateAccount(), 'DIFF_AGG_ACC');

(actualToAmount, haircut) = _quoteFrom(fromAsset, toAsset, fromAmount);
require(minimumToAmount <= actualToAmount, 'AMOUNT_TOO_LOW');
require(minimumToAmount <= actualToAmount, 'AM_TOO_LOW');

fromERC20.safeTransferFrom(address(msg.sender), address(fromAsset), fromAmount);
fromAsset.addCash(fromAmount);
Expand Down Expand Up @@ -824,18 +827,18 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address payable to,
uint256 deadline
) external ensure(deadline) nonReentrant whenNotPaused returns (uint256 actualToAmount, uint256 haircut) {
require(fromToken != address(0), 'PTP:ZERO_ADDRESS');
require(fromToken != weth, 'PTP:SAME_ADDRESS');
require(fromToken != address(0), 'Z_ADDR');
require(fromToken != weth, 'SAME_ADDR');

IERC20 fromERC20 = IERC20(fromToken);
Asset fromAsset = _assetOf(fromToken);
require(address(fromAsset) != address(0), 'PTP:ASSET_NOT_EXIST');
require(address(fromAsset) != address(0), 'ASSET_NOT_EXIST');
Asset toAsset = _assetOf(weth);

require(toAsset.aggregateAccount() == fromAsset.aggregateAccount(), 'DIFF_AGG_ACC');

(actualToAmount, haircut) = _quoteFrom(fromAsset, toAsset, fromAmount);
require(minimumToAmount <= actualToAmount, 'PTP:AMOUNT_TOO_LOW');
require(minimumToAmount <= actualToAmount, 'AM_TOO_LOW');

fromERC20.safeTransferFrom(address(msg.sender), address(fromAsset), fromAmount);
fromAsset.addCash(fromAmount);
Expand All @@ -862,18 +865,18 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address to,
uint256 deadline
) external payable ensure(deadline) nonReentrant whenNotPaused returns (uint256 actualToAmount, uint256 haircut) {
require(toToken != address(0), 'PTP:ZERO_ADDRESS');
require(toToken != weth, 'PTP:SAME_ADDRESS');
require(toToken != address(0), 'Z_ADDR');
require(toToken != weth, 'SAME_ADDR');

uint256 fromAmount = msg.value;

Asset fromAsset = _assetOf(weth);
Asset toAsset = _assetOf(toToken);
require(address(toAsset) != address(0), 'PTP:ASSET_NOT_EXIST');
require(address(toAsset) != address(0), 'ASSET_NOT_EXIST');
require(toAsset.aggregateAccount() == fromAsset.aggregateAccount(), 'DIFF_AGG_ACC');

(actualToAmount, haircut) = _quoteFrom(fromAsset, toAsset, fromAmount);
require(minimumToAmount <= actualToAmount, 'PTP:AMOUNT_TOO_LOW');
require(minimumToAmount <= actualToAmount, 'AM_TOO_LOW');

IWETH(weth).deposit{value: fromAmount}();
IERC20(weth).safeTransfer(address(fromAsset), fromAmount);
Expand Down Expand Up @@ -941,7 +944,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
Asset toAsset,
uint256 fromAmount
) private view returns (uint256 idealToAmount) {
require(aAvaxc != address(0), 'aAvaxc_NOT_SET');
require(aAvaxc != address(0), 'aAvaxc_N_SET');
// get aAvaxc rate in wad
uint256 aAvaxcRate = ETH_UNIT.wdiv(_priceOracle.ratio());
require(aAvaxcRate > 0, 'INVALID_PRICE');
Expand All @@ -959,7 +962,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
idealToAmount = fromAmount.wdiv(aAvaxcRate);
} else {
// we assume only these two tokens can be added to the pool
revert('UNSUPPORTED_SWAP');
revert('UNSUPPOR');
}
}

Expand All @@ -977,10 +980,10 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address toToken,
uint256 fromAmount
) external view override whenNotPaused returns (uint256 potentialOutcome, uint256 haircut) {
require(fromToken != address(0), 'ZERO');
require(toToken != address(0), 'ZERO');
require(fromToken != toToken, 'SAME_ADDRESS');
require(fromAmount > 0, 'ZERO_FROM_AMOUNT');
require(fromToken != address(0), 'Z');
require(toToken != address(0), 'Z');
require(fromToken != toToken, 'SAME_ADDR');
require(fromAmount > 0, 'Z_FR_AM');

Asset fromAsset = _assetOf(fromToken);
Asset toAsset = _assetOf(toToken);
Expand Down Expand Up @@ -1011,7 +1014,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
bool enoughCash
)
{
require(token != address(0), 'ZERO');
require(token != address(0), 'Z');
require(liquidity > 0, 'LIQ=0');

Asset asset = _assetOf(token);
Expand All @@ -1032,8 +1035,8 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
address wantedToken,
uint256 liquidity
) external view whenNotPaused returns (uint256 amount, uint256 fee) {
require(initialToken != address(0), 'ZERO');
require(wantedToken != address(0), 'ZERO');
require(initialToken != address(0), 'Z');
require(wantedToken != address(0), 'Z');
require(liquidity > 0, 'LIQ=0');

Asset initialAsset = _assetOf(initialToken);
Expand All @@ -1044,7 +1047,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
bool enoughCash;
(amount, , fee, enoughCash) = _withdrawFrom(wantedAsset, liquidity);

require(enoughCash, 'NOT_ENOUGH_CASH');
require(enoughCash, 'N_ENOUGH_CASH');

// require after withdrawal coverage to >= 1
require((wantedAsset.cash() - amount).wdiv(wantedAsset.liability()) >= ETH_UNIT, 'COV_RATIO_LOW');
Expand All @@ -1066,7 +1069,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade

uint256 wantedAssetCov = (wantedAsset.cash()).wdiv(wantedAsset.liability());

require(aAvaxc != address(0), 'aAvaxc_NOT_SET');
require(aAvaxc != address(0), 'aAvaxc_N_SET');
uint256 aAvaxcRate = ETH_UNIT.wdiv(_priceOracle.ratio());
require(aAvaxcRate > 0, 'INVALID_PRICE');

Expand All @@ -1084,7 +1087,7 @@ contract PoolAAvaxC is Initializable, OwnableUpgradeable, ReentrancyGuardUpgrade
maxInitialAssetAmount = maxInitialAssetAmount.wmul(aAvaxcRate);
} else {
// we assume only these two tokens can be added to the pool
revert('UNSUPPORTED_WITHDRAW_IN_OTHER_ASSET');
revert('UNSUP');
}
} else {
maxInitialAssetAmount = 0;
Expand Down
Loading

0 comments on commit 65e5d16

Please sign in to comment.