diff --git a/docs/basics.md b/docs/basics.md index 14b0f16..c2922b8 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -10,6 +10,8 @@ contract("ContractName"): ``` +The string paramater given to the `contract` block macro can be any unique string. + # Function Visibility All nimplay functions need to be annotated as either private or public, all functions marked with @@ -20,7 +22,7 @@ If a contract is not marked as public, it will be unreachable from outside the c To mark a function open to receive funds, the `payable` function pragma is used. -``` +```nim contract("Main"): proc pay_me() {.payable.}: uint256 = @@ -47,3 +49,27 @@ msg.sender | address | Current address making CALL to contract msg.value | wei_value (uint128)| Ether value in wei +# Logging + +To let the world know an event occured from a contract, Ethereum uses events. In Nimplay this can +be achieved using the `{.event.}` with function without a body of code. +To emit an event the `log` base keyword is used. + +```nim +contract("Main"): + # Events + proc nameGiven(name: bytes32, address: uint128) {.event.} + + proc setName*(name: bytes32): + self.name = name + log.nameGiven(name, msg.sender) +``` + +To indicate what parameters have to be indexed, for easy retrieval later use `{.indexed.}`, a maximum of 3 indexed topics (or parameters) are allowed. + +```nim +contract("Main"): + # Events + proc nameGiven(name: bytes32, address {.indexed.}: uint128) {.event.} + +``` diff --git a/examples/king_of_the_hill.nim b/examples/king_of_the_hill.nim index c273f8d..d37fac8 100644 --- a/examples/king_of_the_hill.nim +++ b/examples/king_of_the_hill.nim @@ -3,46 +3,35 @@ import ../nimplay/types import ../nimplay/ewasm_eei import stint +# import nimplay0_1 contract("KingOfTheHill"): + # Globals var king_name: bytes32 king_addr: address king_value: wei_value king_else: uint128 + # Events - # proc shouldFail2(a: uint256): val {.event.} - # proc KingEvent(id {.indexed.}: uint256, name: bytes32, value: uint128) {.event.} - proc Transferred(fromm: address, value: uint128) {.event.} - proc Transferred2(fromm {.indexed.}: address, value {.indexed.}: uint128) {.event.} + proc BecameKing(name: bytes32, value: uint128) {.event.} + proc BecameKing2(name {.indexed.}: bytes32, value {.indexed.}: uint128) {.event.} # Methods - proc becomeKing*(name: bytes32) {.payable.} = - log.Transferred(msg.sender, msg.value) - log.Transferred2(msg.sender, msg.value * 2) + proc becomeKing*(name: bytes32) {.payable,self,log,msg.} = + if msg.value > self.king_value: + self.king_name = name + self.king_addr = msg.sender + self.king_value = msg.value + log.BecameKing(name, msg.value) + log.BecameKing2(name, msg.value) - # proc log__KingEvent(name: bytes32, value: uint128): - # ... - # ... - # ... + proc getKing*(): bytes32 = + self.king_name - # if msg.value > self.king_value: - # self.king_name = name - # self.king_addr = msg.sender - # self.king_value = msg.value - # transfer funds. - # Transfer() - + proc getKingAddr*(): address = + self.king_addr - # proc getKing*(): bytes32 = - # self.king_name - - # proc getKingAddr*(): address = - # self.king_addr - - # proc getKingValue*(one: uint256): uint128 = - # if true: - # self.king_value - # else: - # self.king_value + proc getKingValue*(): uint128 = + self.king_value