[Ethereum] How to use solidity extension for visual studio code

go-ethereumsolidityvisual-studio-code

I am new to Ethereum and I am using visual studio code as an editor for solidity.

I wrote a simple program and save it with .sol extension.

pragma solidity ^0.4.0;

contract SimpleStorage {
   uint storedData;

   function set(uint x) {
       storedData = x;
   }

   function get() constant returns (uint) {
       return storedData;
   }
} 

On Compilation of the current contract (Press F1, Solidity: Compile Current Solidity Contract)

enter image description here

A bin folder with above files is generated after compiling.

Open 'SimpleStorage.json' after compilation from the bin folder. Press F1 and press Solidity: Code generate from compilation output.

enter image description here

A new file with .cs extension a generated as shown above.

I had done up to these steps, What will I have to do for deploying the contract? (Running the solidity contract).

Is there a tutorial for visual studio code solidity extension?

Best Answer

To deploy a contract a new transaction is created which will include the bytecode as part of the Data and no address. If you have parameters in the constructor this will need to be encoded correctly and appended at the end.

The are different libraries that simplify the process, depending on the programming language you are using to interact with Ethereum.

1. C# As per the code generated in the question. Just use the function DeployContractAsync. You will have to create a .net project and add Nethereum as a reference using Nuget.

2. Javascript: This is the most common language used to interact with Ethereum, you will need Web3 js, the abi used here can be found on the abi file, and the code is the bytecode.


    web3.eth.contract(abi).new({data: code}, function (err, contract) {
                if(err) {
                    console.error(err);
                    return;
                // callback fires twice, we only want the second call when the   contract is deployed
                } else if(contract.address){
                    myContract = contract;
                    console.log('address: ' + myContract.address);
                    document.getElementById('status').innerText = 'Mined!';
                    document.getElementById('call').style.visibility = 'visible';
                }
            });

3. Java To deploy the contract using Java you can use Web3j, there is a console application which will allow you to create a code generated contract wrapper using the abi and bytecode as an input. Then you should be able to do something as follows:

    YourSmartContract contract = YourSmartContract.deploy(
            , , GAS_PRICE, GAS_LIMIT,
            ,
            , ..., );

4. Truffle: Last but not least, you can install truffle which will take care of the deployment for you using migrations.

Related Topic