Cryptozombie Part6

Cryptozombie Part6_第1张图片

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

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:

Cryptozombie Part6_第2张图片

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();

Cryptozombie Part6_第3张图片

你可能感兴趣的:(Cryptozombie Part6)