Cryptozombie Part6

In this article, I will show you how to interact with your smart contract and build a basic front-end for your DApp using a javascript library from Ethereum Foundation called Web3.js.

1. Web3.js

Ethereum network is made up of nodes, with each containing a copy of the blockchain. When you want to call a function on a smart contract, you need to query one of these nodes and tell it:

1)The address of the smart contract

2)The function you want to call, and

3)The variables you want to pass to that function.

Ethereum nodes only speak a language called JSON-RPC, which isn't very human-readable. A query to tell the node you want to call a function on a contract looks something like this:

{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567","gas":"0x76c0","gasPrice":"0x9184e72a000","value":"0x9184e72a","data":"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"}],"id":1}

Web3.js hides these nasty queries below the surface, so you only need to interact with a convenient and easily readable JavaScript interface.

Instead of needing to construct the above query, calling a function in your code will look something like this:

CryptoZombies.methods.createRandomZombie("Vitalik Nakamoto ??")

? .send({ from: "0xb60e8dd61c5d32be8058bb8eb970870f07233155", gas: "3000000" })

Firstly, add web3.js to your project

<script language="javascript" type="text/javascript" src="web3.min.js"></script>

2. Web3 Provider

Before initialized Web3 and talking to the blockchain. we need aWeb3 Provider.

As Ethereum is made up of nodes that all share a copy of the same data. Setting a Web3 Provider in Web3.js tells our code which node we should be talking to handle our reads and writes. It's kind of like setting the URL of the remote web server for your API calls in a traditional web app.

You could host your own Ethereum node as a provider. However, there's a third-party service that makes your life easier so you don't need to maintain your own Ethereum node in order to provide a DApp for your users — Infura.

Infura

Infura is a service that maintains a set of Ethereum nodes with a caching layer for fast reads, which you can access for free through their API. Using Infura as a provider, you can reliably send and receive messages to/from the Ethereum blockchain without needing to set up and maintain your own node.

Metamask

Metamask is a browser extension for Chrome and Firefox that lets users securely manage their Ethereum accounts and private keys, and use these accounts to interact with websites that are using Web3.js.

Note: Metamask uses Infura's servers under the hood as a web3 provider.

Here's a list of template code provided by Metamask for how we can detect to see if the user has Metamask installed, and if not tell them they'll need to install it to use our app:

3. After initializing Web3.js with MetaMask's Web3 provider, we will set it up to talk to smart contract.

Web3.js needs 2 things to talk to your contract: 1). Address; 2). ABI.

Contract Address

After deploy contract, it gets a fixed address on Ethereum where it will live forever. Copy this address after deploying in order to talk to your smart contract.

Contract ABI

The other thing Web3.js will need to talk to your contract is its ABI.

ABI stands for Application Binary Interface. Basically it's a representation of your contracts' methods in JSON format that tells Web3.js how to format function calls in a way your contract will understand.

When you compile your contract to deploy to Ethereum, the Solidity compiler will give you the ABI, so you'll need to copy and save this in addition to the contract address.

Instantiating a Web3.js Contract

Once have contract's address and ABI, we can instantiate it in Web3 as follows:

//Instantiate myContract

var myContract = new web3js.eth.Contract(myABI,myContractAddress);

4. Calling Contract Functions

Web3.js has two methods to call functions on contract: call and send.

Call

call is used for view and pure functions. It only runs on the local node, and won't create a transaction on the blockchain.

Using Web3.js, you would call a function named myMethodwith the parameter 123 as follows:

myContract.methods.myMethod(123).call();

Send

send will create a transaction and change data on the blockchain. You'll need to use send for any functions that aren't view or pure.

Note: sending a transaction will require the user to pay gas, and will pop up their Metamask to prompt them to sign a transaction. When we use Metamask as our web3 provider, this all happens automatically when we call send(), and we don't need to do anything special in our code.

Using Web3.js, you would send a transaction calling a function named myMethod with the parameter 123 as follows:

myContract.methods.myMethod(123).send();

?著作權(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)容