Skip to content

Commit

Permalink
Remove nonce from SRC-15 log
Browse files Browse the repository at this point in the history
  • Loading branch information
bitzoic committed Dec 5, 2024
1 parent 42ebfb6 commit 0b8b9bb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 65 deletions.
4 changes: 1 addition & 3 deletions docs/src/src-15-offchain-asset-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@ The following logs MUST be implemented and emitted to follow the SRC-15 standard

#### SRC15MetadataEvent

The `SRC15MetadataEvent` MUST be emitted once for each distinct piece of metadata.
The `SRC15MetadataEvent` MUST be emitted at least once for each distinct piece of metadata. The latest emitted `SRC15MetadataEvent` is determined to be the current metadata.

There SHALL be the following fields in the `SRC15MetadataEvent` struct:

* `asset`: The `asset` field SHALL be used for the corresponding `AssetId` for the metadata.
* `metadata`: The `metadata` field SHALL be used for the corresponding `Metadata` which represents the metadata of the asset.
* `nonce`: The nonce of the metadata. This SHALL begin at zero and increment by one upon every emission such that each emission has a unique nonce.

Example:

```sway
pub struct SRC15MetadataEvent {
pub asset: AssetId,
pub metadata: Metadata,
pub nonce: u64,
}
```

Expand Down
18 changes: 6 additions & 12 deletions examples/src15-offchain-metadata/multi_asset/src/multi_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ 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, write)]
#[storage(read)]
fn emit_src15_events(asset: AssetId, svg_image: String, health_attribute: u64);
}

impl EmitSRC15Events for Contract {
#[storage(read, write)]
#[storage(read)]
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 @@ -62,14 +60,10 @@ impl EmitSRC15Events for Contract {
let metadata_3 = Metadata::String(svg_image);
let metadata_4 = Metadata::Int(health_attribute);

// 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();
SRC15MetadataEvent::new(asset, metadata_1).log();
SRC15MetadataEvent::new(asset, metadata_2).log();
SRC15MetadataEvent::new(asset, metadata_3).log();
SRC15MetadataEvent::new(asset, metadata_4).log();
}
}

Expand Down
17 changes: 3 additions & 14 deletions examples/src15-offchain-metadata/single_asset/src/single_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,11 @@ 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.
Expand All @@ -55,13 +48,9 @@ impl EmitSRC15Events for Contract {
let metadata_2 = Metadata::String(String::from_ascii_str(from_str_array(SITE_FORUM)));
let metadata_3 = Metadata::Int(ATTR_HEALTH);

// 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_1).log();
SRC15MetadataEvent::new(asset, metadata_2).log();
SRC15MetadataEvent::new(asset, metadata_3).log();
}
}

Expand Down
47 changes: 11 additions & 36 deletions standards/src/src15.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ pub struct SRC15MetadataEvent {
pub asset: AssetId,
/// The Metadata of the SRC-15 event.
pub metadata: Metadata,
/// 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.nonce == other.nonce
self.asset == other.asset && self.metadata == other.metadata
}
}

Expand All @@ -25,7 +23,6 @@ impl SRC15MetadataEvent {
///
/// * `asset`: [AssetId] - The asset for which metadata is set.
/// * `metadata`: [Option<Metdata>] - The Metadata that is set.
/// * `nonce`: [u64] - The unique nonce of the metadata.
///
/// # Returns
///
Expand All @@ -36,18 +33,16 @@ impl SRC15MetadataEvent {
/// ```sway
/// use standards::{src7::Metadata, src15::SRC15MetadataEvent};
///
/// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) {
/// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce);
/// fn foo(asset: AssetId, metadata: Metadata) {
/// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata);
/// assert(my_src15_metadata_event.asset == asset);
/// assert(my_src15_metadata_event.metadata == metadata);
/// assert(my_src15_metadata_event.nonce == nonce);
/// }
/// ```
pub fn new(asset: AssetId, metadata: Metadata, nonce: u64) -> Self {
pub fn new(asset: AssetId, metadata: Metadata) -> Self {
Self {
asset,
metadata,
nonce,
}
}

Expand All @@ -62,16 +57,16 @@ impl SRC15MetadataEvent {
/// ```sway
/// use standards::{src7::Metadata, src15::SRC15MetadataEvent};
///
/// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) {
/// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce);
/// fn foo(asset: AssetId, metadata: Metadata) {
/// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata);
/// assert(my_src15_metadata_event.asset() == asset);
/// }
/// ```
pub fn asset(self) -> AssetId {
self.asset
}

/// Returns the metadata of the `v` event.
/// Returns the metadata of the `SRC15MetadataEvent` event.
///
/// # Returns
///
Expand All @@ -82,44 +77,24 @@ impl SRC15MetadataEvent {
/// ```sway
/// use standards::{src7::Metadata, src15::SRC15MetadataEvent};
///
/// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) {
/// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata, nonce);
/// fn foo(asset: AssetId, metadata: Metadata) {
/// let my_src15_metadata_event = SRC15MetadataEvent::new(asset, metadata);
/// assert(my_src15_metadata_event.metadata() == metadata);
/// }
/// ```
pub fn metadata(self) -> Metadata {
self.metadata
}

/// Returns the unique nonce of the `SRC15MetadataEvent` event.
///
/// # Returns
///
/// * [u64] - The nonce of the event.
///
/// # Examples
///
/// ```sway
/// use standards::{src7::Metadata, src15::SRC15MetadataEvent};
///
/// 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 nonce(self) -> u64 {
self.nonce
}

/// Logs the `SRC15MetadataEvent`.
///
/// # Examples
///
/// ```sway
/// use standards::{src7::Metadata, src15::SRC15MetadataEvent};
///
/// fn foo(asset: AssetId, metadata: Metadata, nonce: u64) {
/// let my_event = SRC15MetadataEvent::new(asset, metadata, nonce);
/// fn foo(asset: AssetId, metadata: Metadata) {
/// let my_event = SRC15MetadataEvent::new(asset, metadata);
/// my_event.log();
/// }
/// ```
Expand Down

0 comments on commit 0b8b9bb

Please sign in to comment.