I'm trying to send a transaction that writes somesthing on ethereum(kovan) but it seems my transaction is not mined and probably not even send to the network? All my view requests to ethereum work, so my guess is that there is a problem with the signing of my transaction? After firing my script I receive a txHash but I can't find that txHash on kovan.etherscan.io. I thought I might put in a too low gas price and that my transaction is stuck in the mining pool, but I can't even find my transaction in the mining pool so I guess my script didn't even send it correctly?
const Web3 = require('web3');
var Tx = require('ethereumjs-tx').Transaction
const web3 = new Web3(new Web3.providers.HttpProvider("https://kovan.infura.io/v3/-XXX-"));
var contractAddress = "0x07026847C635D66d243971838E69365c9675Cd68";
var abi = -XXX-;
var contract = new web3.eth.Contract(abi, contractAddress);
var account = "-XXX-";
const privateKey = Buffer.from('-XXX-', 'hex');
const data = contract.methods.createLocation("test-location7").encodeABI()
contract.methods.viewNewestComment().call().then(console.log);
web3.eth.getTransactionCount(account, (err, txCount) => {
console.log(txCount);
const txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(200000),
gasPrice: web3.utils.toHex(web3.utils.toWei('9', 'gwei')),
to: contractAddress,
data: data
}
const tx = new Tx(txObject)
tx.sign(privateKey)
const serializedTx = tx.serialize()
const raw = '0x' + serializedTx.toString('hex')
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('err:', err, 'txHash:', txHash)
})
})
So what my script should do after firing it is: using the createLocation methods and "write" a new location name on the blockchain, signing the transaction and finally give me the txHash. But even though I get a txHash I don't think the transation was successful. After 750 seconds I get the following error message in my console: "Error: Transaction was not mined within 750 seconds, please make sure your transaction was properly sent."
I deployed my contract with remix.ethereum.org. And when I use the createLocation method in remix it works! Metamask ask me to sign the transaction and after that I can find my transaction on etherscan.
pragma solidity ^0.5.12;
contract NewContract {
struct Location {
string JSON;
uint time;
string comment;
}
Location[] locations;
function createLocation(string memory _json) public {
locations.push(Location(_json, now, 'no comment'));
}
function addComment(string memory _comment, uint _id) public {
locations[_id].comment = _comment;
}
function addCommentToNewest(string memory _comment) public {
uint _id = locations.length - 1;
locations[_id].comment = _comment;
}
function viewLocation(uint _id) public view returns(string memory) {
string memory result = locations[_id].JSON;
return result;
}
function viewTimestamp(uint _id) public view returns(uint) {
uint result = locations[_id].time;
return result;
}
function viewComment(uint _id) public view returns(string memory) {
string memory result = locations[_id].comment;
return result;
}
function viewEverything(uint _id) public view returns(string memory, uint, string memory) {
string memory result1 = locations[_id].JSON;
uint result2 = locations[_id].time;
string memory result3 = locations[_id].comment;
return (result1, result2, result3);
}
function viewNewestLocation() public view returns(string memory) {
uint _id = locations.length - 1;
string memory result = locations[_id].JSON;
return result;
}
function viewNewestTimestamp() public view returns(uint) {
uint _id = locations.length - 1;
uint result = locations[_id].time;
return result;
}
function viewNewestComment() public view returns(string memory) {
uint _id = locations.length - 1;
string memory result = locations[_id].comment;
return result;
}
function viewNewestEverything() public view returns(string memory, uint, string memory) {
uint _id = locations.length - 1;
string memory result1 = locations[_id].JSON;
uint result2 = locations[_id].time;
string memory result3 = locations[_id].comment;
return (result1, result2, result3);
}
}
This is my smart contract btw. I actually want to create new Locations names as strings and want to add comments after they are created.
I hope you can help me :)