0

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

  • You should use a framework like populus or truffle to automate deployment. – Ismael Jan 2 '18 at 1:56
  • @Ismael I could do that, but how could I make this tool work? I was using truffle but found the testing and deployment process cumbersome and slow whereas being able to whip up a deployable web3 text and throw it into geth would make things quicker to me – steve Jan 2 '18 at 2:25
1

You need to use the Solidity compiler (solc) to compile your Solidity code into bytecode. If you can't use Remix or py-solc, you could try just installing solc directly and shelling out to it. But I would recommend figuring out what went wrong in your py-solc installation. I use py-solc and find it quite easy to use.

EDIT

The ABI (the part that includes "stateMutability") will also be emitted by the Solidity compiler.

  • Thanks! I will look into what happened. I got a ModuleNotFound error so I probably didn't install it in the right place – steve Jan 2 '18 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.