1

I noticed some smart contracts have there name,symbol, and supply set up like this example #1 and setup a defined constructor. Notice, they did not fill in the "string public symbol", "string public name", or "unit decimals", or "unit total supply".

Example #1:

contract PucksToken is ERC20Interface, Owned, SafeMath {
    string public symbol;
    string public  name;
    uint8 public decimals;
    uint public _totalSupply;

    mapping(address => uint) balances;
    mapping(address => mapping(address => uint)) allowed;


    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------
    constructor() public {
        symbol = "0PUCKS";
        name = "0 Pucks Token";
        decimals = 18;
        _totalSupply = 100000000000000000000000000;
        balances[0x5A86f0cafD4ef3ba4f0344C138afcC84bd1ED222] = _totalSupply;
        emit Transfer(address(0), 0x5A86f0cafD4ef3ba4f0344C138afcC84bd1ED222, _totalSupply);

And some set the smart contract like this and use the "string public constant" to define. Example #2:

contract BTClite is StandardToken, Ownable {

  string public constant name = "BTC LITE";
  string public constant symbol = "BTCL";
  uint8 public constant decimals = 8;

  uint256 public constant SUPPLY_CAP = 21000000 * (10 ** uint256(decimals));

  address NULL_ADDRESS = address(0);

My question is when writing a smart contract, in example #1 is the defined "construct" code info sufficicent to define the terms of the contract and you do not have to fill in the information "string public constant"?

Basically why did example #1 not fill/input this information in below?

string public symbol;
    string public  name;
    uint8 public decimals;
    uint public _totalSupply;

or are they both the same?

0

If the values were the same the end result would be the same. It's just a matter of programming style.

If you give the values as parameters to the constructor you can possibly launch multiple contracts with different constructor parameters and the contracts become different. If the values are hardcoded inside the code there's no way to change them and you can't reuse the same code for different contracts.

So it all depends on what you want to achieve. Typically it's better to give such values as constructor parameters so you can possibly reuse the code elsewhere and you/or you don't have to decide the exact values before you deploy the contract.

0

The first variant is less efficient, than the second one. It consumes more gas on deployment. It consumes more gas on accessing symbol, name, and decimals. It has longer byte code. And, finally, it occupies more storage space.

Here are two contracts and some stats for them (Solidity 0.5.7 with optimizations enabled):

pragma solidity 0.5.7;

contract First {
    string public name;
    string public symbol;
    uint8 public decimals;

    constructor () public {
        name = "Foo";
        symbol = "Bar";
        decimals = 18;
    }
}

contract Second {
    string public constant name = "Foo";
    string public constant symbol = "Bar";
    uint8 public constant decimals = 18;

    constructor () public {
        // Do nothing
    }
}

First:

Deployment cost: 270297 gas units
Byte code size: 1722 bytes
Access cost for `name`: 1172 gas units
Access cost for `decimals`: 412 gas units
Storage used: 3 slots

Second:

Deployment cost: 147621 gas units
Byte code size: 764 bytes
Access cost for `name`: 587 gas units
Access cost for `decimals`: 207 gas units
Storage used: 0 slots

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.