Skip to content

Commit

Permalink
docs: txhash notification example
Browse files Browse the repository at this point in the history
  • Loading branch information
darwinzer0 committed Aug 8, 2024
1 parent 4413ffe commit d18cf7e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/notification/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ These functions are meant to help you easily create notification channels for pr

### Implementing a `NotificationData` struct

Each notification channel will have a specified data format, which is defined by creating a struct that implements the `NotificationData` trait, which has two methods: `to_cbor` and `channel_id`. The following example illustrates how you might implement this for a channel called `my_channel` and notification data containing two fields: `message` and `amount`.
Each notification channel will have a specified data format, which is defined by creating a struct that implements the `NotificationData` trait, which has two methods: `to_cbor` and `channel_id`.

The following example illustrates how you might implement this for a channel called `my_channel` and notification data containing two fields: `message` and `amount`.

```rust
#[derive(Serialize, Debug, Deserialize, Clone)]
Expand Down Expand Up @@ -36,25 +38,28 @@ The `api` parameter for `to_cbor` is not used in this example, but is there for

### Sending a TxHash notification

To send a notification to a recipient you then create a new `Notification` passing in the address of the recipient along with the notification data you want to send. The following creates a notification for the above `my_channel` and adds it to the contract `Response` as a plaintext attribute.
To send a notification to a recipient you first create a new `Notification` struct passing in the address of the recipient along with the notification data you want to send. Then to turn it into a `TxHashNotification` execute the `to_txhash_notification` method on the `Notification` by passing in `deps.api`, `env`, and an internal `secret`, which is a randomly generated byte slice that has been stored previously in your contract during initialization.

The following code snippet creates a notification for the above `my_channel` and adds it to the contract `Response` as a plaintext attribute.

```rust
let note = Notification::new(
let notification = Notification::new(
recipient,
MyNotificationData {
"hello".to_string(),
1000_u128,
}
);
)
.to_txhash_notification(deps.api, &env, secret)?;

// ... other code

// add notification to response
Ok(Response::new()
.set_data(to_binary(&ExecuteAnswer::MyMessage { status: Success } )?)
.add_attribute_plaintext(
note.id_plaintext(),
note.data_plaintext(),
notification.id_plaintext(),
notification.data_plaintext(),
)
)
```
Expand Down

0 comments on commit d18cf7e

Please sign in to comment.