Skip to content

Commit

Permalink
feat: changed contract id to Nat
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Nov 8, 2023
1 parent 7db22b9 commit cf8f9b5
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 399 deletions.
312 changes: 18 additions & 294 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ version = "0.1.0"
async-trait = "0.1"
candid = "0.9"
chrono = "0.4"
ethereum-types = "0.14"
ic-cdk = "0.11"
ic-cdk-macros = "0.8"
ic-stable-structures = "0.6"
Expand Down
1 change: 0 additions & 1 deletion src/did/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ repository = { workspace = true }

[dependencies]
candid = { workspace = true }
ethereum-types = { workspace = true }
dip721 = { path = "../dip721" }
ic-stable-structures = { workspace = true }
num-bigint = { workspace = true }
Expand Down
79 changes: 2 additions & 77 deletions src/did/src/common/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,7 @@
//!
//! Common types
use std::fmt::Display;
use std::rc::Rc;

use candid::types::{Type, TypeInner};
use candid::{CandidType, Decode, Deserialize, Encode};
use ethereum_types::H160;
use ic_stable_structures::storable::Bound;
use ic_stable_structures::Storable;
use candid::Nat;

/// An identifier in the dilazionato environment. It has the same syntax as an Ethereum address
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ID(H160);

impl From<H160> for ID {
fn from(value: H160) -> Self {
Self(value)
}
}

impl ID {
/// Generate a random ID
pub fn random() -> Self {
H160::random().into()
}

/// Get the ID as a H160
pub fn as_h160(&self) -> &H160 {
&self.0
}

pub fn from_slice(slice: &[u8]) -> Self {
Self(ethereum_types::H160::from_slice(slice))
}

pub fn as_hex_str(&self) -> String {
format!("0x{:x}", self.0)
}

pub const fn zero() -> Self {
Self(ethereum_types::H160::zero())
}
}

impl Display for ID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.as_hex_str())
}
}

impl CandidType for ID {
fn _ty() -> candid::types::Type {
Type(Rc::new(TypeInner::Text))
}

fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
where
S: candid::types::Serializer,
{
serializer.serialize_text(&self.as_hex_str())
}
}

impl Storable for ID {
const BOUND: Bound = Bound::Bounded {
max_size: 30,
is_fixed_size: true,
};

fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
Encode!(&self.as_h160().as_bytes()).unwrap().into()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
let bytes: [u8; 20] = Decode!(&bytes, [u8; 20]).unwrap();
let h160 = H160::from_slice(&bytes);

Self::from(h160)
}
}
pub type ID = Nat;
4 changes: 2 additions & 2 deletions src/did/src/sell_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ mod test {
fn test_should_encode_token() {
let token = Token {
id: TokenIdentifier::from(1),
contract_id: ID::random(),
contract_id: ID::from(1),
owner: Some(
Principal::from_text(
"zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae",
Expand Down Expand Up @@ -277,7 +277,7 @@ mod test {
#[test]
fn test_should_encode_contract() {
let contract = Contract {
id: ID::random(),
id: ID::from(1),
seller: Principal::from_text(
"zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae",
)
Expand Down
10 changes: 5 additions & 5 deletions src/sell_contract/sell_contract.did
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type BuildingData = record { city : text };
type ConfigurationError = variant { CustodialsCantBeEmpty; AnonymousCustodial };
type Contract = record {
id : text;
id : nat;
value : nat64;
building : BuildingData;
seller : principal;
Expand All @@ -28,7 +28,7 @@ type TokenError = variant {
ContractValueIsNotMultipleOfInstallments;
TokenAlreadyExists : nat;
TokensMismatch;
ContractAlreadyExists : text;
ContractAlreadyExists : nat;
TokenDoesNotBelongToContract : nat;
TokenNotFound : nat;
ContractHasNoTokens;
Expand All @@ -38,7 +38,7 @@ type TokenError = variant {
};
service : (SellContractInitData) -> {
admin_register_contract : (
text,
nat,
principal,
vec principal,
text,
Expand All @@ -48,6 +48,6 @@ service : (SellContractInitData) -> {
) -> (Result);
admin_set_fly_canister : (principal) -> ();
admin_set_marketplace_canister : (principal) -> ();
get_contract : (text) -> (opt Contract) query;
get_contracts : () -> (vec text) query;
get_contract : (nat) -> (opt Contract) query;
get_contracts : () -> (vec nat) query;
}
37 changes: 19 additions & 18 deletions src/sell_contract/src/app/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Storage;

thread_local! {
/// Contracts storage (1 contract has many tokens)
static CONTRACTS: RefCell<BTreeMap<ID, Contract, VirtualMemory<DefaultMemoryImpl>>> =
static CONTRACTS: RefCell<BTreeMap<StorableNat, Contract, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(BTreeMap::new(MEMORY_MANAGER.with(|mm| mm.get(CONTRACTS_MEMORY_ID))));

/// Tokens storage (NFTs)
Expand All @@ -37,7 +37,7 @@ thread_local! {
impl Storage {
/// Get contract by id
pub fn get_contract(id: &ID) -> Option<Contract> {
CONTRACTS.with_borrow(|contracts| contracts.get(id).clone())
CONTRACTS.with_borrow(|contracts| contracts.get(&StorableNat::from(id.clone())).clone())
}

/// Insert contract
Expand Down Expand Up @@ -95,7 +95,8 @@ impl Storage {

Ok(())
})?;
CONTRACTS.with_borrow_mut(|contracts| contracts.insert(contract.id.clone(), contract));
CONTRACTS
.with_borrow_mut(|contracts| contracts.insert(contract.id.clone().into(), contract));

Ok(())
}
Expand All @@ -107,7 +108,7 @@ impl Storage {

/// get contracts
pub fn get_contracts() -> Vec<ID> {
CONTRACTS.with_borrow(|contracts| contracts.iter().map(|(key, _)| key.clone()).collect())
CONTRACTS.with_borrow(|contracts| contracts.iter().map(|(key, _)| key.0.clone()).collect())
}

/// Get tokens by contract
Expand Down Expand Up @@ -260,7 +261,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let next_token_id = Storage::total_supply();
assert_eq!(next_token_id, Nat::from(0));
let token_1 = Token {
Expand Down Expand Up @@ -328,7 +329,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down Expand Up @@ -366,7 +367,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let contract = Contract {
id: contract_id,
seller,
Expand All @@ -388,7 +389,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_id = TokenIdentifier::from(1);
let token_1 = Token {
id: token_id.clone(),
Expand Down Expand Up @@ -423,7 +424,7 @@ mod test {
operator: None,
};
let contract = Contract {
id: ID::random(),
id: ID::from(1),
seller,
buyers: vec![Principal::anonymous()],
tokens: vec![token_1.id.clone(), token_2.id.clone()],
Expand All @@ -442,7 +443,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand All @@ -461,7 +462,7 @@ mod test {
};
let token_2 = Token {
id: TokenIdentifier::from(2),
contract_id: ID::random(),
contract_id: ID::from(2),
owner: Some(seller),
value: 100,
is_burned: false,
Expand All @@ -476,7 +477,7 @@ mod test {
operator: None,
};
let contract = Contract {
id: ID::random(),
id: ID::from(1),
seller,
buyers: vec![Principal::anonymous()],
tokens: vec![token_1.id.clone(), token_2.id.clone()],
Expand All @@ -495,7 +496,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down Expand Up @@ -551,7 +552,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down Expand Up @@ -627,7 +628,7 @@ mod test {

#[test]
fn test_should_burn_token() {
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down Expand Up @@ -667,7 +668,7 @@ mod test {

#[test]
fn test_should_transfer_token() {
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down Expand Up @@ -722,7 +723,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down Expand Up @@ -780,7 +781,7 @@ mod test {
let seller =
Principal::from_text("zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae")
.unwrap();
let contract_id = ID::random();
let contract_id = ID::from(1);
let token_1 = Token {
id: TokenIdentifier::from(1),
contract_id: contract_id.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/sell_contract/src/app/storage/tx_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod test {
fn test_should_insert_transactions() {
let token = Token {
id: TokenIdentifier::from(1),
contract_id: ID::random(),
contract_id: ID::from(1),
owner: Some(
Principal::from_text(
"zrrb4-gyxmq-nx67d-wmbky-k6xyt-byhmw-tr5ct-vsxu4-nuv2g-6rr65-aae",
Expand Down

0 comments on commit cf8f9b5

Please sign in to comment.