From d529da4b8779023567d4289a3ec5bb0186e8237a Mon Sep 17 00:00:00 2001 From: Benjamin Fuentes Date: Thu, 11 Jan 2024 16:50:57 +0100 Subject: [PATCH 1/3] adding Ligo example for events --- docs/smart-contracts/events.md | 101 +++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 42 deletions(-) diff --git a/docs/smart-contracts/events.md b/docs/smart-contracts/events.md index 4619a8a51..9038e914f 100644 --- a/docs/smart-contracts/events.md +++ b/docs/smart-contracts/events.md @@ -23,7 +23,27 @@ The event can also include these optional fields: Each high-level language has its own way of creating events. The compiled Michelson code uses the `EMIT` command to emit the event. -For example, this SmartPy contract stores a number and emits events when that amount changes: +For example, this contract stores a number and emits events when that amount changes: + +jsLIGO + +```ligo +type storage = int; + +@entry +const add = (addAmount: int, s: storage): [list, storage] => + [list([Tezos.emit("%add",{ source: Tezos.get_source(), addAmount: addAmount })]), + s + addAmount + ]; + +@entry +const reset = (_: unit, s: storage): [list, storage] => + [list([Tezos.emit("%reset",{ source: Tezos.get_source(), previousValue: s })]), + 0 + ]; +``` + +SmartPy ```python import smartpy as sp @@ -82,7 +102,7 @@ Tezos.setStreamProvider( Tezos.getFactory(PollingSubscribeProvider)({ shouldObservableSubscriptionRetry: true, pollingIntervalMilliseconds: 1500, - }), + }) ); try { @@ -91,7 +111,7 @@ try { address: contractAddress, }); - sub.on("data", console.log); + sub.on('data', console.log); } catch (e) { console.log(e); } @@ -103,50 +123,47 @@ The event data is in Michelson format, so an event from the `reset` entrypoint o ```json { - "opHash": "onw8EwWVnZbx2yBHhL72ECRdCPBbw7z1d5hVCJxp7vzihVELM2m", - "blockHash": "BM1avumf2rXSFYKf4JS7YJePAL3gutRJwmazvqcSAoaqVBPAmTf", - "level": 4908983, - "kind": "event", - "source": "KT1AJ6EjaJHmH6WiExCGc3PgHo3JB5hBMhEx", - "nonce": 0, - "type": { - "prim": "pair", - "args": [ - { - "prim": "int", - "annots": [ - "%previousValue" - ] - }, - { - "prim": "address", - "annots": [ - "%source" - ] - } - ] - }, - "tag": "reset", - "payload": { - "prim": "Pair", - "args": [ - { - "int": "17" - }, - { - "bytes": "000032041dca76bac940b478aae673e362bd15847ed8" - } - ] - }, - "result": { - "status": "applied", - "consumed_milligas": "100000" - } + "opHash": "onw8EwWVnZbx2yBHhL72ECRdCPBbw7z1d5hVCJxp7vzihVELM2m", + "blockHash": "BM1avumf2rXSFYKf4JS7YJePAL3gutRJwmazvqcSAoaqVBPAmTf", + "level": 4908983, + "kind": "event", + "source": "KT1AJ6EjaJHmH6WiExCGc3PgHo3JB5hBMhEx", + "nonce": 0, + "type": { + "prim": "pair", + "args": [ + { + "prim": "int", + "annots": ["%previousValue"] + }, + { + "prim": "address", + "annots": ["%source"] + } + ] + }, + "tag": "reset", + "payload": { + "prim": "Pair", + "args": [ + { + "int": "17" + }, + { + "bytes": "000032041dca76bac940b478aae673e362bd15847ed8" + } + ] + }, + "result": { + "status": "applied", + "consumed_milligas": "100000" + } } ``` Note that the address field is returned as a byte value. To convert the bytes to an address, use the `encodePubKey` function in `@taquito/utils`. + You can see the complete content of the event operation by looking up the operation hash in a block explorer. From 7e05763bcf276577e5b98666ea94b6146928e959 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Fri, 12 Jan 2024 15:19:29 -0500 Subject: [PATCH 2/3] Capitalize JsLIGO --- docs/smart-contracts/events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/smart-contracts/events.md b/docs/smart-contracts/events.md index 9038e914f..84ac37afc 100644 --- a/docs/smart-contracts/events.md +++ b/docs/smart-contracts/events.md @@ -25,7 +25,7 @@ The compiled Michelson code uses the `EMIT` command to emit the event. For example, this contract stores a number and emits events when that amount changes: -jsLIGO +JsLIGO ```ligo type storage = int; From e209c69812fb02d3da338bb9c5339325ad72c5f5 Mon Sep 17 00:00:00 2001 From: Tim McMackin Date: Fri, 19 Jan 2024 11:47:26 -0500 Subject: [PATCH 3/3] correct language annotation for ligo --- docs/smart-contracts/events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/smart-contracts/events.md b/docs/smart-contracts/events.md index 84ac37afc..a1f01f2f4 100644 --- a/docs/smart-contracts/events.md +++ b/docs/smart-contracts/events.md @@ -27,7 +27,7 @@ For example, this contract stores a number and emits events when that amount cha JsLIGO -```ligo +```ligolang type storage = int; @entry