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
});