Skip to main content

Listen To Events

Listening to events in an application is essential for real-time data updates and a seamless user experience. By subscribing to events, such as transfers, deposits, or withdrawals, you can trigger a callback as soon as the event occurs.

Unlock Conditions

Outputs may have multiple UnlockConditions, which may require returning some or all of the transferred amount. The outputs could also expire if not claimed in time, or may not be unlockable for a predefined period.

To get outputs with only the AddressUnlockCondition, you should synchronize with the option syncOnlyMostBasicOutputs: true.

If you are synchronizing outputs with other unlock conditions, you should check the unlock conditions carefully before crediting users any balance.

You can find an example illustrating how to check if an output has only the address unlock condition, where the address belongs to the account in the Check Unlock Conditions how-to guide.

Example Code

  1. Instantiate a Wallet.
sdk/examples/wallet/events.rs
loading...
  1. Use the Wallet instance to listen to events with the Wallet.listen() function. You can listen for a specific WalletEventType.
sdk/examples/wallet/events.rs
loading...

Full Example Code

sdk/examples/wallet/events.rs
loading...

Expected Output

AccountIndex: 0
Event: {
type: 2,
output: {
outputId: '0x581f43218322d742c0ba1f0d738d6afbc9beaaf4c47f439ab048766b25843f920100',
metadata: {
blockId: '0x7fdf4079802bd00d022cc382a422491724af6564d31522b7f34133d119b7979d',
transactionId: '0x581f43218322d742c0ba1f0d738d6afbc9beaaf4c47f439ab048766b25843f92',
outputIndex: 1,
isSpent: false,
milestoneIndexBooked: 6316391,
milestoneTimestampBooked: 1690125643,
ledgerIndex: 6450179
},
output: {
type: 3,
amount: '3043981300',
nativeTokens: [Array],
unlockConditions: [Array]
},
isSpent: false,
address: {
type: 0,
pubKeyHash: '0x194eb32b9b6c61207192c7073562a0b3adf50a7c1f268182b552ec8999380acb'
},
networkId: '1856588631910923207',
remainder: false,
chain: { coinType: 4218, account: 0, change: 0, addressIndex: 0 }
}
}