以太坊合约部署
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
console.log('Setting up ...'); const keythereum = require("keythereum"); const EthereumTx = require('ethereumjs-tx'); const fs = require('fs'); const solc = require('solc'); const Web3 = require('web3');const upAccountAddress = "0xb8133c92291a3a1218d7fa0fe83d28742e46756a" const web3 = new Web3(new Web3.providers.HttpProvider("http://192.168.1.245:8545")); //http://127.0.0.1:7545function balanceWei(address) {let balanceWei = web3.eth.getBalance(address).toNumber();return web3.fromWei(balanceWei, 'ether'); }console.log("upAccountAddress的余額: ", balanceWei(upAccountAddress))console.log('Reading Contract...', web3.version.network); const input = fs.readFileSync('GreenCredit.sol');console.log('Compiling Contract...'); const output = solc.compile(input.toString(), 1);const bytecode = output.contracts[':GreenCreditToken'].bytecode; const abi = output.contracts[':GreenCreditToken'].interface;// Contract object const GreenCreditTokenContract = web3.eth.contract(JSON.parse(abi));// Get contract data const contractData = GreenCreditTokenContract.new.getData({data: '0x' + bytecode });const privateKey = Buffer.from('dfdfa0c59c7a87326a3de18b8d847b3a9e4d62303160a61bdb4985c2e0cec8xx', 'hex');// web3.personal.unlockAccount(upAccountAddress, "123", (err, res) => { // if (err) { // console.log("unlockAccount: ",err) // } else {// if (res) { // console.log( " 解鎖成功:", res) //true// var accounts = web3.eth.accounts; // console.log(accounts);// console.log("新建合約 前 upAccountAddress余額 : ", balanceWei(upAccountAddress))// createContract(); //新建合約// } else { // console.log(account + "解鎖失敗 :", res) // } // }// });createContract(); //新建合約function createContract() {const gasPrice = web3.eth.gasPrice;console.log("68行 - gasPrice : ", gasPrice.toNumber())const gasPriceHex = web3.toHex(gasPrice);const gasLimitHex = web3.toHex(6000000);const nonce = web3.eth.getTransactionCount(upAccountAddress);console.log("72行 - nonce: ", nonce)const nonceHex = web3.toHex(nonce);const txParams = {nonce: nonceHex, gasPrice: gasPriceHex, gasLimit: gasLimitHex, data: contractData,from: upAccountAddress}//建立 tx transactionconst tx = new EthereumTx(txParams);//private key signtx.sign(privateKey);//Send tx transactionconst serializedTx = tx.serialize()var feeCost = tx.getUpfrontCost()tx.gas = feeCostconsole.log('Total Amount of wei needed (feeCost預):' + feeCost.toString(), tx.gas)web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function (err, hash) {if (err) {console.log("sendRawTransaction: ",err)} else {waitForTransactionReceipt(hash);}});}function waitForTransactionReceipt(hash) {console.log('waiting for contract to be mined');const receipt = web3.eth.getTransactionReceipt(hash);// If no receipt, try again in 1sif (receipt == null) {setTimeout(() => {waitForTransactionReceipt(hash);}, 1000);} else {// The transaction was mined, we can retrieve the contract addressconsole.log('新的合約部署成功了 contract address: ' + receipt.contractAddress);// 0x26468cbbee29291b0729f3ae23cfc3fb695dc695 (245機器上 )// testContract(receipt.contractAddress);console.log(web3.fromWei(web3.eth.getBalance(upAccountAddress), "ether").toNumber())} }// /* // *轉賬邏輯 // */// function fetchEther(sender, receiver) {// let log = { // time: (new Date).getTime(), // type: "succeed", // msg: "獲取ether成功" // }; // let str = JSON.stringify(log); // let data = Buffer.from(str).toString('hex'); // data = '0x' + data; // let value = web3.toHex(2000)// web3.eth.sendTransaction({ to: receiver, from: sender, data: data, value: value, }, function (err, transactionHash) { // if (err) { // console.log("sendTransaction's ",err) // } else { // waitForFetchEtherReceipt(transactionHash); // }// });// }// function waitForFetchEtherReceipt(hash) { // console.log('waiting for 轉賬'); // const receipt = web3.eth.getTransactionReceipt(hash);// if (receipt == null) { // setTimeout(() => { // waitForFetchEtherReceipt(hash); // }, 1000); // } else { // var balance = web3.eth.getBalance(receipt.to) // console.log('轉賬交易信息成功 ', balance.toNumber()); // } // }轉載于:https://my.oschina.net/u/3734107/blog/1828318
總結
- 上一篇: linux修改系统时间为北京时间(Cen
- 下一篇: 如何查看机器是否为vmware虚拟机