CSBC 2010 - Smart Contracts And Decentralized Applications
Class 6: Solidity By Examples - II
Dhruvin Parikh
Class Plan
- Solidity Smart Contracts
- Fallback function
- Overriding function modifier
- Function overloading and overriding
- Function types
enum
- Use to model choice and keep track of state.
- Enums can also be declared on the file level, outside of contract or library definitions.
- Enums cannot have more than 256 members.
Declaring enum
- File that the enum is declared in
pragma solidity ^0.6.10;
enum Status {
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
- Enums require at least one member.
Importing enum
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
import "./EnumDeclaration.sol";
contract Enum {
Status public status;
}
- First member is default value when declared.
Modifier Overriding(1)
- Function modifiers can override each other.
- Same as function overriding.
- Modifiers cannot be overloaded.
- Use
virtual
on the overridden modifier.
- Use
override
on the overriding modifier.
- Specify base contracts explicitly for multiple inheritance.
Modifier Overriding(2)
contract Base {
modifier foo() virtual {_;}
}
contract Inherited is Base{
modifier foo() override {_;}
}
Modifier Overriding(3)
contract Base1 {
modifier foo() virtual {_
}
contract Base2 {
modifier foo() virtual {_
}
contract Inherited is Base1, Base2 {
modifier foo() override(Base1, Base2) {_
}
function overloading(1)
- Multiple function with same name but different signatures
contract A {
function f(uint _in) public pure returns (uint out) {
out = _in;
}
function f(uint _in, bool _really) public pure returns (uint out) {
if (_really)
out = _in;
}
}
function overloading(2)
- error if two externally visible functions differ by their Solidity types but not by their external type
contract B {}
contract A {
function f(B _in) public pure returns (B out) {
out = _in;
}
function f(address _in) public pure returns (address out) {
out = _in;
}
}
function overriding(1)
- Override base functions using inheritance to change behavior if they are marked as
virtual
.
- Overriding function must use the
override
keyword in the function header.
contract Base {
function foo() virtual public {}
}
contract Middle is Base {}
contract Inherited is Middle {
function foo() public override {}
}
function overriding(2)
contract Base1 {
function foo() virtual public {}
}
contract Base2 {
function foo() virtual public {}
}
contract Inherited is Base1, Base2 {
function foo() public override(Base1, Base2) {}
}
function overriding(3)
- Public state variables can override external functions if the parameter and return types of the function matches the getter function of the variable:
contract A {
function f() external pure virtual returns(uint) { return 5; }
}
contract B is A {
uint public override f;
}
fallback function
- Does not take any arguments
- Does not return anything.
- It is executed either when :
- a function that does not exist is called or
- Ether is sent directly to a contract but
receive()
does not exist
- fallback has a 2300 gas limit when called by
transfer
or send
.
fallback function
fallback () external payable {
// thanks for the funds
}
fallback () external payable {
require(false, "please don't just send me ETH");
}
- If somebody calls an unknown function, or no function at all, the fallback function is called
- It's just a regular function with reserved named
- Used to handle unknown function call, proxy contracts
Demo : Ballot(1)
An organization invites proposals for a project. A chairperson
organizes this process. The number of proposals is specified at
the time of creation of the smart contract.Chairperson registers
all the voters. Voters including the chairperson vote on the
proposals. To make things interesting, we have given a weight of 2
for the chairperson’s vote vs. the weight of 1 for the regular
voter. Winning proposal is determined and announced to the world.
Important details need to be recorded on the blockchain for
provenance.
- Let's walkthrough this. You can follow along
Demo : Ballot(2)
- Users : voters, chairperson
- Digital assets :
- Identity of users - vote, voted, weight
- list of voters
- list of proposals
- Transactions :register, vote(need to recorded), voteCount per proposal
- Roles: chairperson and voter
- Phases : Init, Register, voting, done
Demo : Ballot(3)
- Rules
- Chairperson organizes the voting.
- Voters are identified by their account addresses.
- Only chairperson can register other voters(reg phase).
- Voter can be registered and vote only once.
- Only registered voters can vote(vote phase).
- Voters can vote only for the items presented.
- Chairperson’s vote has double (2) the weight of voters(1).
On-chain randomness
- Blockchains are global, deterministic data-structures
- Generating something unpredictable is impossible
- Everybody needs to generate the same value
- What is vulnerability then?
- Review Lottery Contract
Function Types
- Function types are the types of functions.
- Variables of function type can be assigned from functions and function parameters of function type can be used to pass functions to and return functions from function calls.
- Function types come in two flavours -
internal
and external
functions,
Internal Function Type
- Internal functions can only be called inside the current contract (more specifically, inside the current code unit, which also includes internal library functions and inherited functions) because they cannot be executed outside of the context of the current contract.
- Calling an internal function is realized by jumping to its entry label, just like when calling a function of the current contract internally.
External Function Type
- External functions consist of an address and a function signature and they can be passed via and returned from external function calls.
"By default, function types are internal, so the internal keyword can be omitted. Note that this only applies to function types. Visibility has to be specified explicitly for functions defined in contracts, they do not have a default."
Function Type Code Examples
- Example that shows internal function type.
- Example that shows external function type.
Miner attacks
- Even if you block contracts from calling it, your coin flip game is still vulnerable to miners
- Miners control the exact construction of a block
- They can simulate running a transaction that bets on the Coin Flip Game
- Only include it in their blocks if it will win
CSBC 2010 - Smart Contracts And Decentralized Applications
Class 6: Solidity By Examples - II
Dhruvin Parikh