Skip to content

Commit

Permalink
feat: FungibleTokenMetadata -> ContractMetadata rename for consistenc…
Browse files Browse the repository at this point in the history
…y with nft; improved reference ergos
  • Loading branch information
encody committed May 6, 2024
1 parent d540ed7 commit ab14199
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 56 deletions.
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

## NFT

```diff
use near_sdk::{near, PanicOnDefault};
+ use near_sdk_contract_tools::nft::*;
```rust
use near_sdk::near;
use near_sdk_contract_tools::nft::*;

#[derive(PanicOnDefault)]
+ #[derive(NonFungibleToken)]
#[derive(Default, NonFungibleToken)]
#[near(contract_state)]
pub struct MyNftContract {}

Expand All @@ -17,11 +16,11 @@ impl MyNftContract {
pub fn new() -> Self {
let mut contract = Self {};

+ contract.set_contract_metadata(ContractMetadata::new(
+ "My NFT".to_string(),
+ "MNFT".to_string(),
+ None,
+ ));
contract.set_contract_metadata(&ContractMetadata::new(
"My NFT".to_string(),
"MNFT".to_string(),
None,
));

contract
}
Expand All @@ -30,12 +29,11 @@ impl MyNftContract {

## FT

```diff
use near_sdk::{near, PanicOnDefault};
+ use near_sdk_contract_tools::ft::*;
```rust
use near_sdk::near;
use near_sdk_contract_tools::ft::*;

#[derive(PanicOnDefault)]
+ #[derive(FungibleToken)]
#[derive(Default, FungibleToken)]
#[near(contract_state)]
pub struct MyFtContract {}

Expand All @@ -45,11 +43,11 @@ impl MyFtContract {
pub fn new() -> Self {
let mut contract = Self {};

+ contract.set_metadata(&FungibleTokenMetadata::new(
+ "My Fungible Token".into(),
+ "MYFT".into(),
+ 24,
+ ));
contract.set_metadata(&ContractMetadata::new(
"My Fungible Token".into(),
"MYFT".into(),
24,
));

contract
}
Expand Down Expand Up @@ -157,10 +155,10 @@ e.emit();
To create a contract that is compatible with the [NEP-141][nep141], [NEP-145][nep145], and [NEP-148][nep148] standards, that emits standard-compliant ([NEP-297][nep297]) events.

```rust
use near_sdk::near;
use near_sdk_contract_tools::ft::*;
use near_sdk::{near, PanicOnDefault};

#[derive(FungibleToken, PanicOnDefault)]
#[derive(Default, FungibleToken)]
#[near(contract_state)]
struct MyFt {}

Expand All @@ -170,7 +168,7 @@ impl MyFt {
pub fn new() -> Self {
let mut contract = Self {};

contract.set_metadata(&FungibleTokenMetadata::new(
contract.set_metadata(&ContractMetadata::new(
"My Fungible Token".to_string(),
"MYFT".to_string(),
24,
Expand Down
2 changes: 1 addition & 1 deletion macros/src/standard/nep148.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn expand(meta: Nep148Meta) -> Result<TokenStream, darling::Error> {

#[#near_sdk::near]
impl #imp #me::standard::nep148::Nep148 for #ident #ty #wher {
fn ft_metadata(&self) -> #me::standard::nep148::FungibleTokenMetadata {
fn ft_metadata(&self) -> #me::standard::nep148::ContractMetadata {
#me::standard::nep148::Nep148Controller::get_metadata(self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub mod ft {
StorageBalance, StorageBalanceBounds,
},
nep148::{
self, ext_nep148, FungibleTokenMetadata, Nep148, Nep148Controller,
self, ext_nep148, ContractMetadata, Nep148, Nep148Controller,
Nep148ControllerInternal,
},
},
Expand Down
18 changes: 9 additions & 9 deletions src/standard/nep148.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const ERR_METADATA_UNSET: &str = "NEP-148 metadata is not set";
/// NEP-148-compatible metadata struct
#[derive(Eq, PartialEq, Clone, Debug)]
#[near(serializers = [borsh, json])]
pub struct FungibleTokenMetadata {
pub struct ContractMetadata {
/// Version of the NEP-148 spec
pub spec: String,
/// Human-friendly name of the token contract
Expand All @@ -35,7 +35,7 @@ pub struct FungibleTokenMetadata {
pub decimals: u8,
}

impl FungibleTokenMetadata {
impl ContractMetadata {
/// Creates a new metadata struct.
pub fn new(name: String, symbol: String, decimals: u8) -> Self {
Self {
Expand Down Expand Up @@ -106,7 +106,7 @@ pub trait Nep148ControllerInternal {
}

/// Returns the storage slot for NEP-148 metadata.
fn metadata() -> Slot<FungibleTokenMetadata> {
fn metadata() -> Slot<ContractMetadata> {
Self::root().field(StorageKey::Metadata)
}
}
Expand All @@ -118,20 +118,20 @@ pub trait Nep148Controller {
/// # Panics
///
/// Panics if the metadata has not been set.
fn get_metadata(&self) -> FungibleTokenMetadata;
fn get_metadata(&self) -> ContractMetadata;

/// Sets the metadata struct for this contract.
fn set_metadata(&mut self, metadata: &FungibleTokenMetadata);
fn set_metadata(&mut self, metadata: &ContractMetadata);
}

impl<T: Nep148ControllerInternal> Nep148Controller for T {
fn get_metadata(&self) -> FungibleTokenMetadata {
fn get_metadata(&self) -> ContractMetadata {
Self::metadata()
.read()
.unwrap_or_else(|| env::panic_str(ERR_METADATA_UNSET))
}

fn set_metadata(&mut self, metadata: &FungibleTokenMetadata) {
fn set_metadata(&mut self, metadata: &ContractMetadata) {
Self::metadata().set(Some(metadata));
}
}
Expand All @@ -141,12 +141,12 @@ mod ext {

use near_sdk::ext_contract;

use super::FungibleTokenMetadata;
use super::ContractMetadata;

/// Contract that supports the NEP-148 metadata standard
#[ext_contract(ext_nep148)]
pub trait Nep148 {
/// Returns the metadata struct for this contract.
fn ft_metadata(&self) -> FungibleTokenMetadata;
fn ft_metadata(&self) -> ContractMetadata;
}
}
24 changes: 14 additions & 10 deletions src/standard/nep177.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub trait Nep177Controller {
&mut self,
token_id: TokenId,
owner_id: AccountId,
metadata: TokenMetadata,
metadata: &TokenMetadata,
) -> Result<(), Nep171MintError>;

/// Burn a token with metadata.
Expand All @@ -240,17 +240,17 @@ pub trait Nep177Controller {

/// Sets the metadata for a token ID without checking whether the token
/// exists, etc. and emits an [`Nep171Event::NftMetadataUpdate`] event.
fn set_token_metadata_unchecked(&mut self, token_id: TokenId, metadata: Option<TokenMetadata>);
fn set_token_metadata_unchecked(&mut self, token_id: TokenId, metadata: Option<&TokenMetadata>);

/// Sets the metadata for a token ID and emits an [`Nep171Event::NftMetadataUpdate`] event.
fn set_token_metadata(
&mut self,
token_id: TokenId,
metadata: TokenMetadata,
metadata: &TokenMetadata,
) -> Result<(), UpdateTokenMetadataError>;

/// Sets the contract metadata and emits an [`Nep171Event::ContractMetadataUpdate`] event.
fn set_contract_metadata(&mut self, metadata: ContractMetadata);
fn set_contract_metadata(&mut self, metadata: &ContractMetadata);

/// Returns the contract metadata.
fn contract_metadata(&self) -> ContractMetadata;
Expand All @@ -271,7 +271,7 @@ impl<T: Nep177ControllerInternal + Nep171Controller> Nep177Controller for T {
fn set_token_metadata(
&mut self,
token_id: TokenId,
metadata: TokenMetadata,
metadata: &TokenMetadata,
) -> Result<(), UpdateTokenMetadataError> {
if self.token_owner(&token_id).is_some() {
self.set_token_metadata_unchecked(token_id, Some(metadata));
Expand All @@ -281,8 +281,8 @@ impl<T: Nep177ControllerInternal + Nep171Controller> Nep177Controller for T {
}
}

fn set_contract_metadata(&mut self, metadata: ContractMetadata) {
Self::slot_contract_metadata().set(Some(&metadata));
fn set_contract_metadata(&mut self, metadata: &ContractMetadata) {
Self::slot_contract_metadata().set(Some(metadata));
Nep171Event::ContractMetadataUpdate(vec![NftContractMetadataUpdateLog { memo: None }])
.emit();
}
Expand All @@ -291,7 +291,7 @@ impl<T: Nep177ControllerInternal + Nep171Controller> Nep177Controller for T {
&mut self,
token_id: TokenId,
owner_id: AccountId,
metadata: TokenMetadata,
metadata: &TokenMetadata,
) -> Result<(), Nep171MintError> {
let token_ids = [token_id];
let action = Nep171Mint {
Expand Down Expand Up @@ -322,8 +322,12 @@ impl<T: Nep177ControllerInternal + Nep171Controller> Nep177Controller for T {
Ok(())
}

fn set_token_metadata_unchecked(&mut self, token_id: TokenId, metadata: Option<TokenMetadata>) {
<Self as Nep177ControllerInternal>::slot_token_metadata(&token_id).set(metadata.as_ref());
fn set_token_metadata_unchecked(
&mut self,
token_id: TokenId,
metadata: Option<&TokenMetadata>,
) {
<Self as Nep177ControllerInternal>::slot_token_metadata(&token_id).set(metadata);
Nep171Event::NftMetadataUpdate(vec![NftMetadataUpdateLog {
token_ids: vec![token_id],
memo: None,
Expand Down
2 changes: 1 addition & 1 deletion tests/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ mod pausable_fungible_token {
pub fn new() -> Self {
let mut contract = Self { storage_usage: 0 };

contract.set_metadata(&FungibleTokenMetadata::new(
contract.set_metadata(&ContractMetadata::new(
"Pausable Fungible Token".into(),
"PFT".into(),
18,
Expand Down
2 changes: 1 addition & 1 deletion tests/macros/standard/fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl MyFungibleTokenContract {
let mut contract = Self {};

contract.set_metadata(
&FungibleTokenMetadata::new("My Fungible Token".into(), "MYFT".into(), 24)
&ContractMetadata::new("My Fungible Token".into(), "MYFT".into(), 24)
.icon("https://example.com/icon.png".into())
.reference("https://example.com/metadata.json".into())
.reference_hash(Base64VecU8::from([97, 115, 100, 102].to_vec())),
Expand Down
2 changes: 1 addition & 1 deletion tests/macros/standard/nep148.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl DerivesFTMetadata {
let mut contract = Self {};

contract.set_metadata(
&FungibleTokenMetadata::new("Test Fungible Token".into(), "TFT".into(), 18)
&ContractMetadata::new("Test Fungible Token".into(), "TFT".into(), 18)
.icon("https://example.com/icon.png".into())
.reference("https://example.com/metadata.json".into())
.reference_hash(Base64VecU8::from([97, 115, 100, 102].to_vec())),
Expand Down
4 changes: 2 additions & 2 deletions tests/macros/standard/nep171/manual_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Contract {
pub fn new() -> Self {
let mut contract = Self { next_token_id: 0 };

contract.set_contract_metadata(nep177::ContractMetadata::new(
contract.set_contract_metadata(&nep177::ContractMetadata::new(
"My NFT".to_string(),
"MYNFT".to_string(),
None,
Expand All @@ -61,7 +61,7 @@ impl Contract {
self,
token_id.clone(),
env::predecessor_account_id(),
nep177::TokenMetadata::new()
&nep177::TokenMetadata::new()
.title(format!("Token {token_id}"))
.description(format!("This is token {token_id}.")),
)
Expand Down
8 changes: 6 additions & 2 deletions tests/macros/standard/nep171/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ mod full_no_hooks {
Nep145Controller::deposit_to_storage_account(&mut n, &alice, NearToken::from_near(1))
.unwrap();

n.mint_with_metadata(token_id.clone(), alice, TokenMetadata::new().title("Title"))
.unwrap();
n.mint_with_metadata(
token_id.clone(),
alice,
&TokenMetadata::new().title("Title"),
)
.unwrap();

let nft_tok = n.nft_token(token_id);
dbg!(nft_tok);
Expand Down
4 changes: 2 additions & 2 deletions tests/macros/standard/nep171/non_fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Contract {
pub fn new() -> Self {
let mut contract = Self { next_token_id: 0 };

contract.set_contract_metadata(ContractMetadata::new(
contract.set_contract_metadata(&ContractMetadata::new(
"My NFT".to_string(),
"MYNFT".to_string(),
None,
Expand All @@ -38,7 +38,7 @@ impl Contract {
self.mint_with_metadata(
token_id.clone(),
env::predecessor_account_id(),
TokenMetadata::new()
&TokenMetadata::new()
.title(format!("Token {token_id}"))
.description(format!("This is token {token_id}.")),
)
Expand Down
2 changes: 1 addition & 1 deletion workspaces-tests/src/bin/fungible_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Contract {
blobs: Vector::new(b"b"),
};

contract.set_metadata(&FungibleTokenMetadata::new(
contract.set_metadata(&ContractMetadata::new(
"My Fungible Token".into(),
"MYFT".into(),
24,
Expand Down
4 changes: 2 additions & 2 deletions workspaces-tests/src/bin/non_fungible_token_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl Contract {
pub fn new() -> Self {
let mut contract = Self {};

contract.set_contract_metadata(ContractMetadata::new(
contract.set_contract_metadata(&ContractMetadata::new(
"My NFT Smart Contract".to_string(),
"MNSC".to_string(),
None,
Expand All @@ -123,7 +123,7 @@ impl Contract {
self.mint_with_metadata(
token_id.clone(),
receiver.clone(),
TokenMetadata::new()
&TokenMetadata::new()
.title(token_id)
.description("description"),
)
Expand Down

0 comments on commit ab14199

Please sign in to comment.