Skip to content

Commit

Permalink
Dynamic NFT support (#96)
Browse files Browse the repository at this point in the history
* Add missing fields

* Consistent ordering

* Hasura permissions

* Royalty handling

* Fmt + lint

* Add rows to struct

* Remove junk

* Remove `empty` in favor of `Default` trait

* Remove `empty` on NftToken in favor of `Default` trait
  • Loading branch information
tifrel authored Mar 4, 2024
1 parent dab5b87 commit 1f9ef8f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@ select_permissions:
- mb_internal_id
- price
- minters_allowlist
- royalties
- royalty_percent
- max_supply
- last_possible_mint
- is_locked
filter: {}
allow_aggregations: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
alter table nft_metadata
drop royalties,
drop royalty_percent,
drop max_supply,
drop last_possible_mint,
drop is_locked;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
alter table nft_metadata
add royalties jsonb,
add royalty_percent numeric,
add max_supply numeric,
add last_possible_mint timestamp,
add is_locked boolean;
97 changes: 9 additions & 88 deletions src/db_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use chrono::NaiveDateTime;

// ----------------------- core/interop functionality ----------------------- //
use crate::schema::*;
#[derive(diesel::Insertable, diesel::Queryable, Clone)]
#[derive(Clone, Default, diesel::Insertable, diesel::Queryable)]
pub struct NftToken {
pub token_id: String,
pub nft_contract_id: String,
Expand All @@ -29,36 +29,9 @@ pub struct NftToken {
pub splits: Option<serde_json::Value>,
}

impl NftToken {
pub fn empty() -> Self {
NftToken {
token_id: "".to_string(),
nft_contract_id: "".to_string(),
owner: "".to_string(),
mint_memo: None,
last_transfer_timestamp: None,
last_transfer_receipt_id: None,
minted_timestamp: None,
minted_receipt_id: None,
burned_timestamp: None,
burned_receipt_id: None,
minter: None,
reference: None,
reference_hash: None,
copies: None,
issued_at: None,
expires_at: None,
starts_at: None,
updated_at: None,
metadata_id: None,
royalties_percent: None,
royalties: None,
splits: None,
}
}
}

#[derive(Clone, diesel::Insertable, diesel::Queryable, diesel::AsChangeset)]
#[derive(
Default, Clone, diesel::Insertable, diesel::Queryable, diesel::AsChangeset,
)]
#[table_name = "nft_metadata"]
pub struct NftMetadata {
pub id: String,
Expand Down Expand Up @@ -91,49 +64,11 @@ pub struct NftMetadata {
pub mb_internal_id: Option<BigDecimal>,
pub price: Option<BigDecimal>,
pub minters_allowlist: Option<Vec<String>>,
}

impl NftMetadata {
pub fn empty() -> Self {
NftMetadata {
id: "::".to_string(),
nft_contract_id: "".to_string(),
reference_blob: None,
title: None,
description: None,
media: None,
media_hash: None,
reference: None,
reference_hash: None,
// media_size: None,
// animation_url: None,
// animation_hash: None,
// animation_size: None,
// youtube_url: None,
// document_url: None,
// document_hash: None,
// external_url: None,
// category: None,
// type_: None,
// visibility: None,
// media_type: None,
// animation_type: None,
// tags: None,
// copies: None,
// issued_at: None,
// expires_at: None,
// starts_at: None,
// updated_at: None,
extra: None,
minter: None, // FIXME: make sure this is always added
base_uri: None, // FIXME: make sure this is always added
mb_internal_id: None,
price: None,
minters_allowlist: None,
}
}

// fn from_blob(blob: serde_json::Value) -> Option<Self> {}
pub royalties: Option<serde_json::Value>,
pub royalty_percent: Option<BigDecimal>,
pub max_supply: Option<BigDecimal>,
pub last_possible_mint: Option<NaiveDateTime>,
pub is_locked: Option<bool>,
}

#[derive(Clone, diesel::Insertable, diesel::Queryable, diesel::AsChangeset)]
Expand All @@ -145,20 +80,6 @@ pub struct NftAttribute {
pub attribute_display_type: Option<String>,
}

impl NftAttribute {
pub fn empty() -> Self {
NftAttribute {
nft_metadata_id: "".to_string(),
nft_contract_id: "".to_string(),
attribute_type: "".to_string(),
attribute_value: None,
attribute_display_type: None,
}
}

// fn from_blob(blob: serde_json::Value) -> Option<Self> {}
}

#[derive(diesel::Insertable, diesel::Queryable, diesel::AsChangeset, Clone)]
pub struct NftContract {
pub id: String,
Expand Down
62 changes: 8 additions & 54 deletions src/rpc_payloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{
Serialize,
};

type RoyaltiesMap = std::collections::HashMap<String, u16>;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
#[serde(tag = "kind", content = "payload")]
Expand All @@ -22,9 +24,14 @@ pub enum RpcMessage {
#[serde(rename = "metadata")]
HandleMetadataPayload {
contract_id: String,
metadata_id: String,
metadata_id: u64,
minters_allowlist: Option<Vec<String>>,
price: String,
royalties: Option<RoyaltiesMap>,
royalty_percent: Option<u16>,
max_supply: Option<u32>,
last_possible_mint: Option<u64>,
is_locked: bool,
refresh: Option<bool>,
creator: String,
},
Expand All @@ -37,59 +44,6 @@ pub enum RpcMessage {
},
}

impl RpcMessage {
pub fn from_contract(contract_id: String, update: bool) -> Self {
Self::HandleContractPayload {
contract_id,
refresh: Some(update),
}
}

pub fn from_token(
contract_id: String,
token_ids: Vec<String>,
minter: Option<String>,
) -> Self {
Self::HandleTokenPayload {
contract_id,
token_ids,
refresh: Some(false),
minter,
}
}

pub fn from_metadata(
contract_id: String,
metadata_id: u64,
minters_allowlist: Option<Vec<String>>,
price: u128,
creator: String,
) -> Self {
Self::HandleMetadataPayload {
contract_id,
metadata_id: metadata_id.to_string(),
minters_allowlist,
price: price.to_string(),
refresh: Some(false),
creator,
}
}

pub fn from_sale(
contract_id: String,
token_id: String,
new_owner_id: String,
receipt_id: String,
) -> Self {
Self::HandleSalePayload {
contract_id,
token_id,
new_owner_id,
receipt_id,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 5 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ diesel::table! {
mb_internal_id -> Nullable<Numeric>,
price -> Nullable<Numeric>,
minters_allowlist -> Nullable<Array<Nullable<Text>>>,
royalties -> Nullable<Jsonb>,
royalty_percent -> Nullable<Numeric>,
max_supply -> Nullable<Numeric>,
last_possible_mint -> Nullable<Timestamp>,
is_locked -> Nullable<Bool>,
}
}

Expand Down

0 comments on commit 1f9ef8f

Please sign in to comment.