difftreelog
Merge pull request #747 from UniqueNetwork/tests/refungible
in: master
Transfer tests
14 files changed
pallets/fungible/src/lib.rsdiffbeforeafterboth--- a/pallets/fungible/src/lib.rs
+++ b/pallets/fungible/src/lib.rs
@@ -379,7 +379,7 @@
let balance_from = <Balance<T>>::get((collection.id, from))
.checked_sub(amount)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
- let balance_to = if from != to {
+ let balance_to = if from != to && amount != 0 {
Some(
<Balance<T>>::get((collection.id, to))
.checked_add(amount)
@@ -391,16 +391,17 @@
// =========
- <PalletStructure<T>>::nest_if_sent_to_token(
- from.clone(),
- to,
- collection.id,
- TokenId::default(),
- nesting_budget,
- )?;
-
if let Some(balance_to) = balance_to {
- // from != to
+ // from != to && amount != 0
+
+ <PalletStructure<T>>::nest_if_sent_to_token(
+ from.clone(),
+ to,
+ collection.id,
+ TokenId::default(),
+ nesting_budget,
+ )?;
+
if balance_from == 0 {
<Balance<T>>::remove((collection.id, from));
<PalletStructure<T>>::unnest_if_nested(from, collection.id, TokenId::default());
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -291,6 +291,7 @@
<CommonWeights<T>>::burn_item(),
)
} else {
+ <Pallet<T>>::check_token_immediate_ownership(self, token, &sender)?;
Ok(().into())
}
}
@@ -320,6 +321,7 @@
<CommonWeights<T>>::transfer(),
)
} else {
+ <Pallet<T>>::check_token_immediate_ownership(self, token, &from)?;
Ok(().into())
}
}
@@ -360,6 +362,8 @@
<CommonWeights<T>>::transfer_from(),
)
} else {
+ <Pallet<T>>::check_allowed(self, &sender, &from, token, nesting_budget)?;
+
Ok(().into())
}
}
@@ -380,6 +384,8 @@
<CommonWeights<T>>::burn_from(),
)
} else {
+ <Pallet<T>>::check_allowed(self, &sender, &from, token, nesting_budget)?;
+
Ok(().into())
}
}
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -814,6 +814,20 @@
<PalletCommon<T>>::set_property_permission(collection, sender, permission)
}
+ pub fn check_token_immediate_ownership(
+ collection: &NonfungibleHandle<T>,
+ token: TokenId,
+ possible_owner: &T::CrossAccountId,
+ ) -> DispatchResult {
+ let token_data =
+ <TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
+ ensure!(
+ &token_data.owner == possible_owner,
+ <CommonError<T>>::NoPermission
+ );
+ Ok(())
+ }
+
/// Transfer NFT token from one account to another.
///
/// `from` account stops being the owner and `to` account becomes the owner of the token.
pallets/refungible/src/erc.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -34,6 +34,7 @@
CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,
erc::{CommonEvmHandler, CollectionCall, static_property::key},
eth::EthCrossAccount,
+ Error as CommonError,
};
use pallet_evm::{account::CrossAccountId, PrecompileHandle};
use pallet_evm_coder_substrate::{call, dispatch_to_evm};
@@ -508,6 +509,13 @@
) -> Result<()> {
collection.consume_store_reads(1)?;
let total_supply = <TotalSupply<T>>::get((collection.id, token));
+
+ if owner_balance == 0 {
+ return Err(dispatch_to_evm::<T>(
+ <CommonError<T>>::MustBeTokenOwner.into(),
+ ));
+ }
+
if total_supply != owner_balance {
return Err("token has multiple owners".into());
}
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -452,6 +452,10 @@
token: TokenId,
amount: u128,
) -> DispatchResult {
+ if <Balance<T>>::get((collection.id, token, owner)) == 0 {
+ return Err(<CommonError<T>>::TokenValueTooLow.into());
+ }
+
let total_supply = <TotalSupply<T>>::get((collection.id, token))
.checked_sub(amount)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
@@ -739,12 +743,17 @@
<PalletCommon<T>>::ensure_correct_receiver(to)?;
let initial_balance_from = <Balance<T>>::get((collection.id, token, from));
+
+ if initial_balance_from == 0 {
+ return Err(<CommonError<T>>::TokenValueTooLow.into());
+ }
+
let updated_balance_from = initial_balance_from
.checked_sub(amount)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
let mut create_target = false;
let from_to_differ = from != to;
- let updated_balance_to = if from != to {
+ let updated_balance_to = if from != to && amount != 0 {
let old_balance = <Balance<T>>::get((collection.id, token, to));
if old_balance == 0 {
create_target = true;
@@ -786,16 +795,17 @@
// =========
- <PalletStructure<T>>::nest_if_sent_to_token(
- from.clone(),
- to,
- collection.id,
- token,
- nesting_budget,
- )?;
+ if let Some(updated_balance_to) = updated_balance_to {
+ // from != to && amount != 0
+
+ <PalletStructure<T>>::nest_if_sent_to_token(
+ from.clone(),
+ to,
+ collection.id,
+ token,
+ nesting_budget,
+ )?;
- if let Some(updated_balance_to) = updated_balance_to {
- // from != to
if updated_balance_from == 0 {
<Balance<T>>::remove((collection.id, token, from));
<PalletStructure<T>>::unnest_if_nested(from, collection.id, token);
tests/src/burnItem.test.tsdiffbeforeafterboth--- a/tests/src/burnItem.test.ts
+++ b/tests/src/burnItem.test.ts
@@ -140,6 +140,31 @@
await expect(token.burn(bob)).to.be.rejectedWith('common.NoPermission');
});
+ itSub.ifWithPallets('RFT: cannot burn non-owned token pieces', [Pallets.ReFungible], async ({helper}) => {
+ const collection = await helper.rft.mintCollection(alice);
+ const aliceToken = await collection.mintToken(alice, 10n, {Substrate: alice.address});
+ const bobToken = await collection.mintToken(alice, 10n, {Substrate: bob.address});
+
+ // 1. Cannot burn non-owned token:
+ await expect(bobToken.burn(alice, 0n)).to.be.rejectedWith('common.TokenValueTooLow');
+ await expect(bobToken.burn(alice, 5n)).to.be.rejectedWith('common.TokenValueTooLow');
+ // 2. Cannot burn non-existing token:
+ await expect(helper.rft.burnToken(alice, 99999, 10)).to.be.rejectedWith('common.CollectionNotFound');
+ await expect(helper.rft.burnToken(alice, collection.collectionId, 99999)).to.be.rejectedWith('common.TokenValueTooLow');
+ // 3. Can burn zero amount of owned tokens (EIP-20)
+ await aliceToken.burn(alice, 0n);
+
+ // 4. Storage is not corrupted:
+ expect(await aliceToken.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);
+ expect(await bobToken.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);
+
+ // 4.1 Tokens can be transfered:
+ await aliceToken.transfer(alice, {Substrate: bob.address}, 10n);
+ await bobToken.transfer(bob, {Substrate: alice.address}, 10n);
+ expect(await aliceToken.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);
+ expect(await bobToken.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);
+ });
+
itSub('Transfer a burned token', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice);
const token = await collection.mintToken(alice);
@@ -155,4 +180,48 @@
await expect(collection.burnTokens(alice, 11n)).to.be.rejectedWith('common.TokenValueTooLow');
expect(await collection.getBalance({Substrate: alice.address})).to.eq(10n);
});
+
+ itSub('Zero burn NFT', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(alice, {name: 'Coll', description: 'Desc', tokenPrefix: 'T'});
+ const tokenAlice = await collection.mintToken(alice, {Substrate: alice.address});
+ const tokenBob = await collection.mintToken(alice, {Substrate: bob.address});
+
+ // 1. Zero burn of own tokens allowed:
+ await helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenAlice.tokenId, 0]);
+ // 2. Zero burn of non-owned tokens not allowed:
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenBob.tokenId, 0])).to.be.rejectedWith('common.NoPermission');
+ // 3. Zero burn of non-existing tokens not allowed:
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, 9999, 0])).to.be.rejectedWith('common.TokenNotFound');
+ expect(await tokenAlice.doesExist()).to.be.true;
+ expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: alice.address});
+ expect(await tokenBob.getOwner()).to.deep.eq({Substrate: bob.address});
+ // 4. Storage is not corrupted:
+ await tokenAlice.transfer(alice, {Substrate: bob.address});
+ await tokenBob.transfer(bob, {Substrate: alice.address});
+ expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: bob.address});
+ expect(await tokenBob.getOwner()).to.deep.eq({Substrate: alice.address});
+ });
+
+ itSub('zero burnFrom NFT', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});
+ const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});
+ const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});
+ await approvedNft.approve(bob, {Substrate: alice.address});
+
+ // 1. Zero burnFrom of non-existing tokens not allowed:
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');
+ // 2. Zero burnFrom of not approved tokens not allowed:
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');
+ // 3. Zero burnFrom of approved tokens allowed:
+ await helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, approvedNft.tokenId, 0]);
+
+ // 4.1 approvedNft still approved:
+ expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;
+ // 4.2 bob is still the owner:
+ expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});
+ expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});
+ // 4.3 Alice can burn approved nft:
+ await approvedNft.burnFrom(alice, {Substrate: bob.address});
+ expect(await approvedNft.doesExist()).to.be.false;
+ });
});
tests/src/eth/fungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/fungible.test.ts
+++ b/tests/src/eth/fungible.test.ts
@@ -277,7 +277,7 @@
}
});
- itEth('Cannot transferCross() more than have', async ({helper}) => {
+ ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} incorrect amount`, async ({helper}) => {
const sender = await helper.eth.createAccountWithBalance(donor);
const receiverEth = await helper.eth.createAccountWithBalance(donor);
const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
@@ -289,8 +289,13 @@
const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', sender);
- await expect(collectionEvm.methods.transferCross(receiverCrossEth, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;
- });
+ // 1. Cannot transfer more than have
+ const receiver = testCase === 'transfer' ? receiverEth : receiverCrossEth;
+ await expect(collectionEvm.methods[testCase](receiver, BALANCE_TO_TRANSFER).send({from: sender})).to.be.rejected;
+ // 2. Zero transfer allowed (EIP-20):
+ await collectionEvm.methods[testCase](receiver, 0n).send({from: sender});
+ }));
+
itEth('Can perform transfer()', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
tests/src/eth/nonFungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -517,6 +517,26 @@
expect(receiverBalance).to.contain(tokenId);
}
});
+
+ ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} non-owned token`, async ({helper}) => {
+ const sender = await helper.eth.createAccountWithBalance(donor);
+ const tokenOwner = await helper.eth.createAccountWithBalance(donor);
+ const receiverSub = minter;
+ const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);
+
+ const collection = await helper.nft.mintCollection(minter, {});
+ const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', sender);
+
+ await collection.mintToken(minter, {Ethereum: sender});
+ const nonSendersToken = await collection.mintToken(minter, {Ethereum: tokenOwner});
+
+ // Cannot transferCross someone else's token:
+ const receiver = testCase === 'transfer' ? helper.address.substrateToEth(receiverSub.address) : receiverCrossSub;
+ await expect(collectionEvm.methods[testCase](receiver, nonSendersToken.tokenId).send({from: sender})).to.be.rejected;
+ // Cannot transfer token if it does not exist:
+ await expect(collectionEvm.methods[testCase](receiver, 999999).send({from: sender})).to.be.rejected;
+ }));
});
describe('NFT: Fees', () => {
tests/src/eth/reFungible.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungible.test.ts
+++ b/tests/src/eth/reFungible.test.ts
@@ -413,9 +413,10 @@
}
});
- itEth.skip('Cannot transferCross with invalid params', async ({helper}) => {
+ ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} non-owned token`, async ({helper}) => {
const sender = await helper.eth.createAccountWithBalance(donor);
const tokenOwner = await helper.eth.createAccountWithBalance(donor);
+ const receiverSub = minter;
const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);
const collection = await helper.rft.mintCollection(minter, {});
@@ -423,12 +424,14 @@
const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);
await collection.mintToken(minter, 50n, {Ethereum: sender});
- const notSendersToken = await collection.mintToken(minter, 50n, {Ethereum: tokenOwner});
+ const nonSendersToken = await collection.mintToken(minter, 50n, {Ethereum: tokenOwner});
+
// Cannot transferCross someone else's token:
- await expect(collectionEvm.methods.transferCross(receiverCrossSub, notSendersToken.tokenId).send({from: sender})).to.be.rejected;
- // FIXME: (transaction successful): Cannot transfer token if it does not exist:
- await expect(collectionEvm.methods.transferCross(receiverCrossSub, 999999).send({from: sender})).to.be.rejected;
- });
+ const receiver = testCase === 'transfer' ? helper.address.substrateToEth(receiverSub.address) : receiverCrossSub;
+ await expect(collectionEvm.methods[testCase](receiver, nonSendersToken.tokenId).send({from: sender})).to.be.rejected;
+ // Cannot transfer token if it does not exist:
+ await expect(collectionEvm.methods[testCase](receiver, 999999).send({from: sender})).to.be.rejected;
+ }));
itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {
const caller = await helper.eth.createAccountWithBalance(donor);
tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -227,6 +227,46 @@
}
});
+ [
+ 'transfer',
+ // 'transferCross', // TODO
+ ].map(testCase =>
+ itEth(`Cannot ${testCase}() non-owned token`, async ({helper}) => {
+ const owner = await helper.eth.createAccountWithBalance(donor);
+ const receiver = await helper.eth.createAccountWithBalance(donor);
+ const collection = await helper.rft.mintCollection(alice);
+ const rftOwner = await collection.mintToken(alice, 10n, {Ethereum: owner});
+ const rftReceiver = await collection.mintToken(alice, 10n, {Ethereum: receiver});
+ const tokenIdNonExist = 9999999;
+
+ const tokenAddress1 = helper.ethAddress.fromTokenId(collection.collectionId, rftOwner.tokenId);
+ const tokenAddress2 = helper.ethAddress.fromTokenId(collection.collectionId, rftReceiver.tokenId);
+ const tokenAddressNonExist = helper.ethAddress.fromTokenId(collection.collectionId, tokenIdNonExist);
+ const tokenEvmOwner = helper.ethNativeContract.rftToken(tokenAddress1, owner);
+ const tokenEvmReceiver = helper.ethNativeContract.rftToken(tokenAddress2, owner);
+ const tokenEvmNonExist = helper.ethNativeContract.rftToken(tokenAddressNonExist, owner);
+
+ // 1. Can transfer zero amount (EIP-20):
+ await tokenEvmOwner.methods[testCase](receiver, 0).send({from: owner});
+ // 2. Cannot transfer non-owned token:
+ await expect(tokenEvmReceiver.methods[testCase](owner, 0).send({from: owner})).to.be.rejected;
+ await expect(tokenEvmReceiver.methods[testCase](owner, 5).send({from: owner})).to.be.rejected;
+ // 3. Cannot transfer non-existing token:
+ await expect(tokenEvmNonExist.methods[testCase](owner, 0).send({from: owner})).to.be.rejected;
+ await expect(tokenEvmNonExist.methods[testCase](owner, 5).send({from: owner})).to.be.rejected;
+
+ // 4. Storage is not corrupted:
+ expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);
+ expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: receiver.toLowerCase()}]);
+ expect(await helper.rft.getTokenTop10Owners(collection.collectionId, tokenIdNonExist)).to.deep.eq([]); // TODO
+
+ // 4.1 Tokens can be transferred:
+ await tokenEvmOwner.methods[testCase](receiver, 10).send({from: owner});
+ await tokenEvmReceiver.methods[testCase](owner, 10).send({from: receiver});
+ expect(await rftOwner.getTop10Owners()).to.deep.eq([{Ethereum: receiver.toLowerCase()}]);
+ expect(await rftReceiver.getTop10Owners()).to.deep.eq([{Ethereum: owner.toLowerCase()}]);
+ }));
+
itEth('Can perform repartition()', async ({helper}) => {
const owner = await helper.eth.createAccountWithBalance(donor);
const receiver = await helper.eth.createAccountWithBalance(donor);
tests/src/fungible.test.tsdiffbeforeafterboth--- a/tests/src/fungible.test.ts
+++ b/tests/src/fungible.test.ts
@@ -15,7 +15,7 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
import {IKeyringPair} from '@polkadot/types/types';
-import {itSub, usingPlaygrounds, expect} from './util';
+import {itSub, usingPlaygrounds, expect, requirePalletsOrSkip, Pallets} from './util';
const U128_MAX = (1n << 128n) - 1n;
@@ -145,3 +145,42 @@
expect(await collection.getBalance(ethAcc)).to.be.equal(10n);
});
});
+
+describe('Fungible negative tests', () => {
+ let donor: IKeyringPair;
+ let alice: IKeyringPair;
+ let bob: IKeyringPair;
+ let charlie: IKeyringPair;
+
+ before(async function() {
+ await usingPlaygrounds(async (helper, privateKey) => {
+ requirePalletsOrSkip(this, helper, [Pallets.Fungible]);
+
+ donor = await privateKey({filename: __filename});
+ [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);
+ });
+ });
+
+ itSub('Cannot transfer incorrect amount of tokens', async ({helper}) => {
+ const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});
+ const nonExistingCollection = helper.ft.getCollectionObject(99999);
+ await collection.mint(alice, 10n, {Substrate: bob.address});
+
+ // 1. Alice cannot transfer more than 0 tokens if balance low:
+ await expect(collection.transfer(alice, {Substrate: charlie.address}, 1n)).to.be.rejectedWith('common.TokenValueTooLow');
+ await expect(collection.transfer(alice, {Substrate: charlie.address}, 100n)).to.be.rejectedWith('common.TokenValueTooLow');
+
+ // 2. Alice cannot transfer non-existing token:
+ await expect(nonExistingCollection.transfer(alice, {Substrate: charlie.address}, 0n)).to.be.rejectedWith('common.CollectionNotFound');
+ await expect(nonExistingCollection.transfer(alice, {Substrate: charlie.address}, 1n)).to.be.rejectedWith('common.CollectionNotFound');
+
+ // 3. Zero transfer allowed (EIP-20):
+ await collection.transfer(bob, {Substrate: charlie.address}, 0n);
+ // 3.1 even if the balance = 0
+ await collection.transfer(alice, {Substrate: charlie.address}, 0n);
+
+ expect(await collection.getBalance({Substrate: alice.address})).to.eq(0n);
+ expect(await collection.getBalance({Substrate: bob.address})).to.eq(10n);
+ expect(await collection.getBalance({Substrate: charlie.address})).to.eq(0n);
+ });
+});
tests/src/refungible.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from './util';1920const MAX_REFUNGIBLE_PIECES = 1_000_000_000_000_000_000_000n;2122describe('integration test: Refungible functionality:', () => {23 let donor: IKeyringPair;24 let alice: IKeyringPair;25 let bob: IKeyringPair;2627 before(async function() {28 await usingPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);3031 donor = await privateKey({filename: __filename});32 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);33 });34 });35 36 itSub('Create refungible collection and token', async ({helper}) => {37 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});3839 const itemCountBefore = await collection.getLastTokenId();40 const token = await collection.mintToken(alice, 100n);41 42 const itemCountAfter = await collection.getLastTokenId();43 44 // What to expect45 expect(token?.tokenId).to.be.gte(itemCountBefore);46 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);47 expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());48 });49 50 itSub('Checking RPC methods when interacting with maximum allowed values (MAX_REFUNGIBLE_PIECES)', async ({helper}) => {51 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});52 53 const token = await collection.mintToken(alice, MAX_REFUNGIBLE_PIECES);54 55 expect(await collection.getTokenBalance(token.tokenId, {Substrate: alice.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);56 57 await collection.transferToken(alice, token.tokenId, {Substrate: bob.address}, MAX_REFUNGIBLE_PIECES);58 expect(await collection.getTokenBalance(token.tokenId, {Substrate: bob.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);59 expect(await token.getTotalPieces()).to.be.equal(MAX_REFUNGIBLE_PIECES);60 61 await expect(collection.mintToken(alice, MAX_REFUNGIBLE_PIECES + 1n))62 .to.eventually.be.rejectedWith(/refungible\.WrongRefungiblePieces/);63 });64 65 itSub('RPC method tokenOwners for refungible collection and token', async ({helper}) => {66 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};67 const facelessCrowd = (await helper.arrange.createAccounts(Array(7).fill(0n), donor)).map(keyring => {return {Substrate: keyring.address};});6869 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7071 const token = await collection.mintToken(alice, 10_000n);7273 await token.transfer(alice, {Substrate: bob.address}, 1000n);74 await token.transfer(alice, ethAcc, 900n);75 76 for (let i = 0; i < 7; i++) {77 await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));78 } 7980 const owners = await token.getTop10Owners();8182 // What to expect83 expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);84 expect(owners.length).to.be.equal(10);85 86 const [eleven] = await helper.arrange.createAccounts([0n], donor);87 expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;88 expect((await token.getTop10Owners()).length).to.be.equal(10);89 });90 91 itSub('Transfer token pieces', async ({helper}) => {92 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});93 const token = await collection.mintToken(alice, 100n);9495 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);96 expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;97 98 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);99 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);100 101 await expect(token.transfer(alice, {Substrate: bob.address}, 41n))102 .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);103 });104105 itSub('Create multiple tokens', async ({helper}) => {106 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});107 // TODO: fix mintMultipleTokens108 // await collection.mintMultipleTokens(alice, [109 // {owner: {Substrate: alice.address}, pieces: 1n},110 // {owner: {Substrate: alice.address}, pieces: 2n},111 // {owner: {Substrate: alice.address}, pieces: 100n},112 // ]);113 await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [114 {pieces: 1n}, 115 {pieces: 2n}, 116 {pieces: 100n},117 ]);118 const lastTokenId = await collection.getLastTokenId();119 expect(lastTokenId).to.be.equal(3);120 expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);121 });122123 itSub('Burn some pieces', async ({helper}) => {124 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});125 const token = await collection.mintToken(alice, 100n);126 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;127 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);128 expect(await token.burn(alice, 99n)).to.be.true;129 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;130 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);131 });132133 itSub('Burn all pieces', async ({helper}) => {134 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});135 const token = await collection.mintToken(alice, 100n);136 137 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;138 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);139140 expect(await token.burn(alice, 100n)).to.be.true;141 expect(await collection.doesTokenExist(token.tokenId)).to.be.false;142 });143144 itSub('Burn some pieces for multiple users', async ({helper}) => {145 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});146 const token = await collection.mintToken(alice, 100n);147148 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;149 150 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);151 expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;152153 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);154 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);155156 expect(await token.burn(alice, 40n)).to.be.true;157158 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;159 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);160161 expect(await token.burn(bob, 59n)).to.be.true;162163 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);164 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;165166 expect(await token.burn(bob, 1n)).to.be.true;167168 expect(await collection.doesTokenExist(token.tokenId)).to.be.false;169 });170171 itSub('Set allowance for token', async ({helper}) => {172 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});173 const token = await collection.mintToken(alice, 100n);174 175 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);176177 expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;178 expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);179180 expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;181 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);182 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);183 expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);184 });185186 itSub('Repartition', async ({helper}) => {187 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});188 const token = await collection.mintToken(alice, 100n);189190 expect(await token.repartition(alice, 200n)).to.be.true;191 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);192 expect(await token.getTotalPieces()).to.be.equal(200n);193 194 expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;195 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);196 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);197 198 await expect(token.repartition(alice, 80n))199 .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/);200 201 expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;202 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);203 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);204205 expect(await token.repartition(bob, 150n)).to.be.true;206 await expect(token.transfer(bob, {Substrate: alice.address}, 160n))207 .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);208 });209210 itSub('Repartition with increased amount', async ({helper}) => {211 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});212 const token = await collection.mintToken(alice, 100n);213 await token.repartition(alice, 200n);214 const chainEvents = helper.chainLog.slice(-1)[0].events;215 const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemCreated');216 expect(event).to.deep.include({217 section: 'common',218 method: 'ItemCreated',219 index: [66, 2],220 data: [221 collection.collectionId,222 token.tokenId,223 {substrate: alice.address}, 224 100n,225 ],226 });227 });228229 itSub('Repartition with decreased amount', async ({helper}) => {230 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});231 const token = await collection.mintToken(alice, 100n);232 await token.repartition(alice, 50n);233 const chainEvents = helper.chainLog.slice(-1)[0].events;234 const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemDestroyed');235 expect(event).to.deep.include({236 section: 'common',237 method: 'ItemDestroyed',238 index: [66, 3],239 data: [240 collection.collectionId,241 token.tokenId,242 {substrate: alice.address}, 243 50n,244 ],245 });246 });247 248 itSub('Create new collection with properties', async ({helper}) => {249 const properties = [{key: 'key1', value: 'val1'}];250 const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];251 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});252 const info = await collection.getData();253 expect(info?.raw.properties).to.be.deep.equal(properties);254 expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);255 });256});2571// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from './util';1920const MAX_REFUNGIBLE_PIECES = 1_000_000_000_000_000_000_000n;2122describe('integration test: Refungible functionality:', () => {23 let donor: IKeyringPair;24 let alice: IKeyringPair;25 let bob: IKeyringPair;2627 before(async function() {28 await usingPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);3031 donor = await privateKey({filename: __filename});32 [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);33 });34 });35 36 itSub('Create refungible collection and token', async ({helper}) => {37 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});3839 const itemCountBefore = await collection.getLastTokenId();40 const token = await collection.mintToken(alice, 100n);41 42 const itemCountAfter = await collection.getLastTokenId();43 44 // What to expect45 expect(token?.tokenId).to.be.gte(itemCountBefore);46 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);47 expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());48 });49 50 itSub('Checking RPC methods when interacting with maximum allowed values (MAX_REFUNGIBLE_PIECES)', async ({helper}) => {51 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});52 53 const token = await collection.mintToken(alice, MAX_REFUNGIBLE_PIECES);54 55 expect(await collection.getTokenBalance(token.tokenId, {Substrate: alice.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);56 57 await collection.transferToken(alice, token.tokenId, {Substrate: bob.address}, MAX_REFUNGIBLE_PIECES);58 expect(await collection.getTokenBalance(token.tokenId, {Substrate: bob.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);59 expect(await token.getTotalPieces()).to.be.equal(MAX_REFUNGIBLE_PIECES);60 61 await expect(collection.mintToken(alice, MAX_REFUNGIBLE_PIECES + 1n))62 .to.eventually.be.rejectedWith(/refungible\.WrongRefungiblePieces/);63 });64 65 itSub('RPC method tokenOwners for refungible collection and token', async ({helper}) => {66 const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};67 const facelessCrowd = (await helper.arrange.createAccounts(Array(7).fill(0n), donor)).map(keyring => {return {Substrate: keyring.address};});6869 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7071 const token = await collection.mintToken(alice, 10_000n);7273 await token.transfer(alice, {Substrate: bob.address}, 1000n);74 await token.transfer(alice, ethAcc, 900n);75 76 for (let i = 0; i < 7; i++) {77 await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));78 } 7980 const owners = await token.getTop10Owners();8182 // What to expect83 expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);84 expect(owners.length).to.be.equal(10);85 86 const [eleven] = await helper.arrange.createAccounts([0n], donor);87 expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;88 expect((await token.getTop10Owners()).length).to.be.equal(10);89 });90 91 itSub('Transfer token pieces', async ({helper}) => {92 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});93 const token = await collection.mintToken(alice, 100n);9495 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);96 expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;97 98 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);99 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);100 101 await expect(token.transfer(alice, {Substrate: bob.address}, 41n))102 .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);103 });104105 itSub('Create multiple tokens', async ({helper}) => {106 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});107 // TODO: fix mintMultipleTokens108 // await collection.mintMultipleTokens(alice, [109 // {owner: {Substrate: alice.address}, pieces: 1n},110 // {owner: {Substrate: alice.address}, pieces: 2n},111 // {owner: {Substrate: alice.address}, pieces: 100n},112 // ]);113 await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [114 {pieces: 1n}, 115 {pieces: 2n}, 116 {pieces: 100n},117 ]);118 const lastTokenId = await collection.getLastTokenId();119 expect(lastTokenId).to.be.equal(3);120 expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);121 });122123 itSub('Burn some pieces', async ({helper}) => {124 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});125 const token = await collection.mintToken(alice, 100n);126 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;127 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);128 expect(await token.burn(alice, 99n)).to.be.true;129 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;130 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);131 });132133 itSub('Burn all pieces', async ({helper}) => {134 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});135 const token = await collection.mintToken(alice, 100n);136 137 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;138 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);139140 expect(await token.burn(alice, 100n)).to.be.true;141 expect(await collection.doesTokenExist(token.tokenId)).to.be.false;142 });143144 itSub('Burn some pieces for multiple users', async ({helper}) => {145 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});146 const token = await collection.mintToken(alice, 100n);147148 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;149 150 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);151 expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;152153 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);154 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);155156 expect(await token.burn(alice, 40n)).to.be.true;157158 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;159 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);160161 expect(await token.burn(bob, 59n)).to.be.true;162163 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);164 expect(await collection.doesTokenExist(token.tokenId)).to.be.true;165166 expect(await token.burn(bob, 1n)).to.be.true;167168 expect(await collection.doesTokenExist(token.tokenId)).to.be.false;169 });170171 itSub('Set allowance for token', async ({helper}) => {172 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});173 const token = await collection.mintToken(alice, 100n);174 175 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);176177 expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;178 expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);179180 expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;181 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);182 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);183 expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);184 });185186 itSub('Repartition', async ({helper}) => {187 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});188 const token = await collection.mintToken(alice, 100n);189190 expect(await token.repartition(alice, 200n)).to.be.true;191 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);192 expect(await token.getTotalPieces()).to.be.equal(200n);193 194 expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;195 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);196 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);197 198 await expect(token.repartition(alice, 80n))199 .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/);200 201 expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;202 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);203 expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);204205 expect(await token.repartition(bob, 150n)).to.be.true;206 await expect(token.transfer(bob, {Substrate: alice.address}, 160n))207 .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);208 });209210 itSub('Repartition with increased amount', async ({helper}) => {211 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});212 const token = await collection.mintToken(alice, 100n);213 await token.repartition(alice, 200n);214 const chainEvents = helper.chainLog.slice(-1)[0].events;215 const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemCreated');216 expect(event).to.deep.include({217 section: 'common',218 method: 'ItemCreated',219 index: [66, 2],220 data: [221 collection.collectionId,222 token.tokenId,223 {substrate: alice.address}, 224 100n,225 ],226 });227 });228229 itSub('Repartition with decreased amount', async ({helper}) => {230 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});231 const token = await collection.mintToken(alice, 100n);232 await token.repartition(alice, 50n);233 const chainEvents = helper.chainLog.slice(-1)[0].events;234 const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemDestroyed');235 expect(event).to.deep.include({236 section: 'common',237 method: 'ItemDestroyed',238 index: [66, 3],239 data: [240 collection.collectionId,241 token.tokenId,242 {substrate: alice.address}, 243 50n,244 ],245 });246 });247 248 itSub('Create new collection with properties', async ({helper}) => {249 const properties = [{key: 'key1', value: 'val1'}];250 const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];251 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});252 const info = await collection.getData();253 expect(info?.raw.properties).to.be.deep.equal(properties);254 expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);255 });256});257258describe('Refungible negative tests', () => {259 let donor: IKeyringPair;260 let alice: IKeyringPair;261 let bob: IKeyringPair;262 let charlie: IKeyringPair;263264 before(async function() {265 await usingPlaygrounds(async (helper, privateKey) => {266 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);267268 donor = await privateKey({filename: __filename});269 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);270 });271 });272273 itSub('Cannot transfer incorrect amount of token pieces', async ({helper}) => {274 const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});275 const tokenAlice = await collection.mintToken(alice, 10n, {Substrate: alice.address});276 const tokenBob = await collection.mintToken(alice, 10n, {Substrate: bob.address});277278 // 1. Alice cannot transfer Bob's token:279 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 0n)).to.be.rejectedWith('common.TokenValueTooLow');280 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 1n)).to.be.rejectedWith('common.TokenValueTooLow');281 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 10n)).to.be.rejectedWith('common.TokenValueTooLow');282 await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 100n)).to.be.rejectedWith('common.TokenValueTooLow');283 284 // 2. Alice cannot transfer non-existing token:285 await expect(collection.transferToken(alice, 100, {Substrate: charlie.address}, 0n)).to.be.rejectedWith('common.TokenValueTooLow');286 await expect(collection.transferToken(alice, 100, {Substrate: charlie.address}, 1n)).to.be.rejectedWith('common.TokenValueTooLow');287288 // 3. Zero transfer allowed (EIP-20):289 await tokenAlice.transfer(alice, {Substrate: charlie.address}, 0n);290291 expect(await tokenAlice.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);292 expect(await tokenBob.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);293 expect(await tokenAlice.getBalance({Substrate: alice.address})).to.eq(10n);294 expect(await tokenBob.getBalance({Substrate: bob.address})).to.eq(10n);295 expect(await tokenBob.getBalance({Substrate: charlie.address})).to.eq(0n);296 });297});tests/src/transfer.test.tsdiffbeforeafterboth--- a/tests/src/transfer.test.ts
+++ b/tests/src/transfer.test.ts
@@ -122,6 +122,7 @@
});
});
+
itSub('[nft] Transfer with not existed collection_id', async ({helper}) => {
const collectionId = (1 << 32) - 1;
await expect(helper.nft.transferToken(alice, collectionId, 1, {Substrate: bob.address}))
@@ -191,6 +192,25 @@
.to.be.rejectedWith(/common\.TokenValueTooLow/);
});
+ itSub('Zero transfer NFT', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(alice, {name: 'Transfer-Neg-3-NFT', description: '', tokenPrefix: 'T'});
+ const tokenAlice = await collection.mintToken(alice, {Substrate: alice.address});
+ const tokenBob = await collection.mintToken(alice, {Substrate: bob.address});
+ // 1. Zero transfer of own tokens allowed:
+ await helper.executeExtrinsic(alice, 'api.tx.unique.transfer', [{Substrate: bob.address}, collection.collectionId, tokenAlice.tokenId, 0]);
+ // 2. Zero transfer of non-owned tokens not allowed:
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transfer', [{Substrate: alice.address}, collection.collectionId, tokenBob.tokenId, 0])).to.be.rejectedWith('common.NoPermission');
+ // 3. Zero transfer of non-existing tokens not allowed:
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transfer', [{Substrate: alice.address}, collection.collectionId, 10, 0])).to.be.rejectedWith('common.TokenNotFound');
+ expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: alice.address});
+ expect(await tokenBob.getOwner()).to.deep.eq({Substrate: bob.address});
+ // 4. Storage is not corrupted:
+ await tokenAlice.transfer(alice, {Substrate: bob.address});
+ await tokenBob.transfer(bob, {Substrate: alice.address});
+ expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: bob.address});
+ expect(await tokenBob.getOwner()).to.deep.eq({Substrate: alice.address});
+ });
+
itSub('[nft] Transfer with deleted item_id', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice, {name: 'Transfer-Neg-3-NFT', description: '', tokenPrefix: 'T'});
const nft = await collection.mintToken(alice);
tests/src/transferFrom.test.tsdiffbeforeafterboth--- a/tests/src/transferFrom.test.ts
+++ b/tests/src/transferFrom.test.ts
@@ -349,4 +349,27 @@
{Substrate: charlie.address},
)).to.be.rejectedWith(/common\.ApprovedValueTooLow/);
});
+
+ itSub('zero transfer NFT', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});
+ const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});
+ const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});
+ await approvedNft.approve(bob, {Substrate: alice.address});
+
+ // 1. Cannot zero transferFrom (non-existing token)
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');
+ // 2. Cannot zero transferFrom (not approved token)
+ await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');
+ // 3. Can zero transferFrom (approved token):
+ await helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, approvedNft.tokenId, 0]);
+
+ // 4.1 approvedNft still approved:
+ expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;
+ // 4.2 bob is still the owner:
+ expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});
+ expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});
+ // 4.3 Alice can transfer approved nft:
+ await approvedNft.transferFrom(alice, {Substrate: bob.address}, {Substrate: alice.address});
+ expect(await approvedNft.getOwner()).to.deep.eq({Substrate: alice.address});
+ });
});