Query messages(events)

How to work with contract event

How to work with contract events

About messages

Account messages are of 3 types:

  • External inbound (msg_type=1 or ExtIn ). It is for example, deploy message or run message.

  • Internal message(msg_type=0). It is when one contract calls another contract it sends an internal message. These messages are constructed with target contract ABI. Can be of 2 subtypes:

    • Internal inbound (IntIn)

    • Internal outbound (IntOut)

  • External outbound messages(msg_type=2 or ExtOut). These can be of 2 subtypes:

Query/subscribe to messages

You can fetch events of you contract like this:

query{
  blockchain{
    account(address:"-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964"){
      messages(msg_type:[ExtOut, ExtIn, IntIn, IntOut],first:2){
        edges{
          node{
            hash
            body
            created_at_string
          }
          cursor
        }
        pageInfo{
          hasNextPage
        }
      }
    }
  }
}

Or subscribe to them:

subscription{
messages(
      filter:{ 
      src:{
        eq:"-1:67f4bf95722e1bd6df845fca7991e5e7128ce4a6d25f6d4ef027d139a11a7964"
      }
      msg_type:{
        in:[0,1,2]
      }
    }
)
{
   id
  body
}
}

Usage

Query

See the full sample here https://github.com/tonlabs/sdk-samples/tree/master/core-examples/node-js/pagination

Read about used API here -> Account messages pagination.

result = await client.net.query({
  query: `query MyQuery($address: String!, $cursor: String, $count: Int, $start_seq_no: Int, end_seq_no: Int) {
    blockchain {
        account(address: $address){
            messages(
                master_seq_no_range: { start: $start_seq_no, end: $end_seq_no }
                first: $count,
                msg_type: [ExtOut, ExtIn, IntIn, IntOut],
                after: $cursor
            ) {
                edges {
                    node { id, created_at }
                }
                pageInfo {
                    endCursor
                    hasNextPage
                }
            }
        }
    }
}`,
       variables:{address, cursor, count, start_seq_no, end_seq_no}
}); 

Subscribe

See the full sample here https://github.com/tonlabs/sdk-samples/tree/master/core-examples/node-js/subscribe-and-decode

To subscribe to all this. Don't forget to specify your own callback.

const messageSubscription = await TonClient.default.net.subscribe_collection({
    collection: "messages",
    filter: {
        dst: { eq: your-contract-address },
        msg_type:{ in: [0,1,2] }
    },
    result: "boc"
}, <callback function>
});

Decode

See the full sample here https://github.com/tonlabs/sdk-samples/tree/master/core-examples/node-js/subscribe-and-decode

const decoded = (await TonClient.default.abi.decode_message({
                    abi: abiContract(your-contract-abi),
                    message: message_boc,
                }));
switch (decoded.body_type) {
                case MessageBodyType.Input:
                    console.log(`External inbound message, function "${decoded.name}", parameters: `, JSON.stringify(decoded.value));
                    break;
                case MessageBodyType.Output:
                    console.log(`External outbound message, function "${decoded.name}", result`, JSON.stringify(decoded.value));
                    break;
                case MessageBodyType.Event:
                    console.log(`External outbound message, event "${decoded.name}", parameters`, JSON.stringify(decoded.value));
                    break;
                }

Check out AppKit documentation for this use case.

Last updated