1. 程式人生 > >21.彩票專案真實環境部署流程

21.彩票專案真實環境部署流程

首先,之前做是在本地開啟一個區塊鏈,然後把智慧合約部署到本地區塊鏈上面,然後進行操作,但是,如果要想部署到真正的網際網路上,需要做一些處理,下面就開始做處理

1.下載真實環境所需要的環境HDWalletProvider

必須指定0.03版本,其他版本有坑

 npm install truffle-hdwallet-[email protected]0.0.3 --save

驗證安裝是否成功

2.然後註冊MetaMask服務商

註冊Infura,使用之前需要訪問令牌 Infura

https://infura.io/

3.獲取 url

#############

4.請求(修改deploy.js)

let Web3 = require('web3')
let {interface, bytecode} = require('./compile')
// web3.setProvider(new Web3.providers.HttpProvider('http://localhost:8545'))
//*************************************************************************************修改
var HDWalletProvider = require("truffle-hdwallet-provider");
var
mnemonic ="ice skirt toy confirm type soup similar gloom girl kidney say demise"; var provider = new HDWalletProvider(mnemonic,"https://ropsten.infura.io/v3/02cd1e3c295c425597fa105999493baa"); //*************************************************************************************修改 let web3 = new Web3(provider)
deploy = async () => { try { //獲取部署賬戶 accounts = await web3.eth.getAccounts() contractInstance = await new web3.eth.Contract(JSON.parse(interface)).deploy( { data: bytecode // arguments: ['']// 建構函式如果沒有引數,就一定不要傳 } ).send({ from: accounts[0], gas: '1000000' }) console.log('address :', contractInstance.options.address) } catch (e) { console.log("11111111111111111111111111111111",e) } } deploy()

5.部署

修改這三行程式碼,意思是把本地的巧克力區塊鏈替換成真實測試鏈上,其他暫時不用動

node deploy.js

然後如果部署成功之後,會出現一個地址address,把這個地址複製到

import web3 from '../utils/getWeb3'
//通過web3,找到我們已經部署好的合約例項
//ABI
//合約的地址
const abi = [ { "constant": true, "inputs": [], "name": "getBalance", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "winner", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "getPlayers", "outputs": [ { "name": "", "type": "address[]" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "round", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "getPlayersCount", "outputs": [ { "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "manager", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "name": "", "type": "uint256" } ], "name": "players", "outputs": [ { "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "play", "outputs": [], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [], "name": "tuijiang", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "inputs": [], "payable": true, "stateMutability": "payable", "type": "constructor" }, { "constant": false, "inputs": [], "name": "kaijiang", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" } ]
//************************地址寫到這裡面
const contractAddress = '0x143066ab1c9432FB8Cf61B48603E74e8B13ee52B'
//
let lotteryContract = new web3.eth.Contract(abi, contractAddress)
//
export default lotteryContract

5.執行測試

 node run start

6.開始gogogogo