Closed PHPJourney closed 4 years ago
Hi @PHPJourney,
Is your problem that you can't broadcast a tx or that you get a TypeError
? The issue's title indicates the latter, but you seem to have already realized how to import this library.
If you are trying to get help about how to sign and broadcast a tx you should provide more info about what you are trying to do.
I suspect your problem is related to this anyway:
var sha3 = web3.utils.sha3("transfer(address,uint256)");
const data =sha3.substr(0,10)+ web3.utils.padLeft(_to.substr(2), 64)+web3.utils.padLeft(_value.substr(2), 64);
hello @alcuadrado What I'm rather confused about TypeError is why many of the examples I've seen before use require ('ethereumjs-tx'). And I need to state the method name.
In addition, when signing broadcasting, I can normally get the signature result and broadcast, but after waiting for a period of time, the transaction ID record is lost.
Here's my actual signature process
const util = require('ethereumjs-util') const web3 = require('web3'); const EthereumTx = require('ethereumjs-tx').Transaction; function _gasPrice(){ return axios.get('/json/ethgasAPI.json').then((response) => { return response; }). catch((error)=> { console.log(error); }); }; function _getTransactionCount(address){ return axios.get('https://api-rinkeby.etherscan.io/api?module=proxy&action=eth_getTransactionCount&address='+ address +'&tag=latest&apikey=YourApiKeyToken').then((response) => { return response; }). catch((error)=> { console.log(error); }); }; export default { _toWei: function(_value){ return web3.utils.toWei(_value, 'Ether'); }, _toHex: function(_value){ console.info(_value); return web3.utils.toHex(_value); }, _sendTransaction(rawHash){ return axios.get('https://api-rinkeby.etherscan.io/api?module=proxy&action=eth_sendRawTransaction&hex='+rawHash+'&apikey=YourApiKeyToken').then((res) => { return res; }).catch((err)=> { console.info(err); }); }, _send: function (_from, _to, _privatekey, _value, _contract){ var rawTx; _privatekey = new Buffer(_privatekey, 'hex'); var gasPrice, predictTable,nonce; return _gasPrice().then((res)=> { gasPrice = res.data; var sha3 = web3.utils.sha3("transfer(address,uint256)"); const data =sha3.substr(0,10)+ web3.utils.padLeft(_to.substr(2), 64)+ web3.utils.padLeft(_value.substr(2), 64); console.info(data, "data"); return _getTransactionCount(_from).then((rest)=> { nonce = parseInt(rest.data.result) + 1; if(_contract !== null){ _value = '0x00'; console.warn('nonce', nonce, _value); rawTx = { nonce: '0x'+nonce, gas: this._toHex(60000), gasLimit: this._toHex(1e8parseInt(gasPrice.fast)), gasPrice: this._toHex(1e8parseInt(gasPrice.average)), from: _from, to: _contract, value: _value, chainId:4, timestamp: new Date().getTime(), data: data }; }else { rawTx = { nonce:'0x'+nonce, gas: this._toHex(60000), gasLimit: this._toHex(gasPrice.fast1e8), gasPrice: this._toHex(gasPrice.average1e8), from: _from, to: _to, value: _value, chainId:4, data: '' }; console.log("rawTx ", rawTx); } const tx = new EthereumTx(rawTx, {chain:'rinkeby',hardfork:'byzantium'}); tx.sign(_privatekey); const serializedTx = tx.serialize(); return this._sendTransaction('0x'+serializedTx.toString('hex')).then((req)=>{ return { hex:'0x'+serializedTx.toString('hex'), data:req.data }; }); }); }); }, }
Call
this._send(from, to, privatekey, this._toHex(this._toWei(value)), contract)
my nonce is Wrong
This packages version 2.1.1 const EthereumTx = require('ethereumjs-tx'); new EthereumTx(rawTx, {chain: "rinkeby", hardfork:"byzantium"});
I'm initialize this Object get this wrong.
TypeError: EthereumTx is not a constructor
at the same time. I const EthereumTx from require('ethereumjs-tx').Transaction.
call normal.
However, the signature result cannot be broadcasted to the chain normally, and the signature result can be broadcast directly, and the success prompt is returned.
why!!??
My Signature fragment