forked from inkdevhub/Investment-dao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67432e7
commit be889f0
Showing
2 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,34 @@ | ||
# Implement PSP2 + PSP22Metadata | ||
[package] | ||
name = "gtoken" | ||
version = "1.0.0" | ||
edition = "2021" | ||
authors = ["The best developer ever"] | ||
|
||
[dependencies] | ||
|
||
#ink = { version = "4.1.0", default-features = false } # DEL | ||
ink = { version = "4.2.1", default-features = false } # ADD | ||
|
||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true } | ||
|
||
# Include brush as a dependency and enable default implementation for PSP22 via brush feature | ||
#openbrush = { tag = "3.1.0", git = "https://github.com/727-Ventures/openbrush-contracts", default-features = false, features = ["psp22", "ownable"] } # DEL | ||
openbrush = { tag = "4.0.0-beta", git = "https://github.com/Brushfam/openbrush-contracts", default-features = false, features = ["psp22", "ownable"] } # ADD | ||
|
||
[lib] | ||
path = "lib.rs" | ||
crate-type = [ | ||
"rlib", | ||
] | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink/std", | ||
"scale/std", | ||
"scale-info/std", | ||
|
||
"openbrush/std", | ||
] | ||
ink-as-dependency = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,81 @@ | ||
// Implement PSP2 + PSP22Metadata | ||
#![cfg_attr(not(feature = "std"), no_std, no_main)] | ||
|
||
#[openbrush::implementation(PSP22, PSP22Metadata)] | ||
#[openbrush::contract] | ||
pub mod my_psp22 { | ||
use openbrush::traits::Storage; | ||
|
||
#[ink(storage)] | ||
#[derive(Default, Storage)] | ||
pub struct Contract { | ||
#[storage_field] | ||
psp22: psp22::Data, | ||
#[storage_field] | ||
metadata: metadata::Data, | ||
} | ||
|
||
impl Contract { | ||
#[ink(constructor)] | ||
pub fn new(total_supply: Balance, name: Option<String>, symbol: Option<String>, decimal: u8) -> Self { | ||
let mut instance = Self::default(); | ||
let caller = instance.env().caller(); | ||
|
||
instance.metadata.name.set(&name); | ||
instance.metadata.symbol.set(&symbol); | ||
instance.metadata.decimals.set(&decimal); | ||
|
||
psp22::Internal::_mint_to(&mut instance, caller, total_supply).expect("Should mint total_supply"); | ||
|
||
instance | ||
} | ||
} | ||
|
||
#[cfg(all(test, feature = "e2e-tests"))] | ||
pub mod tests { | ||
use openbrush::contracts::psp22::extensions::metadata::psp22metadata_external::PSP22Metadata; | ||
|
||
#[rustfmt::skip] | ||
use super::*; | ||
#[rustfmt::skip] | ||
use ink_e2e::build_message; | ||
|
||
type E2EResult<T> = Result<T, Box<dyn std::error::Error>>; | ||
|
||
#[ink_e2e::test] | ||
async fn metadata_works(client: ink_e2e::Client<C, E>) -> E2EResult<()> { | ||
let _name = String::from("TOKEN"); | ||
let _symbol = String::from("TKN"); | ||
|
||
let constructor = ContractRef::new(1000, Some(_name), Some(_symbol), 18); | ||
let address = client | ||
.instantiate("my_psp22_metadata", &ink_e2e::alice(), constructor, 0, None) | ||
.await | ||
.expect("instantiate failed") | ||
.account_id; | ||
|
||
let token_name = { | ||
let _msg = build_message::<ContractRef>(address.clone()).call(|contract| contract.token_name()); | ||
client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await | ||
} | ||
.return_value(); | ||
|
||
let token_symbol = { | ||
let _msg = build_message::<ContractRef>(address.clone()).call(|contract| contract.token_symbol()); | ||
client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await | ||
} | ||
.return_value(); | ||
|
||
let token_decimals = { | ||
let _msg = build_message::<ContractRef>(address.clone()).call(|contract| contract.token_decimals()); | ||
client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await | ||
} | ||
.return_value(); | ||
|
||
assert!(matches!(token_name, Some(_name))); | ||
assert!(matches!(token_symbol, Some(_symbol))); | ||
assert!(matches!(token_decimals, 18)); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} |