Created
March 20, 2021 16:57
-
-
Save spalladino/e0b867402b6c22447c54f32a399c885f to your computer and use it in GitHub Desktop.
Strawman for flashloans for flashbots
This file contains hidden or 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.7.0; | |
// Each mining pool that intends to provide flash loans deploys a Loaner contract and transfers ETH to it | |
// When testing each bundle, the diff in balance in this contract is taking into account for calculating effective gas price | |
// The contract loans funds only on blocks mined by the miner and on zero-gasprice txs | |
contract Loaner { | |
address immutable owner; | |
constructor(address _owner) { | |
owner = _owner; | |
} | |
function loan(address payable to, uint256 amount) public returns (uint256) { | |
require(tx.gasprice == 0 && block.coinbase == owner); | |
to.transfer(amount); | |
return amount; | |
} | |
function withdraw(address payable to) public { | |
require(msg.sender == owner); | |
to.transfer(address(this).balance); | |
} | |
} | |
// Users interact with the router, which redirects to the corresponding loaner based on the miner that picked up the bundle | |
contract Router { | |
mapping(address => Loaner) registry; | |
function getBalance() public view returns(uint256) { | |
Loaner loaner = registry[block.coinbase]; | |
if (address(loaner) == address(0)) return 0; | |
return address(loaner).balance; | |
} | |
function loan(address payable to, uint256 amount) public returns(uint256) { | |
Loaner loaner = registry[block.coinbase]; | |
if (address(loaner) == address(0)) return 0; | |
return loaner.loan(to, amount); | |
} | |
function register(Loaner loaner) public { | |
registry[msg.sender] = loaner; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This bit probably needs to be adjusted to post-1559