Skip to content

Commit

Permalink
Merge pull request #12 from swankyhub/feat/migration-ink-4.2.1
Browse files Browse the repository at this point in the history
Uplift to ink 4.2.1 nas Openbrush 4.0.0-beta
  • Loading branch information
bobo-k2 authored Jul 26, 2023
2 parents 7c11b66 + 232ad8f commit afd774b
Show file tree
Hide file tree
Showing 14 changed files with 646 additions and 581 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ jobs:
# hacky way to install rust. Rustup is pre-installed on runners. Calling rustup show will detect the rust-toolchain.toml, and subsequently
# download the needed toolchain and components.
run: |
rustup toolchain install nightly
rustup toolchain install stable
rustup show
- name: Check
run: cargo +nightly check
run: cargo check

- name: Test
run: cargo +nightly test
run: cargo test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,5 @@ node_modules
oclif.manifest.json
artifacts/
types/
typed_contracts/

8 changes: 4 additions & 4 deletions contracts/shiden34/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "shiden34"
version = "1.0.0"
version = "1.1.0"
authors = ["Astar builder"]
edition = "2021"

[dependencies]
ink = { version = "~4.0.0", default-features = false}
ink = { version = "~4.2.1", default-features = false}

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }

openbrush = { tag = "3.0.0", git = "https://github.com/727-Ventures/openbrush-contracts", default-features = false, features = ["psp34", "ownable"] }
openbrush = { tag = "v4.0.0-beta", git = "https://github.com/Brushfam/openbrush-contracts", default-features = false, features = ["psp34", "ownable", "reentrancy_guard"] }
payable_mint_pkg = { path = "../../logics", default-features = false }

[lib]
Expand Down
196 changes: 117 additions & 79 deletions contracts/shiden34/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[openbrush::implementation(PSP34, PSP34Metadata, PSP34Enumerable, Ownable)]
#[openbrush::contract]
pub mod shiden34 {
use ink::codegen::{
Expand All @@ -9,30 +9,29 @@ pub mod shiden34 {
};
use openbrush::{
contracts::{
ownable::*,
psp34::extensions::{
enumerable::*,
metadata::*,
ownable,
psp34::{
extensions::{
enumerable,
metadata,
},
PSP34Impl,
},
reentrancy_guard::*,
},
traits::{
Storage,
String,
reentrancy_guard,
},
traits::Storage,
};

use payable_mint_pkg::{
impls::payable_mint::*,
traits::payable_mint::*,
use payable_mint_pkg::impls::payable_mint::{
payable_mint::*,
*,
};

// Shiden34Contract contract storage
#[ink(storage)]
#[derive(Default, Storage)]
pub struct Shiden34Contract {
#[storage_field]
psp34: psp34::Data<enumerable::Balances>,
psp34: psp34::Data,
#[storage_field]
guard: reentrancy_guard::Data,
#[storage_field]
Expand All @@ -41,13 +40,10 @@ pub mod shiden34 {
metadata: metadata::Data,
#[storage_field]
payable_mint: types::Data,
#[storage_field]
enumerable: enumerable::Data,
}

impl PSP34 for Shiden34Contract {}
impl PSP34Enumerable for Shiden34Contract {}
impl PSP34Metadata for Shiden34Contract {}
impl Ownable for Shiden34Contract {}

/// Event emitted when a token transfer occurs.
#[ink(event)]
pub struct Transfer {
Expand All @@ -71,6 +67,25 @@ pub mod shiden34 {
approved: bool,
}

// Override event emission methods
#[overrider(psp34::Internal)]
fn _emit_transfer_event(&self, from: Option<AccountId>, to: Option<AccountId>, id: Id) {
self.env().emit_event(Transfer { from, to, id });
}

#[overrider(psp34::Internal)]
fn _emit_approval_event(&self, from: AccountId, to: AccountId, id: Option<Id>, approved: bool) {
self.env().emit_event(Approval {
from,
to,
id,
approved,
});
}

impl payable_mint_pkg::impls::payable_mint::payable_mint::Internal for Shiden34Contract {}
impl PayableMintImpl for Shiden34Contract {}

impl Shiden34Contract {
#[ink(constructor)]
pub fn new(
Expand All @@ -81,11 +96,27 @@ pub mod shiden34 {
price_per_mint: Balance,
) -> Self {
let mut instance = Self::default();
instance._init_with_owner(instance.env().caller());
let collection_id = instance.collection_id();
instance._set_attribute(collection_id.clone(), String::from("name"), name);
instance._set_attribute(collection_id.clone(), String::from("symbol"), symbol);
instance._set_attribute(collection_id, String::from("baseUri"), base_uri);
let caller = instance.env().caller();
ownable::InternalImpl::_init_with_owner(&mut instance, caller);
let collection_id = PSP34Impl::collection_id(&instance);
metadata::InternalImpl::_set_attribute(
&mut instance,
collection_id.clone(),
String::from("name"),
name,
);
metadata::InternalImpl::_set_attribute(
&mut instance,
collection_id.clone(),
String::from("symbol"),
symbol,
);
metadata::InternalImpl::_set_attribute(
&mut instance,
collection_id,
String::from("baseUri"),
base_uri,
);
instance.payable_mint.max_supply = max_supply;
instance.payable_mint.price_per_mint = price_per_mint;
instance.payable_mint.last_token_id = 0;
Expand All @@ -94,30 +125,6 @@ pub mod shiden34 {
}
}

// Override event emission methods
impl psp34::Internal for Shiden34Contract {
fn _emit_transfer_event(&self, from: Option<AccountId>, to: Option<AccountId>, id: Id) {
self.env().emit_event(Transfer { from, to, id });
}

fn _emit_approval_event(
&self,
from: AccountId,
to: AccountId,
id: Option<Id>,
approved: bool,
) {
self.env().emit_event(Approval {
from,
to,
id,
approved,
});
}
}

impl PayableMint for Shiden34Contract {}

// ------------------- T E S T -----------------------------------------------------
#[cfg(test)]
mod tests {
Expand All @@ -128,8 +135,9 @@ pub mod shiden34 {
pay_with_call,
test,
},
prelude::string::String as PreludeString,
prelude::string::String,
};
use openbrush::contracts::psp34::PSP34Impl;
use payable_mint_pkg::impls::payable_mint::{
payable_mint::Internal,
types::Shiden34Error,
Expand All @@ -141,17 +149,29 @@ pub mod shiden34 {
#[ink::test]
fn init_works() {
let sh34 = init();
let collection_id = sh34.collection_id();
let collection_id = PSP34Impl::collection_id(&sh34);
assert_eq!(
sh34.get_attribute(collection_id.clone(), String::from("name")),
metadata::PSP34MetadataImpl::get_attribute(
&sh34,
collection_id.clone(),
String::from("name")
),
Some(String::from("Shiden34"))
);
assert_eq!(
sh34.get_attribute(collection_id.clone(), String::from("symbol")),
metadata::PSP34MetadataImpl::get_attribute(
&sh34,
collection_id.clone(),
String::from("symbol")
),
Some(String::from("SH34"))
);
assert_eq!(
sh34.get_attribute(collection_id, String::from("baseUri")),
metadata::PSP34MetadataImpl::get_attribute(
&sh34,
collection_id,
String::from("baseUri")
),
Some(String::from(BASE_URI))
);
assert_eq!(sh34.max_supply(), MAX_SUPPLY);
Expand All @@ -172,17 +192,20 @@ pub mod shiden34 {
fn mint_single_works() {
let mut sh34 = init();
let accounts = default_accounts();
assert_eq!(sh34.owner(), accounts.alice);
assert_eq!(Ownable::owner(&sh34).unwrap(), accounts.alice);
set_sender(accounts.bob);

assert_eq!(sh34.total_supply(), 0);
assert_eq!(PSP34Impl::total_supply(&sh34), 0);
test::set_value_transferred::<ink::env::DefaultEnvironment>(PRICE);
assert!(sh34.mint_next().is_ok());
assert_eq!(sh34.total_supply(), 1);
assert_eq!(sh34.owner_of(Id::U64(1)), Some(accounts.bob));
assert_eq!(sh34.balance_of(accounts.bob), 1);
assert_eq!(PSP34Impl::total_supply(&sh34), 1);
assert_eq!(PSP34Impl::owner_of(&sh34, Id::U64(1)), Some(accounts.bob));
assert_eq!(PSP34Impl::balance_of(&sh34, accounts.bob), 1);

assert_eq!(sh34.owners_token_by_index(accounts.bob, 0), Ok(Id::U64(1)));
assert_eq!(
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 0),
Ok(Id::U64(1))
);
assert_eq!(sh34.payable_mint.last_token_id, 1);
assert_eq!(1, ink::env::test::recorded_events().count());
}
Expand All @@ -202,21 +225,36 @@ pub mod shiden34 {
))
);

assert_eq!(sh34.total_supply(), 0);
assert_eq!(PSP34Impl::total_supply(&sh34), 0);
test::set_value_transferred::<ink::env::DefaultEnvironment>(
PRICE * num_of_mints as u128,
);
assert!(sh34.mint(accounts.bob, num_of_mints).is_ok());
assert_eq!(sh34.total_supply(), num_of_mints as u128);
assert_eq!(sh34.balance_of(accounts.bob), 5);
assert_eq!(sh34.owners_token_by_index(accounts.bob, 0), Ok(Id::U64(1)));
assert_eq!(sh34.owners_token_by_index(accounts.bob, 1), Ok(Id::U64(2)));
assert_eq!(sh34.owners_token_by_index(accounts.bob, 2), Ok(Id::U64(3)));
assert_eq!(sh34.owners_token_by_index(accounts.bob, 3), Ok(Id::U64(4)));
assert_eq!(sh34.owners_token_by_index(accounts.bob, 4), Ok(Id::U64(5)));
assert_eq!(PSP34Impl::total_supply(&sh34), num_of_mints as u128);
assert_eq!(PSP34Impl::balance_of(&sh34, accounts.bob), 5);
assert_eq!(
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 0),
Ok(Id::U64(1))
);
assert_eq!(
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 1),
Ok(Id::U64(2))
);
assert_eq!(
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 2),
Ok(Id::U64(3))
);
assert_eq!(
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 3),
Ok(Id::U64(4))
);
assert_eq!(
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 4),
Ok(Id::U64(5))
);
assert_eq!(5, ink::env::test::recorded_events().count());
assert_eq!(
sh34.owners_token_by_index(accounts.bob, 5),
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 5),
Err(TokenNotExists)
);
}
Expand All @@ -228,7 +266,7 @@ pub mod shiden34 {
set_sender(accounts.alice);
let num_of_mints: u64 = MAX_SUPPLY + 1;

assert_eq!(sh34.total_supply(), 0);
assert_eq!(PSP34Impl::total_supply(&sh34), 0);
test::set_value_transferred::<ink::env::DefaultEnvironment>(
PRICE * num_of_mints as u128,
);
Expand All @@ -246,7 +284,7 @@ pub mod shiden34 {
set_sender(accounts.bob);
let num_of_mints = 1;

assert_eq!(sh34.total_supply(), 0);
assert_eq!(PSP34Impl::total_supply(&sh34), 0);
test::set_value_transferred::<ink::env::DefaultEnvironment>(
PRICE * num_of_mints as u128 - 1,
);
Expand All @@ -261,7 +299,7 @@ pub mod shiden34 {
sh34.mint_next(),
Err(PSP34Error::Custom(Shiden34Error::BadMintValue.as_str()))
);
assert_eq!(sh34.total_supply(), 0);
assert_eq!(PSP34Impl::total_supply(&sh34), 0);
}

#[ink::test]
Expand Down Expand Up @@ -298,26 +336,26 @@ pub mod shiden34 {
assert_eq!(sh34.token_uri(42), Err(TokenNotExists));
assert_eq!(
sh34.token_uri(1),
Ok(PreludeString::from(BASE_URI.to_owned() + "1.json"))
Ok(String::from(BASE_URI.to_owned() + "1.json"))
);

// return error if request is for not yet minted token
assert_eq!(sh34.token_uri(42), Err(TokenNotExists));

// verify token_uri when baseUri is empty
set_sender(accounts.alice);
assert!(sh34.set_base_uri(PreludeString::from("")).is_ok());
assert!(sh34.set_base_uri(String::from("")).is_ok());
assert_eq!(
sh34.token_uri(1),
Ok("".to_owned() + &PreludeString::from("1.json"))
Ok("".to_owned() + &String::from("1.json"))
);
}

#[ink::test]
fn owner_is_set() {
let accounts = default_accounts();
let sh34 = init();
assert_eq!(sh34.owner(), accounts.alice);
assert_eq!(Ownable::owner(&sh34).unwrap(), accounts.alice);
}

#[ink::test]
Expand All @@ -327,10 +365,10 @@ pub mod shiden34 {
let mut sh34 = init();

set_sender(accounts.alice);
let collection_id = sh34.collection_id();
let collection_id = PSP34Impl::collection_id(&sh34);
assert!(sh34.set_base_uri(NEW_BASE_URI.into()).is_ok());
assert_eq!(
sh34.get_attribute(collection_id, String::from("baseUri")),
PSP34MetadataImpl::get_attribute(&sh34, collection_id, String::from("baseUri")),
Some(String::from(NEW_BASE_URI))
);
set_sender(accounts.bob);
Expand Down
Loading

0 comments on commit afd774b

Please sign in to comment.