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:7545 function 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 transaction const tx = new EthereumTx(txParams); //private key sign tx.sign(privateKey); //Send tx transaction const serializedTx = tx.serialize() var feeCost = tx.getUpfrontCost() tx.gas = feeCost console.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 1s if (receipt == null) { setTimeout(() => { waitForTransactionReceipt(hash); }, 1000); } else { // The transaction was mined, we can retrieve the contract address console.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()); // } // }