Structure of a Contract¶
Contracts in Viper are contained within files, with each file being one smart-contract. Files in Viper are similar to classes in object-oriented languages. Each file can contain declarations of State Variables, Functions, and structure-structs-types.
State Variables¶
State variables are values which are permanently stored in contract storage.
storedData: num
See the Types section for valid state variable types and visibility-and-getters for possible choices for visibility.
Functions¶
Functions are the executable units of code within a contract.
@public
@payable
def bid(): // Function
// ...
}
function-calls can happen internally or externally and have different levels of visibility (visibility-and-getters) towards other contracts. Functions must be decorated with either @public or @internal.
Events¶
Events may be logged in specially indexed data structures that allow clients, including light clients, to efficiently search for them.
Payment: __log__({amount: num, arg2: indexed(address)})
total_paid: num
@public
def pay():
self.total_paid += msg.value
log.Payment(msg.value, msg.sender)
Events must be declared before global declarations and function definitions.