difftreelog
Merge pull request #210 from UniqueNetwork/feature/CORE-180-v2
in: master
CORE-180. burnItem
10 files changed
launch-config.jsondiffbeforeafterboth--- a/launch-config.json
+++ b/launch-config.json
@@ -66,7 +66,9 @@
"name": "alice",
"flags": [
"--rpc-cors=all",
- "--rpc-port=9933", "--unsafe-ws-external"
+ "--rpc-port=9933",
+ "--unsafe-rpc-external",
+ "--unsafe-ws-external"
]
},
{
@@ -76,7 +78,9 @@
"name": "bob",
"flags": [
"--rpc-cors=all",
- "--rpc-port=9934", "--unsafe-rpc-external"
+ "--rpc-port=9934",
+ "--unsafe-rpc-external",
+ "--unsafe-ws-external"
]
}
]
pallets/nft/src/eth/erc.rsdiffbeforeafterboth--- a/pallets/nft/src/eth/erc.rs
+++ b/pallets/nft/src/eth/erc.rs
@@ -194,7 +194,8 @@
let caller = T::CrossAccountId::from_eth(caller);
let token_id = token_id.try_into().map_err(|_| "amount overflow")?;
- <Module<T>>::burn_item_internal(&caller, &self, token_id, 1).map_err(|_| "burn error")?;
+ <Module<T>>::burn_item_internal(&caller, &self, token_id, 1, true)
+ .map_err(|_| "burn error")?;
Ok(())
}
}
pallets/nft/src/lib.rsdiffbeforeafterboth992 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);992 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);993 let target_collection = Self::get_collection(collection_id)?;993 let target_collection = Self::get_collection(collection_id)?;994994995 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;995 Self::burn_item_internal(&sender, &target_collection, item_id, value, true)?;996996997 target_collection.submit_logs()997 target_collection.submit_logs()998 }998 }9991000 /// Destroys a concrete instance of NFT on behalf of the owner1001 /// See also: [`approve`]1002 ///1003 /// # Permissions1004 ///1005 /// * Collection Owner.1006 /// * Collection Admin.1007 /// * Current NFT Owner.1008 ///1009 /// # Arguments1010 ///1011 /// * collection_id: ID of the collection.1012 ///1013 /// * item_id: ID of NFT to burn.1014 ///1015 /// * from: owner of item1016 #[weight = <SelfWeightOf<T>>::burn_item()]1017 #[transactional]1018 pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> DispatchResult {10191020 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1021 let target_collection = Self::get_collection(collection_id)?;10221023 Self::burn_from_internal(&sender, &target_collection, &from, item_id, value)?;10241025 target_collection.submit_logs()1026 }99910271000 /// Change ownership of the token.1028 /// Change ownership of the token.1001 ///1029 ///1593 }1621 }159416221595 pub fn burn_item_internal(1623 pub fn burn_item_internal(1596 sender: &T::CrossAccountId,1624 owner: &T::CrossAccountId,1597 collection: &CollectionHandle<T>,1625 collection: &CollectionHandle<T>,1598 item_id: TokenId,1626 item_id: TokenId,1599 value: u128,1627 value: u128,1628 allow_escalation: bool,1600 ) -> DispatchResult {1629 ) -> DispatchResult {1601 ensure!(1630 ensure!(1602 Self::is_item_owner(sender, collection, item_id)?1631 Self::is_item_owner(owner, collection, item_id)?1603 || (collection.limits.owner_can_transfer1632 || (allow_escalation1633 && collection.limits.owner_can_transfer1604 && Self::is_owner_or_admin_permissions(collection, sender)?),1634 && Self::is_owner_or_admin_permissions(collection, owner)?),1605 Error::<T>::NoPermission1635 Error::<T>::NoPermission1606 );1636 );160716371608 if collection.access == AccessMode::WhiteList {1638 if collection.access == AccessMode::WhiteList {1609 Self::check_white_list(collection, sender)?;1639 Self::check_white_list(collection, owner)?;1610 }1640 }161116411612 match collection.mode {1642 match collection.mode {1613 CollectionMode::NFT => Self::burn_nft_item(collection, item_id)?,1643 CollectionMode::NFT => match value {1644 1 => Self::burn_nft_item(collection, item_id)?,1645 0 => (),1646 _ => fail!(<Error<T>>::TokenValueTooLow),1647 },1614 CollectionMode::Fungible(_) => Self::burn_fungible_item(sender, collection, value)?,1648 CollectionMode::Fungible(_) => Self::burn_fungible_item(collection, owner, value)?,1615 CollectionMode::ReFungible => Self::burn_refungible_item(collection, item_id, sender)?,1649 CollectionMode::ReFungible => {1650 Self::burn_refungible_item(collection, item_id, owner, value)?1651 }1652 _ => (),1616 };1653 };161716541618 Ok(())1655 Ok(())1619 }1656 }16571658 pub fn burn_from_internal(1659 sender: &T::CrossAccountId,1660 collection: &CollectionHandle<T>,1661 from: &T::CrossAccountId,1662 item_id: TokenId,1663 amount: u128,1664 ) -> DispatchResult {1665 if sender == from {1666 // Transfer by `from`, because it is either equal to sender, or derived from him1667 return Self::burn_item_internal(from, collection, item_id, amount, true);1668 }16691670 // Check approval1671 collection.consume_sload()?;1672 let approval: u128 =1673 <Allowances<T>>::get(collection.id, (item_id, from.as_sub(), sender.as_sub()));16741675 // Transfer permissions check1676 ensure!(1677 approval >= amount1678 || (collection.limits.owner_can_transfer1679 && Self::is_owner_or_admin_permissions(collection, sender)?),1680 Error::<T>::NoPermission1681 );16821683 if collection.access == AccessMode::WhiteList {1684 Self::check_white_list(collection, sender)?;1685 }16861687 // Reduce approval by transferred amount or remove if remaining approval drops to 01688 let allowance = approval.saturating_sub(amount);1689 collection.consume_sstore()?;1690 if allowance > 0 {1691 <Allowances<T>>::insert(1692 collection.id,1693 (item_id, from.as_sub(), sender.as_sub()),1694 allowance,1695 );1696 } else {1697 <Allowances<T>>::remove(collection.id, (item_id, from.as_sub(), sender.as_sub()));1698 }16991700 // Escalation is disallowed here, because we need to be sure that passed owner is real1701 Self::burn_item_internal(from, collection, item_id, amount, false)?;17021703 if matches!(collection.mode, CollectionMode::Fungible(_)) {1704 collection.log(ERC20Events::Approval {1705 owner: *from.as_eth(),1706 spender: *sender.as_eth(),1707 value: allowance.into(),1708 })?;1709 }17101711 Ok(())1712 }162017131621 pub fn toggle_white_list_internal(1714 pub fn toggle_white_list_internal(1622 sender: &T::CrossAccountId,1715 sender: &T::CrossAccountId,1884 collection: &CollectionHandle<T>,1977 collection: &CollectionHandle<T>,1885 item_id: TokenId,1978 item_id: TokenId,1886 owner: &T::CrossAccountId,1979 owner: &T::CrossAccountId,1980 value: u128,1887 ) -> DispatchResult {1981 ) -> DispatchResult {1888 let collection_id = collection.id;1982 let collection_id = collection.id;188919831890 let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)1984 let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)1891 .ok_or(Error::<T>::TokenNotFound)?;1985 .ok_or(Error::<T>::TokenNotFound)?;1892 let rft_balance = token1986 let mut rft_balance = token1893 .owner1987 .owner1894 .iter()1988 .iter()1895 .find(|&i| i.owner == *owner)1989 .find(|&i| i.owner == *owner)1896 .ok_or(Error::<T>::TokenNotFound)?;1990 .ok_or(Error::<T>::TokenNotFound)?1991 .clone();1897 Self::remove_token_index(collection, item_id, owner)?;1992 Self::remove_token_index(collection, item_id, owner)?;189819931899 // update balance1994 // update balance1900 let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.as_sub())1995 let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.as_sub())1901 .checked_sub(rft_balance.fraction)1996 .checked_sub(value)1902 .ok_or(Error::<T>::NumOverflow)?;1997 .ok_or(Error::<T>::NumOverflow)?;1903 <Balance<T>>::insert(collection_id, rft_balance.owner.as_sub(), new_balance);1998 <Balance<T>>::insert(collection_id, rft_balance.owner.as_sub(), new_balance);190419991905 // Re-create owners list with sender removed2000 rft_balance.fraction = (rft_balance.fraction)2001 .checked_sub(value)2002 .ok_or(Error::<T>::NumOverflow)?;20031906 let index = token2004 let index = token1907 .owner2005 .owner1908 .iter()2006 .iter()1909 .position(|i| i.owner == *owner)2007 .position(|i| i.owner == *owner)1910 .expect("owned item is exists");2008 .expect("owned item is exists");2009 if rft_balance.fraction == 0 {2010 // Re-create owners list with sender removed1911 token.owner.remove(index);2011 token.owner.remove(index);2012 } else {2013 token.owner[index] = rft_balance;2014 }20151912 let owner_count = token.owner.len();2016 let owner_count = token.owner.len();191320171947 }2051 }194820521949 fn burn_fungible_item(2053 fn burn_fungible_item(1950 owner: &T::CrossAccountId,2054 collection: &CollectionHandle<T>,1951 collection: &CollectionHandle<T>,2055 owner: &T::CrossAccountId,1952 value: u128,2056 value: u128,1953 ) -> DispatchResult {2057 ) -> DispatchResult {1954 let collection_id = collection.id;2058 let collection_id = collection.id;pallets/nft/src/tests.rsdiffbeforeafterboth--- a/pallets/nft/src/tests.rs
+++ b/pallets/nft/src/tests.rs
@@ -707,9 +707,15 @@
assert_eq!(TemplateModule::balance_count(1, 1), 1);
// burn item
- assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 5));
+ assert_ok!(TemplateModule::burn_item(
+ origin1.clone(),
+ 1,
+ 1,
+ account(1),
+ 5
+ ));
assert_noop!(
- TemplateModule::burn_item(origin1, 1, 1, 5),
+ TemplateModule::burn_item(origin1, 1, 1, account(1), 5),
Error::<Test>::TokenNotFound
);
@@ -736,9 +742,15 @@
assert_eq!(TemplateModule::balance_count(1, 1), 5);
// burn item
- assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 5));
+ assert_ok!(TemplateModule::burn_item(
+ origin1.clone(),
+ 1,
+ 1,
+ account(1),
+ 5
+ ));
assert_noop!(
- TemplateModule::burn_item(origin1, 1, 1, 5),
+ TemplateModule::burn_item(origin1, 1, 1, account(1), 5),
Error::<Test>::TokenValueNotEnough
);
@@ -781,9 +793,15 @@
assert_eq!(TemplateModule::balance_count(1, 1), 1023);
// burn item
- assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 1023));
+ assert_ok!(TemplateModule::burn_item(
+ origin1.clone(),
+ 1,
+ 1,
+ account(1),
+ 1023
+ ));
assert_noop!(
- TemplateModule::burn_item(origin1, 1, 1, 1023),
+ TemplateModule::burn_item(origin1, 1, 1, account(1), 1023),
Error::<Test>::TokenNotFound
);
@@ -1378,7 +1396,7 @@
AccessMode::WhiteList
));
assert_noop!(
- TemplateModule::burn_item(origin1, 1, 1, 5),
+ TemplateModule::burn_item(origin1.clone(), 1, 1, account(1), 5),
Error::<Test>::AddresNotInWhiteList
);
});
tests/.vscode/settings.jsondiffbeforeafterboth--- a/tests/.vscode/settings.json
+++ b/tests/.vscode/settings.json
@@ -1,3 +1,5 @@
{
- "mocha.enabled": true
-}
\ No newline at end of file
+ "mocha.enabled": true,
+ "mochaExplorer.files": "**/*.test.ts",
+ "mochaExplorer.require": "ts-node/register"
+}
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -20,6 +20,10 @@
"tslint": "^6.1.3",
"typescript": "^4.2.4"
},
+ "mocha": {
+ "timeout": 9999999,
+ "require": "ts-node/register"
+ },
"scripts": {
"lint": "eslint --ext .ts,.js src/",
"fix": "eslint --ext .ts,.js src/ --fix",
tests/src/burnItem.test.tsdiffbeforeafterboth--- a/tests/src/burnItem.test.ts
+++ b/tests/src/burnItem.test.ts
@@ -38,7 +38,7 @@
const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
await usingApi(async (api) => {
- const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
const events = await submitTransactionAsync(alice, tx);
const result = getGenericResult(events);
// Get the item
@@ -79,7 +79,7 @@
const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
await usingApi(async (api) => {
- const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 100);
const events = await submitTransactionAsync(alice, tx);
const result = getGenericResult(events);
@@ -107,14 +107,14 @@
const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
// Bob burns his portion
- const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
const events2 = await submitTransactionAsync(bob, tx);
const result2 = getGenericResult(events2);
- // Get balances
+ // Get balances
const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
// console.log(balance);
-
+
// What to expect before burning
expect(result1.success).to.be.true;
expect(balanceBefore).to.be.not.null;
@@ -152,7 +152,7 @@
await addCollectionAdminExpectSuccess(alice, collectionId, bob);
await usingApi(async (api) => {
- const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
const events = await submitTransactionAsync(bob, tx);
const result = getGenericResult(events);
// Get the item
@@ -164,6 +164,48 @@
expect(item).to.be.null;
});
});
+
+
+ it('Burn item in Fungible collection', async () => {
+ const createMode = 'Fungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens
+ await addCollectionAdminExpectSuccess(alice, collectionId, bob);
+
+ await usingApi(async (api) => {
+ // Destroy 1 of 10
+ const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);
+ const events = await submitTransactionAsync(bob, tx);
+ const result = getGenericResult(events);
+
+ // Get alice balance
+ const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();
+
+ // What to expect
+ expect(result.success).to.be.true;
+ expect(balance).to.be.not.null;
+ expect(balance.Value).to.be.equal(9);
+ });
+ });
+
+ it('Burn item in ReFungible collection', async () => {
+ const createMode = 'ReFungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+ await addCollectionAdminExpectSuccess(alice, collectionId, bob);
+
+ await usingApi(async (api) => {
+ const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);
+ const events = await submitTransactionAsync(bob, tx);
+ const result = getGenericResult(events);
+ // Get alice balance
+ const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
+
+ // What to expect
+ expect(result.success).to.be.true;
+ expect(balance).to.be.null;
+ });
+ });
});
describe('Negative integration test: ext. burnItem():', () => {
@@ -197,7 +239,7 @@
const tokenId = 10;
await usingApi(async (api) => {
- const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
const badTransaction = async function () {
await submitTransactionExpectFailAsync(alice, tx);
};
@@ -228,7 +270,7 @@
await usingApi(async (api) => {
- const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+ const burntx = api.tx.nft.burnItem(collectionId, tokenId, 1);
const events1 = await submitTransactionAsync(alice, burntx);
const result1 = getGenericResult(events1);
expect(result1.success).to.be.true;
tests/src/transfer.test.tsdiffbeforeafterboth--- a/tests/src/transfer.test.ts
+++ b/tests/src/transfer.test.ts
@@ -199,7 +199,7 @@
const reFungibleCollectionId = await
createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
- await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+ await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 100);
await transferExpectFailure(
reFungibleCollectionId,
newReFungibleTokenId,
tests/src/transferFrom.test.tsdiffbeforeafterboth--- a/tests/src/transferFrom.test.ts
+++ b/tests/src/transferFrom.test.ts
@@ -248,7 +248,7 @@
const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
await burnItemExpectSuccess(Alice, nftCollectionId, newNftTokenId, 1);
await approveExpectFail(nftCollectionId, newNftTokenId, Alice, Bob);
- await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1);
+ await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1);
});
});
it( 'transferFrom burnt token before approve Fungible', async () => {
@@ -258,20 +258,20 @@
await burnItemExpectSuccess(Alice, fungibleCollectionId, 1, 10);
await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1);
-
+
});
- });
+ });
it( 'transferFrom burnt token before approve ReFungible', async () => {
await usingApi(async () => {
const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
- await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+ await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 100);
await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice, Charlie, 1);
-
+
});
});
-
+
it( 'transferFrom burnt token after approve NFT', async () => {
await usingApi(async () => {
// nft
@@ -279,7 +279,7 @@
const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');
await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);
await burnItemExpectSuccess(Alice, nftCollectionId, newNftTokenId, 1);
- await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1);
+ await transferFromExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, Charlie, 1);
});
});
it( 'transferFrom burnt token after approve Fungible', async () => {
@@ -289,17 +289,17 @@
await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);
await burnItemExpectSuccess(Alice, fungibleCollectionId, 1, 10);
await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice, Charlie, 1);
-
+
});
- });
+ });
it( 'transferFrom burnt token after approve ReFungible', async () => {
await usingApi(async () => {
const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
- await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+ await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 100);
await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice, Charlie, 1);
-
+
});
});
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -700,10 +700,10 @@
ReFungible: CreateReFungibleData;
};
-export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {
+export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value = 1) {
await usingApi(async (api) => {
const tx = api.tx.nft.burnItem(collectionId, tokenId, value);
- const events = await submitTransactionAsync(owner, tx);
+ const events = await submitTransactionAsync(sender, tx);
const result = getGenericResult(events);
// Get the item
const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();