I got sick of using remix and copying-pasting my web3 deploy text so I'm trying to write a python script that will generate this deploy test for me. However, one of the fields in the instantiation code is data, and it holds a bytecode representation of the contract. How can I generate this bytecode as well? I'm unable to use py-solc for some reason (when I type from solc import * I get a ModuleNotFound error) so if I could recreate this bytecode manually that'd be awesome.
Here's an example of what I'm trying to do:
for smart contract "Storage.sol" below:
pragma solidity ^0.4.0;
contract Storage {
uint storedData;
address lastSetter;
function set(uint x) public {
storedData = x;
lastSetter = msg.sender;
}
function get() public constant returns (uint) {
return storedData;
}
function getLastSetter() public constant returns (address) {
return lastSetter;
}
}
Remix generates this web3 deploy:
var storageContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getLastSetter","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"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"}]);
var storage = storageContract.new(
{
from: web3.eth.accounts[0],
data: * long hex number *,
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
My script can make this:
var storageContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"payable":false,"stateMutability":"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[],"payable":false,"stateMutability":"type":"function"},{"constant":true,"inputs":[],"name":"getLastSetter","outputs":[],"payable":false,"stateMutability":"type":"function"},])
var storage = storageContract.new(
{
from: web3.eth.accounts[0],
data: '',
gas: '470000'
}, function(e, contract){
console.log(e, contract);
if (typeof contract.address != 'undefined') {
console.log('Contract successfully mined. address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
(still figuring out the statemutability (what even is that?) and output parsing as well, but thats a question for another day)
To sum up, I am trying to write a Python script that goes from solidity to the 'web3 deploy' text remix generates. How can I generate the required bytecode field?
Edit: I bit the bullet and tackled my installation of py-solc again. Got it right this time; I was accidentally installing using pip2.x rather than pip3.x. I'd still like to know how py-solc generates the bytecode, but the project works know. Thanks all for your help