Skip to content

devonwesley/build-your-own-token

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

build-your-own-token

command list of how to build your very own token.

1. Install NodeJS

2. Npm packages to install:

$ npm i -g ganache-cli
$ npm i -g truffle

3. Create you project directory:

$ mkdir basic_token
$ cd basic_token

4. Scaffold a Truffle project:

truffle init

5. Creating the Token contract & Deploy script:

$ touch contracts/BasicToken.sol
$ touch migrations/2_deploy_basic_token.js

6. Configure network file, paste this file inside of the truffle.js file.

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*",
    }
  }
}

7. Run the Ganache blockchain.

$ ganache-cli -u 0

8. Paste the Basic Token contract into the BasicToken.sol file we created earlier.

pragma solidity ^0.4.24;

contract BasicToken {
  mapping(address => uint256) balances;
  string public constant NAME = "BasicToken";
  string public constant SYMBOL = "BTN";
  uint256 totalSupply_;
  
  event Transfer(address indexed from, address indexed to, uint256 values);

  constructor(uint256 INTIAL_SUPPLY) public {
    totalSupply_ = INTIAL_SUPPLY;
    balances[msg.sender] = INTIAL_SUPPLY;
    emit Transfer(0x0, msg.sender, INTIAL_SUPPLY);
  }

  function totalSupply() public view returns(uint256) {
    return totalSupply_;
  }

  function transfer(address _to, uint256 _value) public returns(bool) {
    require(_to != address(0), "Unauthorized");
    require(_value <= balances[msg.sender], "Unauthorized");

    balances[msg.sender] = balances[msg.sender] - _value;
    balances[_to] = balances[_to]   _value;
    emit Transfer(msg.sender, _to, _value);
    return true;
  }

  function balanceOf(address _owner) public view returns(uint balance) {
    return balances[_owner];
  }
}

9. Now we'll create our deployment script, paste the script into the 2_deploy_basic_token.js file.

const BasicToken = artifacts.require('./BasicToken.sol');
module.exports = deployer => deployer.deploy(BasicToken, 100000000);

10. Now run the migration command.

$ truffle migrate

11. Open up you Truffle console.

$ truffle console

12. Let get our deployed contract instance.

truffle(development)> BasicToken.deployed().then(t => token = t)

13. Contract interactions. token.totalSupply()

truffle(development)> token.totalSupply().then(s => s.toNumber())

14. Contract interactions. token.balanceOf(ADDRESS)

> token.balanceOf(web3.eth.accounts[0]).then(b => b.toNumber())

15. Contract interactions. token.transfer(ADDRESS, AMOUNT)

truffle(development)> token.transfer(web3.eth.accounts[1], 10000)

16. Check the balances after the transfer.

> token.balanceOf(web3.eth.accounts[0]).then(b => b.toNumber())
> token.balanceOf(web3.eth.accounts[1]).then(b => b.toNumber())

About

command list of how to build your very own token.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published