-
-
Save mg6maciej/8af66a0cfb6ac318fa8cedeb4fd246d3 to your computer and use it in GitHub Desktop.
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.4.24; | |
contract ERC20 { | |
function transferFrom(address from, address to, uint amount) public returns (bool); | |
} | |
contract ERC777 { | |
function send(address to, uint amount, bytes data) public; | |
} | |
contract Vault { | |
mapping (address => mapping (address => uint)) private balances; | |
bool private duringDeposit; | |
// ERC20 (requires approval) | |
function deposit(ERC20 erc20, uint amount) external setDuringDeposit { | |
require(erc20.transferFrom(msg.sender, this, amount)); | |
balances[erc20][msg.sender] += amount; | |
} | |
function withdraw(ERC20 erc20, uint amount) external { | |
require(balances[erc20][msg.sender] >= amount); | |
balances[erc20][msg.sender] -= amount; | |
require(erc20.transferFrom(this, msg.sender, amount)); | |
} | |
// ERC223 / ERC677 compatibility | |
function tokenFallback(address from, uint amount, bytes /*data*/) external notDuringDeposit returns (bytes4) { | |
balances[msg.sender][from] += amount; | |
return 0xc0ee0b8a; | |
} | |
// ERC777 compatibility (assume this contract has registered itself as defined in ERC820/ERC777) | |
function tokensReceived(address operator, address /*from*/, address /*to*/, uint amount, bytes /*userData*/, bytes /*operatorData*/) external notDuringDeposit { | |
balances[msg.sender][operator] += amount; | |
} | |
function safeWithdraw777(ERC777 erc777, uint amount, bytes data) external { | |
require(balances[erc777][msg.sender] >= amount); | |
balances[erc777][msg.sender] -= amount; | |
erc777.send(msg.sender, amount, data); | |
} | |
modifier setDuringDeposit { | |
duringDeposit = true; | |
_; | |
duringDeposit = false; | |
} | |
modifier notDuringDeposit { | |
if (!duringDeposit) { | |
_; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment