Install truffle
Truffle is a development environment, testing framework and asset pipeline for blockchains.
npm install -g truffle
1. create a new folder, my folder name is token
mkdir token
cd token
2. Initialize a Truffle project.
truffle init
If you see the following result on the terminal, this step is successful:
✔ Preparing to download
✔ Downloading
✔ Cleaning up temporary files
✔ Setting up box
Unbox successful. Sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
Then you can see the file structure like this:
├── contracts
│ └── Migrations.sol
├── migrations
│ └── 1_initial_migration.js
├── test
└── truffle-config.js
In my editor,it shows like this:
3. Initialize a npm project.
npm init -y
4. Install openzeppelin-solidity.
These libraries will install not only the main libraries of our token but also libraries for ownership, safe math and many other facilities. It’s worth mentioning that these libraries have been reviewed to accomplish high standards of security so contracts that depend on them are less susceptible to hacking when used correctly.
Inside the folder, we import the libraries from OpenZeppelin with
npm install openzeppelin-solidity@1.12.0
The version 1.12.0 is what we need.
5. Install @truffle/hdwallet-provider.
To connect to RSK, we are going to modify the Truffle configuration. We are going to use a provider that allows us to connect to any network but unlocking an account locally. We are going to use @truffle/hdwallet-provider.(Node >= 7.6)
npm install @truffle/hdwallet-provider
6. Create an RSK account
To create our wallet we are going to use this web app: https://iancoleman.io/bip39/. This may not be used for any ‘real’ wallet; it’s not a secure way to generate a private key! We are going to use it just for learning the basics.
6.2 Create an account:
var HDWalletProvider = require('@truffle/hdwallet-provider')
var mnemonic = 'rocket fault regular ... YOUR MNEMONIC';// 12 key words we generated before
var publicNode = 'https://public-node.testnet.rsk.co:443';
module.exports = {
networks: {
testnet: {
provider: () =>
new HDWalletProvider(mnemonic, publicNode),
network_id: '*',
gas: 2500000,
gasPrice: 183000
}
},
compilers : {
solc: {
version: "0.5.0",
evmVersion: "byzantium"
}
}
}
truffle console --network testnet
truffle(testnet)>
What we are doing is telling truffle to connect to RSK public test node, and having control of your recently created account.
truffle(testnet)> var account = Object.keys(web3.currentProvider.wallets)[0]
undefined
truffle(testnet)> account
'0xf08f6c2eac2183dfc0a5910c58c186496f32498d'
This string in the last line is our address.
We mentioned before that RSK Testnet is a free network. To get funds to use in this network, we are going to use a faucet. A faucet is commonly a site where you enter your address and it automatically sends you some testnet funds for testing. Let’s go to RSK Faucet: https://faucet.testnet.rsk.co.
Steps of usage:
truffle(testnet)> web3.eth.getBalance(account, (err, res) => console.log(res))
The string displayed on my terminal is the funds I got:
'999969677083000'
7. Create a simple Token
pragma solidity ^0.4.17;
import 'openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
contract YourNewToken is StandardToken, Ownable {
string public name = 'CoinFabrik';
string public symbol = 'CF';
uint8 public decimals = 18;
uint public INITIAL_SUPPLY = 1000;
string Owner;
event Yes(string);
event No(string);
constructor() public {
totalSupply_ = INITIAL_SUPPLY * (10**uint(decimals));
balances[msg.sender] = totalSupply_;
}
function setON(string _n) public onlyOwner returns (bool) {
Owner = _n;
return true;
}
function getON() public view returns (string) {
return Owner;
}
function () public payable {
if (msg.value > 0) {
emit Yes('Thanks for donating R-BTC! :)');
} else {
emit No('Error 404: Function not found :P');
}
}
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
8. Let me explain the above code.
pragma solidity ^0.4.17;
import 'openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
To create our ERC20 Token, we will import 2 libraries from that repository: the StandardToken.sol, which has the main functionality of a Token and already imports a bunch of libraries more by itself such as SafeMath.sol, and Ownable.sol. These allows us to set owner control over functions in contracts.
contract YourNewToken is StandardToken, Ownable { }
After that, we have all the functions from those libraries and from their imported upward libraries.
string public name = 'YourNewToken';
string public symbol = 'YNT';
uint8 public decimals = 18;
uint public INITIAL_SUPPLY = 1000;
string Owner;
constructor() public {
totalSupply_ = INITIAL_SUPPLY * (10**uint(decimals));
balances[msg.sender] = totalSupply_;
}
totalSupply_ = INITIAL_SUPPLY * (10**uint(decimals));
balances[msg.sender] = totalSupply_;
function setON(string _n) public onlyOwner returns (bool) {
Owner = _n;
return true;
}
function getON() public view returns (string) {
return Owner;
}
function () public payable {
if (msg.value > 0) {
emit Yes('Thanks for donating R-BTC! :)');
} else {
emit No('Error 404: Function not found :P');
}
}
8.9 Finally, we add a destroyable capability to the contract in which the owner is the only one who can execute it.
function destroy() public onlyOwner {
selfdestruct(owner);
}
9. Creating the Migration
var YourNewToken = artifacts.require("./YourNewToken.sol");
module.exports = function(deployer) {
deployer.deploy(YourNewToken);
};
10.Deploy contract
truffle(testnet)> compile
Output:
Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Compiling ./contracts/YourNewTokens.sol
> Compiling openzeppelin-solidity/contracts/math/SafeMath.sol
> Compiling openzeppelin-solidity/contracts/ownership/Ownable.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/BasicToken.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/ERC20.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/ERC20Basic.sol
> Compiling openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol
> Artifacts written to /Users/huangxu/Project/RIL-DOCS/Smart Contract/build/contracts
> Compiled successfully using:
- solc: 0.4.24+commit.e67f0147.Emscripten.clang
truffle(testnet)> migrate --reset
Output:
Using network 'testnet'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0xf00d4ecf2b5752022384f7609fe991aa72dda00a0167a974e8c69864844ae270
Migrations: 0x1dc2550023bc8858a7e5521292356a3d42cdcbe9
Saving successful migration to network...
... 0x3e759e8ff8a7b8e47a441481fa5573ccf502b83f3d591ad3047e622af0f9169e
Saving artifacts...
Running migration: 2_deploy_token.js
Deploying YourNewToken...
... 0x300c8bb1e434e2aa4b13dcc76087d42fcbe0cb953989ca53a336c59298716433
YourNewToken: 0xc341678c01bcffa4f7362b2fceb23fbfd33373ea
Saving successful migration to network...
... 0x71771f7ee5d4e251e386979122bdda8728fa519d95a054572751bb10d40eb8c5
Saving artifacts...
truffle(testnet)> migrate --all --reset
The migration contract will be deployed first. Truffle gives us the transaction hashes of each operation, so we can check for details or logs later. Here is the complete output that I’ve received.
Go to top