From 87dbdbef39381acc9fb4e1d358fd8c77dda155a0 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sun, 13 May 2018 06:17:39 -0300 Subject: [PATCH 1/4] missing emit --- contracts/deploy/UpdatableInstance.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From 31fb6a416dcad0390e9f4f7cf6175827e3d4c534 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sun, 13 May 2018 12:41:38 -0300 Subject: [PATCH 2/4] add package-lock to.json .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) 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/ From b2c07c63f76594d661745af3223d1d65fc613fdb Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sun, 13 May 2018 14:01:59 -0300 Subject: [PATCH 3/4] add ERC20Receiver+tests --- config/contracts.json | 3 ++ contracts/token/ERC20Receiver.sol | 90 +++++++++++++++++++++++++++++++ test/erc20token.js | 21 ++++++-- 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 contracts/token/ERC20Receiver.sol 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 Date: Sun, 13 May 2018 17:54:05 -0300 Subject: [PATCH 4/4] fix tests --- test/testtoken.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/testtoken.js b/test/testtoken.js index 2dd3fb7..08f8794 100644 --- a/test/testtoken.js +++ b/test/testtoken.js @@ -1,34 +1,34 @@ -describe("TestToken", function() { +describe("TestToken", async function() { this.timeout(0); var accountsArr; + before(function(done) { this.timeout(0); var contractsConfig = { "TestToken": { } }; - EmbarkSpec.deployAll(contractsConfig, (accounts) => { 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