add ERC20Receiver+tests
This commit is contained in:
parent
31fb6a416d
commit
b2c07c63f7
|
@ -15,6 +15,9 @@
|
|||
],
|
||||
"gas": "auto",
|
||||
"contracts": {
|
||||
"ERC20Receiver": {
|
||||
"deploy": false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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.length;i++){
|
||||
await ERC20Token.methods.mint(100).send({from: accountsArr[i]})
|
||||
await ERC20Token.methods.mint(7 * 10 ^ 18).send({from: accountsArr[i]})
|
||||
}
|
||||
done()
|
||||
});
|
||||
|
@ -61,6 +61,21 @@ describe("ERC20Token", async function() {
|
|||
assert.equal(result, 0);
|
||||
});
|
||||
|
||||
it("should deposit approved amount to contract ERC20Receiver", async function() {
|
||||
await ERC20Token.methods.approve(ERC20Receiver.address,10).send({from: accountsArr[0]});
|
||||
await ERC20Receiver.methods.depositToken(ERC20Token.address).send({from: accountsArr[0]});
|
||||
let result = await ERC20Receiver.methods.tokenBalanceOf(ERC20Token.address, accountsArr[0]).call();
|
||||
assert.equal(result, 10, "ERC20Receiver.tokenBalanceOf("+ERC20Token.address+","+accountsArr[0]+") wrong");
|
||||
});
|
||||
|
||||
it("should witdraw approved amount from contract ERC20Receiver", async function() {
|
||||
let tokenBalance = await ERC20Receiver.methods.tokenBalanceOf(ERC20Token.address, accountsArr[0]).call();
|
||||
await ERC20Receiver.methods.withdrawToken(ERC20Token.address, tokenBalance).send({from: accountsArr[0]});
|
||||
tokenBalance = await ERC20Receiver.methods.tokenBalanceOf(ERC20Token.address, accountsArr[0]).call();
|
||||
assert.equal(tokenBalance, 0, "ERC20Receiver.tokenBalanceOf("+ERC20Token.address+","+accountsArr[0]+") wrong");
|
||||
});
|
||||
|
||||
//TODO: include checks for expected events fired
|
||||
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue