Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,370 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
module.exports = { | ||
skipFiles: [ | ||
'interfaces', | ||
'integrations', | ||
'z_mocks', | ||
'shared/InitializableReentrancyGuard.sol', | ||
'integrations', | ||
'masset/peripheral', | ||
'savings/peripheral', | ||
'upgradability' | ||
] | ||
}; | ||
skipFiles: [ | ||
"interfaces", | ||
"integrations", | ||
"z_mocks", | ||
"shared/InitializableReentrancyGuard.sol", | ||
"integrations", | ||
"masset/peripheral", | ||
"peripheral", | ||
"savings/peripheral", | ||
"upgradability", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
pragma solidity 0.8.0; | ||
|
||
interface ICurve { | ||
function get_virtual_price() external returns (uint256 out); | ||
|
||
function add_liquidity(uint256[2] calldata amounts, uint256 deadline) external; | ||
|
||
function get_dy(int128 i, int128 j, uint256 dx) | ||
external | ||
returns (uint256 out); | ||
|
||
function get_dy_underlying(int128 i, int128 j, uint256 dx) | ||
external | ||
returns (uint256 out); | ||
|
||
function exchange( | ||
int128 i, | ||
int128 j, | ||
uint256 dx, | ||
uint256 min_dy | ||
) external; | ||
|
||
function exchange( | ||
int128 i, | ||
int128 j, | ||
uint256 dx, | ||
uint256 min_dy, | ||
uint256 deadline | ||
) external; | ||
|
||
function exchange_underlying( | ||
int128 i, | ||
int128 j, | ||
uint256 dx, | ||
uint256 min_dy | ||
) external; | ||
|
||
function exchange_underlying( | ||
int128 i, | ||
int128 j, | ||
uint256 dx, | ||
uint256 min_dy, | ||
uint256 deadline | ||
) external; | ||
|
||
function remove_liquidity( | ||
uint256 _amount, | ||
uint256 deadline, | ||
uint256[2] calldata min_amounts | ||
) external; | ||
|
||
function remove_liquidity_imbalance(uint256[2] calldata amounts, uint256 deadline) | ||
external; | ||
|
||
function commit_new_parameters( | ||
int128 amplification, | ||
int128 new_fee, | ||
int128 new_admin_fee | ||
) external; | ||
|
||
function apply_new_parameters() external; | ||
|
||
function revert_new_parameters() external; | ||
|
||
function commit_transfer_ownership(address _owner) external; | ||
|
||
function apply_transfer_ownership() external; | ||
|
||
function revert_transfer_ownership() external; | ||
|
||
function withdraw_admin_fees() external; | ||
|
||
function coins(int128 arg0) external returns (address out); | ||
|
||
function underlying_coins(int128 arg0) external returns (address out); | ||
|
||
function balances(int128 arg0) external returns (uint256 out); | ||
|
||
function A() external returns (int128 out); | ||
|
||
function fee() external returns (int128 out); | ||
|
||
function admin_fee() external returns (int128 out); | ||
|
||
function owner() external returns (address out); | ||
|
||
function admin_actions_deadline() external returns (uint256 out); | ||
|
||
function transfer_ownership_deadline() external returns (uint256 out); | ||
|
||
function future_A() external returns (int128 out); | ||
|
||
function future_fee() external returns (int128 out); | ||
|
||
function future_admin_fee() external returns (int128 out); | ||
|
||
function future_owner() external returns (address out); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
pragma solidity ^0.8.0; | ||
pragma experimental ABIEncoderV2; | ||
|
||
interface Structs { | ||
struct Val { | ||
uint256 value; | ||
} | ||
|
||
enum ActionType { | ||
Deposit, // supply tokens | ||
Withdraw, // borrow tokens | ||
Transfer, // transfer balance between accounts | ||
Buy, // buy an amount of some token (externally) | ||
Sell, // sell an amount of some token (externally) | ||
Trade, // trade tokens against another account | ||
Liquidate, // liquidate an undercollateralized or expiring account | ||
Vaporize, // use excess tokens to zero-out a completely negative account | ||
Call // send arbitrary data to an address | ||
} | ||
|
||
enum AssetDenomination { | ||
Wei // the amount is denominated in wei | ||
} | ||
|
||
enum AssetReference { | ||
Delta // the amount is given as a delta from the current value | ||
} | ||
|
||
struct AssetAmount { | ||
bool sign; // true if positive | ||
AssetDenomination denomination; | ||
AssetReference ref; | ||
uint256 value; | ||
} | ||
|
||
struct ActionArgs { | ||
ActionType actionType; | ||
uint256 accountId; | ||
AssetAmount amount; | ||
uint256 primaryMarketId; | ||
uint256 secondaryMarketId; | ||
address otherAddress; | ||
uint256 otherAccountId; | ||
bytes data; | ||
} | ||
|
||
struct Info { | ||
address owner; // The address that owns the account | ||
uint256 number; // A nonce that allows a single address to control many accounts | ||
} | ||
|
||
struct Wei { | ||
bool sign; // true if positive | ||
uint256 value; | ||
} | ||
} | ||
|
||
interface DyDxPool is Structs { | ||
function getAccountWei(Info memory account, uint256 marketId) external view returns (Wei memory); | ||
function operate(Info[] memory, ActionArgs[] memory) external; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
pragma solidity ^0.8.0; | ||
|
||
import "./DyDx.sol"; | ||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
|
||
contract DyDxFlashLoan is Structs { | ||
|
||
using SafeERC20 for IERC20; | ||
|
||
DyDxPool constant pool = DyDxPool(0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e); | ||
|
||
// DyDx tokens | ||
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; | ||
address constant SAI = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359; | ||
|
||
// Both DyDx and mUSD tokens | ||
address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; | ||
address constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F; | ||
|
||
// mUSD tokens | ||
address constant sUSD = 0x57Ab1ec28D129707052df4dF418D58a2D46d5f51; | ||
address constant TUSD = 0x0000000000085d4780B73119b644AE5ecd22b376; | ||
address constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7; | ||
|
||
mapping(address => uint256) public currencies; | ||
|
||
constructor() public { | ||
currencies[WETH] = 1; | ||
currencies[SAI] = 2; | ||
currencies[USDC] = 3; | ||
currencies[DAI] = 4; | ||
} | ||
|
||
modifier onlyPool() { | ||
require( | ||
msg.sender == address(pool), | ||
"FlashLoan: only called by DyDx pool" | ||
); | ||
_; | ||
} | ||
|
||
function tokenToMarketId(address token) public view returns (uint256) { | ||
uint256 marketId = currencies[token]; | ||
require(marketId != 0, "FlashLoan: Unsupported token"); | ||
return marketId - 1; | ||
} | ||
|
||
// the DyDx will call `callFunction(address sender, Info memory accountInfo, bytes memory data) public` after during `operate` call | ||
function flashloan(address token, uint256 amount, bytes memory data) | ||
internal virtual | ||
{ | ||
IERC20(token).approve(address(pool), amount + 1); | ||
Info[] memory infos = new Info[](1); | ||
ActionArgs[] memory args = new ActionArgs[](3); | ||
|
||
infos[0] = Info(address(this), 0); | ||
|
||
AssetAmount memory wamt = AssetAmount( | ||
false, | ||
AssetDenomination.Wei, | ||
AssetReference.Delta, | ||
amount | ||
); | ||
ActionArgs memory withdraw; | ||
withdraw.actionType = ActionType.Withdraw; | ||
withdraw.accountId = 0; | ||
withdraw.amount = wamt; | ||
withdraw.primaryMarketId = tokenToMarketId(token); | ||
withdraw.otherAddress = address(this); | ||
|
||
args[0] = withdraw; | ||
|
||
ActionArgs memory call; | ||
call.actionType = ActionType.Call; | ||
call.accountId = 0; | ||
call.otherAddress = address(this); | ||
call.data = data; | ||
|
||
args[1] = call; | ||
|
||
ActionArgs memory deposit; | ||
AssetAmount memory damt = AssetAmount( | ||
true, | ||
AssetDenomination.Wei, | ||
AssetReference.Delta, | ||
amount + 1 | ||
); | ||
deposit.actionType = ActionType.Deposit; | ||
deposit.accountId = 0; | ||
deposit.amount = damt; | ||
deposit.primaryMarketId = tokenToMarketId(token); | ||
deposit.otherAddress = address(this); | ||
|
||
args[2] = deposit; | ||
|
||
pool.operate(infos, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
pragma solidity 0.8.0; | ||
|
||
/** | ||
* @title mUSD interface before upgrade to V3 | ||
*/ | ||
interface IMassetV2 { | ||
|
||
/** @dev Calc interest */ | ||
function collectInterest() external returns (uint256 swapFeesGained, uint256 newTotalSupply); | ||
function collectPlatformInterest() external returns (uint256 interestGained, uint256 newTotalSupply); | ||
|
||
/** @dev Minting */ | ||
function mint(address _basset, uint256 _bassetQuantity) | ||
external returns (uint256 massetMinted); | ||
function mintTo(address _basset, uint256 _bassetQuantity, address _recipient) | ||
external returns (uint256 massetMinted); | ||
function mintMulti(address[] calldata _bAssets, uint256[] calldata _bassetQuantity, address _recipient) | ||
external returns (uint256 massetMinted); | ||
|
||
/** @dev Swapping */ | ||
function swap( address _input, address _output, uint256 _quantity, address _recipient) | ||
external returns (uint256 output); | ||
function getSwapOutput( address _input, address _output, uint256 _quantity) | ||
external view returns (bool, string memory, uint256 output); | ||
|
||
/** @dev Redeeming */ | ||
function redeem(address _basset, uint256 _bassetQuantity) | ||
external returns (uint256 massetRedeemed); | ||
function redeemTo(address _basset, uint256 _bassetQuantity, address _recipient) | ||
external returns (uint256 massetRedeemed); | ||
function redeemMulti(address[] calldata _bAssets, uint256[] calldata _bassetQuantities, address _recipient) | ||
external returns (uint256 massetRedeemed); | ||
function redeemMasset(uint256 _mAssetQuantity, address _recipient) external; | ||
|
||
/** @dev Setters for the Manager or Gov to update module info */ | ||
function upgradeForgeValidator(address _newForgeValidator) external; | ||
|
||
/** @dev Setters for Gov to set system params */ | ||
function setSwapFee(uint256 _swapFee) external; | ||
|
||
/** @dev Getters */ | ||
function getBasketManager() external view returns(address); | ||
function forgeValidator() external view returns (address); | ||
function totalSupply() external view returns (uint256); | ||
function swapFee() external view returns (uint256); | ||
} |
Oops, something went wrong.