Ethereum, tokens & smart contracts.

Notes on getting started : Part 11. Some intermediate considerations.

Previous notes in case you are just joining us:Part 1. Setting up.
Part 2. Web3.js/node.
Part 3. Solidity.
Part 4. Smart Contracts.
Part 5. Smarter Contracts.
Part 6. Tokens & Inheritance.
Part 7. ERC20 Token Standard.
Part 8. Crowdfunding and ICOs.
Part 9. Dapps & MetaMask.
Part 10. Remix, Truffle, TestRPC.

Working with solidity function parameters in web3

pragma solidity ^0.4.0;contract SetGetArray { uint[] someNumbers; function getArray() public constant returns (uint[]) {
return someNumbers;
}
function setArray(uint[] setNumbers) public { someNumbers = setNumbers; }}txHash:0x1cc74caab5c64c493a67cf8e3a8de656ba0db1d6f59c634e3c68e27a41afe7bf
Successfully deployed Contract with address: 0x1e1300614978efe2bf5c4b532daef69441314205
// File Setter.jsconsole.log('Setting up...');
const solc = require ('solc');
const Web3 = require ('web3');
console.log('Reading abi');
const contractABI = require("./SetGetArray.json");
console.log('Connecting');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log('Creating contract instance');
var contract = web3.eth.contract(contractABI).at("0x1e1300614978efe2bf5c4b532daef69441314205");
var receiverAddress = '0x1e1300614978efe2bf5c4b532daef69441314205';
var setNumbers = [5,2,4,1];
var setData = contract.setArray.getData(setNumbers);
// console.log(setData);var gasEstimate = web3.eth.estimateGas({
from: web3.eth.coinbase,
to: receiverAddress,
data: setData
});
var gasPrice = web3.eth.gasPrice;console.log('gas Price: ' + gasPrice);
console.log('Estimated Transaction gas: ' + gasEstimate);
console.log('unlocking Coinbase account');
const password = "yourPassword";
try {
web3.personal.unlockAccount(web3.eth.coinbase, password);
} catch(e) {
console.log(e);
return;
}
console.log ('sending Transaction to the contract');const transaction = {
from: web3.eth.coinbase,
to:receiverAddress,
value: '0x00',
gas: gasEstimate + 1,
gasPrice: gasPrice + 1,
data: setData
}
web3.eth.sendTransaction( transaction, function(err, txHash) {
if (err != null) {
console.error("Error while sending transaction: " + err);
}
else{
console.log("Transaction Sent here's you txHash: " + txHash);
}
});
// Transaction Sent here's you txHash:
// 0x591ab8bf4b1fe6a81721d6ecab3b99b85248cd4302134c5a5674d9421a8612f6
// File Caller.jsconsole.log('Setting up...');
const solc = require ('solc');
const Web3 = require ('web3');
console.log('Reading abi');
const contractABI = require("./SetGetArray.json");
console.log('Connecting');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
console.log('Creating contract instance');
var contract = web3.eth.contract(contractABI).at("0x1e1300614978efe2bf5c4b532daef69441314205");
console.log ('calling contract');
var getArray = contract.getArray();
console.log('Get Array : ' + getArray);
// Get Array : 5,2,4,1
//
getArray[2] = 4
See: https://ethereum.stackexchange.com/questions/8362/passing-an-array-as-a-parameter-from-javascrpt-web3-to-a-solidity-function for further discussion.

Error handling: assert, require,revert,throw.

pragma solidity ^0.4.0;contract ErrorHandler {    uint hundred = 100;
uint ten = 10;
uint[] testvals = [0,1,2,3];
function testAssert() public view returns (uint) {
assert(hundred > ten);
return testvals[1];
}

function testRequire() public view returns (uint) {
require(hundred > ten);
return testvals[2];
}
}
Revert & Throw:When a transaction reverts, the state goes back to it’s original state, and ideally returns unused gas, you can also use it ( although it might be bad design beyond testing) to force the condition, throw; is the now deprecated version of revert(), but you might still see it here and there.

Can you kill a contract ?

function kill() {
if (msg.sender == creator)
suicide(creator);
}
// kills this contract and sends remaining funds back to creator

How to update a contract ?

pragma solidity ^0.4.0;contract season1 {string color = "Purple Haze"; function seasonColor() view public returns(string) {
return color;
}
}// calling season1 seasonColor... Purple Haze.
pragma solidity ^0.4.0;contract season2 {string color = "Gunmetal Pink"; function seasonColor() view public returns(string) {
return color;
}
}// calling season2 seasonColor... Gunmetal Pink.
pragma solidity ^0.4.0;contract seasons {  string color;
address owner;

function seasons() public {
owner = msg.sender;

}
function seasonColor() view public returns(string) {
return color;
}

function setSeasonColor(string newSeasonColor) public{
require(msg.sender == owner);
color = newSeasonColor;
}
}

Contract to contract interaction.

pragma solidity ^0.4.0;contract TransferableEther{address owner;function TransferableEther() public{
owner = msg.sender;
}
function transferEther(address forwardAddress, uint amount) public {
require(msg.sender == owner);
forwardAddress.transfer(amount);
}function () public payable {}}// Deployed Contract: 0x522f9a0ccd60d46bcb143f16ba39425b408112c9
So who pays? Whoever submits the transaction, in this case it would be the contracts owner (K3NOKOVAN), even though this account didn't receive any eth.
pragma solidity ^0.4.0;contract Base {

uint number = 72;

function getNumber() view public returns (uint){
return number;
}

}
pragma solidity ^0.4.0;import "./Base.sol";contract A {

function getNumberAddress(address keyAddress) public constant returns (uint) {
Base baseInstance = Base(keyAddress);
return baseInstance.getNumber();
}
}
contractInstance.call(bytes4(keccak256("method()")) 

Look down the road traveled :

✨✨ Buy the Book ! ✨✨If you are looking for an introduction to Ethereum, Solidity and Smart Contracts these notes were edited and revised into a book, a super convenient way for you to get started!Available in ebook and paperback:https://www.amazon.com/dp/B078CQ8L7V    🙏 🙏 😊 🙏 🙏

AI, Software Developer, Designer : www.k3no.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store