diff --git a/config/contracts.json b/config/contracts.json index 3ebb190..923d2c9 100644 --- a/config/contracts.json +++ b/config/contracts.json @@ -15,6 +15,9 @@ ], "gas": "auto", "contracts": { + "ERC20Receiver": { + "deploy": false + } } } } diff --git a/contracts/token/ERC20Receiver.sol b/contracts/token/ERC20Receiver.sol new file mode 100644 index 0000000..9a6af70 --- /dev/null +++ b/contracts/token/ERC20Receiver.sol @@ -0,0 +1,90 @@ +pragma solidity ^0.4.23; + +import "./ERC20Token.sol"; + +contract ERC20Receiver { + + event TokenDeposited(address indexed token, address indexed sender, uint256 amount); + event TokenWithdrawn(address indexed token, address indexed sender, uint256 amount); + + mapping (address => mapping(address => uint256)) tokenBalances; + + constructor() public { + + } + + function depositToken( + ERC20Token _token + ) + external + { + _depositToken( + msg.sender, + _token, + _token.allowance( + msg.sender, + address(this) + ) + ); + } + + function withdrawToken( + ERC20Token _token, + uint256 _amount + ) + external + { + _withdrawToken(msg.sender, _token, _amount); + } + + function depositToken( + ERC20Token _token, + uint256 _amount + ) + external + { + require(_token.allowance(msg.sender, address(this)) >= _amount); + _depositToken(msg.sender, _token, _amount); + } + + function tokenBalanceOf( + ERC20Token _token, + address _from + ) + external + view + returns(uint256 fromTokenBalance) + { + return tokenBalances[address(_token)][_from]; + } + + function _depositToken( + address _from, + ERC20Token _token, + uint256 _amount + ) + private + { + require(_amount > 0); + if (_token.transferFrom(_from, address(this), _amount)) { + tokenBalances[address(_token)][_from] += _amount; + emit TokenDeposited(address(_token), _from, _amount); + } + } + + function _withdrawToken( + address _from, + ERC20Token _token, + uint256 _amount + ) + private + { + require(_amount > 0); + require(tokenBalances[address(_token)][_from] >= _amount); + tokenBalances[address(_token)][_from] -= _amount; + require(_token.transfer(_from, _amount)); + emit TokenWithdrawn(address(_token), _from, _amount); + } + + +} \ No newline at end of file diff --git a/test/erc20token.js b/test/erc20token.js index 075ddeb..3a0c0c0 100644 --- a/test/erc20token.js +++ b/test/erc20token.js @@ -5,14 +5,14 @@ describe("ERC20Token", async function() { before(function(done) { this.timeout(0); var contractsConfig = { - "TestToken": { - } + "TestToken": { }, + "ERC20Receiver": { } }; EmbarkSpec.deployAll(contractsConfig, async function(accounts) { ERC20Token = TestToken; accountsArr = accounts; for(i=0;i