Skip to main content
fix formatting code
Source Link
Antonio Carito
  • 2.5k
  • 3
  • 10
  • 24
    contract WhiteCrypto{

    //Token id

    string  public name = "White Crypto";
    string  public symbol = "WHC";
    string public standard = "White Crypto v1.0";

    uint256 public totalSupply;

    event Transfer(

    address indexed _from,
    address indexed _to,
    uint256 _value

);

   );

    mapping(address => uint256) public balanceOf;
    

        function WhiteCryptoFunction (uint256 _initialSupply) public {
            balanceOf[msg.sender]= _initialSupply;
            totalSupply = _initialSupply;
    }

    //transfer functions
    function transfer(address _to,uint256 _value) public returns (bool success){
       require(balanceOf[msg.sender] >=_value);

       balanceOf[msg.sender] -=_value;
       balanceOf[_to] += _value;

       emit Transfer(msg.sender, _to, _value);

       return true;

        }

and my js-JS code:

contract WhiteCrypto{

//Token id

string  public name = "White Crypto";
string  public symbol = "WHC";
string public standard = "White Crypto v1.0";

uint256 public totalSupply;

event Transfer(

address indexed _from,
address indexed _to,
uint256 _value

);

mapping(address => uint256) public balanceOf;


    function WhiteCryptoFunction (uint256 _initialSupply) public {
        balanceOf[msg.sender]= _initialSupply;
        totalSupply = _initialSupply;
}

//transfer functions
function transfer(address _to,uint256 _value) public returns (bool success){
   require(balanceOf[msg.sender] >=_value);

   balanceOf[msg.sender] -=_value;
   balanceOf[_to] += _value;

   emit Transfer(msg.sender, _to, _value);

   return true;

    }

and my js-

    contract WhiteCrypto{

    //Token id

    string  public name = "White Crypto";
    string  public symbol = "WHC";
    string public standard = "White Crypto v1.0";

    uint256 public totalSupply;

    event Transfer(

    address indexed _from,
    address indexed _to,
    uint256 _value
   );

    mapping(address => uint256) public balanceOf;
    

        function WhiteCryptoFunction (uint256 _initialSupply) public {
            balanceOf[msg.sender]= _initialSupply;
            totalSupply = _initialSupply;
    }

    //transfer functions
    function transfer(address _to,uint256 _value) public returns (bool success){
       require(balanceOf[msg.sender] >=_value);

       balanceOf[msg.sender] -=_value;
       balanceOf[_to] += _value;

       emit Transfer(msg.sender, _to, _value);

       return true;

        }

and JS code:

Source Link

contract testing returning total supply 0

So I am new to solidity and following this tutorial - Code Your Own Cryptocurrency.

And I am running some tests on truffle and I have two failed tests not sure why-

  1. The first is to make sure that the contract allocates the tokens to the correct admin address.
  2. To make a transfer function.

Here is my solidity -

contract WhiteCrypto{

//Token id

string  public name = "White Crypto";
string  public symbol = "WHC";
string public standard = "White Crypto v1.0";

uint256 public totalSupply;

event Transfer(

address indexed _from,
address indexed _to,
uint256 _value

);

mapping(address => uint256) public balanceOf;


    function WhiteCryptoFunction (uint256 _initialSupply) public {
        balanceOf[msg.sender]= _initialSupply;
        totalSupply = _initialSupply;
}

//transfer functions
function transfer(address _to,uint256 _value) public returns (bool success){
   require(balanceOf[msg.sender] >=_value);

   balanceOf[msg.sender] -=_value;
   balanceOf[_to] += _value;

   emit Transfer(msg.sender, _to, _value);

   return true;

    }

and my js-

var WhiteCrypto = artifacts.require("./WhiteCrypto.sol");

contract('WhiteCrypto', function(accounts){ var tokenInstance;

it('initializes the contract with the correct values', function(){
    return WhiteCrypto.deployed().then(function(instance){
        tokenInstance = instance;
        return tokenInstance.name();
    }).then(function(name){
    assert.equal(name, 'White Crypto', 'has the correct name');
    return tokenInstance.symbol();
    }).then(function(symbol){
        assert.equal(symbol, 'WHC' , 'has the correct symbol');
        return tokenInstance.standard();
    }).then(function(standard){
        assert.equal(standard, 'White Crypto v1.0', 'has the correct standard');
    });

})

it('allocates the total supply upon deployment', function() {

    return WhiteCrypto.deployed().then(function(instance){
        tokenInstance = instance;
        return tokenInstance.totalSupply();

    }).then(function(totalSupply) {
        assert.equal(totalSupply.toNumber(),360000000, 'sets the total supply to 360,000,000');
        return tokenInstance.balanceOf(accounts[0]);
    }).then(function(adminBalance){
        assert.equal(adminBalance.toNumber(),360000000, 'it allocates the initial supply to the admin balance')
    });

});

it('transfers token ownership', function(){
    return WhiteCrypto.deployed().then(function(instance){
        tokenInstance = instance;

        //will test the statement ( not having enough tokens) by transferring something larger than the senders balance
        return tokenInstance.transfer.call(accounts[1],99999999999999999999999999999999999);
    }).then(assert.fail).catch(function(error){
        assert(error.message.indexOf('revert') >=0, 'error message must contain revert');
        return tokenInstance.transfer.call(account[1],250000, {from: accounts[0]});
    }). then(function(success){
        assert.equal(success,true,'it returns true');
        return tokenInstance.transfer(accounts[1], 250000,{from: accounts[0]});
    }).then(function(receipt){
        assert.equal(receipt.logs.length, 1, 'triggers one event');
        assert.equal(receipt.logs[0].event, 'Transfer', 'should be the "Transfer" event');
        assert.equal(receipt.logs[0].args._from, accounts[0], 'logs the account the tokens are transferred from');
        assert.equal(receipt.logs[0].args._to, accounts[1], 'logs the account the tokens are transferred to');
        assert.equal(receipt.logs[0].args._value, 250000, 'logs the transfer amount');
        return tokenInstance.balanceOf(accounts[1]);
    }).then(function(balance){
        assert.equal(balance.toNumber(),250000,'adds the amount to the receiving account');
        return tokenInstance.blaanceOf(accounts[0]);
    }).then(function(balance){
        assert.equal(balance.toNumber(), 750000, 'dedcuts the amount from the sending account');
    });
});

});

and the errors -

Contract: WhiteCrypto
√ initializes the contract with the correct values (388ms)
1) allocates the total supply upon deployment
> No events were emitted
2) transfers token ownership
> No events were emitted

1 passing (763ms) 2 failing

  1. Contract: WhiteCrypto allocates the total supply upon deployment:

    it allocates the initial supply to the admin balance

    • expected - actual

    -0 +360000000

    at C:\Users\Foxy098\WhiteCrypto\test\WhiteCrypto.js:32:20 at processTicksAndRejections (node:internal/process/task_queues:96:5)

  2. Contract: WhiteCrypto transfers token ownership: AssertionError: error message must contain revert at C:\Users\Foxy098\WhiteCrypto\test\WhiteCrypto.js:44:13 at processTicksAndRejections (node:internal/process/task_queues:96:5)

Any ideas? Thanks