Compiling and deploying contracts using solc

If you love compiling and running code using command lines, you can use the Solidity compiler, solc. To use the latest stable version of the Solidity compiler, run the following:

sudo apt-get update
sudo apt-get install solc

Start by copying the contract code into a file named auction.sol, and afterwards run the following command to compile the contract and generate the needed ABI and bytecode. The output will be saved in a details.js file as follows:

echo "var ContractDetails=`solc --optimize --combined-json abi,bin auction.sol`" > details.js

If you see a set of warnings, neglect them and then run in succession the following commands in your Geth CLI, in succession:

loadScript(“your Path/details.js”)
var ContractAbi = ContractDetails.contracts["auction.sol:MyAuction"].abi;
var ContractInstance = eth.contract(JSON.parse(ContractAbi));
var ContractBin = "0x" + ContractDetails.contracts["auction.sol:MyAuction"].bin;

Before you continue, make sue you have unlocked your first account:

var deploymentTransationObject = { from: eth.accounts[0], data: ContractBin, gas: 1000000 };
var auctionInstance = ContractInstance.new(deploymentTransationObject);
var AuctionAddress = eth.getTransactionReceipt(auctionInstance.transactionHash).contractAddress;

From the console, to interact with the contract, you can create an object pointing to your contract's address as follows:

var Mycontract = ContractInstance.at(AuctionAddress);

This is similar to what we have done so far in Web3Js and, depending on the nature of the function, we can execute a call using Mycontract.FunctionName.call() or send a transaction using Mycontract.FunctionName.sendTransaction(FunctionArguments, {from: eth.accounts[0], gas: 1000000}).

In order to validate your transaction, you'll need to run the mining process in Geth using miner.start() or use the mining script we used earlier when we configured the private chain.

Here, we have seen a winding path for compiling and deploying a contract using solc, web3.js, and a local blockchain. Tools such as Truffle (that you'll discover in the next chapter) do all that for you under the hood, and make perfect alternatives for quick development and testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset