Skip to content

Commit

Permalink
Add TokenId to MetadataValue (#20)
Browse files Browse the repository at this point in the history
* add TokenId to MetadataValue

* add TokenId to README
  • Loading branch information
jonmattgray authored Jan 13, 2022
1 parent 7b7d8d4 commit 1e52ee9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 17 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ In order to use the API within `polkadot.js` you'll need to configure the follow
"_enum": {
"File": "Hash",
"Literal": "[u8; 32]",
"TokenId": "TokenId",
"None": "null"
}
},
Expand Down
4 changes: 2 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = '2018'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/vitalam-node/'
name = 'vitalam-node'
version = '2.5.1'
version = '2.6.0'

[[bin]]
name = 'vitalam-node'
Expand All @@ -25,7 +25,7 @@ structopt = '0.3.8'
hex-literal = "0.3.1"
bs58 = "0.4.0"

vitalam-node-runtime = { path = '../runtime', version = '2.0.1' }
vitalam-node-runtime = { path = '../runtime', version = '2.1.0' }

# Substrate dependencies
frame-benchmarking = '3.0.0'
Expand Down
2 changes: 1 addition & 1 deletion pallets/simple-nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = '2018'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/vitalam-node/'
name = 'pallet-simple-nft'
version = "2.0.0"
version = "2.1.0"


[package.metadata.docs.rs]
Expand Down
9 changes: 5 additions & 4 deletions pallets/simple-nft/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl system::Config for Test {
}

parameter_types! {
pub const MaxMetadataCount: u32 = 3;
pub const MaxMetadataCount: u32 = 4;
}

#[derive(Encode, Decode, Clone, PartialEq, Debug, Eq, Ord, PartialOrd)]
Expand All @@ -78,13 +78,14 @@ impl Default for Role {
}

#[derive(Encode, Decode, Clone, PartialEq, Debug, Eq)]
pub enum MetadataValue {
pub enum MetadataValue<TokenId> {
File(Hash),
Literal([u8; 1]),
TokenId(TokenId),
None,
}

impl Default for MetadataValue {
impl<T> Default for MetadataValue<T> {
fn default() -> Self {
MetadataValue::None
}
Expand All @@ -96,7 +97,7 @@ impl pallet_simple_nft::Config for Test {
type TokenId = u64;
type RoleKey = Role;
type TokenMetadataKey = u64;
type TokenMetadataValue = MetadataValue;
type TokenMetadataValue = MetadataValue<Self::TokenId>;

type WeightInfo = ();

Expand Down
40 changes: 39 additions & 1 deletion pallets/simple-nft/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,42 @@ fn it_works_for_creating_token_with_literal() {
});
}

#[test]
fn it_works_for_creating_token_with_token_id_in_metadata() {
new_test_ext().execute_with(|| {
// create a token with no parents
let roles = BTreeMap::from_iter(vec![(Default::default(), 1)]);
let metadata = BTreeMap::from_iter(vec![(0, MetadataValue::TokenId(0))]);
assert_ok!(SimpleNFTModule::run_process(
Origin::signed(1),
Vec::new(),
vec![Output {
roles: roles.clone(),
metadata: metadata.clone(),
parent_index: None
}]
));
// last token should be 1
assert_eq!(SimpleNFTModule::last_token(), 1);
// get the token
let token = SimpleNFTModule::tokens_by_id(1);
assert_eq!(
token,
Token {
id: 1,
original_id: 1,
roles: roles.clone(),
creator: 1,
created_at: 0,
destroyed_at: None,
metadata: metadata.clone(),
parents: Vec::new(),
children: None
}
);
});
}

#[test]
fn it_works_for_creating_token_with_no_metadata_value() {
new_test_ext().execute_with(|| {
Expand Down Expand Up @@ -123,7 +159,8 @@ fn it_works_for_creating_token_with_multiple_metadata_items() {
let metadata = BTreeMap::from_iter(vec![
(0, MetadataValue::File(H256::zero())),
(1, MetadataValue::Literal([0])),
(2, MetadataValue::None),
(2, MetadataValue::TokenId(0)),
(3, MetadataValue::None),
]);
assert_ok!(SimpleNFTModule::run_process(
Origin::signed(1),
Expand Down Expand Up @@ -930,6 +967,7 @@ fn it_fails_for_creating_single_token_with_too_many_metadata_items() {
(1, MetadataValue::None),
(2, MetadataValue::None),
(3, MetadataValue::None),
(4, MetadataValue::None),
]);
SimpleNFTModule::run_process(
Origin::signed(1),
Expand Down
4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ edition = '2018'
license = 'Apache-2.0'
repository = 'https://github.com/digicatapult/vitalam-node/'
name = 'vitalam-node-runtime'
version = '2.0.1'
version = '2.1.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
Expand All @@ -25,7 +25,7 @@ hex-literal = { optional = true, version = '0.3.1' }
serde = { features = ['derive'], optional = true, version = '1.0.101' }

# local dependencies
pallet-simple-nft = { version = '2.0.0', default-features = false, package = 'pallet-simple-nft', path = '../pallets/simple-nft' }
pallet-simple-nft = { version = '2.1.0', default-features = false, package = 'pallet-simple-nft', path = '../pallets/simple-nft' }
pallet-symmetric-key = { version = '1.0.1', default-features = false, package = 'pallet-symmetric-key', path = '../pallets/symmetric-key' }
frame-benchmarking = { default-features = false, optional = true, version = '3.0.0' }
frame-executive = { default-features = false, version = '3.0.0' }
Expand Down
9 changes: 5 additions & 4 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("vitalam-node"),
impl_name: create_runtime_str!("vitalam-node"),
authoring_version: 1,
spec_version: 201,
spec_version: 210,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -307,13 +307,14 @@ impl Default for Role {
}

#[derive(Encode, Decode, Clone, PartialEq, Debug, Eq)]
pub enum MetadataValue {
pub enum MetadataValue<TokenId> {
File(Hash),
Literal([u8; 32]),
TokenId(TokenId),
None,
}

impl Default for MetadataValue {
impl <T>Default for MetadataValue<T> {
fn default() -> Self {
MetadataValue::None
}
Expand All @@ -325,7 +326,7 @@ impl pallet_simple_nft::Config for Runtime {
type TokenId = u128;
type RoleKey = Role;
type TokenMetadataKey = [u8; 32];
type TokenMetadataValue = MetadataValue;
type TokenMetadataValue = MetadataValue<Self::TokenId>;
type WeightInfo = pallet_simple_nft::weights::SubstrateWeight<Runtime>;
type MaxMetadataCount = MaxMetadataCount;
}
Expand Down

0 comments on commit 1e52ee9

Please sign in to comment.