Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial step 6 - Uplift to latest ink! and Openbrush #17

Open
wants to merge 2 commits into
base: tutorial/events
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions contracts/shiden34/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
[package]
name = "shiden34"
version = "1.0.0"
version = "3.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 }

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

[lib]
name = "shiden34"
path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
]

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",

"openbrush/std",
"payable_mint_pkg/std",
]
ink-as-dependency = []
ink-as-dependency = []
146 changes: 102 additions & 44 deletions contracts/shiden34/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![feature(min_specialization)]
#![cfg_attr(not(feature = "std"), no_std, no_main)]

#[openbrush::implementation(PSP34, PSP34Enumerable, PSP34Metadata, PSP34Mintable, Ownable)]
#[openbrush::contract]
pub mod shiden34 {
use ink::codegen::{EmitEvent, Env};
use openbrush::{
contracts::ownable::*,
contracts::psp34::extensions::{enumerable::*, metadata::*},
traits::{Storage, String},
};
use openbrush::traits::Storage;
use payable_mint_pkg::impls::payable_mint::*;
use payable_mint_pkg::traits::payable_mint::*;

#[ink(storage)]
#[derive(Default, Storage)]
pub struct Shiden34 {
#[storage_field]
psp34: psp34::Data<enumerable::Balances>,
psp34: psp34::Data,
#[storage_field]
ownable: ownable::Data,
#[storage_field]
metadata: metadata::Data,
#[storage_field]
enumerable: enumerable::Data,
#[storage_field]
payable_mint: types::Data,
}

Expand All @@ -48,30 +46,57 @@ pub mod shiden34 {
approved: bool,
}

impl PSP34 for Shiden34 {}
impl Ownable for Shiden34 {}
impl PSP34Enumerable for Shiden34 {}
impl PSP34Metadata for Shiden34 {}
impl PayableMint for Shiden34 {}
// 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::Internal for Shiden34 {}

impl payable_mint::PayableMintImpl for Shiden34 {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, atm, otherwise the code won't compile


impl PayableMint for Shiden34 {
#[ink(message, payable)]
fn mint(&mut self, to: AccountId, mint_amount: u64) -> Result<(), PSP34Error> {
payable_mint::PayableMintImpl::mint(self, to, mint_amount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this is a good way. You have 2 implementation of basically the same trait
It should be as simple as


impl payable_mint::PayableMint for Shiden34Contract {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be ideal. I will investigate how to do that with the latest Openbrush

}

#[ink(message)]
#[openbrush::modifiers(only_owner)]
fn set_base_uri(&mut self, uri: String) -> Result<(), PSP34Error> {
payable_mint::PayableMintImpl::set_base_uri(self, uri)
}

#[ink(message)]
fn token_uri(&self, token_id: u64) -> Result<String, PSP34Error> {
payable_mint::PayableMintImpl::token_uri(self, token_id)
}

#[ink(message)]
#[openbrush::modifiers(only_owner)]
fn withdraw(&mut self) -> Result<(), PSP34Error> {
payable_mint::PayableMintImpl::withdraw(self)
}

impl psp34::Internal for Shiden34 {
fn _emit_transfer_event(&self, from: Option<AccountId>, to: Option<AccountId>, id: Id) {
self.env().emit_event(Transfer { from, to, id });
#[ink(message)]
fn max_supply(&self) -> u64 {
payable_mint::PayableMintImpl::max_supply(self)
}

fn _emit_approval_event(
&self,
from: AccountId,
to: AccountId,
id: Option<Id>,
approved: bool,
) {
self.env().emit_event(Approval {
from,
to,
id,
approved,
});
#[ink(message)]
fn price(&self) -> Balance {
payable_mint::PayableMintImpl::price(self)
}
}

Expand All @@ -85,11 +110,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 = psp34::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 Down Expand Up @@ -124,20 +165,37 @@ pub mod shiden34 {
set_sender(accounts.bob);
let num_of_mints: u64 = 5;

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!(
payable_mint::PayableMintImpl::mint(&mut sh34, accounts.bob, num_of_mints).is_ok()
);
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!(
sh34.owners_token_by_index(accounts.bob, 5),
PSP34EnumerableImpl::owners_token_by_index(&sh34, accounts.bob, 5),
Err(TokenNotExists)
);
assert_eq!(5, ink::env::test::recorded_events().count());
Expand Down
12 changes: 5 additions & 7 deletions logics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
[package]
name = "payable_mint_pkg"
version = "0.3.0"
version = "3.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 }

openbrush = { tag = "3.0.0", git = "https://github.com/727-Ventures/openbrush-contracts", default-features = false, features = ["psp34", "ownable"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
openbrush = { tag = "v4.0.0-beta", git = "https://github.com/Brushfam/openbrush-contracts", default-features = false, features = ["psp34", "ownable"] }

[lib]
path = "lib.rs"
Expand All @@ -23,4 +21,4 @@ std = [
"scale/std",
"scale-info",
"openbrush/std",
]
]
Loading