0

Please help !

When i try to run test from truffle, i got this problem, here my test js code:

var DappToken = artifacts.require("./DappToken.sol")

contract(DappToken, function(accounts) {
    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");
            });
        });
    })

and here my contract code:

pragma solidity ^0.5.0;

contract DappToken {
    uint256 public totalSupply;
    function DToken () public {
        totalSupply = 1000000;
    }   
}

and here the result of the test:

test contract truffle

PS: for more information, I follow the instruction video "Code Your Own Cryptocurrency on Ethereum (Full)" from Dapp University chanel on youtube , at 40:03

1

Your error is here.

contract DappToken {
    uint256 public totalSupply;
    function DToken () public { <----
        totalSupply = 1000000;
    }   
}

I suppose you meant it to be the constructor. Since the version 0.4.22 of Solidity, you have to design the constructor with a special keyword constructor. So it becomes like that :

contract DappToken {
    uint256 public totalSupply;
     constructor() public {
        totalSupply = 1000000;
    }   
}
  • may i ask, Is the constructor in solidity as same meaning as constructor in javascript ? – johnho.91 Apr 15 at 9:20
  • What do you mean? – Eli Drion Apr 15 at 9:29
  • Sorry, I just a newbie and not native english speaker, i don't know how to explain my problem clearly. I got another problem and the answer like this again, so i think I lack some basic knowledge about programming language to fully understand this line [i] "Since the version 0.4.22 of Solidity, you have to design the constructor with a special keyword constructor" [/i] – johnho.91 Apr 15 at 9:41
  • It means you have to use the word constructor as the function's name for the constructor, just like in the example I put in my answer. – Eli Drion Apr 15 at 10:21

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.