Created
March 15, 2017 04:19
-
-
Save cicorias/c049f56ae7a7c828a7a0558435af11f0 to your computer and use it in GitHub Desktop.
Send raw ethereum tx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Skip to content | |
This repository | |
Pull requests | |
Issues | |
Gist | |
@cicorias | |
Watch 81 | |
Unstar 610 | |
Fork 264 ethereum/web3.js | |
Code Issues 144 Pull requests 10 Projects 1 Wiki Pulse Graphs | |
New issue | |
How to create contract with sendRawTransaction #416 | |
Open akktis opened this issue on Mar 23, 2016 · 2 comments | |
Assignees | |
No one assigned | |
Labels | |
None yet | |
Projects | |
None yet | |
Milestone | |
No milestone | |
Notifications | |
Unsubscribe | |
You’re receiving notifications because you’re subscribed to this thread. | |
3 participants | |
@akktis @simondlr @jonathancross | |
@akktis | |
akktis commented on Mar 23, 2016 | |
Hello Guys, I'm trying to create a contract with sendRawTransaction | |
I firstly try to use web3 + hooked-web3-provider + ethereumjs-accounts | |
on nodejs but i get some Error Invalid sender and some are OK but the transaction stuck in queued txpool, and sometime all it's ok, is just a bit crazy :s | |
so after that i decide to create the contract by my self. | |
so i directly inspire from : web3.js/lib/web3/contract.js to write de code behind. | |
but the result is that ask for much more gas | |
and when i compare my previous contract and the new one there are completely different | |
and the new one doesn't work. | |
so i think i'm very close to solution. | |
this is my code | |
// Dependencies | |
var Web3 = require('web3'); | |
var Tx = require('ethereumjs-tx'); | |
var ContractFactory = require('web3/lib/web3/contract'); | |
var coder = require('web3/lib/solidity/coder'); | |
var SolidityFunction = require('web3/lib/web3/function'); | |
var SolidityEvent = require('web3/lib/web3/event'); | |
var AllEvents = require('web3/lib/web3/allevents'); | |
var Contract = function (eth, abi, address) { | |
this._eth = eth; | |
this.transactionHash = null; | |
this.address = address; | |
this.abi = abi; | |
}; | |
var encodeConstructorParams = function (abi, params) { | |
return abi.filter(function (json) { | |
return json.type === 'constructor' && json.inputs.length === params.length; | |
}).map(function (json) { | |
return json.inputs.map(function (input) { | |
return input.type; | |
}); | |
}).map(function (types) { | |
return coder.encodeParams(types, params); | |
})[0] || ''; | |
}; | |
var addFunctionsToContract = function (contract) { | |
contract.abi.filter(function (json) { | |
return json.type === 'function'; | |
}).map(function (json) { | |
return new SolidityFunction(contract._eth, json, contract.address); | |
}).forEach(function (f) { | |
f.attachToContract(contract); | |
}); | |
}; | |
var addEventsToContract = function (contract) { | |
var events = contract.abi.filter(function (json) { | |
return json.type === 'event'; | |
}); | |
var All = new AllEvents(contract._eth._requestManager, events, contract.address); | |
All.attachToContract(contract); | |
events.map(function (json) { | |
return new SolidityEvent(contract._eth._requestManager, json, contract.address); | |
}).forEach(function (e) { | |
e.attachToContract(contract); | |
}); | |
}; | |
var checkForContractAddress = function(contract, callback){ | |
console.log('checkForContractAddress'); | |
var count = 0, | |
callbackFired = false; | |
// wait for receipt | |
var filter = contract._eth.filter('latest', function(e){ | |
if (!e && !callbackFired) { | |
count++; | |
// stop watching after 50 blocks (timeout) | |
if (count > 50) { | |
filter.stopWatching(); | |
callbackFired = true; | |
if (callback) | |
callback(new Error('Contract transaction couldn\'t be found after 50 blocks')); | |
else | |
throw new Error('Contract transaction couldn\'t be found after 50 blocks'); | |
} else { | |
contract._eth.getTransactionReceipt(contract.transactionHash, function(e, receipt){ | |
if(receipt && !callbackFired) { | |
contract._eth.getCode(receipt.contractAddress, function(e, code){ | |
/*jshint maxcomplexity: 6 */ | |
if(callbackFired || !code) | |
return; | |
filter.stopWatching(); | |
callbackFired = true; | |
console.log('CODE', code); | |
if(code.length > 2) { | |
// console.log('Contract code deployed!'); | |
contract.address = receipt.contractAddress; | |
// attach events and methods again after we have | |
addFunctionsToContract(contract); | |
addEventsToContract(contract); | |
// call callback for the second time | |
if(callback) | |
callback(null, contract); | |
} else { | |
if(callback) | |
callback(new Error('The contract code couldn\'t be stored, please check your gas amount.')); | |
else | |
throw new Error('The contract code couldn\'t be stored, please check your gas amount.'); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
}); | |
}; | |
// Initialize connection | |
var web3 = new Web3(); | |
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545")); | |
// Variables | |
var account = { address: '0x1502c42992450de0a5381692f84080000f043495', | |
encrypted: true, | |
locked: false, | |
hash: '0e9de6cacecdc7ce22539401729d9a2bfe8a7a1410a19b9ec89195c6c1b6af74', | |
private: 'a055f1bd991d2c0f5a21578ffb54b820e7b3846078da73cc3d2d0bf8e21cc9d7', | |
public: '015f834aa98342b930be7d4076c23da88583d389bfe931c51f198450f73ac7286c61a3f36fd87406200d528c3502d02c9510239b3b3d7279627db63433af8b33' } | |
var current_poll = { | |
"__v" : 0, | |
"_id" : "56f071358a55eb1a326457fa", | |
"address" : "0x1502c42992450de0f5381692f84080000f043195", | |
"contract_address" : "0x8daff4ba058daea9e92a699ed66a7f4ad13b534c", | |
"createdDate" : new Date("2016-03-21T22:35:15.675Z"), | |
"isActive" : true, | |
"isCreated" : true, | |
"poll" : { | |
"description" : "test 2eme vote", | |
"limit_days" : 14, | |
"limit_hours" : 1, | |
"vote_limit" : 0, | |
"vote_public" : true, | |
"name" : "test 2eme vote", | |
"region" : "56efcc28d20e4a2b207197e1", | |
"options" : [ | |
"ops 1", | |
"ops 2", | |
"ops 3" | |
] | |
} | |
} | |
var abi = [{ | |
"constant":false, | |
"inputs":[], | |
"name":"endPoll", | |
"outputs":[{ | |
"name":"", | |
"type":"bool" | |
}], | |
"type":"function" | |
},{ | |
"constant":true, | |
"inputs":[], | |
"name":"p", | |
"outputs":[{ | |
"name":"owner", | |
"type":"address" | |
},{ | |
"name":"title", | |
"type":"string" | |
},{ | |
"name":"votelimit", | |
"type":"uint256" | |
},{ | |
"name":"options", | |
"type":"string" | |
},{ | |
"name":"deadline", | |
"type":"uint256" | |
},{ | |
"name":"status", | |
"type":"bool" | |
},{ | |
"name":"numVotes", | |
"type":"uint256" | |
}], | |
"type":"function" | |
},{ | |
"constant":false, | |
"inputs":[{ | |
"name":"choice", | |
"type":"string" | |
}], | |
"name":"vote", | |
"outputs":[{ | |
"name":"", | |
"type":"bool" | |
}], | |
"type":"function" | |
},{ | |
"inputs":[{ | |
"name":"_options", | |
"type":"string" | |
},{ | |
"name":"_title", | |
"type":"string" | |
},{ | |
"name":"_votelimit", | |
"type":"uint256" | |
},{ | |
"name":"_deadline", | |
"type":"uint256" | |
}], | |
"type":"constructor" | |
},{ | |
"anonymous":false, | |
"inputs":[{ | |
"indexed":false, | |
"name":"votechoice", | |
"type":"string" | |
}], | |
"name":"NewVote", | |
"type":"event" | |
}]; | |
var cur_date = (new Date()).getTime(); | |
var days = (current_poll.poll.limit_days) * 86400000; | |
var hours = (current_poll.poll.limit_hours) * 3600000; | |
var _deadline = cur_date + days + hours; | |
var _options = JSON.stringify(current_poll.poll.options); | |
var _votelimit = current_poll.poll.vote_limit; | |
var _title = current_poll.poll.name; | |
var contract = new Contract(web3.eth, abi); | |
var gasPrice = web3.eth.gasPrice; | |
var gasPriceHex = web3.toHex(gasPrice); | |
var nonce = web3.eth.getTransactionCount(account.address) ; | |
var nonceHex = web3.toHex(nonce); | |
var options = { | |
nonce: nonceHex, | |
from: account.address, | |
value: '0x00', | |
data: '60606040526040516106f93803806106f9833981016040528080518201919060200180518201919060200180519060200190919080519060200190919050505b33600060005060000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055508360006000506003016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100c557805160ff19168380011785556100f6565b828001600101855582156100f6579182015b828111156100f55782518260005055916020019190600101906100d7565b5b5090506101219190610103565b8082111561011d5760008181506000905550600101610103565b5090565b50508260006000506001016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017857805160ff19168380011785556101a9565b828001600101855582156101a9579182015b828111156101a857825182600050559160200191906001019061018a565b5b5090506101d491906101b6565b808211156101d057600081815060009055506001016101b6565b5090565b505081600060005060020160005081905550806000600050600401600050819055506001600060005060050160006101000a81548160ff0219169083021790555060006000600050600601600050819055505b505050506104c0806102396000396000f360606040526000357c0100000000000000000000000000000000000000000000000000000000900480638d99b2eb1461004f5780639ae8886a14610072578063fc36e15b146101d95761004d565b005b61005c600480505061042d565b6040518082815260200191505060405180910390f35b61007f6004805050610243565b604051808873ffffffffffffffffffffffffffffffffffffffff16815260200180602001878152602001806020018681526020018581526020018481526020018381038352898181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156101405780601f1061011557610100808354040283529160200191610140565b820191906000526020600020905b81548152906001019060200180831161012357829003601f168201915b50508381038252878181546001816001161561010002031660029004815260200191508054600181600116156101000203166002900480156101c35780601f10610198576101008083540402835291602001916101c3565b820191906000526020600020905b8154815290600101906020018083116101a657829003601f168201915b5050995050505050505050505060405180910390f35b61022d6004808035906020019082018035906020019191908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509090919050506102b0565b6040518082815260200191505060405180910390f35b60006000508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690806001016000509080600201600050549080600301600050908060040160005054908060050160009054906101000a900460ff16908060060160005054905087565b6000600060005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614158061032b57506001600060005060050160009054906101000a900460ff1614155b156103395760009050610428565b600160006000506006016000828282505401925050819055507f24bcf19562365f6510754002f8d7b818d275886315d29c7aa04785570b97a3638260405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156103d45780820380516001836020036101000a031916815260200191505b509250505060405180910390a16000600060005060020160005054111561041f5760006000506002016000505460006000506006016000505410151561041e5761041c61042d565b505b5b60019050610428565b919050565b6000600060005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561049557600090506104bd565b6000600060005060050160006101000a81548160ff02191690830217905550600190506104bd565b9056', | |
gas: web3.toHex(1000000), | |
gasPrice: gasPriceHex | |
}; // required! | |
var callback = function(err, contract) { | |
console.log(arguments); | |
} | |
// parse arguments | |
var args = [ | |
_options, | |
_title, | |
_votelimit, | |
_deadline | |
]; | |
var bytes = encodeConstructorParams(abi, args); | |
options.data += bytes; | |
var tx = new Tx(options); | |
var privateKey = new Buffer(account.private, 'hex') | |
tx.sign(privateKey); | |
var serializedTx = tx.serialize(); | |
web3.eth.sendRawTransaction(serializedTx.toString('hex'), function (err, hash) { | |
if (err) { | |
callback(err); | |
} else { | |
// add the transaction hash | |
contract.transactionHash = hash; | |
// call callback for the first time | |
callback(null, contract); | |
checkForContractAddress(contract, callback); | |
} | |
}); | |
@simondlr | |
simondlr commented on May 20, 2016 | |
@fbmx did you eventually solve this? Seems since Homestead, we've been getting invalid sender as well. | |
@jonathancross | |
jonathancross commented on May 29, 2016 • edited | |
I am seeing this "Invalid sender" error (probably thrown here) as well when using https://www.kraken.com/ether/ (based on web3.js) and https://www.myetherwallet.com (may not even seem to use web3.js). Some transactions go through, some not. | |
Users on reddit mentioning the same error message: | |
https://www.reddit.com/r/ethereum/comments/4cmzqx/issues_with_recovering_presale/ | |
Seems this error may be caused by some malformed "from" address? | |
@cicorias | |
Write Preview | |
Attach files by dragging & dropping or selecting them. | |
Styling with Markdown is supported | |
Comment | |
Contact GitHub API Training Shop Blog About | |
© 2017 GitHub, Inc. Terms Privacy Security Status Help |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment