版权声明:忠于祖国,忠于人民 https://blog.csdn.net/boss2967/article/details/83511863
首先,之前做是在本地开启一个区块链,然后把智能合约部署到本地区块链上面,然后进行操作,但是,如果要想部署到真正的互联网上,需要做一些处理,下面就开始做处理
1.下载真实环境所需要的环境HDWalletProvider
必须指定0.03版本,其他版本有坑
npm install truffle-hdwallet-provider@0.0.3 --save
验证安装是否成功
2.然后注册MetaMask服务商
注册Infura,使用之前需要访问令牌 Infura
https://infura.io/
选择ropsten,然后复制链接:ropsten.infura.io/v3/0…
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