I try test function transfer in my contract. I following by tutorial. Full project locate on my git. It is my test.js code:
var SPT = artifacts.require("StepanToken2");
contract("Testing ERC20 Contract", function(accounts){
it("should send coin correctly", function() {
var meta;
// Get initial balances of first and second account.
var account_one = accounts[0];
var account_two = accounts[1];
console.log("account_two", typeof(account_two));
var account_one_starting_balance;
var account_two_starting_balance;
var account_one_ending_balance;
var account_two_ending_balance;
var amount = 10;
return SPT.deployed(account_one,account_two).then(function(instance) {
meta = instance;
return meta.balanceOf.call(account_one);
}).then(function(balance) {
account_one_starting_balance = balance.toNumber();
return meta.balanceOf.call(account_two);
}).then(function(balance) {
account_two_starting_balance = balance.toNumber();
return meta.transfer(account_two, amount, {from: account_one});
}).then(function() {
return meta.balanceOf.call(account_one);
}).then(function(balance) {
account_one_ending_balance = balance.toNumber();
return meta.balanceOf.call(account_two);
}).then(function(balance) {
account_two_ending_balance = balance.toNumber();
assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender");
assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver");
});
});
})
It is code of my contract:
pragma solidity ^0.4.15;
import './ERC20.sol';
import './SafeMath.sol';
contract StepanToken2 is SafeMath, ERC20 {
address public ownerFirst;
address public ownerSecond;
string public constant name = "Stepan Token";
string public constant symbol = "SPT";
uint8 public constant decimals = 8;
uint totalTokens = 100000000000000;
uint public checkSum = 0;
//balances for accaunts
mapping(address => uint) balances;
//Owner of account approves the transfer of an amount to another account
mapping(address => mapping(address => uint)) allowed;
function StepanToken2 (address _ownerFirst, address _ownerSecond){
ownerFirst = _ownerFirst;
ownerSecond = _ownerSecond;
balances[ownerFirst] = safeDiv(totalTokens, 3);
balances[ownerSecond] = safeSub(totalTokens, balances[ownerFirst]);
// balances[ownerFirst] -= 10;
checkSum = safeAdd(balances[ownerFirst], balances[ownerSecond]);
if(checkSum != totalTokens){
balances[ownerFirst] = totalTokens;
balances[ownerSecond] = 0;
}
}
function totalSupply() constant returns (uint256 totalSupply){
return totalTokens;
}
function balanceOf(address _owner) constant returns (uint256 balance){
return balances[_owner];
}
function transfer(address _to, uint _value) returns (bool success){
require(balances[msg.sender] >= _value && _value > 0 && safeAdd(balances[_to], _value) > balances[_to]);
balances[msg.sender] = safeSub(balances[msg.sender],_value);
balances[_to] = safeAdd(balances[_to], _value);
Transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint _value) returns (bool success){
require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0 && safeAdd(balances[_to], _value) > 0);
balances[_from] = safeSub(balances[_from], _value);
allowed[_from][msg.sender] = safeSub(allowed[_from][msg.sender], _value);
balances[_to] = safeAdd(balances[_to], _value);
Transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint _value) returns (bool success){
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining){
return allowed[_owner][_spender];
}
}
I don`t understand how fix this problem?