2

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?

enter image description here

4
  • Can you narrow down to the failing call?
    – Ismael
    Nov 10 '17 at 17:26
  • What does it mean? Nov 13 '17 at 8:31
  • 1
    Can you simplify your contract to a minimal test that causes the same error?
    – Ismael
    Nov 13 '17 at 8:46
  • Yes.I haven`t error when I try testing simple erc20 contract and function "transfer" complete test successfull but when I try do it for ERC20 token with two owners, which set durring deployment in construcor that test not complete successfull. Nov 15 '17 at 9:30
0

Can you simplify your contract to a minimal test that causes the same error?

I simplify my contract for testing "transfer function" and it don`t work yet. I try call function transfer in my test for contract which has two owners and I have the same error:

Error: VM Exception while processing transaction: invalid opcode

pragma solidity ^0.4.15;    

import './SafeMath.sol';

contract TransferToken is SafeMath {
    event Transfer(address indexed _from, address indexed _to, uint _value);
    event Approval(address indexed _owner, address indexed _spender, uint _value);

    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 TransferToken (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 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;     
    }   
}

And I have test for it:

var TFT = artifacts.require('TransferToken');

contract("Testing Transfer function in token", 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 = 100000000;

        return TFT.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");
      });
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.