Ethereum Petshop

After practising the first Dapp of Metacoin, this article will take through the process of building my second dapp---an adoption tracking system for a pet shop!

Olivia's Pet Shop aims to use ethereum as an efficient way to handle pet adoptions.? This dapp associates an Ethereum address with a pet to be adopted.

Steps covering:

1. Setting up the development environment

2. Creating a Truffle project using a Truffle Box

3. Writing the smart contract

4. Compiling and migrating the smart contract

5. Testing the smart contract

6. Creating a user interface to interact with the smart contract

7. Interacting with the dapp in a browser

Setting up the development environment

Envionment set up is same with metacoin, i am going to skip this step.

Create a Truffle Box called?pet-shop which includes the basic project structure as well as code for the user interface.?

Writing the smart contract

Writing the smart contract that acts as the back-end logic and storage.Create a new file named?Adoption.sol?in the?contracts/?directory. Inside of smart contract, functions of 'Adopting a pet','Retrieving the adopters' should be included.

Compiling and migrating the smart contract

The next steps are to compile and migrate it. Truffle has a built-in developer console called truffle develop, which generates a development blockchain that we can use to test deploy contracts.? Compile is to translate our human-readable solidity into somthing the EVM understand.

Migrate

Create a new file named?2_deploy_contracts.js?in the?migrations/?directory.

Before migrating our contract to the blockchain, we need to have a blockchain running. For my pratice, i am using Ganachecli?a personal blockchain for Ethereum development you can use to deploy contracts, develop applications, and run tests.This will generate a blockchain running locally on port 8545.

Then we migrate the contract to blockchain. You will find the blockchain address of the deployed contract.

Testing the smart contract

Create a new file named?TestAdoption.sol?in the?test/?directory.

You will see output similar like below

Creating a user interface to interact with the smart contract

The app structure is already there under /src directory. Then we need firstly to instantiating web3.

Open?/src/js/app.js?in a text editor and call the function?initWeb3(). The?web3 JavaScript library?interacts with the Ethereum blockchain. It can retrieve user accounts, send transactions, interact with smart contracts, and more. Then instantiating the contract, getting the adopted pets and updating the UI and handling the adopt() function.

Interacting with the dapp in a browser

To use the dapp, we need to install Metamask to interact with the dapp. Go to custom network and input your localhost ip address http://x.x.x.x:8545, import one of the private keys which generate from the ganachecli to create a local account. Later we will use the ether in this account to adopt pet.

Installing and configuring lite-server

npm run dev to start the local web server.The dev server will launch and automatically open a new browser tab containing your dapp.?

Olivia's petshop

To use the dapp, click the?Adopt?button on the pet of your choice. You will be automatically promted to approve the transaction by Metamask. Click Confirm to approve the transaction.

You'll see the button next to the adopted pet change to say "Success" and become disabled, just as we specified, because the pet has now been adopted.

Transaction Confirmed
Adoption success

Back to your account, you will see 100th has been deducted and also your transaction history each of which owns a hash which is uniquely to identify the transaction, as well as your total amount you have consumed.

You'll also see the same transaction listed in Ganachecli log.

For now, the dapp has been succesfully deployed into your local blockchain network, If you'd like to make your dapp live for others to use, it should be able to deploy to the Ropsten testnet.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末摊阀,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子绞旅,更是在濱河造成了極大的恐慌努隙,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,843評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡盗舰,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評論 3 392
  • 文/潘曉璐 我一進店門桂躏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人川陆,你說我怎么就攤上這事剂习。” “怎么了?”我有些...
    開封第一講書人閱讀 163,187評論 0 353
  • 文/不壞的土叔 我叫張陵鳞绕,是天一觀的道長失仁。 經(jīng)常有香客問我,道長们何,這世上最難降的妖魔是什么萄焦? 我笑而不...
    開封第一講書人閱讀 58,264評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮冤竹,結(jié)果婚禮上拂封,老公的妹妹穿的比我還像新娘。我一直安慰自己鹦蠕,他們只是感情好冒签,可當我...
    茶點故事閱讀 67,289評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著钟病,像睡著了一般萧恕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上肠阱,一...
    開封第一講書人閱讀 51,231評論 1 299
  • 那天票唆,我揣著相機與錄音,去河邊找鬼屹徘。 笑死走趋,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的缘回。 我是一名探鬼主播吆视,決...
    沈念sama閱讀 40,116評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼酥宴!你這毒婦竟也來了啦吧?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,945評論 0 275
  • 序言:老撾萬榮一對情侶失蹤拙寡,失蹤者是張志新(化名)和其女友劉穎授滓,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肆糕,經(jīng)...
    沈念sama閱讀 45,367評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡般堆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,581評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了诚啃。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淮摔。...
    茶點故事閱讀 39,754評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖始赎,靈堂內(nèi)的尸體忽然破棺而出和橙,到底是詐尸還是另有隱情仔燕,我是刑警寧澤,帶...
    沈念sama閱讀 35,458評論 5 344
  • 正文 年R本政府宣布魔招,位于F島的核電站晰搀,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏办斑。R本人自食惡果不足惜外恕,卻給世界環(huán)境...
    茶點故事閱讀 41,068評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望乡翅。 院中可真熱鬧鳞疲,春花似錦、人聲如沸峦朗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽波势。三九已至翎朱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間尺铣,已是汗流浹背拴曲。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凛忿,地道東北人澈灼。 一個月前我還...
    沈念sama閱讀 47,797評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像店溢,于是被迫代替她去往敵國和親叁熔。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,654評論 2 354

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,324評論 0 10
  • crystal_li閱讀 152評論 0 1
  • 你在的城市 天晴溫適 好想 此刻存在 卻知道 你并不期待
    粗腿妹閱讀 166評論 0 1
  • 傳銷床牧,是指組織者或者經(jīng)營者發(fā)展人員荣回,通過對被發(fā)展人員以其直接或者間接發(fā)展的人員數(shù)量或者銷售業(yè)績?yōu)橐罁?jù)計算和給付報酬...
    周柳生閱讀 462評論 0 0