Ethereum – Solidity and Smart Contracts
September 13, 2018
Author: Michael Gord
Ethereum Solidity and Smart Contracts
You must have node and npm downloaded on your computer. After this tutorial you should have created and deployed an ethereum smart contract on the testrpc blockchain.
{
"dependencies": {
"web3": "0.17.0-alpha",
"solc": "^0.4.4"
}
}
Include the code above in your package.json file.
npm install
Download the dependencies from your command line.
var Web3 = require("web3")
var solc = require("solc")
Require dependencies in your node console.
var web3 = new Web3(new Web3.providers.httpProvider("https://localhost:8545"))
Create new web3 instance. Localhost:8545 is the default host.
contract Introduction {
function displayMessage() constant returns (string) {
return "Congrats on your first smart contract";
}
}
Make a new file called Introduction.sol (or whatever you want) and include the above code. This contract has a displayMessage function that can return a string.
var source = `[[contract syntax]]`
Create a variable for the source of the contract.
var compiled = solc.compile(source)
Create a variable for the compiled contract.
compiled.contracts.Introduction
Query the compiled contract and view the bytecode and opcodes.
compiled.contracts.Introduction.interface
View the ABI, which is the public facing interface that shows what methods are available to call.
var abi = JSON.parse(compiled.contracts.Introduction.interface)
Set the ABI as a variable.
var IntroductionContract = web3.eth.contract(abi)
Create a contract object that is deployed to the ethereum testrpc blockchain.
var deployed = Introduction.new({
from: web3.eth.accounts[0],
data: compiled.contracts.Introduction.bytecode,
gas: 4700000,
gasPrice: 5,
}, (error, contract) => { })
Make a variable of the deployed contract.
web3.eth.getTransacton("[[transaction id]]")
In your testrpc terminal window you will see that the contract has gone through. A contract and a transaction address will be posted. Find transaction info with the above command.
deployed.address
View the address of the deployed contract.
deployed.address
Call a function on the contract. In this case, we will call the displayMessage function and the message we input into the solidity.
This post on ethereum solidity and smart contracts is one of many posts in a series about the ethereum network.
MLG Blockchain is a blockchain development and consulting firm based in Toronto that is focused on building next generation applications using blockchain and smart contract technology. View all our blockchain development tutorials at www.mlgblockchain.com/learn.html.