日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

以太坊合约部署

發布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 以太坊合约部署 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘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

總結

以上是生活随笔為你收集整理的以太坊合约部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。