From f3b05153dbb0d18c0ac8aa3030d50e11999088bc Mon Sep 17 00:00:00 2001 From: bitzoic Date: Mon, 2 Dec 2024 12:58:22 +0100 Subject: [PATCH] Update standards and examples with nonce --- .../multi_asset/src/multi_asset.sw | 19 +++++--- .../single_asset/src/single_asset.sw | 18 ++++++-- standards/src/src15.sw | 44 +++++++++---------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/examples/src15-offchain-metadata/multi_asset/src/multi_asset.sw b/examples/src15-offchain-metadata/multi_asset/src/multi_asset.sw index 8d67c96..a5ca028 100644 --- a/examples/src15-offchain-metadata/multi_asset/src/multi_asset.sw +++ b/examples/src15-offchain-metadata/multi_asset/src/multi_asset.sw @@ -37,15 +37,17 @@ storage { total_assets: u64 = 0, /// The total supply of a particular asset. total_supply: StorageMap = StorageMap {}, + /// The nonce for the SRC15 Metadata event. + src_15_nonce: u64 = 0, } abi EmitSRC15Events { - #[storage(read)] + #[storage(read, write)] fn emit_src15_events(asset: AssetId, svg_image: String, health_attribute: u64); } impl EmitSRC15Events for Contract { - #[storage(read)] + #[storage(read, write)] fn emit_src15_events(asset: AssetId, svg_image: String, health_attribute: u64) { // NOTE: There are no checks for if the caller has permissions to emit the metadata // NOTE: Nothing is stored in storage and there is no method to retrieve the configurables. @@ -55,16 +57,19 @@ impl EmitSRC15Events for Contract { revert(0); } - let sender = msg_sender().unwrap(); let metadata_1 = Metadata::String(String::from_ascii_str(from_str_array(SOCIAL_X))); let metadata_2 = Metadata::String(String::from_ascii_str(from_str_array(SITE_FORUM))); let metadata_3 = Metadata::String(svg_image); let metadata_4 = Metadata::Int(health_attribute); - SRC15MetadataEvent::new(asset, metadata_1, sender).log(); - SRC15MetadataEvent::new(asset, metadata_2, sender).log(); - SRC15MetadataEvent::new(asset, metadata_3, sender).log(); - SRC15MetadataEvent::new(asset, metadata_4, sender).log(); + // Update the nonce + let nonce = storage.src_15_nonce.read(); + storage.src_15_nonce.write(nonce + 1); + + SRC15MetadataEvent::new(asset, metadata_1, nonce).log(); + SRC15MetadataEvent::new(asset, metadata_2, nonce).log(); + SRC15MetadataEvent::new(asset, metadata_3, nonce).log(); + SRC15MetadataEvent::new(asset, metadata_4, nonce).log(); } } diff --git a/examples/src15-offchain-metadata/single_asset/src/single_asset.sw b/examples/src15-offchain-metadata/single_asset/src/single_asset.sw index f08522a..a2a4369 100644 --- a/examples/src15-offchain-metadata/single_asset/src/single_asset.sw +++ b/examples/src15-offchain-metadata/single_asset/src/single_asset.sw @@ -35,23 +35,33 @@ configurable { ATTR_HEALTH: u64 = 100, } +storage { + /// The nonce for the SRC15 Metadata event. + src_15_nonce: u64 = 0, +} + abi EmitSRC15Events { + #[storage(read, write)] fn emit_src15_events(); } impl EmitSRC15Events for Contract { + #[storage(read, write)] fn emit_src15_events() { // NOTE: There are no checks for if the caller has permissions to emit the metadata. // NOTE: Nothing is stored in storage and there is no method to retrieve the configurables. let asset = AssetId::default(); - let sender = msg_sender().unwrap(); let metadata_1 = Metadata::String(String::from_ascii_str(from_str_array(SOCIAL_X))); let metadata_2 = Metadata::String(String::from_ascii_str(from_str_array(SITE_FORUM))); let metadata_3 = Metadata::Int(ATTR_HEALTH); - SRC15MetadataEvent::new(asset, metadata_1, sender).log(); - SRC15MetadataEvent::new(asset, metadata_2, sender).log(); - SRC15MetadataEvent::new(asset, metadata_3, sender).log(); + // Update the nonce + let nonce = storage.src_15_nonce.read(); + storage.src_15_nonce.write(nonce + 1); + + SRC15MetadataEvent::new(asset, metadata_1, nonce).log(); + SRC15MetadataEvent::new(asset, metadata_2, nonce).log(); + SRC15MetadataEvent::new(asset, metadata_3, nonce).log(); } } diff --git a/standards/src/src15.sw b/standards/src/src15.sw index 8c6dac3..26e7822 100644 --- a/standards/src/src15.sw +++ b/standards/src/src15.sw @@ -8,13 +8,13 @@ pub struct SRC15MetadataEvent { pub asset: AssetId, /// The Metadata of the SRC-15 event. pub metadata: Metadata, - /// The `Identity` of the caller that emitted the metadata. - pub sender: Identity, + /// The unique nonce for the metadata. + pub nonce: u64, } impl core::ops::Eq for SRC15MetadataEvent { fn eq(self, other: Self) -> bool { - self.asset == other.asset && self.metadata == other.metadata && self.sender == other.sender + self.asset == other.asset && self.metadata == other.metadata && self.nonce == other.nonce } } @@ -25,7 +25,7 @@ impl SRC15MetadataEvent { /// /// * `asset`: [AssetId] - The asset for which metadata is set. /// * `metadata`: [Option] - The Metadata that is set. - /// * `sender`: [Identity] - The caller that set the metadata. + /// * `nonce`: [u64] - The unique nonce of the metadata. /// /// # Returns /// @@ -36,18 +36,18 @@ impl SRC15MetadataEvent { /// ```sway /// use standards::{src7::Metadata, src15::SRC15MetadataEvent}; /// - /// fn foo(asset: AssetId, metadata: Metadata, sender: Identity) { - /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, sender); + /// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) { + /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce); /// assert(my_src15_metadata_event.asset == asset); /// assert(my_src15_metadata_event.metadata == metadata); - /// assert(my_src15_metadata_event.sender == sender); + /// assert(my_src15_metadata_event.nonce == nonce); /// } /// ``` - pub fn new(asset: AssetId, metadata: Metadata, sender: Identity) -> Self { + pub fn new(asset: AssetId, metadata: Metadata, nonce: u64) -> Self { Self { asset, metadata, - sender, + nonce, } } @@ -62,8 +62,8 @@ impl SRC15MetadataEvent { /// ```sway /// use standards::{src7::Metadata, src15::SRC15MetadataEvent}; /// - /// fn foo(asset: AssetId, metadata: Metadata, sender: Identity) { - /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, sender); + /// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) { + /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce); /// assert(my_src15_metadata_event.asset() == asset); /// } /// ``` @@ -82,8 +82,8 @@ impl SRC15MetadataEvent { /// ```sway /// use standards::{src7::Metadata, src15::SRC15MetadataEvent}; /// - /// fn foo(asset: AssetId, metadata: Metadata, sender: Identity) { - /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, sender); + /// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) { + /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce); /// assert(my_src15_metadata_event.metadata() == metadata); /// } /// ``` @@ -91,24 +91,24 @@ impl SRC15MetadataEvent { self.metadata } - /// Returns the sender of the `SRC15MetadataEvent` event. + /// Returns the unique nonce of the `SRC15MetadataEvent` event. /// /// # Returns /// - /// * [Identity] - The sender of the event. + /// * [u64] - The nonce of the event. /// /// # Examples /// /// ```sway /// use standards::{src7::Metadata, src15::SRC15MetadataEvent}; /// - /// fn foo(asset: AssetId, metadata: Metadata, sender: Identity) { - /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, sender); - /// assert(my_src15_metadata_event.sender() == sender); + /// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) { + /// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce); + /// assert(my_src15_metadata_event.nonce() == nonce); /// } /// ``` - pub fn sender(self) -> Identity { - self.sender + pub fn nonce(self) -> u64 { + self.nonce } /// Logs the `SRC15MetadataEvent`. @@ -118,8 +118,8 @@ impl SRC15MetadataEvent { /// ```sway /// use standards::{src7::Metadata, src15::SRC15MetadataEvent}; /// - /// fn foo(asset: AssetId, metadata: Metadata, sender: Identity) { - /// let my_event = SRC15MetadataEvent::new(asset, metadata, sender); + /// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) { + /// let my_event = SRC15MetadataEvent::new(asset, metadata, nonce); /// my_event.log(); /// } /// ```