Skip to content

Latest commit

 

History

History
520 lines (446 loc) · 17.8 KB

event_api.md

File metadata and controls

520 lines (446 loc) · 17.8 KB
title
Sui Events API

Sui Full nodes support publish / subscribe using JSON-RPC notifications via the WebSocket API. You can use this service with Sui client to filter and subscribe to a real-time event stream generated from Move or from the Sui network.

The client provides an event filter to limit the scope of events. Sui returns a notification with the event data and subscription ID for each event that matches the filter.

Event types

A Sui node emits the following types of events:

Move event

Move calls emit Move events. You can define custom events in Move contracts.

Attributes

Move event attributes:

  • packageId
  • transactionModule
  • sender
  • type
  • fields
  • bcs

Example Move event

{
  "moveEvent": {
    "packageId": "0x0000000000000000000000000000000000000002",
    "transactionModule": "devnet_nft",
    "sender": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1",
    "type": "0x2::devnet_nft::MintNFTEvent",
    "fields": {
      "creator": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1",
      "name": "Example NFT",
      "object_id": "0x497913a47dc0028a85f24c70d825991b71c60001"
    },
    "bcs": "SXkTpH3AAoqF8kxw2CWZG3HGAAFwYT9PF64TY/en5yUdqrXFsG9owQtFeGFtcGxlIE5GVA=="
  }
}

Publish event

Publish events occur when you publish a package to the network.

Attributes

Publish event attributes:

  • sender
  • packageId

Example Publish event

{
  "publish": {
    "sender": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1",
    "packageId": "0x2d052c9de3dd02f28ec0f8e4dfdee175a5c597c3"
  }
}

Transfer object event

Transfer object events occur you transfer an object from one address to another.

Attributes

Transfer event attributes:

  • packageId
  • transactionModule
  • sender
  • recipient
  • objectId
  • version
  • type

Example Transfer object event

{
  "transferObject": {
    "packageId": "0x0000000000000000000000000000000000000002",
    "transactionModule": "native",
    "sender": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1",
    "recipient": {
      "AddressOwner": "0x741a9a7ea380aed286341fcf16176c8653feb667"
    },
    "objectId": "0x591fbb00a6c9676186cb44402040a8350520cbe9",
    "version": 1,
    "type": "Coin"
  }
}

Delete object event

Delete object events occur when you delete an object.

Attributes

  • packageId
  • transactionModule
  • sender
  • objectId

Example Delete object event

{
  "deleteObject": {
    "packageId": "0x2d052c9de3dd02f28ec0f8e4dfdee175a5c597c3",
    "transactionModule": "discount_coupon",
    "sender": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1",
    "objectId": "0xe3a6bc7bf1dba4d17a91724009c461bd69870719"
  }
}

New object event

New object events occur for you create an object on the network.

Attributes

New object event attributes:

  • packageId
  • transactionModule
  • sender
  • recipient
  • objectId

Example New object event

{
  "newObject": {
    "packageId": "0x0000000000000000000000000000000000000002",
    "transactionModule": "devnet_nft",
    "sender": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1",
    "recipient": {
      "AddressOwner": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1"
    },
    "objectId": "0x497913a47dc0028a85f24c70d825991b71c60001"
  }
}

Epoch change event

Epoch change events occur when an epoch ends and a new epoch starts.

Attributes

None, Epoch change events do not have any attributes. The event includes an Epoch ID associated with the epochChange.

Example Epoch change event

{
  "epochChange": 20
}

Checkpoint event

A checkpoint event occurs for each checkpoint.

Attributes

None, Checkpoint events do not have any attributes. The event includes the Checkpoint sequence number associated with the checkpoint.

Example Checkpoint event

{
  "checkpoint": 10
}

Sui event query criteria

You can use the EventQuery criteria object to query a Sui node and retrieve events that match query criteria.

Query Description JSON-RPC Parameter Example
All All events {"All"}
Transaction Events emitted from the specified transaction. {"Transaction":"DGUe2TXiJdN3FI6MH1FwghYbiHw+NKu8Nh579zdFtUk="}
MoveModule Events emitted from the specified Move module {"MoveModule":{"package":"0x2", "module":"devnet_nft"}}
MoveEvent Move struct name of the event {"MoveEvent":"0x2::event_nft::MintNFTEvent"}
EventType Type of event described in Events section {"EventType": "NewObject"}
Sender Query by sender address {"Sender":"0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1"}
Recipient Query by recipient {"Recipient":{"AddressOwner":"0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1"}}
Object Return events associated with the given object {"Object":"0xe3a6bc7bf1dba4d17a91724009c461bd69870719"}
TimeRange Return events emitted in [start_time, end_time] interval {"TimeRange":{"startTime":1669039504014, "endTime":1669039604014}}

Pagination

The Event Query API provides cursor-based pagination to make it easier to work with large result sets. You can provide a cursor parameter in paginated query to indicate the starting position of the query. The query returns the number of results specified by limit, and returns the next_cursor value when there are additional results. The maximum limit is 1000 per query.

The following examples demonstrate how to create queries that use pagination for the results.

1. Get all events emitted by the devnet_nft module, in descending time order

Request

curl --location --request POST '127.0.0.1:9000' \
--header 'Content-Type: application/json' \
--data-raw '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "sui_getEvents",
  "params": [
    {"MoveModule":{"package":"0x2", "module":"devnet_nft"}},
    null,
    null,
    true
  ]
}'

Response

{
    "jsonrpc": "2.0",
    "result": {
        "data": [
            {
                "timestamp": 1666699837426,
                "txDigest": "cZXsToU6r0Uia6HIAwvr1eMlGsrg6b9+2oYZAskJ0wc=",
                "id": {
                    "txSeq": 1001,
                    "eventSeq": 1,
                },
                "event": {
                    "moveEvent": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "type": "0x2::devnet_nft::MintNFTEvent",
                        "fields": {
                            "creator": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                            "name": "Example NFT",
                            "object_id": "0x2ee80b4a2d203365dfbd68a90a8ad9a0dca19155"
                        },
                        "bcs": "LugLSi0gM2XfvWipCorZoNyhkVX+1JBtcbilg//9jpVnYCe2u4HXzwtFeGFtcGxlIE5GVA=="
                    }
                }
            },
            {
                "timestamp": 1666699837426,
                "txDigest": "cZXsToU6r0Uia6HIAwvr1eMlGsrg6b9+2oYZAskJ0wc=",
                "id": {
                    "txSeq": 1001,
                    "eventSeq": 0,
                },
                "event": {
                    "newObject": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "recipient": {
                            "AddressOwner": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf"
                        },
                        "objectId": "0x2ee80b4a2d203365dfbd68a90a8ad9a0dca19155"
                    }
                }
            },
            {
                "timestamp": 1666698739180,
                "txDigest": "WF2V6FM6y/kpAgRqzsQmR/osy4pmTgVVbE6qvSJxWh4=",
                "id": {
                    "txSeq": 998,
                    "eventSeq": 1,
                },
                "event": {
                    "moveEvent": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "type": "0x2::devnet_nft::MintNFTEvent",
                        "fields": {
                            "creator": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                            "name": "Example NFT",
                            "object_id": "0xd5657cf6acaba958c5b01ec0516f4f0dac77c7d2"
                        },
                        "bcs": "1WV89qyrqVjFsB7AUW9PDax3x9L+1JBtcbilg//9jpVnYCe2u4HXzwtFeGFtcGxlIE5GVA=="
                    }
                }
            },
            {
                "timestamp": 1666698739180,
                "txDigest": "WF2V6FM6y/kpAgRqzsQmR/osy4pmTgVVbE6qvSJxWh4=",
                "id": {
                    "txSeq": 998,
                    "eventSeq": 0,
                },
                "event": {
                    "newObject": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "recipient": {
                            "AddressOwner": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf"
                        },
                        "objectId": "0xd5657cf6acaba958c5b01ec0516f4f0dac77c7d2"
                    }
                }
            }
        ],
        "nextCursor": null
    },
    "id": 1
}

2. Get all 0x2::devnet_nft::MintNFTEvent events

Request

curl --location --request POST '127.0.0.1:9000' \
--header 'Content-Type: application/json' \
--data-raw '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "sui_getEvents",
  "params": [
    {"MoveEvent":"0x2::devnet_nft::MintNFTEvent"},
    null,
    null,
    "Ascending"
  ]
}'

Response

{
    "jsonrpc": "2.0",
    "result": {
        "data": [
            {
                "timestamp": 1666699837426,
                "txDigest": "cZXsToU6r0Uia6HIAwvr1eMlGsrg6b9+2oYZAskJ0wc=",
                "id": {
                    "txSeq": 1001,
                    "eventSeq": 1,
                },
                "event": {
                    "moveEvent": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "type": "0x2::devnet_nft::MintNFTEvent",
                        "fields": {
                            "creator": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                            "name": "Example NFT",
                            "object_id": "0x2ee80b4a2d203365dfbd68a90a8ad9a0dca19155"
                        },
                        "bcs": "LugLSi0gM2XfvWipCorZoNyhkVX+1JBtcbilg//9jpVnYCe2u4HXzwtFeGFtcGxlIE5GVA=="
                    }
                }
            },
            {
                "timestamp": 1666698739180,
                "txDigest": "WF2V6FM6y/kpAgRqzsQmR/osy4pmTgVVbE6qvSJxWh4=",
                "id": {
                    "txSeq": 998,
                    "eventSeq": 1,
                },
                "event": {
                    "moveEvent": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "type": "0x2::devnet_nft::MintNFTEvent",
                        "fields": {
                            "creator": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                            "name": "Example NFT",
                            "object_id": "0xd5657cf6acaba958c5b01ec0516f4f0dac77c7d2"
                        },
                        "bcs": "1WV89qyrqVjFsB7AUW9PDax3x9L+1JBtcbilg//9jpVnYCe2u4HXzwtFeGFtcGxlIE5GVA=="
                    }
                }
            }
        ],
        "nextCursor": null
    },
    "id": 1
}

3. Get all events and return 2 items per page in descending time order

Request

curl --location --request POST '127.0.0.1:9000' \
--header 'Content-Type: application/json' \
--data-raw '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "sui_getEvents",
  "params": [
    "All",
    null,
    2,
    "Ascending"
  ]
}'

Response

{
    "jsonrpc": "2.0",
    "result": {
        "data": [
            {
                "timestamp": 1666698739180,
                "txDigest": "WF2V6FM6y/kpAgRqzsQmR/osy4pmTgVVbE6qvSJxWh4=",
                "id": {
                    "txSeq": 998,
                    "eventSeq": 0,
                },
                "event": {
                    "newObject": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "recipient": {
                            "AddressOwner": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf"
                        },
                        "objectId": "0xd5657cf6acaba958c5b01ec0516f4f0dac77c7d2"
                    }
                }
            },
            {
                "timestamp": 1666698739180,
                "txDigest": "WF2V6FM6y/kpAgRqzsQmR/osy4pmTgVVbE6qvSJxWh4=",
                "id": {
                    "txSeq": 998,
                    "eventSeq": 1,
                },
                "event": {
                    "moveEvent": {
                        "packageId": "0x0000000000000000000000000000000000000002",
                        "transactionModule": "devnet_nft",
                        "sender": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                        "type": "0x2::devnet_nft::MintNFTEvent",
                        "fields": {
                            "creator": "0xfed4906d71b8a583fffd8e95676027b6bb81d7cf",
                            "name": "Example NFT",
                            "object_id": "0xd5657cf6acaba958c5b01ec0516f4f0dac77c7d2"
                        },
                        "bcs": "1WV89qyrqVjFsB7AUW9PDax3x9L+1JBtcbilg//9jpVnYCe2u4HXzwtFeGFtcGxlIE5GVA=="
                    }
                }
            }
        ],
        "nextCursor": 3
    },
    "id": 1
}

Subscribe to Sui events

When you subscribe to the events described in the preceding sections, you can apply event filters to match the events you want to filter.

Event filters

You can use EventFilter to filter the events included in your subscription to the event stream. EventFilter supports filtering on one attribute or a combination of attributes.

List of attributes that support filters

Filter Description Applicable to Event Type JSON-RPC Parameter Example
Package Move package ID MoveEvent
Publish
TransferObject
DeleteObject
NewObject
{"Package":"0x2"}
Module Move module name MoveEvent
TransferObject
DeleteObject
NewObject
{"Module":"devnet_nft"}
MoveEventType Move event type defined in the move code MoveEvent {"MoveEventType":"0x2::devnet_nft::MintNFTEvent"}
MoveEventField Filter using the data fields in the move event object MoveEvent {"MoveEventField":{ "path":"/name", "value":"Example NFT"}}
SenderAddress Address that started the transaction MoveEvent
Publish
TransferObject
DeleteObject
NewObject
{"SenderAddress": "0x70613f4f17ae1363f7a7e7251daab5c5b06f68c1"}
EventType Type of event described in the Events section MoveEvent
Publish
TransferObject
DeleteObject
NewObject
EpochChange
Checkpoint
{"EventType":"Publish"}
ObjectId Object ID TransferObject
DeleteObject
NewObject
{"ObjectId":"0xe3a6bc7bf1dba4d17a91724009c461bd69870719"}

Combining filters

We provide a few operators for combining filters:

Operator Description JSON-RPC Parameter Example
And Combine two filters; behaves the same as boolean And operator {"And":[{"Package":"0x2"}, {"Module":"devnet_nft"}]}
Or Combine two filters; behaves the same as boolean Or operator {"Or":[{"Package":"0x2"}, {"Package":"0x1"}]}
All Combine a list of filters; returns true if all filters match the event {"All":[{"EventType":"MoveEvent"}, {"Package":"0x2"}, {"Module":"devnet_nft"}]}
Any Combine a list of filters; returns true if any filter matches the event {"Any":[{"EventType":"MoveEvent"}, {"EventType":"TransferObject"}, {"EventType":"DeleteObject"}]}

Example using a combined filter

The following example demonstrates how to subscribe to Move events (MoveEvent) emitted by the 0x2::devnet_nft package from the Sui Client CLI create-example-nft command:

>> {"jsonrpc":"2.0", "id": 1, "method": "sui_subscribeEvent", "params": [{"All":[{"EventType":"MoveEvent"}, {"Package":"0x2"}, {"Module":"devnet_nft"}]}]}
<< {"jsonrpc":"2.0","result":3121662727959200,"id":1}

To unsubscribe from this stream, use:

>> {"jsonrpc":"2.0", "id": 1, "method": "sui_unsubscribeEvent", "params": [3121662727959200]}
<< {"jsonrpc":"2.0","result":true,"id":1}