mirror of https://github.com/status-im/nimplay.git
Add notes on logging functionality.
This commit is contained in:
parent
c3742619ed
commit
a6d1b31e97
|
@ -10,6 +10,8 @@ contract("ContractName"):
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The string paramater given to the `contract` block macro can be any unique string.
|
||||||
|
|
||||||
# Function Visibility
|
# Function Visibility
|
||||||
|
|
||||||
All nimplay functions need to be annotated as either private or public, all functions marked with
|
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.
|
To mark a function open to receive funds, the `payable` function pragma is used.
|
||||||
|
|
||||||
```
|
```nim
|
||||||
contract("Main"):
|
contract("Main"):
|
||||||
|
|
||||||
proc pay_me() {.payable.}: uint256 =
|
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
|
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.}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
|
@ -3,46 +3,35 @@ import ../nimplay/types
|
||||||
import ../nimplay/ewasm_eei
|
import ../nimplay/ewasm_eei
|
||||||
import stint
|
import stint
|
||||||
|
|
||||||
|
# import nimplay0_1
|
||||||
|
|
||||||
contract("KingOfTheHill"):
|
contract("KingOfTheHill"):
|
||||||
|
|
||||||
# Globals
|
# Globals
|
||||||
var
|
var
|
||||||
king_name: bytes32
|
king_name: bytes32
|
||||||
king_addr: address
|
king_addr: address
|
||||||
king_value: wei_value
|
king_value: wei_value
|
||||||
king_else: uint128
|
king_else: uint128
|
||||||
|
|
||||||
# Events
|
# Events
|
||||||
# proc shouldFail2(a: uint256): val {.event.}
|
proc BecameKing(name: bytes32, value: uint128) {.event.}
|
||||||
# proc KingEvent(id {.indexed.}: uint256, name: bytes32, value: uint128) {.event.}
|
proc BecameKing2(name {.indexed.}: bytes32, value {.indexed.}: uint128) {.event.}
|
||||||
proc Transferred(fromm: address, value: uint128) {.event.}
|
|
||||||
proc Transferred2(fromm {.indexed.}: address, value {.indexed.}: uint128) {.event.}
|
|
||||||
|
|
||||||
# Methods
|
# Methods
|
||||||
proc becomeKing*(name: bytes32) {.payable.} =
|
proc becomeKing*(name: bytes32) {.payable,self,log,msg.} =
|
||||||
log.Transferred(msg.sender, msg.value)
|
if msg.value > self.king_value:
|
||||||
log.Transferred2(msg.sender, msg.value * 2)
|
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:
|
proc getKingAddr*(): address =
|
||||||
# self.king_name = name
|
self.king_addr
|
||||||
# self.king_addr = msg.sender
|
|
||||||
# self.king_value = msg.value
|
|
||||||
# transfer funds.
|
|
||||||
# Transfer()
|
|
||||||
|
|
||||||
|
|
||||||
# proc getKing*(): bytes32 =
|
proc getKingValue*(): uint128 =
|
||||||
# self.king_name
|
self.king_value
|
||||||
|
|
||||||
# proc getKingAddr*(): address =
|
|
||||||
# self.king_addr
|
|
||||||
|
|
||||||
# proc getKingValue*(one: uint256): uint128 =
|
|
||||||
# if true:
|
|
||||||
# self.king_value
|
|
||||||
# else:
|
|
||||||
# self.king_value
|
|
||||||
|
|
Loading…
Reference in New Issue