Skip to main content

Respect Coin

Respect

I think it's time to talk about currency. Let's create a Respect Coin.

Step 1. Install OpenZeppelin library 

npm install zeppelin-solidity

When it comes to coins, I like to use some functions that smart people already implemented and other smart people verified. I think that Zeppelin is a nice collection of Solidity contracts that can be trusted. Let's use the StandardToken contract and use it as a parent class for our own RespectCoin contract.

Step 2. Create RespectCoin contract and store it in "contracts/RespectCoin.sol" file 

pragma solidity ^0.4.4;
import "../node_modules/zeppelin-solidity/contracts/token/StandardToken.sol";

/**
 * @title RespectCoin
 * @dev ERC20 Token example, where all tokens are pre-assigned to th
e creator.
 * Note they can later distribute these tokens as they wish using `transfer` and
 other
 * `StandardToken` functions.
 */
contract RespectCoin is StandardToken {

  string public constant name = "RespectCoin";
  string public constant symbol = "RESPECT";
  uint8 public constant decimals = 18;

  uint256 public constant INITIAL_SUPPLY = 10000 * (10 ** uint256(decimals));

  /**
   * @dev Constructor gives msg.sender all tokens
   */
  function RespectCoin() public {
    totalSupply = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
  }

}


Step 3. Update "2_deploy_contract.js" file in /migrations directory

var Hello1 = artifacts.require("./Hello.sol");
var Hello2 = artifacts.require("./HelloWithCounter.sol");
var Respect = artifacts.require("./RespectCoin.sol");


module.exports = function(deployer) {
  deployer.deploy(Hello1);
  deployer.deploy(Hello2);
  deployer.deploy(Respect);
};


Step 4. Start Test RPC

 testrpc


Step 5. Start Truffle Console  (in another window)

truffle console

Step 6. Run Migration

truffle(development)> truffle migrate --reset

Compiling ./contracts/RespectCoin.sol...
Compiling ./node_modules/zeppelin-solidity/contracts/math/SafeMath.sol...
Compiling ./node_modules/zeppelin-solidity/contracts/token/BasicToken.sol...
Compiling ./node_modules/zeppelin-solidity/contracts/token/ERC20.sol...
Compiling ./node_modules/zeppelin-solidity/contracts/token/ERC20Basic.sol...
Compiling ./node_modules/zeppelin-solidity/contracts/token/StandardToken.sol...
Writing artifacts to ./build/contracts

Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0xb6bbeaaf3649ecb38d548cba96f681682dad9e0225726924fbee3ce36eff94e3
  Migrations: 0xbe2af3c53f45bb63b7ef3201baedeb88c74be0f2
Saving successful migration to network...
  ... 0x2f20859fa0995f1c0d859fd9d66d72ed70aa3f81f51fa9acd0ed1118c096f1f2
Saving artifacts...
Running migration: 2_deploy_contracts.js
  Deploying Hello...
  ... 0x9c332c025894fd0fd4303d4d10988e54f4ca5ad6f32b64a05b0b45b6432b56ed
  Hello: 0x2b2b0051d64f1866cb6ec3364f586fa8649e8bcf
  Deploying HelloWithCounter...
  ... 0x848901eaf0df857871357f09927dd708bcc88a9204b99613f5e3251d155590c4
  HelloWithCounter: 0x6dc23d081bbcb3861b44fb4801a37286c4e1df18
  Deploying RespectCoin...
  ... 0xf120d49937fcb3b66b8a72947c2c5d6d34a535adbc17e3a743f06b2ebe5ef73e
  RespectCoin: 0xf57e3f9a16620b29097f6654d0208b07e035d626
Saving successful migration to network...
  ... 0xfd0bdd99055b9ebe4afe06426c07d65c91f0c38afb35390ae037c67fe565f29a
Saving artifacts...
truffle(development)>


Step 7. Check "respect" balance on the default  account

truffle(development)>  var RespectCoin = RespectCoin.at(RespectCoin.address)
undefined
truffle(development)> RespectCoin.balanceOf(web3.eth.accounts[0]).then(result => result.toString())


'1e+22'

That's a lot of respect!

Note that the result is a BigNumber JavaScript object as defined in https://github.com/MikeMcl/bignumber.js/.  Native Javascript numbers are not large enough.


Step 7. Give some "respect" 

We will give 100 respect coins to another account
truffle(development)> RespectCoin.transfer(web3.eth.accounts[1], 100)

{ tx: '0x6f8fd611b817324fd1f3094e764b5c3dcc97b54d534c258677a9c0f388444039',
  receipt:
   { transactionHash: '0x6f8fd611b817324fd1f3094e764b5c3dcc97b54d534c258677a9c0f388444039',
     transactionIndex: 0,
     blockHash: '0xd5e061b9c29d7e76d725b366d63aeece8b138e30b4666ae099cd619b826fa862',
     blockNumber: 14,
     gasUsed: 51541,
     cumulativeGasUsed: 51541,
     contractAddress: null,
     logs: [ [Object] ],
     status: 1 },
  logs:
   [ { logIndex: 0,
       transactionIndex: 0,
       transactionHash: '0x6f8fd611b817324fd1f3094e764b5c3dcc97b54d534c258677a9c0f388444039',
       blockHash: '0xd5e061b9c29d7e76d725b366d63aeece8b138e30b4666ae099cd619b826fa862',
       blockNumber: 14,
       address: '0x53544a253c7f9958b8fc3131c4e2134a8320dab4',
       type: 'mined',
       event: 'Transfer',
       args: [Object] } ] }


Step 7. Check that another account got "respect" coins

Note that we will use "promise" notation here since call will not be returned immediately, as data needs to be retrieved from the blockchain first. Also, data will be returned in the BigData object and toString() conversion is necessary here.  
truffle(development)> RespectCoin.balanceOf(web3.eth.accounts[1]).then(result => result.toString())
'100'

Comments

Popular posts from this blog

Facebook Friends Connect

Is a way to extend external sites to provide: FB identity FB friends (relationship) Feed to FB   Demo app at http://www.somethingtoputhere.com/therunaround User experience: login: js login method requiresession(): detects state of usr-FB relationship, log-in into FB if needed. If user has not authorised app - present app auth dialog. If already has session - just go init JS, require session   access FB data: - FBML on external site - use JS FBML parser and replace in browser DOM with FB data - JS based API to get FB data, REST API on the server site. Sessions work accross any API - only small subset of FBML us supported at the moment   adding social content: - use access API   Connections: app developers can suggest connections (using e-mail hash) user get connect request on FB Move content from external sites to FB app can register feed template (3 types of stories) call JS "showfeeddialog" to request user to confirm data sharing on FB. privacy protection: app ca

Posting to FaceBook feed using Graph API

Graph API was announced at F8 with a promise to dramatically simplify the FB API. I checked the read access over the new interface during the presentations and to my big surprise it worked flawlessly and from the first time. When I tried https://graph.facebook.com/facebook , JSON-formatted info about the FaceBook page was returned (as expected). Then I tried OAuth 2.0 way of accessing the API to post a message to the feed. And to my even bigger surprise it worked too! Here is what you need to do to access Graph API over OAuth: 1. Create a FB app, store app properties to a file: $appkey = '7925873fbfb5347e571744515a9d2804' ; $appsecret = 'THE SECRET' ; $canvas = 'http://apps.facebook.com/graphapi/' ; 2. Create a page that will prompt user the access permission (I am prompting for the publish_stream and offline_access permissions at the same time) //http://apps.facebook.com/graphapi/ require 'settings.php' ; $url = "https://graph.face

Ripple Baby Steps

Ripple and XPR What is Ripple for people in business and finance? Ripple is a currency exchange and payment settlement platform built using blockchain technology. Unlike Ethereum that is a more universal distributed application platform, Ripple solves a more narrow set of problems such as sending payments (similar to Bitcoin), currency remittance, payments for invoices, as well as number of other use cases related to payment in different currencies between parties that may or may not trust each other. Ripple is fast, scalable, and provides number of functions needed to support different payment scenarios. XPR is a native Ripple currency with a fixed and limited supply coins. 100 Billion XPR cryptocoins are in circulation today and the same number will be in circulation tomorrow.  What is Ripple for a software engineer?  For a software developer, Ripple is distributed ledger platform accessible trough API. There are number of libraries to accommodate different developers&#