0

I'm starting programming. I wanted to do my first contract and I assigned totalSupply to 1000000.

pragma solidity ^0.5.8;

/**
 * The DappToken
  contract Constructor
  Set the total number of tokens
  Read the total number of tokens
 */

  contract DappToken {

    uint256 public totalSupply; 

    function Supply () public {
    totalSupply = 1000000;  
    }
}

When I run test it doesn't pass. It says actual totalSupply is set to zero. Here is the code:

var DappToken = artifacts.require ("DappToken");

contract("DappToken", function(accounts) {
    var tokenInstance;

    it("sets the total supply upon deployment", function() {
        return DappToken.deployed().then(function(instance) {
            tokenInstance = instance; 
            return tokenInstance.totalSupply();
        })  .then(function(totalSupply) {
                assert.equal(totalSupply.toNumber(), 1000000, "sets the total supply to 1000000")
        });
    });
})

The result when I run truffle migrate is

Contract:DappToken
1) sets the total supply upon deployment 
> No events were emitted

and doesn't pass saying expected 1000000 actual 0

4
  • 2
    You haven't called function Supply(), what did you expect??? Jul 4 '19 at 8:03
  • 2
    From the comment in the code, I think there's some confusion about constructors. I'd suggest changing function Supply() for constructor(). Jul 4 '19 at 8:29
  • 1
    @RichardHorrocks: That was my initial thought, but I immediately noticed that the contract's name was DappToken, so there's a little more than "constructor-confusion" here (also since this dude could have simply initialized that variable at declaration). I believe that he/she wants to test the value after it has been set, in which case, an explicit function (and an explicit function-call) is required. Jul 4 '19 at 8:57
  • 1
    @goodvibration - Yeah, good point - that argument probably makes more sense. Either way, the value is never set :-) Jul 4 '19 at 9:25
1

Currently your code has a single public state variable totalSupply. To set the value you need to execute the Supply function (as per the comments you received).

Recommend reading the OpenZeppelin guide on Tokens https://docs.openzeppelin.org/v2.3.0/tokens

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.