Skip to content

Instantly share code, notes, and snippets.

@christianhent
Last active August 11, 2021 11:29
Show Gist options
  • Save christianhent/7028b3218d2562f3a067a4484240b1e8 to your computer and use it in GitHub Desktop.
Save christianhent/7028b3218d2562f3a067a4484240b1e8 to your computer and use it in GitHub Desktop.
Ganache & Truffle Console cheat sheet

Ganache & Truffle Console cheat sheet

ERC20 Token contract

YourToken.sol

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";


contract YourToken is ERC20, Ownable, ERC20Burnable {

  mapping(address => uint256) balances;

  mapping(address => mapping (address => uint256)) allowed;

  constructor(uint256 initialSupply) ERC20("YourToken", "YTK") {
    _mint(msg.sender, initialSupply);
  }

  function decimals() public view virtual override returns (uint8) {
    return 2;
  }

  function mint(address to, uint256 amount) public onlyOwner {
    _mintMinerReward();
    _mint(to, amount);
  }

  function _mintMinerReward() internal {
    _mint(0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0, 1); // Replace address 
  }

  function _transfer(address from, address to, uint256 value) internal override {
    _mintMinerReward();
    super._transfer(from, to, value);
  }
}

2_deploy_contracts.js

constYourToken	= artifacts.require('./YourToken.sol');
module.exports 	= async function (deployer) {
   deployer.deploy(YourToken, 10000);
};

ganache-cli

command line version of Ganache

ganache-cli -q -d -a 5 -e 1000 -h 192.168.178.21 -p 7545 --db /path/to/save/the/chain/db/

truffle console

get instance

let i = await YourToken.deployed()

get accounts

let a = await web3.eth.getAccounts()

a | a[0] | a[1] | a[2] | ...

mint tokens to any address / account

 i.mint("0x90F8bf6A479f320ead074411a4B0e79...", 10000)
 
 i.mint(a[1],1000)

burn tokens from the caller/owner.

i.burn(10000)

transfer tokens from account to account, OVERRIDEN

i.transfer(a[0], 100, {from: a[2]}) //from owner to account

i.transfer(a[2], 100, {from: a[0]}) //from account to owner

i.transfer(a[2], 100, {from: a[3]}) //from account to account

transfer tokens from address to address, OVERRIDEN

i.transfer('0xd03ea8624C8C59872350489...', 100, {from: '0x90F8bf6A479f320ead074411a4B...'})

get balance of any address/account

i.balanceOf("0x85BA6f94F4Bc5734228032...").then(function(balance) { balanceInstance = balance})

i.balanceOf(a[0]).then(function(balance) { balanceInstance = balance})

balanceInstance.words[0]

get total token supply

i.totalSupply().then(function(balance) { balanceInstance = balance})

balanceInstance.words[0]

notes

Truffle v5.4.2 (core: 5.4.2) Solidity - 0.8.0 (solc-js) Node v12.18.2 Web3.js v1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment