diff --git a/.gitignore b/.gitignore index 33d2b94..5c20c22 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ coverage.json # node node_modules/ npm-debug.log +package-lock.json # other .vs/ diff --git a/config/contracts.json b/config/contracts.json index c1a7970..66ea3c2 100644 --- a/config/contracts.json +++ b/config/contracts.json @@ -15,6 +15,9 @@ ], "gas": "auto", "contracts": { + "ERC20Receiver": { + "deploy": false + }, "Factory": { "deploy": false }, diff --git a/contracts/deploy/UpdatableInstance.sol b/contracts/deploy/UpdatableInstance.sol index 9427c5b..0cbb6f2 100644 --- a/contracts/deploy/UpdatableInstance.sol +++ b/contracts/deploy/UpdatableInstance.sol @@ -21,7 +21,7 @@ contract UpdatableInstance is Instance { function updateUpdatableInstance(address _kernel) external { require(msg.sender == address(this)); - InstanceUpdated(kernel, _kernel); + emit InstanceUpdated(kernel, _kernel); kernel = _kernel; } 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 { accountsArr = accounts; done() }); - }); - - - it("should start totalSupply 0", async function() { - let result = await TestToken.methods.totalSupply().call(); - assert.equal(result, 0); + EmbarkSpec.deployAll(contractsConfig, async function(accounts) { + accountsArr = accounts + for(i=0;i