instance factory merge bootstrap

This commit is contained in:
Ricardo Guilherme Schmidt 2018-05-13 18:00:07 -03:00
commit 675cbd2377
5 changed files with 126 additions and 17 deletions

1
.gitignore vendored
View File

@ -36,6 +36,7 @@ coverage.json
# node # node
node_modules/ node_modules/
npm-debug.log npm-debug.log
package-lock.json
# other # other
.vs/ .vs/

View File

@ -15,6 +15,9 @@
], ],
"gas": "auto", "gas": "auto",
"contracts": { "contracts": {
"ERC20Receiver": {
"deploy": false
},
"Factory": { "Factory": {
"deploy": false "deploy": false
}, },
@ -23,7 +26,7 @@
}, },
"UpdatableInstance": { "UpdatableInstance": {
"deploy": false "deploy": false
} }
} }
} }
} }

View File

@ -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);
}
}

View File

@ -5,14 +5,14 @@ describe("ERC20Token", async function() {
before(function(done) { before(function(done) {
this.timeout(0); this.timeout(0);
var contractsConfig = { var contractsConfig = {
"TestToken": { "TestToken": { },
} "ERC20Receiver": { }
}; };
EmbarkSpec.deployAll(contractsConfig, async function(accounts) { EmbarkSpec.deployAll(contractsConfig, async function(accounts) {
ERC20Token = TestToken; ERC20Token = TestToken;
accountsArr = accounts; accountsArr = accounts;
for(i=0;i<accountsArr.length;i++){ 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() done()
}); });
@ -61,6 +61,21 @@ describe("ERC20Token", async function() {
assert.equal(result, 0); 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 //TODO: include checks for expected events fired
}); });

View File

@ -1,34 +1,34 @@
describe("TestToken", function() { describe("TestToken", async function() {
this.timeout(0); this.timeout(0);
var accountsArr; var accountsArr;
before(function(done) { before(function(done) {
this.timeout(0); this.timeout(0);
var contractsConfig = { var contractsConfig = {
"TestToken": { "TestToken": {
} }
}; };
EmbarkSpec.deployAll(contractsConfig, (accounts) => { accountsArr = accounts; done() }); EmbarkSpec.deployAll(contractsConfig, async function(accounts) {
}); accountsArr = accounts
for(i=0;i<accountsArr.length;i++) {
await TestToken.methods.mint(7 * 10 ^ 18).send({from: accountsArr[i]})
it("should start totalSupply 0", async function() { }
let result = await TestToken.methods.totalSupply().call(); done()
assert.equal(result, 0); });
}); });
it("should increase totalSupply in mint", async function() { it("should increase totalSupply in mint", async function() {
let initialSupply = await TestToken.methods.balanceOf(accountsArr[0]).call(); let initialSupply = await TestToken.methods.totalSupply().call();
await TestToken.methods.mint(100).send({from: accountsArr[0]}); await TestToken.methods.mint(100).send({from: accountsArr[0]});
let result = await TestToken.methods.totalSupply().call(); let result = await TestToken.methods.totalSupply().call();
assert.equal(result, 100); assert.equal(result, +initialSupply+100);
}); });
it("should increase accountBalance in mint", async function() { it("should increase accountBalance in mint", async function() {
let initialBalance = await TestToken.methods.balanceOf(accountsArr[0]).call(); let initialBalance = await TestToken.methods.balanceOf(accountsArr[0]).call();
await TestToken.methods.mint(100).send({from: accountsArr[0]}); await TestToken.methods.mint(100).send({from: accountsArr[0]});
let result = await TestToken.methods.totalSupply().call(); let result = await TestToken.methods.balanceOf(accountsArr[0]).call();
assert.equal(result, +initialBalance+100); assert.equal(result, +initialBalance+100);
}); });
}); });