Skip to content

Commit

Permalink
refactor: transfer_should_workd
Browse files Browse the repository at this point in the history
  • Loading branch information
chungquantin committed Dec 6, 2024
1 parent 1afd4ba commit 672ceae
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions pallets/nfts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn lifecycle_should_work() {
account(20),
default_item_config()
));
assert_eq!(AccountBalance::<Test>::get(0, account(20)), 1);
assert_eq!(AccountBalance::<Test>::get(collection_id, account(20)), 1);
assert_eq!(Balances::reserved_balance(&owner), 7);
assert_ok!(Nfts::mint(
RuntimeOrigin::signed(owner.clone()),
Expand Down Expand Up @@ -576,57 +576,70 @@ fn mint_should_work() {
#[test]
fn transfer_should_work() {
new_test_ext().execute_with(|| {
let collection_id = 0;
let item_id = 42;
let owner = account(1);
assert_ok!(Nfts::force_create(
RuntimeOrigin::root(),
account(1),
owner.clone(),
default_collection_config()
));
assert_ok!(Nfts::force_mint(
RuntimeOrigin::signed(account(1)),
0,
42,
collection_id,
item_id,
account(2),
default_item_config()
));
assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(3)));
assert_eq!(AccountBalance::<Test>::get(0, account(2)), 0);
assert_eq!(AccountBalance::<Test>::get(0, account(3)), 1);
assert_eq!(items(), vec![(account(3), 0, 42)]);
assert_ok!(Nfts::transfer(
RuntimeOrigin::signed(account(2)),
collection_id,
item_id,
account(3)
));
assert_eq!(AccountBalance::<Test>::get(collection_id, account(2)), 0);
assert_eq!(AccountBalance::<Test>::get(collection_id, account(3)), 1);
assert_eq!(items(), vec![(account(3), collection_id, item_id)]);
assert_noop!(
Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4)),
Nfts::transfer(RuntimeOrigin::signed(account(2)), collection_id, item_id, account(4)),
Error::<Test>::NoPermission
);

assert_ok!(Nfts::approve_transfer(
RuntimeOrigin::signed(account(3)),
0,
42,
collection_id,
item_id,
account(2),
None
));
assert_ok!(Nfts::transfer(RuntimeOrigin::signed(account(2)), 0, 42, account(4)));
assert_eq!(AccountBalance::<Test>::get(0, account(3)), 0);
assert_eq!(AccountBalance::<Test>::get(0, account(4)), 1);
assert_ok!(Nfts::transfer(
RuntimeOrigin::signed(account(2)),
collection_id,
item_id,
account(4)
));
assert_eq!(AccountBalance::<Test>::get(collection_id, account(3)), 0);
assert_eq!(AccountBalance::<Test>::get(collection_id, account(4)), 1);
// validate we can't transfer non-transferable items
let collection_id = 1;
assert_ok!(Nfts::force_create(
RuntimeOrigin::root(),
account(1),
owner.clone(),
collection_config_from_disabled_settings(
CollectionSetting::TransferableItems | CollectionSetting::DepositRequired
)
));

assert_ok!(Nfts::force_mint(
RuntimeOrigin::signed(account(1)),
1,
RuntimeOrigin::signed(owner.clone()),
collection_id,
1,
account(42),
default_item_config()
));

assert_noop!(
Nfts::transfer(RuntimeOrigin::signed(account(1)), collection_id, 42, account(3)),
Nfts::transfer(RuntimeOrigin::signed(owner), collection_id, item_id, account(3)),
Error::<Test>::ItemsNonTransferable
);
});
Expand Down

0 comments on commit 672ceae

Please sign in to comment.