Work with Events
About events

Query/subscribe to events
Query/Subscribe with SDK
AppKit syntax
Query
Subscribe
Decode
Last updated

Last updated
query{
messages(
filter:{
src:{
eq:"-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964"
}
msg_type:{
eq:2
}
}
)
{
id
body
}
}subscription{
messages(
filter:{
src:{
eq:"-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964"
}
msg_type:{
eq:2
}
}
)
{
id
body
}
}pragma ton-solidity >= 0.38.2;
pragma AbiHeader expire;
contract HelloEvents {
// Event is an external message generated by the contract functions.
// Here we will emit this external outbound message (event)
// every time we have changed the hello text.
event TextUpdated(string text, uint32 time);
// Instance variable storing some user data.
string helloText;
// Instance variable storing the time of `constructor` call or `setHelloText` function call.
uint32 textUpdateTime;
// Constructor sets instance variables.
// All contracts need to call `tvm.accept()` for deploying.
constructor(string text) public {
tvm.accept();
helloText = text;
textUpdateTime = now;
}
// Function `setHelloText` updates instance variables
// `helloText` and `textUpdateTime`
// and emits `TextUpdated` event.
function setHelloText(string text) external returns (string oldText) {
require(msg.pubkey() == tvm.pubkey(), 100);
tvm.accept();
string saveText = helloText;
helloText = text;
textUpdateTime = now;
emit TextUpdated(helloText, textUpdateTime);
return saveText;
}
// Function returns value of instance variable `helloText`.
// This function is a get method (it does not change state and has no `accept` function)
// so it can be called only on local TVM.
function getHelloText() public view returns (string text) {
return helloText;
}
// Function returns value of instance variable `textUpdateTime`.
// This function is a get method (it does not change state and has no `accept` function)
// so it can be called only on local TVM.
function getTextUpdateTime() public view returns (uint32 time) {
return textUpdateTime;
}
}result = (await client.net.query_collection({
collection: "messages",
filter: {
src: {
eq: "-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964",
},
msg_type:{ eq:2 }
},
result: "boc",
})).result;await account.subscribeMessages("boc",callback)const decoded = await hello.decodeMessage(msg.boc);
switch (decoded.body_type) {
// Message that triggered an on-chain contract call
case MessageBodyType.Input:
if (decoded.name === "setHelloText") {
decoded.value.text = decodeText(decoded.value.text);
}
console.log(`External inbound message, function "${decoded.name}", parameters: `, JSON.stringify(decoded.value));
break;
// External outbound message generated by a function's `return`
case MessageBodyType.Output:
if (decoded.name === "setHelloText") {
decoded.value.oldText = decodeText(decoded.value.text);
}
console.log(`External outbound message, function "${decoded.name}", result`, JSON.stringify(decoded.value));
break;
// Event generated by the contract
case MessageBodyType.Event:
if (decoded.name === "TextUpdated") {
decoded.value.text = decodeText(decoded.value.text);
}
console.log(`External outbound message, event "${decoded.name}", parameters`, JSON.stringify(decoded.value));
break;
}