Created
May 24, 2021 17:08
-
-
Save adidonato/902b1aa01d593d46603e09f163ca04d6 to your computer and use it in GitHub Desktop.
kovan
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
pragma solidity ^0.5.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 { //https://kovan.etherscan.io/tx/0xd20d8dd82c3308342256d8c15d94a98e92c200451f5ce82ce4c13ac0cd76fe79 | |
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; | |
} | |
} | |
contract DyDxPool is Structs { | |
function getAccountWei(Info memory account, uint256 marketId) public view returns (Wei memory); | |
function operate(Info[] memory, ActionArgs[] memory) public; | |
} | |
pragma solidity ^0.5.0; | |
/** | |
* @dev Interface of the ERC20 standard as defined in the EIP. Does not include | |
* the optional functions; to access them see `ERC20Detailed`. | |
*/ | |
interface IERC20 { | |
function balanceOf(address account) external view returns (uint256); | |
function approve(address spender, uint256 amount) external returns (bool); | |
} | |
pragma solidity ^0.5.0; | |
contract DyDxFlashLoan is Structs { | |
DyDxPool pool = DyDxPool(0x4EC3570cADaAEE08Ae384779B0f3A45EF85289DE); | |
address public WETH = 0xd0A1E359811322d97991E03f863a0C30C2cF029C; | |
address public SAI = 0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359; | |
address public USDC = 0xb7a4F3E9097C08dA09517b5aB877F7a917224ede; | |
address public DAI = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa; | |
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: could be called by DyDx pool only" | |
); | |
_; | |
} | |
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 | |
{ | |
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); | |
} | |
} | |
pragma solidity ^0.5.0; | |
contract FlashLoanKovan is DyDxFlashLoan { | |
uint256 public loan; | |
constructor() public payable { | |
(bool success, ) = WETH.call.value(msg.value)(""); | |
require(success, "fail to get weth"); | |
} | |
function getFlashloan(address flashToken, uint256 flashAmount) external { | |
uint256 balanceBefore = IERC20(flashToken).balanceOf(address(this)); | |
bytes memory data = abi.encode(flashToken, flashAmount, balanceBefore); | |
flashloan(flashToken, flashAmount, data); // execution goes to `callFunction` | |
} | |
function callFunction( | |
address, /* sender */ | |
Info calldata, /* accountInfo */ | |
bytes calldata data | |
) external onlyPool { | |
(address flashToken, uint256 flashAmount, uint256 balanceBefore) = abi | |
.decode(data, (address, uint256, uint256)); | |
uint256 balanceAfter = IERC20(flashToken).balanceOf(address(this)); | |
require( | |
balanceAfter - balanceBefore == flashAmount, | |
"contract did not get the loan" | |
); | |
loan = balanceAfter; | |
// Use the money here! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment