Skip to content

Commit

Permalink
Update standards and examples with nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
bitzoic committed Dec 2, 2024
1 parent f7e7f22 commit f3b0515
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
19 changes: 12 additions & 7 deletions examples/src15-offchain-metadata/multi_asset/src/multi_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ storage {
total_assets: u64 = 0,
/// The total supply of a particular asset.
total_supply: StorageMap<AssetId, u64> = 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.
Expand All @@ -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();
}
}

Expand Down
18 changes: 14 additions & 4 deletions examples/src15-offchain-metadata/single_asset/src/single_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
44 changes: 22 additions & 22 deletions standards/src/src15.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -25,7 +25,7 @@ impl SRC15MetadataEvent {
///
/// * `asset`: [AssetId] - The asset for which metadata is set.
/// * `metadata`: [Option<Metdata>] - The Metadata that is set.
/// * `sender`: [Identity] - The caller that set the metadata.
/// * `nonce`: [u64] - The unique nonce of the metadata.
///
/// # Returns
///
Expand All @@ -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,
}
}

Expand All @@ -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);
/// }
/// ```
Expand All @@ -82,33 +82,33 @@ 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);
/// }
/// ```
pub fn metadata(self) -> Metadata {
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`.
Expand All @@ -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();
/// }
/// ```
Expand Down

0 comments on commit f3b0515

Please sign in to comment.