Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize #5

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion script/ArbitrumDeployment.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// import "../src/Zap.sol";
// import "../src/Router.sol";
// import "./Addresses.sol";
// import "../src/interfaces/IERC20Detailed.sol";

// // Arbitrum DEPLOYMENT
// contract ContractScript is Script {
Expand Down
1 change: 0 additions & 1 deletion script/PolygonDeployment.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import "../src/CurveFactoryV3.sol";
import "../src/Zap.sol";
import "../src/Router.sol";
import "./Addresses.sol";
import "../src/interfaces/IERC20Detailed.sol";

// POLYGON DEPLOYMENT
contract ContractScript is Script {
Expand Down
6 changes: 3 additions & 3 deletions src/CurveFactoryV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import "./Curve.sol";
import "./AssimilatorFactory.sol";
import "./assimilators/AssimilatorV3.sol";
import "./interfaces/ICurveFactory.sol";
import "./interfaces/IAssimilatorFactory.sol";
import "./interfaces/IERC20Detailed.sol";
import "./interfaces/IConfig.sol";

contract CurveFactoryV3 is ICurveFactory, Ownable {
Expand Down Expand Up @@ -88,8 +88,8 @@ contract CurveFactoryV3 is ICurveFactory, Ownable {
require(_info._baseCurrency != _info._quoteCurrency, "quote-base-currencies-same");
require((_info._baseWeight + _info._quoteWeight) == 1e18, "invalid-weights");

uint256 quoteDec = IERC20Detailed(_info._quoteCurrency).decimals();
uint256 baseDec = IERC20Detailed(_info._baseCurrency).decimals();
uint256 quoteDec = IERC20Metadata(_info._quoteCurrency).decimals();
uint256 baseDec = IERC20Metadata(_info._baseCurrency).decimals();

CurveIDPair memory idPair = generateCurveID(_info._baseCurrency, _info._quoteCurrency);
if (curves[idPair.curveId] != address(0) || curves[idPair.curveIdReversed] != address(0)) revert("pair-exists");
Expand Down
53 changes: 27 additions & 26 deletions src/Zap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";

import "./Curve.sol";
import "./interfaces/IERC20Detailed.sol";
import "./interfaces/IWeth.sol";
import "./interfaces/ICurve.sol";
import "./interfaces/IOracle.sol";
Expand All @@ -29,7 +30,7 @@ import "./assimilators/AssimilatorV3.sol";

contract Zap {
using SafeMath for uint256;
using SafeERC20 for IERC20Detailed;
using SafeERC20 for IERC20Metadata;

struct ZapData {
address curve;
Expand Down Expand Up @@ -68,10 +69,10 @@ contract Zap {
bool _toETH
) public isDFXCurve(_curve) returns (uint256) {
address wETH = ICurve(_curve).getWeth();
IERC20Detailed base = IERC20Detailed(Curve(payable(_curve)).numeraires(0));
IERC20Detailed quote = IERC20Detailed(Curve(payable(_curve)).numeraires(1));
IERC20Metadata base = IERC20Metadata(Curve(payable(_curve)).numeraires(0));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_token == address(base) || _token == address(quote), "zap/token-not-supported");
IERC20Detailed(_curve).safeTransferFrom(msg.sender, address(this), _lpAmount);
IERC20Metadata(_curve).safeTransferFrom(msg.sender, address(this), _lpAmount);
Curve(payable(_curve)).withdraw(_lpAmount, _deadline);
// from base
if (_token == address(base)) {
Expand Down Expand Up @@ -118,8 +119,8 @@ contract Zap {
isDFXCurve(_curve)
returns (uint256)
{
IERC20Detailed base = IERC20Detailed(Curve(payable(_curve)).numeraires(0));
IERC20Detailed quote = IERC20Detailed(Curve(payable(_curve)).numeraires(1));
IERC20Metadata base = IERC20Metadata(Curve(payable(_curve)).numeraires(0));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_token == address(base) || _token == address(quote), "zap/token-not-supported");
bool isFromBase = _token == address(base) ? true : false;
(, uint256 swapAmount) = calcSwapAmountForZap(_curve, _zapAmount, isFromBase);
Expand All @@ -143,10 +144,10 @@ contract Zap {
address _token = ICurve(_curve).getWeth();
// first convert coming ETH to WETH & send wrapped amount to user back
IWETH(_token).deposit{value: msg.value}();
IERC20Detailed(_token).safeTransferFrom(address(this), msg.sender, msg.value);
IERC20Metadata(_token).safeTransferFrom(address(this), msg.sender, msg.value);

IERC20Detailed base = IERC20Detailed(Curve(payable(_curve)).numeraires(0));
IERC20Detailed quote = IERC20Detailed(Curve(payable(_curve)).numeraires(1));
IERC20Metadata base = IERC20Metadata(Curve(payable(_curve)).numeraires(0));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));
require(_token == address(base) || _token == address(quote), "zap/token-not-supported");
bool isFromBase = _token == address(base) ? true : false;
(, uint256 swapAmount) = calcSwapAmountForZap(_curve, msg.value, isFromBase);
Expand All @@ -163,7 +164,7 @@ contract Zap {
// helpers for zap
function _zapFromBase(
address _curve,
IERC20Detailed _base,
IERC20Metadata _base,
address _quote,
uint256 _swapAmount,
uint256 _deadline,
Expand All @@ -179,7 +180,7 @@ contract Zap {
function _zapFromQuote(
address _curve,
address _base,
IERC20Detailed _quote,
IERC20Metadata _quote,
uint256 _swapAmount,
uint256 _deadline,
uint256 _zapAmount
Expand All @@ -191,7 +192,7 @@ contract Zap {
Curve(payable(_curve)).originSwap(address(_quote), _base, _swapAmount, 0, _deadline);
}

function zap_(address _curve, IERC20Detailed _base, IERC20Detailed _quote, uint256 _deadline, uint256 _minLPAmount)
function zap_(address _curve, IERC20Metadata _base, IERC20Metadata _quote, uint256 _deadline, uint256 _minLPAmount)
private
returns (uint256)
{
Expand All @@ -218,7 +219,7 @@ contract Zap {
Curve(payable(_curve)).deposit(depositAmount, 0, 0, type(uint256).max, type(uint256).max, _deadline);
require(lpAmount >= _minLPAmount, "!Zap/not-enough-lp-amount");
// send lp to user
IERC20Detailed(_curve).safeTransfer(msg.sender, IERC20Detailed(_curve).balanceOf(address(this)));
IERC20Metadata(_curve).safeTransfer(msg.sender, IERC20Metadata(_curve).balanceOf(address(this)));
// Transfer all remaining balances back to user
_base.safeTransfer(msg.sender, _base.balanceOf(address(this)));
_quote.safeTransfer(msg.sender, _quote.balanceOf(address(this)));
Expand Down Expand Up @@ -259,11 +260,11 @@ contract Zap {
{
// Base will always be index 0
address base = Curve(payable(_curve)).reserves(0);
IERC20Detailed quote = IERC20Detailed(Curve(payable(_curve)).reserves(1));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).reserves(1));

// Ratio of base quote in 18 decimals
uint256 curveBaseBal = IERC20Detailed(base).balanceOf(_curve);
uint8 curveBaseDecimals = IERC20Detailed(base).decimals();
uint256 curveBaseBal = IERC20Metadata(base).balanceOf(_curve);
uint8 curveBaseDecimals = IERC20Metadata(base).decimals();
uint256 curveQuoteBal = quote.balanceOf(_curve);

// How much user wants to swap
Expand Down Expand Up @@ -326,7 +327,7 @@ contract Zap {

return _calcDepositAmount(
_curve,
IERC20Detailed(base),
IERC20Metadata(base),
ZapDepositData({
curBaseAmount: maxBaseAmount,
curQuoteAmount: _quoteAmount,
Expand Down Expand Up @@ -354,7 +355,7 @@ contract Zap {

return _calcDepositAmount(
_curve,
IERC20Detailed(base),
IERC20Metadata(base),
ZapDepositData({
curBaseAmount: _baseAmount,
curQuoteAmount: maxQuoteAmount,
Expand All @@ -373,7 +374,7 @@ contract Zap {
view
returns (uint256 baseAmount, uint256 usdAmount, uint256 lptAmount)
{
uint8 curveQuoteDecimals = IERC20Detailed(Curve(payable(_curve)).reserves(1)).decimals();
uint8 curveQuoteDecimals = IERC20Metadata(Curve(payable(_curve)).reserves(1)).decimals();

require(curveQuoteDecimals <= 18, "zap/big-decimals");

Expand All @@ -390,7 +391,7 @@ contract Zap {
view
returns (uint256 quoteAmount, uint256 usdAmount, uint256 lptAmount)
{
uint8 curveBaseDecimals = IERC20Detailed(Curve(payable(_curve)).reserves(0)).decimals();
uint8 curveBaseDecimals = IERC20Metadata(Curve(payable(_curve)).reserves(0)).decimals();

require(curveBaseDecimals <= 18, "zap/big-decimals");

Expand Down Expand Up @@ -522,13 +523,13 @@ contract Zap {
/// @return uint256 - The deposit amount
/// @return uint256 - The LPTs received
/// @return uint256[] memory - The baseAmount and quoteAmount
function _calcDepositAmount(address _curve, IERC20Detailed _base, ZapDepositData memory dd)
function _calcDepositAmount(address _curve, IERC20Metadata _base, ZapDepositData memory dd)
internal
view
returns (uint256, uint256, uint256[] memory)
{
// Calculate _depositAmount
IERC20Detailed quote = IERC20Detailed(Curve(payable(_curve)).numeraires(1));
IERC20Metadata quote = IERC20Metadata(Curve(payable(_curve)).numeraires(1));

require(_base.decimals() <= 18, "zap/big-decimals");

Expand Down Expand Up @@ -570,8 +571,8 @@ contract Zap {
returns (uint256 usdAmt, uint256 lptAmt)
{
Curve curve = Curve(payable(_curve));
IERC20Detailed base = IERC20Detailed(curve.reserves(0));
IERC20Detailed quote = IERC20Detailed(curve.reserves(1));
IERC20Metadata base = IERC20Metadata(curve.reserves(0));
IERC20Metadata quote = IERC20Metadata(curve.reserves(1));
uint256 curveBaseAmt = base.balanceOf(_curve);
uint256 curveQuoteAmt = quote.balanceOf(_curve);
AssimilatorV3 baseAssim = AssimilatorV3(curve.assimilator(address(base)));
Expand All @@ -583,7 +584,7 @@ contract Zap {
}

// get a usd amount of token
function _getUsdAmount(AssimilatorV3 _assim, IERC20Detailed _token, uint256 _amt) internal view returns (uint256) {
function _getUsdAmount(AssimilatorV3 _assim, IERC20Metadata _token, uint256 _amt) internal view returns (uint256) {
IOracle oracle = IOracle(_assim.oracle());
(, int256 price,,,) = oracle.latestRoundData();
return _amt.mul(uint256(price)).mul(10 ** (18 - oracle.decimals())).div(10 ** (_token.decimals()));
Expand Down
12 changes: 6 additions & 6 deletions src/assimilators/AssimilatorV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@
pragma solidity ^0.8.13;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";

import "../lib/ABDKMath64x64.sol";
import "../interfaces/IAssimilator.sol";
import "../interfaces/IOracle.sol";
import "../interfaces/IERC20Detailed.sol";
import "../interfaces/IWeth.sol";

contract AssimilatorV3 is IAssimilator {
using ABDKMath64x64 for int128;
using ABDKMath64x64 for uint256;

using SafeMath for uint256;
using SafeERC20 for IERC20Detailed;
using SafeERC20 for IERC20Metadata;

IERC20Detailed public immutable pairToken;
IERC20Metadata public immutable pairToken;

IOracle public immutable oracle;
IERC20Detailed public immutable token;
IERC20Metadata public immutable token;
uint256 public immutable oracleDecimals;
uint256 public immutable tokenDecimals;
uint256 public immutable pairTokenDecimals;
Expand All @@ -53,10 +53,10 @@ contract AssimilatorV3 is IAssimilator {
) {
wETH = _wETH;
oracle = _oracle;
token = IERC20Detailed(_token);
token = IERC20Metadata(_token);
oracleDecimals = _oracleDecimals;
tokenDecimals = _tokenDecimals;
pairToken = IERC20Detailed(_pairToken);
pairToken = IERC20Metadata(_pairToken);
pairTokenDecimals = pairToken.decimals();
}

Expand Down
28 changes: 0 additions & 28 deletions src/interfaces/IERC20Detailed.sol

This file was deleted.

32 changes: 16 additions & 16 deletions test/PolygonDeployed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ pragma solidity ^0.8.10;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";

import "../src/interfaces/IAssimilator.sol";
import "../src/interfaces/IOracle.sol";
import "../src/interfaces/IERC20Detailed.sol";
import "../src/AssimilatorFactory.sol";
import "../src/CurveFactoryV3.sol";
import "../src/Curve.sol";
Expand All @@ -33,7 +33,7 @@ import "forge-std/StdAssertions.sol";

contract DepositTest is Test {
using SafeMath for uint256;
using SafeERC20 for IERC20Detailed;
using SafeERC20 for IERC20Metadata;

CheatCodes cheats = CheatCodes(HEVM_ADDRESS);
Utils utils;
Expand All @@ -45,10 +45,10 @@ contract DepositTest is Test {
Curve public trybCurve;
Curve public ngncCurve;
// tokens
IERC20Detailed public cadc;
IERC20Detailed public usdc;
IERC20Detailed public tryb;
IERC20Detailed public ngnc;
IERC20Metadata public cadc;
IERC20Metadata public usdc;
IERC20Metadata public tryb;
IERC20Metadata public ngnc;

// oracles
IOracle public cadcOracle;
Expand All @@ -73,10 +73,10 @@ contract DepositTest is Test {
accounts.push(new MockUser());
}
// init tokens
cadc = IERC20Detailed(Polygon.CADC);
usdc = IERC20Detailed(Polygon.USDC);
tryb = IERC20Detailed(Polygon.TRYB);
ngnc = IERC20Detailed(Polygon.NGNC);
cadc = IERC20Metadata(Polygon.CADC);
usdc = IERC20Metadata(Polygon.USDC);
tryb = IERC20Metadata(Polygon.TRYB);
ngnc = IERC20Metadata(Polygon.NGNC);

//init oracles
cadcOracle = IOracle(Polygon.CHAINLINK_CAD_USD);
Expand Down Expand Up @@ -117,7 +117,7 @@ contract DepositTest is Test {
console.log("pair token balances after first deposit");
console.log(cadc.balanceOf(address(cadcCurve)));
console.log(usdc.balanceOf(address(cadcCurve)));
uint256 userLptAfterFirstDeposit = IERC20Detailed(address(cadcCurve)).balanceOf(address(accounts[0]));
uint256 userLptAfterFirstDeposit = IERC20Metadata(address(cadcCurve)).balanceOf(address(accounts[0]));
cheats.stopPrank();
(lptAmt, outs) = cadcCurve.viewDeposit(175000 * 1e18);
console.log("second deposit : ", lptAmt, outs[0], outs[1]);
Expand All @@ -130,7 +130,7 @@ contract DepositTest is Test {
console.log(cadc.balanceOf(address(cadcCurve)));
console.log(usdc.balanceOf(address(cadcCurve)));
cheats.stopPrank();
uint256 userLptAfterSecondDeposit = IERC20Detailed(address(cadcCurve)).balanceOf(address(accounts[0]));
uint256 userLptAfterSecondDeposit = IERC20Metadata(address(cadcCurve)).balanceOf(address(accounts[0]));
console.log("user lpt balance");
console.log(
userLptAfterFirstDeposit, userLptAfterSecondDeposit, userLptAfterSecondDeposit - userLptAfterFirstDeposit
Expand Down Expand Up @@ -172,17 +172,17 @@ contract DepositTest is Test {
cheats.stopPrank();
// now mint base token, update decimals map
uint256 mintAmt = 300_000_000_000;
uint256 baseDecimals = utils.tenToPowerOf(IERC20Detailed(base).decimals());
uint256 baseDecimals = utils.tenToPowerOf(IERC20Metadata(base).decimals());
decimals[base] = baseDecimals;
deal(base, address(accounts[0]), mintAmt.mul(baseDecimals));
// now mint quote token, update decimals map
uint256 quoteDecimals = utils.tenToPowerOf(IERC20Detailed(quote).decimals());
uint256 quoteDecimals = utils.tenToPowerOf(IERC20Metadata(quote).decimals());
decimals[quote] = quoteDecimals;
deal(quote, address(accounts[0]), mintAmt.mul(quoteDecimals));
// now approve the deployed curve
cheats.startPrank(address(accounts[0]));
IERC20Detailed(base).safeApprove(address(_curve), type(uint256).max);
IERC20Detailed(quote).safeApprove(address(_curve), type(uint256).max);
IERC20Metadata(base).safeApprove(address(_curve), type(uint256).max);
IERC20Metadata(quote).safeApprove(address(_curve), type(uint256).max);
cheats.stopPrank();
return _curve;
}
Expand Down
Loading