Skip to content

Commit

Permalink
finished tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JGcarv committed Aug 17, 2017
1 parent 6f47076 commit 27755c8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 24 deletions.
33 changes: 33 additions & 0 deletions contracts/token/TokenWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,59 @@ pragma solidity ^0.4.11;
import "./ERC20Basic.sol";
import "../ownership/Ownable.sol";

/**
* @title Token Wallet
* @notice Base contract that allows children contracts to handle ERC20 tokens
*/
contract TokenWallet is Ownable{

mapping(bytes32 => address) public tokens;

/**
* @dev Register a token to be accessible by identifier
* @param tokenAddr address the token address
* @param tokenName bytes32 the desired token identifier
*/
function registerToken(address tokenAddr, bytes32 tokenName) onlyOwner{
tokens[tokenName] = tokenAddr;
}

/**
* @dev Transfer owned tokes
* @param tokenAddr address the token address
* @param to address The address to transfer tokens to
* @param value uint256 The amount of tokens to transfer
* @return bool if transfer is successful
*/
function transferToken(address tokenAddr, address to, uint value) onlyOwner returns(bool){
return ERC20Basic(tokenAddr).transfer(to, value);
}

/**
* @dev Transfer owned tokes
* @param tokenName bytes32 the token identifier
* @param to address The address to transfer tokens to
* @param value uint256 The amount of tokens to transfer
* @return bool if transfer is successful
*/
function transferToken(bytes32 tokenName, address to, uint value) onlyOwner returns(bool){
return ERC20Basic(tokens[tokenName]).transfer(to, value);
}

/**
* @dev Check the balance of a token
* @param tokenAddr address the token address
* @return uint representing the amount of tokens
*/
function checkBalance(address tokenAddr) constant returns(uint){
return ERC20Basic(tokenAddr).balanceOf(this);
}

/**
* @dev Check the balance of a token
* @param tokenName bytes32 the token identifier
* @return uint representing the amount of tokens
*/
function checkBalance(bytes32 tokenName) constant returns(uint){
return ERC20Basic(tokens[tokenName]).balanceOf(this);
}
Expand Down
70 changes: 46 additions & 24 deletions test/TokenWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,61 @@ var TokenWallet = artifacts.require("../contracts/token/TokenWallet.sol")
var BasicTokenMock = artifacts.require("./helpers/BasicTokenMock.sol");

contract('TokenWallet', function(accounts) {
let token;
let tokenWallet;
let owner;
var token;
var tokenWallet;

beforeEach(async () => {
token = await BasicTokenMock.new(accounts[0], 100);
tokenWallet = await TokenWallet();
owner = await tokenWallet.owner();
token = await BasicTokenMock.new(accounts[1], 100);
tokenWallet = await TokenWallet.new({from: accounts[0]});

await token.transfer(tokenWallet, 10 , {from: owner});
await token.transfer(tokenWallet.address, 10 , {from: accounts[1]});
});

it("should be able to register tokens", async function() {
let tokenAddress = await token.address;
await tokenWallet.registerToken(tokenAddress, "BasicToken", {from: owner});
let tkAddr = await tokenWallet.tokens["BasicToken"];
assert.isEqual(tkAddr, tokenAddress);
it("should be able to register tokens", async () => {
tokenWallet.registerToken(token.address, "BasicToken", {from: accounts[0]})
assert.equal(await tokenWallet.tokens("BasicToken"), token.address)
})

it("should check balance correctly by address", async function(){
let tokenAddress = await token.addres;
let balance = await tokenWallet.checkBalance(tokenAddress);
assert.equal(balance, 10);
let balance = await tokenWallet.checkBalance(token.address);
assert.equal(balance.toNumber(), 10);
});

it("should transfer owned tokens correctly", async () => {
await tokenWallet.transferToken(token.address, accounts[2], 5, {from: accounts[0]})
assert.equal(await token.balanceOf(tokenWallet.address), 5)
assert.equal(await token.balanceOf(accounts[2]), 5)
})

it("should transfer correctly by token name", async function(){
const transferByName = (n,t,v,p) => {
return new Promise((resolve, reject) => {
tokenWallet.contract.transferToken['bytes32,address,uint256'](n,t,v,p, (err, res) => {
if(err) return reject(err)
resolve(res)
})
})
}

await tokenWallet.registerToken(token.address, "BasicToken", {from: accounts[0]})
await transferByName("BasicToken", accounts[3], 7, {from:accounts[0]});
assert.equal(await token.balanceOf(tokenWallet.address), 3)
assert.equal(await token.balanceOf(accounts[3]), 7)
});

it("should check balance correctly by token name", async function(){
const checkBalanceByName = (n,p) => {
return new Promise((resolve, reject) => {
tokenWallet.contract.checkBalance['bytes32'](n, p, (err, res) => {
if(err || !res) return reject(err)
resolve(res)
})
})
}
await tokenWallet.registerToken(token.address, "BasicToken", {from: accounts[0]})
let balance = await checkBalanceByName("BasicToken", {from:accounts[0]});
assert.equal(balance.toNumber(), 10);
});

// it("should throw an error when trying to transfer more than balance", async function() {
// let token = await BasicTokenMock.new(accounts[0], 100);
// try {
// let transfer = await token.transfer(accounts[1], 101);
// assert.fail('should have thrown before');
// } catch(error) {
// assertJump(error);
// }
// });

});

0 comments on commit 27755c8

Please sign in to comment.