How to test your self ethereum geth private poa truffle part2

every sec send transaction nonce++ https://medium.com/finnovate-io/how-do-i-sign-transactions-with-web3-f90a853904a2 https://ethereum.stackexchange.com/questions/60611/defining-the-transaction-object-for-offline-transaction-signing-using-web3-js-f https://github.com/ethereum/web3.js/issues/1430 https://programtheblockchain.com/posts/ signTransaction(tx, “0x”+privateKey) “0x” privatekey need becarful. --ws --wsaddr 0.0.0.0 --wsorigins "*" --wsapi "db,admin,debug,miner,eth,net,web3,network,txpool" var fs = require('fs'); var Web3 = require("web3"); var provider = new Web3.providers.HttpProvider("http://192.168.99.100:18545"); var wsprovider = new Web3.providers.WebsocketProvider("ws://192.168.99.100:18546"); //var web3 = new Web3(provider); var web3 = new Web3(wsprovider); console.log("before web set account: %o", web3.eth.defaultAccount); const privateKey = '138cbbfb21686ddc3b5ffeb2cfc83491175af68319977acb81d0ae93392c626c'; const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey); //web3.eth.accounts.wallet.add(account); //console.log("private key import to account: %o", account.address) web3.eth.defaultAccount = account.address; // try { // web3.eth.personal.unlockAccount(account.address, "").then(console.log('Account unlocked!')); // } catch (err) { // console.error('web3 unlockAccount Error: %o', err); // } var certjson; var certjsonpath = './Cert.json'; try { certjson = JSON.parse(fs.readFileSync(certjsonpath)); } catch (err) { console.error('readFileSync Error: %o', err); } var contractjson; var contractjsonpath = './MetaCoin.json'; try { contractjson = JSON.parse(fs.readFileSync(contractjsonpath)); } catch (err) { console.error('readFileSync Error: %o', err); } const getNonce = () => { return new Promise((resolve, reject) => { web3.eth.getTransactionCount(web3.eth.defaultAccount, 'pending', (error, result) => { if(error) reject(error); resolve(web3.utils.toHex(result)); }) }) } const getGasPrice = () => { return new Promise((resolve, reject) => { web3.eth.getGasPrice((error, result) => { if(error) reject(error); resolve(web3.utils.toHex(result)); }) }) } const createContract = (contractfrom_caller, nonce="") => { return new Promise((resolve, reject) => { const tx = { from: contractfrom_caller, gasPrice: web3.utils.toHex(web3.utils.toWei('2', 'gwei')), //20,000,000,000 gas: web3.utils.toHex('6819490'), //gasLimit: 9000000, value: '0x00', // web3.utils.toHex('0'), data: certjson.bytecode }; if(nonce!="") { tx.nonce = nonce; console.log("tx: %o", tx); } // const keystore = "Contents of keystore file"; // const decryptedAccount = web3.eth.accounts.decrypt(keystore, 'PASSWORD'); // web3.eth.accounts.signTransaction(rawTransaction, decryptedAccount.privateKey) // .then(console.log); // OR // decryptedAccount.signTransaction(tx) //const signPromise = web3.eth.accounts.signTransaction(tx, "0x"+privateKey); web3.eth.accounts.signTransaction(tx, "0x"+privateKey) .then(resolve) .catch(reject); }) } const getBalance = (contractAddr, coinOwnerAddr) => { return new Promise((resolve, reject) => { web3.eth.call({ to: contractAddr, data: metaCoinContract.methods.getBalance(coinOwnerAddr).encodeABI() }) .then(resolve) .catch(reject); // .then(o => { // resolve(o); // }) // .catch((error) => { // reject(error) // }); }) } const sendCoin = (contractAddr, sendCointoAddr, coinNumber, nonce="") => { return new Promise((resolve, reject) => { const tx = { from: contractfrom_caller, to: contractAddr, gasPrice: web3.utils.toHex(web3.utils.toWei('2', 'gwei')), //20,000,000,000 gas: web3.utils.toHex('181949'), //gasLimit: 9000000, value: '0x00', // web3.utils.toHex('0'), data: metaCoinContract.methods.sendCoin(sendCointoAddr, coinNumber).encodeABI() }; if(nonce!="") { tx.nonce = nonce; console.log("tx: %o", tx); } // const keystore = "Contents of keystore file"; // const decryptedAccount = web3.eth.accounts.decrypt(keystore, 'PASSWORD'); // web3.eth.accounts.signTransaction(rawTransaction, decryptedAccount.privateKey) // .then(console.log); // OR // decryptedAccount.signTransaction(tx) //const signPromise = web3.eth.accounts.signTransaction(tx, "0x"+privateKey); web3.eth.accounts.signTransaction(tx, "0x"+privateKey) .then(resolve) .catch(reject); }) } contractAddr = "0x3Da963B807bF892F7A10B61E9ffD830068f8C23d"; contractfrom_caller = "0xe79d33e93bd888b35e055f1a12d876354729037b"; coinOwnerAddr = "0xe79d33e93bd888b35e055f1a12d876354729037b" sendCointoAddr = "0x5921a4C1B13afbD4b61d63e9c7BD47741C47B176"; const metaCoinContract = new web3.eth.Contract(contractjson.abi, contractAddr); // getBalance // web3.eth.call({ // to: contractAddr, // data: metaCoinContract.methods.getBalance(coinOwnerAddr).encodeABI() // }) // .then(o => { // console.log("%s getBalance at %s contract: %o", coinOwnerAddr, contractAddr, web3.utils.hexToNumberString(o)) // }); getBalance(contractAddr, coinOwnerAddr) .then(o => { console.log("%s getBalance at %s contract: %o", coinOwnerAddr, contractAddr, web3.utils.hexToNumberString(o)) }) .catch((error) => { console.log("getBalance catch error: %o", error.message); }); // transfor coin // const signPromise = sendCoin(contractAddr, sendCointoAddr, 10); // signPromise.then((signedTx) => { // const sentTx = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction); // sentTx.on("receipt", receipt => { // console.log("receipt: %o", receipt); // console.log("receipt.contractAddress: %o", receipt.contractAddress); // }); // sentTx.on("error", error => { // console.log("sendSignedTransaction error: %o", error.message); // }) // sentTx.then(o => { // TxHash = o.transactionHash; // console.log("##### TxHash: %o", TxHash) // }); // }).catch((error) => { // console.log("sendSignedTransaction catch error: %o", error.message); // }); // getBalance coin sender & reciver getBalance(contractAddr, coinOwnerAddr) .then(o => { console.log("coinOwnerAddr %s getBalance at %s contract: %o", coinOwnerAddr, contractAddr, web3.utils.hexToNumberString(o)) }) .catch((error) => { console.log("getBalance catch error: %o", error.message); }); getBalance(contractAddr, sendCointoAddr) .then(o => { console.log("sendCointoAddr %s getBalance at %s contract: %o", coinOwnerAddr, contractAddr, web3.utils.hexToNumberString(o)) }) .catch((error) => { console.log("getBalance catch error: %o", error.message); }); Promise.all([getNonce(), getGasPrice()]).then(values => { var nonce = web3.utils.hexToNumberString(values[0]); console.log("Nonce: %o", nonce); createContract(contractAddr, web3.utils.toHex(nonce)).then((signedTx) => { console.log('createContract signedTx: %o', signedTx); const sentTx = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction); sentTx.then(o => { TxHash = o.transactionHash; console.log("##### createContract TxHash: %o", TxHash); web3.eth.getTransactionReceipt(TxHash).then(o=>{ console.log("check contractAddress object: %s", o.contractAddress); }); }) .catch((error) => { console.log("createContract sendSignedTransaction catch error: %o", error.message); }); }).catch((error) => { console.log("screateContract catch error: %o", error.message); }); nonce++; setInterval( () => { sendCoin(contractAddr, sendCointoAddr, 10, web3.utils.toHex(nonce)).then((signedTx) => { console.log('sendCoin 3 signedTx: %o', signedTx); const sentTx3 = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction); sentTx3.then(o => { TxHash = o.transactionHash; console.log("##### sendCoin 3 TxHash: %o", TxHash); }) .catch((error) => { console.log("sendCoin 3 catch error: %o", error.message); }); }).catch((error) => { console.log("sendSignedTransaction3 catch error: %o", error.message); }); nonce++; console.log("Nonce: %o", nonce); getBalance(contractAddr, coinOwnerAddr) .then(o => { console.log("coinOwnerAddr %s getBalance at %s contract: %o", coinOwnerAddr, contractAddr, web3.utils.hexToNumberString(o)) }) .catch((error) => { console.log("getBalance catch error: %o", error.message); }); getBalance(contractAddr, sendCointoAddr) .then(o => { console.log("sendCointoAddr %s getBalance at %s contract: %o", coinOwnerAddr, contractAddr, web3.utils.hexToNumberString(o)) }) .catch((error) => { console.log("getBalance catch error: %o", error.message); }); }, Math.random() * 1000); }) .then(console.log("Promise all transaction ok!")) .catch(e => console.log("promise all error: %o", e.message)) // sendCoin(contractAddr, sendCointoAddr, 10).then((signedTx) => { // console.log('sendCoin 2 signedTx: %o', signedTx); // const sentTx2 = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction); // sentTx2.then(o => { // TxHash = o.transactionHash; // console.log("##### sendCoin 2 TxHash: %o", TxHash) // }) // .catch((error) => { console.log("sendCoin 2 catch error: %o", error.message); }); // }).catch((error) => { console.log("sendSignedTransaction2 catch error: %o", error.message); }); // sendCoin(contractAddr, sendCointoAddr, 10).then((signedTx) => { // console.log('sendCoin 3 signedTx: %o', signedTx); // const sentTx3 = web3.eth.sendSignedTransaction(signedTx.raw || signedTx.rawTransaction); // sentTx3.then(o => { // TxHash = o.transactionHash; // console.log("##### sendCoin 3 TxHash: %o", TxHash) // }) // .catch((error) => { console.log("sendCoin 3 catch error: %o", error.message); }); // }).catch((error) => { console.log("sendSignedTransaction3 catch error: %o", error.message); }); //metaCoinContract.events.allEvents() metaCoinContract.events.Transfer() .on('data', function(event){ console.log("##### events data: %o", event); // same results as the optional callback above }) .on('changed', function(event){ console.log("##### events changed: %o", event); }) .on('error', console.error); metaCoinContract.getPastEvents('Transfer', function(error, events){ console.log("##### getPastEvents changed: %o", events); }) .then(function(events){ console.log("##### getPastEvents changed: %o", events) // same results as the optional callback above });

2020-01-02 · 5 min · 935 words · Me

How to test your self ethereum geth private poa truffle

Important!! web3.eth.sendTransaction({ data: bytecode “certjson.bytecode” or “certjson.bytecode.object” MUST Have “0x” at line First Character Use Truffle 1. run https://www.trufflesuite.com/docs/truffle/quickstart 1-1. run command mkdir testtruffle cd testtruffle npm i web3 truffle unbox metacoin truffle test ./test/TestMetaCoin.sol truffle test ./test/metacoin.js truffle compile 2. modify truffle-config.js Here use your private poa chain networkinfo. from address must be can used. genesis file can put this address. module.exports = { // Uncommenting the defaults below // provides for an easier quick-start with Ganache. // You can also follow this format for other networks; // see // for more details on how to specify configuration options! // networks: { development: { host: "192.168.99.100", port: 8545, network_id: "*", from: "0x5921a4c1b13afbd4b61d63e9c7bd47741c47b176" }, // test: { // host: "127.0.0.1", // port: 7545, // network_id: "*" // } } }; 3. modify migrations/1_initial_migration.js ...

2019-12-26 · 8 min · 1511 words · Me

smart contract storage struct 未初始化 直接使用問題

https://github.com/knownsec/Ethereum-Smart-Contracts-Security-CheckList/blob/master/%E4%BB%A5%E5%A4%AA%E5%9D%8A%E6%99%BA%E8%83%BD%E5%90%88%E7%BA%A6%E5%AE%A1%E8%AE%A1CheckList.md#11-%E6%9C%AA%E5%88%9D%E5%A7%8B%E5%8C%96%E7%9A%84%E5%82%A8%E5%AD%98%E6%8C%87%E9%92%88 https://www.chaindd.com/3102377.html https://blog.b9lab.com/storage-pointers-in-solidity-7dcfaa536089 https://medium.com/loom-network/ethereum-solidity-memory-vs-storage-how-to-initialize-an-array-inside-a-struct-184baf6aa2eb Use delete or new

2019-09-26 · 1 min · 8 words · Me

web3 deploy smart contract 1.2.1

Use https://remix.ethereum.org Get Contract json and data. In remix website, Compile finish. See Compliation Details. 1. ABI: click ABI buttion, get data. Use http://jsonviewer.stack.hu/ remove space 2. Compliation Details -> WEB3DEPLOY -> get data 3. cContract.options.from need put correct. var Web3 = require("web3"); var provider = new Web3.providers.HttpProvider("http://ganache:8545"); var web3 = new Web3(provider); //abi var cContract = new web3.eth.Contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]) //bytecode cContract.options.data = '0x608060405234801561001057600080fd5b5060bf8061001f6000396000f30060806040526004361060485763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166360fe47b18114604d5780636d4ce63c146064575b600080fd5b348015605857600080fd5b5060626004356088565b005b348015606f57600080fd5b506076608d565b60408051918252519081900360200190f35b600055565b600054905600a165627a7a72305820765480c908e5e28e3233e18bfa422944b42cad5fc08b77d7b22d3ddd7016a1380029' cContract.options.from = '0xoooxxxoooxxxoooxxxoooxxxoooxxx' cContract.options.gas = '4700000' console.log("web3 version: %o", web3.version) web3.eth.getAccounts().then(o=>{console.log(o)}) cContract.deploy().send() .on('error', (error) => { console.log("error: %o", error) }) .on('transactionHash', (transactionHash) => { console.log("transactionHash: %o", transactionHash) }) .on('receipt', (receipt) => { // receipt will contain deployed contract address console.log("receipt: %o", receipt) console.log("receipt.contractAddress: %o", receipt.contractAddress) }) .on('confirmation', (confirmationNumber, receipt) => { console.log("confirmationNumber: %o", confirmationNumber) console.log("confirmation receipt: %o", receipt) }) .then(function(newContractInstance){ if (typeof newContractInstance.options.address !== 'undefined') { console.log('Contract mined! address: ' + newContractInstance.options.address); }else{ console.log("newContractInstance.address is undefined!") } });

2019-09-26 · 1 min · 141 words · Me

ethereum smart contract 基本

uint256 => 2^256 byte32 => 2^8^32 (byte=2^8) or 32個byte 放32字元(ascii) 0x => HEX 16進位 0x64 => 6 * (16^1) + 4 * (16^0) = 100 0x0164 => 1 * (16^2) + 6 * (16^1) + 4 * (16^0) = 365 0x HEX 16進位 最小0 最大F 回到最上面byte32 轉成 0x HEX 為了避免太長,2^8 = 256 => 16^2 => 16位元顯示要兩位 0256 => 00FF || FF => 15 * (16^1) + 15 * (16^0) 所以 0x0164,通常都是兩位處理 0x 01 64 ...

2019-05-30 · 2 min · 408 words · Me