BUILD YOUR OWN CRYPTOCURRENCY - step by stepsteemCreated with Sketch.

in bitcoin •  6 years ago 

Background

Cryptocurrencies have been around for nearly 10 years, the first being the most famous - Bitcoin. Cryptocurrencies have the practicable to disrupt almost all industries where price is surpassed between two parties by means of a third, as they offer the ability to substitute luxurious relied on intermediaries with inexpensive software-based algorithms that put into effect the switch of price when predefined prerequisites are met. Banking fees are replaced through mining fees, which can minimize costs.

Ethereum shares many residences with Bitcoin, however also consists of a Turing-complete allotted laptop gadget known as the Ethereum Virtual Machine (EVM). Ethereum’s killer feature is clever contracts; smart contracts are stateful, and execution of features exposed by means of the contract modifications the kingdom of the contract. Value being transferred between two parties is an instance of such a change of state; on-line vote casting is another.

Smart contracts are typically written in a language referred to as Solidity, which is compiled to bytecode; the contract is then deployed to the EVM and its functionality can be invoked by means of everyone with an Ethereum address. Anyone can also install clever contracts to the EVM (at a cost), so the gadget is also permssionless (i.e. open to everyone).

ERC 20 tokens

In this record I am going to show how effortless it is to create a very primary cryptocurrency. We’ll call this the GPasToken. This will be an ERC 20 token written in Solidity and deployed to a neighborhood blockchain to make sure correct functionality. We’ll additionally show how to test our contract to make sure our implementation is correct. We will use numerous tools and libraries to make matters easier.

ERC 20 tokens (or cryptocurrencies) are nothing extra than smart contracts that conform to a predefined interface; most of the best-known Ethereum-based cryptocurrencies implement the ERC 20 interface.

This exercising used to be carried out on a Mac; some steps may additionally differ barely for Windows.

Getting Started
First lets create a folder for our new project:

mkdir GPasToken
cd GPasToken
Now we introduce a framework that permits us to build, take a look at and install our clever contracts, and a library to make sure our tokens are tightly closed and ERC 20 compliant. The framework we’ll use is called Truffle. We will also use a library with some predefined contract templates, called OpenZeppelin. We inherit from the contract most intently matching the problem at hand, in this case the introduction of new digital tokens is a solved trouble so we won’t reinvent any wheels here.

To install Truffle globally the use of npm, and setup your project, run these instructions in a terminal window:

npm i -g truffle
truffle init
npm init -y
npm i --save truffle
Finally install OpenZeppelin the use of npm:

npm deploy -E openzeppelin-solidity
Creating an ERC 20 Token
If you drill into node_modules/openzeppelin-solidity/token/ERC20 you will locate numerous templates you can use for your smart contract. These are all ERC 20 compliant, and the use of them is as simple as inheriting from your desired contract template. Lets create GPasToken.sol and add the required code for our clever contract.

Create the contract the usage of touch contracts/GPasToken.sol, and then open the resulting file and add this code…

pragma solidity ^0.4.24;
import "openzeppelin-solidity/contracts/token/ERC20/BasicToken.sol";
contract GPasToken is BasicToken {
two string public steady identify = "GPas Token";
string public steady symbol = "GPAS";
two uint8 public steady decimals = 0;
uint steady INITIAL_SUPPLY = 5000000;
two constructor () public {
two two totalSupply_ = INITIAL_SUPPLY;
two balances[msg.sender] = INITIAL_SUPPLY;
}
}
BasicToken (and the base contracts that it inherits from) implements most of the functionality we need, we just outline a name, symbol, supply, and number of decimals (I’ve chosen 0, however many ERC 20 tokens use 18 decimal places; this is a remember of non-public preference). An instance of the code being applied within BasicToken is given below:

contract BasicToken is ERC20Basic {
...
two mapping(address => uint256) inside balances;

function balanceOf(address _owner) public view returns (uint256) {
two return balances[_owner];
}
...
}
Code additionally exists within the base contracts to manage transfers and so on. If you’re drawing analogy with normal programming, imagine a single instance of a classification strolling on your computer, every invocation of a feature potentially altering the state of the object. In this case the contract is the type and each invocation of a characteristic is a transaction on the Ethereum blockchain (with the order of every call to a feature on the contract managed by using the blockchain ledger).

Testing

With our contract in region we add a check file the use of contact test/GPasTokenTest.sol, and some code to check our contract…

pragma solidity ^0.4.24;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import '../contracts/GPasToken.sol';
contract GPasTokenTest {
characteristic testEnsureTokenNameIsCorrect() public {
two two GPasToken token = GPasToken(DeployedAddresses.GPasToken());
two two string reminiscence anticipated = "GPas Token";
two string reminiscence real = token.name();
two two Assert.equal(actual, expected, "Ensure token name is correct");
two }
two characteristic testEnsureTotalSupplyIs5Million() public {
two two GPasToken token = GPasToken(DeployedAddresses.GPasToken());
two uint256 anticipated = 5000000;
uint256 true = token.totalSupply();
Assert.equal(actual, expected, "Ensure total furnish is correct");
}
feature testEnsureSymbolIsCorrect() public {
two GPasToken token = GPasToken(DeployedAddresses.GPasToken());
two string reminiscence expected = "GPAS";
two string memory true = token.symbol();
Assert.equal(actual, expected, "Ensure image is correct");
two }
two feature testEnsure0Decimals() public {
two two GPasToken token = GPasToken(DeployedAddresses.GPasToken());
uint256 anticipated = 0;
two two two uint256 proper = token.decimals();
two two Assert.equal(actual, expected, "Ensure zero decimals");
two }
}
Ensure that take a look at names are prefixed with the phrase test. The tests aren’t doing something in particular interesting, however have to accurately exhibit how to write a basic test.

Deployment
To make sure the contract is deployed, first run this command…

touch migrations/2_deploy_contracts.js
…then add this code to the resulting file…

var GPasToken = artifacts.require("./GPasToken.sol");
module.exports = function(deployer) {
deployer.deploy(GPasToken);
};
We’re now going to use some other tool from the Truffle suite known as ganache-cli, which is basically an Ethereum node going for walks on our personal machine; this lets in us to install clever contracts to our very own nearby blockchain so we don’t need to use real Ether! Ganache can be established globally with npm the usage of this command (a GUI tool is additionally handy if you prefer):

npm i -g ganache-cli
Run Ganache by using typing ganache-cli into the terminal. You should see it has generated 10 accounts, entire with private keys, each funded with 100 Ether.

We want to inform Truffle to set up our contract to our Ganache instance, and we can do this through enhancing truffle.js (or truffle-config.js for Windows) to point to the right location:

module.exports = {
networks:{
two development: {
two two two two host:'localhost',
two port: 8545,
two two two two network_id: "5777"
two }
}
};
To ensure the contract is compiled, run truffle compile from the root of the project, and then truffle migrate to deploy. Make a notice of the contract ID, the output will seem some thing like this:

GPasToken: 0xfe724d8e61ffec437b0432fbb365bf8e051fd2cd
The contract ID is the 0x… part.

You’ve now deployed your own digital tokens to your local Ganache instance.

View your tokens
You can view your tokens using any ERC 20 pockets that can be pointed to a localhost testnet. I use MyEtherWallet, which is an online service. In the pinnacle right of the homepage there should be a drop down the place you can pick out which network to connect to. Select Custom and enter localhost and the port of your Ganache instance (ganache-cli is 8545 by using default). Now import your wallet the usage of View Wallet Info and enter the personal key of the first address your Ganache instance generated. You can now add custom tokens (in this case use the image GPAS) and verify your balance.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Nicely presented. Thank you for the information. Up vote and follow done. Keep it up.

Thank for your support brother, even i give you same response......in return i also upvote+follow...