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.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 {Pallets, requirePalletsOrSkip} from '../util';18import {expect, itEth, usingEthPlaygrounds} from './util';19import {IKeyringPair} from '@polkadot/types/types';2021describe('Refungible: Information getting', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);2728 donor = await privateKey({filename: __filename});29 });30 });3132 itEth('totalSupply', async ({helper}) => {33 const caller = await helper.eth.createAccountWithBalance(donor);34 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'TotalSupply', '6', '6');35 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);3637 await contract.methods.mint(caller).send();3839 const totalSupply = await contract.methods.totalSupply().call();40 expect(totalSupply).to.equal('1');41 });4243 itEth('balanceOf', async ({helper}) => {44 const caller = await helper.eth.createAccountWithBalance(donor);45 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'BalanceOf', '6', '6');46 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);4748 await contract.methods.mint(caller).send();49 await contract.methods.mint(caller).send();50 await contract.methods.mint(caller).send();5152 const balance = await contract.methods.balanceOf(caller).call();53 expect(balance).to.equal('3');54 });5556 itEth('ownerOf', async ({helper}) => {57 const caller = await helper.eth.createAccountWithBalance(donor);58 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf', '6', '6');59 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);6061 const result = await contract.methods.mint(caller).send();62 const tokenId = result.events.Transfer.returnValues.tokenId;6364 const owner = await contract.methods.ownerOf(tokenId).call();65 expect(owner).to.equal(caller);66 });6768 itEth('ownerOf after burn', async ({helper}) => {69 const caller = await helper.eth.createAccountWithBalance(donor);70 const receiver = helper.eth.createAccount();71 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf-AfterBurn', '6', '6');72 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);7374 const result = await contract.methods.mint(caller).send();75 const tokenId = result.events.Transfer.returnValues.tokenId;76 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);7778 await tokenContract.methods.repartition(2).send();79 await tokenContract.methods.transfer(receiver, 1).send();8081 await tokenContract.methods.burnFrom(caller, 1).send();8283 const owner = await contract.methods.ownerOf(tokenId).call();84 expect(owner).to.equal(receiver);85 });8687 itEth('ownerOf for partial ownership', async ({helper}) => {88 const caller = await helper.eth.createAccountWithBalance(donor);89 const receiver = helper.eth.createAccount();90 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Partial-OwnerOf', '6', '6');91 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);9293 const result = await contract.methods.mint(caller).send();94 const tokenId = result.events.Transfer.returnValues.tokenId;95 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);9697 await tokenContract.methods.repartition(2).send();98 await tokenContract.methods.transfer(receiver, 1).send();99100 const owner = await contract.methods.ownerOf(tokenId).call();101 expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');102 });103});104105describe('Refungible: Plain calls', () => {106 let donor: IKeyringPair;107 let minter: IKeyringPair;108 let bob: IKeyringPair;109 let charlie: IKeyringPair;110111 before(async function() {112 await usingEthPlaygrounds(async (helper, privateKey) => {113 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);114115 donor = await privateKey({filename: __filename});116 [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);117 });118 });119120 itEth('Can perform mint() & crossOwnerOf()', async ({helper}) => {121 const owner = await helper.eth.createAccountWithBalance(donor);122 const receiver = helper.eth.createAccount();123 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Minty', '6', '6', '');124 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);125126 const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();127128 const event = result.events.Transfer;129 expect(event.address).to.equal(collectionAddress);130 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');131 expect(event.returnValues.to).to.equal(receiver);132 const tokenId = event.returnValues.tokenId;133 expect(tokenId).to.be.equal('1');134135 expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);136 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');137 });138139 itEth.skip('Can perform mintBulk()', async ({helper}) => {140 const owner = await helper.eth.createAccountWithBalance(donor);141 const receiver = helper.eth.createAccount();142 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'MintBulky', '6', '6', '');143 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);144145 {146 const nextTokenId = await contract.methods.nextTokenId().call();147 expect(nextTokenId).to.be.equal('1');148 const result = await contract.methods.mintBulkWithTokenURI(149 receiver,150 [151 [nextTokenId, 'Test URI 0'],152 [+nextTokenId + 1, 'Test URI 1'],153 [+nextTokenId + 2, 'Test URI 2'],154 ],155 ).send();156157 const events = result.events.Transfer;158 for (let i = 0; i < 2; i++) {159 const event = events[i];160 expect(event.address).to.equal(collectionAddress);161 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');162 expect(event.returnValues.to).to.equal(receiver);163 expect(event.returnValues.tokenId).to.equal(String(+nextTokenId + i));164 }165166 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');167 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');168 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');169 }170 });171172 itEth('Can perform burn()', async ({helper}) => {173 const caller = await helper.eth.createAccountWithBalance(donor);174 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Burny', '6', '6');175 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);176177 const result = await contract.methods.mint(caller).send();178 const tokenId = result.events.Transfer.returnValues.tokenId;179 {180 const result = await contract.methods.burn(tokenId).send();181 const event = result.events.Transfer;182 expect(event.address).to.equal(collectionAddress);183 expect(event.returnValues.from).to.equal(caller);184 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');185 expect(event.returnValues.tokenId).to.equal(tokenId.toString());186 }187 });188189 itEth('Can perform transferFrom()', async ({helper}) => {190 const caller = await helper.eth.createAccountWithBalance(donor);191 const receiver = helper.eth.createAccount();192 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'TransferFromy', '6', '6');193 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);194195 const result = await contract.methods.mint(caller).send();196 const tokenId = result.events.Transfer.returnValues.tokenId;197198 const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);199200 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, caller);201 await tokenContract.methods.repartition(15).send();202203 {204 const tokenEvents: any = [];205 tokenContract.events.allEvents((_: any, event: any) => {206 tokenEvents.push(event);207 });208 const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();209 if (tokenEvents.length == 0) await helper.wait.newBlocks(1);210211 let event = result.events.Transfer;212 expect(event.address).to.equal(collectionAddress);213 expect(event.returnValues.from).to.equal(caller);214 expect(event.returnValues.to).to.equal(receiver);215 expect(event.returnValues.tokenId).to.equal(tokenId.toString());216217 event = tokenEvents[0];218 expect(event.address).to.equal(tokenAddress);219 expect(event.returnValues.from).to.equal(caller);220 expect(event.returnValues.to).to.equal(receiver);221 expect(event.returnValues.value).to.equal('15');222 }223224 {225 const balance = await contract.methods.balanceOf(receiver).call();226 expect(+balance).to.equal(1);227 }228229 {230 const balance = await contract.methods.balanceOf(caller).call();231 expect(+balance).to.equal(0);232 }233 });234235 // Soft-deprecated236 itEth('Can perform burnFrom()', async ({helper}) => {237 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});238239 const owner = await helper.eth.createAccountWithBalance(donor, 100n);240 const spender = await helper.eth.createAccountWithBalance(donor, 100n);241242 const token = await collection.mintToken(minter, 100n, {Ethereum: owner});243244 const address = helper.ethAddress.fromCollectionId(collection.collectionId);245 const contract = helper.ethNativeContract.collection(address, 'rft', spender, true);246247 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);248 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);249 await tokenContract.methods.repartition(15).send();250 await tokenContract.methods.approve(spender, 15).send();251252 {253 const result = await contract.methods.burnFrom(owner, token.tokenId).send();254 const event = result.events.Transfer;255 expect(event).to.be.like({256 address: helper.ethAddress.fromCollectionId(collection.collectionId),257 event: 'Transfer',258 returnValues: {259 from: owner,260 to: '0x0000000000000000000000000000000000000000',261 tokenId: token.tokenId.toString(),262 },263 });264 }265266 expect(await collection.getTokenBalance(token.tokenId, {Ethereum: owner})).to.be.eq(0n);267 });268269 itEth('Can perform burnFromCross()', async ({helper}) => {270 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});271 272 const owner = bob;273 const spender = await helper.eth.createAccountWithBalance(donor, 100n);274275 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});276277 const address = helper.ethAddress.fromCollectionId(collection.collectionId);278 const contract = helper.ethNativeContract.collection(address, 'rft');279280 await token.repartition(owner, 15n);281 await token.approve(owner, {Ethereum: spender}, 15n);282283 {284 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);285 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});286 const event = result.events.Transfer;287 expect(event).to.be.like({288 address: helper.ethAddress.fromCollectionId(collection.collectionId),289 event: 'Transfer',290 returnValues: {291 from: helper.address.substrateToEth(owner.address),292 to: '0x0000000000000000000000000000000000000000',293 tokenId: token.tokenId.toString(),294 },295 });296 }297298 expect(await collection.getTokenBalance(token.tokenId, {Substrate: owner.address})).to.be.eq(0n);299 });300301 itEth('Can perform transferFromCross()', async ({helper}) => {302 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});303304 const owner = bob;305 const spender = await helper.eth.createAccountWithBalance(donor, 100n);306 const receiver = charlie;307308 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});309310 const address = helper.ethAddress.fromCollectionId(collection.collectionId);311 const contract = helper.ethNativeContract.collection(address, 'rft');312313 await token.repartition(owner, 15n);314 await token.approve(owner, {Ethereum: spender}, 15n);315316 {317 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);318 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);319 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});320 const event = result.events.Transfer;321 expect(event).to.be.like({322 address: helper.ethAddress.fromCollectionId(collection.collectionId),323 event: 'Transfer',324 returnValues: {325 from: helper.address.substrateToEth(owner.address),326 to: helper.address.substrateToEth(receiver.address),327 tokenId: token.tokenId.toString(),328 },329 });330 }331332 expect(await token.getTop10Owners()).to.be.like([{Substrate: receiver.address}]);333 });334335 itEth('Can perform transfer()', async ({helper}) => {336 const caller = await helper.eth.createAccountWithBalance(donor);337 const receiver = helper.eth.createAccount();338 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry', '6', '6');339 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);340341 const result = await contract.methods.mint(caller).send();342 const tokenId = result.events.Transfer.returnValues.tokenId;343344 {345 const result = await contract.methods.transfer(receiver, tokenId).send();346347 const event = result.events.Transfer;348 expect(event.address).to.equal(collectionAddress);349 expect(event.returnValues.from).to.equal(caller);350 expect(event.returnValues.to).to.equal(receiver);351 expect(event.returnValues.tokenId).to.equal(tokenId.toString());352 }353354 {355 const balance = await contract.methods.balanceOf(caller).call();356 expect(+balance).to.equal(0);357 }358359 {360 const balance = await contract.methods.balanceOf(receiver).call();361 expect(+balance).to.equal(1);362 }363 });364 365 itEth('Can perform transferCross()', async ({helper}) => {366 const sender = await helper.eth.createAccountWithBalance(donor);367 const receiverEth = await helper.eth.createAccountWithBalance(donor);368 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);369 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);370371 const collection = await helper.rft.mintCollection(minter, {});372 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);373 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);374375 const token = await collection.mintToken(minter, 50n, {Ethereum: sender});376377 {378 // Can transferCross to ethereum address:379 const result = await collectionEvm.methods.transferCross(receiverCrossEth, token.tokenId).send({from: sender});380 // Check events:381 const event = result.events.Transfer;382 expect(event.address).to.equal(collectionAddress);383 expect(event.returnValues.from).to.equal(sender);384 expect(event.returnValues.to).to.equal(receiverEth);385 expect(event.returnValues.tokenId).to.equal(token.tokenId.toString());386 // Sender's balance decreased:387 const senderBalance = await collectionEvm.methods.balanceOf(sender).call();388 expect(+senderBalance).to.equal(0);389 expect(await token.getBalance({Ethereum: sender})).to.eq(0n);390 // Receiver's balance increased:391 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();392 expect(+receiverBalance).to.equal(1);393 expect(await token.getBalance({Ethereum: receiverEth})).to.eq(50n);394 }395 396 {397 // Can transferCross to substrate address:398 const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, token.tokenId).send({from: receiverEth});399 // Check events:400 const event = substrateResult.events.Transfer;401 expect(event.address).to.be.equal(collectionAddress);402 expect(event.returnValues.from).to.be.equal(receiverEth);403 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));404 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);405 // Sender's balance decreased:406 const senderBalance = await collectionEvm.methods.balanceOf(receiverEth).call();407 expect(+senderBalance).to.equal(0);408 expect(await token.getBalance({Ethereum: receiverEth})).to.eq(0n);409 // Receiver's balance increased:410 const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});411 expect(receiverBalance).to.contain(token.tokenId);412 expect(await token.getBalance({Substrate: minter.address})).to.eq(50n);413 }414 });415416 itEth.skip('Cannot transferCross with invalid params', async ({helper}) => {417 const sender = await helper.eth.createAccountWithBalance(donor);418 const tokenOwner = await helper.eth.createAccountWithBalance(donor);419 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);420421 const collection = await helper.rft.mintCollection(minter, {});422 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);423 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);424425 await collection.mintToken(minter, 50n, {Ethereum: sender});426 const notSendersToken = await collection.mintToken(minter, 50n, {Ethereum: tokenOwner});427 // Cannot transferCross someone else's token:428 await expect(collectionEvm.methods.transferCross(receiverCrossSub, notSendersToken.tokenId).send({from: sender})).to.be.rejected;429 // FIXME: (transaction successful): Cannot transfer token if it does not exist:430 await expect(collectionEvm.methods.transferCross(receiverCrossSub, 999999).send({from: sender})).to.be.rejected;431 });432433 itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {434 const caller = await helper.eth.createAccountWithBalance(donor);435 const receiver = helper.eth.createAccount();436 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Partial-to-Full', '6', '6');437 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);438439 const result = await contract.methods.mint(caller).send();440 const tokenId = result.events.Transfer.returnValues.tokenId;441442 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);443444 await tokenContract.methods.repartition(2).send();445 await tokenContract.methods.transfer(receiver, 1).send();446447 const events: any = [];448 contract.events.allEvents((_: any, event: any) => {449 events.push(event);450 });451452 await tokenContract.methods.transfer(receiver, 1).send();453 if (events.length == 0) await helper.wait.newBlocks(1);454 const event = events[0];455456 expect(event.address).to.equal(collectionAddress);457 expect(event.returnValues.from).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');458 expect(event.returnValues.to).to.equal(receiver);459 expect(event.returnValues.tokenId).to.equal(tokenId.toString());460 });461462 itEth('transfer event on transfer from full ownership to partial ownership', async ({helper}) => {463 const caller = await helper.eth.createAccountWithBalance(donor);464 const receiver = helper.eth.createAccount();465 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Full-to-Partial', '6', '6');466 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);467468 const result = await contract.methods.mint(caller).send();469 const tokenId = result.events.Transfer.returnValues.tokenId;470471 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);472473 await tokenContract.methods.repartition(2).send();474475 const events: any = [];476 contract.events.allEvents((_: any, event: any) => {477 events.push(event);478 });479480 await tokenContract.methods.transfer(receiver, 1).send();481 if (events.length == 0) await helper.wait.newBlocks(1);482 const event = events[0];483484 expect(event.address).to.equal(collectionAddress);485 expect(event.returnValues.from).to.equal(caller);486 expect(event.returnValues.to).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');487 expect(event.returnValues.tokenId).to.equal(tokenId.toString());488 });489});490491describe('RFT: Fees', () => {492 let donor: IKeyringPair;493494 before(async function() {495 await usingEthPlaygrounds(async (helper, privateKey) => {496 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);497498 donor = await privateKey({filename: __filename});499 });500 });501502 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {503 const caller = await helper.eth.createAccountWithBalance(donor);504 const receiver = helper.eth.createAccount();505 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer-From', '6', '6');506 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);507508 const result = await contract.methods.mint(caller).send();509 const tokenId = result.events.Transfer.returnValues.tokenId;510511 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());512 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));513 expect(cost > 0n);514 });515516 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {517 const caller = await helper.eth.createAccountWithBalance(donor);518 const receiver = helper.eth.createAccount();519 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer', '6', '6');520 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);521522 const result = await contract.methods.mint(caller).send();523 const tokenId = result.events.Transfer.returnValues.tokenId;524525 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transfer(receiver, tokenId).send());526 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));527 expect(cost > 0n);528 });529});530531describe('Common metadata', () => {532 let donor: IKeyringPair;533 let alice: IKeyringPair;534535 before(async function() {536 await usingEthPlaygrounds(async (helper, privateKey) => {537 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);538539 donor = await privateKey({filename: __filename});540 [alice] = await helper.arrange.createAccounts([20n], donor);541 });542 });543544 itEth('Returns collection name', async ({helper}) => {545 const caller = helper.eth.createAccount();546 const tokenPropertyPermissions = [{547 key: 'URI',548 permission: {549 mutable: true,550 collectionAdmin: true,551 tokenOwner: false,552 },553 }];554 const collection = await helper.rft.mintCollection(555 alice,556 {557 name: 'Leviathan',558 tokenPrefix: '11',559 properties: [{key: 'ERC721Metadata', value: '1'}],560 tokenPropertyPermissions,561 },562 );563564 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'rft', caller);565 const name = await contract.methods.name().call();566 expect(name).to.equal('Leviathan');567 });568569 itEth('Returns symbol name', async ({helper}) => {570 const caller = await helper.eth.createAccountWithBalance(donor);571 const tokenPropertyPermissions = [{572 key: 'URI',573 permission: {574 mutable: true,575 collectionAdmin: true,576 tokenOwner: false,577 },578 }];579 const {collectionId} = await helper.rft.mintCollection(580 alice,581 {582 name: 'Leviathan',583 tokenPrefix: '12',584 properties: [{key: 'ERC721Metadata', value: '1'}],585 tokenPropertyPermissions,586 },587 );588589 const contract = helper.ethNativeContract.collectionById(collectionId, 'rft', caller);590 const symbol = await contract.methods.symbol().call();591 expect(symbol).to.equal('12');592 });593});1// 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 {Pallets, requirePalletsOrSkip} from '../util';18import {expect, itEth, usingEthPlaygrounds} from './util';19import {IKeyringPair} from '@polkadot/types/types';2021describe('Refungible: Information getting', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (helper, privateKey) => {26 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);2728 donor = await privateKey({filename: __filename});29 });30 });3132 itEth('totalSupply', async ({helper}) => {33 const caller = await helper.eth.createAccountWithBalance(donor);34 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'TotalSupply', '6', '6');35 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);3637 await contract.methods.mint(caller).send();3839 const totalSupply = await contract.methods.totalSupply().call();40 expect(totalSupply).to.equal('1');41 });4243 itEth('balanceOf', async ({helper}) => {44 const caller = await helper.eth.createAccountWithBalance(donor);45 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'BalanceOf', '6', '6');46 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);4748 await contract.methods.mint(caller).send();49 await contract.methods.mint(caller).send();50 await contract.methods.mint(caller).send();5152 const balance = await contract.methods.balanceOf(caller).call();53 expect(balance).to.equal('3');54 });5556 itEth('ownerOf', async ({helper}) => {57 const caller = await helper.eth.createAccountWithBalance(donor);58 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf', '6', '6');59 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);6061 const result = await contract.methods.mint(caller).send();62 const tokenId = result.events.Transfer.returnValues.tokenId;6364 const owner = await contract.methods.ownerOf(tokenId).call();65 expect(owner).to.equal(caller);66 });6768 itEth('ownerOf after burn', async ({helper}) => {69 const caller = await helper.eth.createAccountWithBalance(donor);70 const receiver = helper.eth.createAccount();71 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf-AfterBurn', '6', '6');72 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);7374 const result = await contract.methods.mint(caller).send();75 const tokenId = result.events.Transfer.returnValues.tokenId;76 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);7778 await tokenContract.methods.repartition(2).send();79 await tokenContract.methods.transfer(receiver, 1).send();8081 await tokenContract.methods.burnFrom(caller, 1).send();8283 const owner = await contract.methods.ownerOf(tokenId).call();84 expect(owner).to.equal(receiver);85 });8687 itEth('ownerOf for partial ownership', async ({helper}) => {88 const caller = await helper.eth.createAccountWithBalance(donor);89 const receiver = helper.eth.createAccount();90 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Partial-OwnerOf', '6', '6');91 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);9293 const result = await contract.methods.mint(caller).send();94 const tokenId = result.events.Transfer.returnValues.tokenId;95 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);9697 await tokenContract.methods.repartition(2).send();98 await tokenContract.methods.transfer(receiver, 1).send();99100 const owner = await contract.methods.ownerOf(tokenId).call();101 expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');102 });103});104105describe('Refungible: Plain calls', () => {106 let donor: IKeyringPair;107 let minter: IKeyringPair;108 let bob: IKeyringPair;109 let charlie: IKeyringPair;110111 before(async function() {112 await usingEthPlaygrounds(async (helper, privateKey) => {113 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);114115 donor = await privateKey({filename: __filename});116 [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);117 });118 });119120 itEth('Can perform mint() & crossOwnerOf()', async ({helper}) => {121 const owner = await helper.eth.createAccountWithBalance(donor);122 const receiver = helper.eth.createAccount();123 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Minty', '6', '6', '');124 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);125126 const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send();127128 const event = result.events.Transfer;129 expect(event.address).to.equal(collectionAddress);130 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');131 expect(event.returnValues.to).to.equal(receiver);132 const tokenId = event.returnValues.tokenId;133 expect(tokenId).to.be.equal('1');134135 expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']);136 expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');137 });138139 itEth.skip('Can perform mintBulk()', async ({helper}) => {140 const owner = await helper.eth.createAccountWithBalance(donor);141 const receiver = helper.eth.createAccount();142 const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'MintBulky', '6', '6', '');143 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);144145 {146 const nextTokenId = await contract.methods.nextTokenId().call();147 expect(nextTokenId).to.be.equal('1');148 const result = await contract.methods.mintBulkWithTokenURI(149 receiver,150 [151 [nextTokenId, 'Test URI 0'],152 [+nextTokenId + 1, 'Test URI 1'],153 [+nextTokenId + 2, 'Test URI 2'],154 ],155 ).send();156157 const events = result.events.Transfer;158 for (let i = 0; i < 2; i++) {159 const event = events[i];160 expect(event.address).to.equal(collectionAddress);161 expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000');162 expect(event.returnValues.to).to.equal(receiver);163 expect(event.returnValues.tokenId).to.equal(String(+nextTokenId + i));164 }165166 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI 0');167 expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');168 expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');169 }170 });171172 itEth('Can perform burn()', async ({helper}) => {173 const caller = await helper.eth.createAccountWithBalance(donor);174 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Burny', '6', '6');175 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);176177 const result = await contract.methods.mint(caller).send();178 const tokenId = result.events.Transfer.returnValues.tokenId;179 {180 const result = await contract.methods.burn(tokenId).send();181 const event = result.events.Transfer;182 expect(event.address).to.equal(collectionAddress);183 expect(event.returnValues.from).to.equal(caller);184 expect(event.returnValues.to).to.equal('0x0000000000000000000000000000000000000000');185 expect(event.returnValues.tokenId).to.equal(tokenId.toString());186 }187 });188189 itEth('Can perform transferFrom()', async ({helper}) => {190 const caller = await helper.eth.createAccountWithBalance(donor);191 const receiver = helper.eth.createAccount();192 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'TransferFromy', '6', '6');193 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);194195 const result = await contract.methods.mint(caller).send();196 const tokenId = result.events.Transfer.returnValues.tokenId;197198 const tokenAddress = helper.ethAddress.fromTokenId(collectionId, tokenId);199200 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, caller);201 await tokenContract.methods.repartition(15).send();202203 {204 const tokenEvents: any = [];205 tokenContract.events.allEvents((_: any, event: any) => {206 tokenEvents.push(event);207 });208 const result = await contract.methods.transferFrom(caller, receiver, tokenId).send();209 if (tokenEvents.length == 0) await helper.wait.newBlocks(1);210211 let event = result.events.Transfer;212 expect(event.address).to.equal(collectionAddress);213 expect(event.returnValues.from).to.equal(caller);214 expect(event.returnValues.to).to.equal(receiver);215 expect(event.returnValues.tokenId).to.equal(tokenId.toString());216217 event = tokenEvents[0];218 expect(event.address).to.equal(tokenAddress);219 expect(event.returnValues.from).to.equal(caller);220 expect(event.returnValues.to).to.equal(receiver);221 expect(event.returnValues.value).to.equal('15');222 }223224 {225 const balance = await contract.methods.balanceOf(receiver).call();226 expect(+balance).to.equal(1);227 }228229 {230 const balance = await contract.methods.balanceOf(caller).call();231 expect(+balance).to.equal(0);232 }233 });234235 // Soft-deprecated236 itEth('Can perform burnFrom()', async ({helper}) => {237 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});238239 const owner = await helper.eth.createAccountWithBalance(donor, 100n);240 const spender = await helper.eth.createAccountWithBalance(donor, 100n);241242 const token = await collection.mintToken(minter, 100n, {Ethereum: owner});243244 const address = helper.ethAddress.fromCollectionId(collection.collectionId);245 const contract = helper.ethNativeContract.collection(address, 'rft', spender, true);246247 const tokenAddress = helper.ethAddress.fromTokenId(collection.collectionId, token.tokenId);248 const tokenContract = helper.ethNativeContract.rftToken(tokenAddress, owner);249 await tokenContract.methods.repartition(15).send();250 await tokenContract.methods.approve(spender, 15).send();251252 {253 const result = await contract.methods.burnFrom(owner, token.tokenId).send();254 const event = result.events.Transfer;255 expect(event).to.be.like({256 address: helper.ethAddress.fromCollectionId(collection.collectionId),257 event: 'Transfer',258 returnValues: {259 from: owner,260 to: '0x0000000000000000000000000000000000000000',261 tokenId: token.tokenId.toString(),262 },263 });264 }265266 expect(await collection.getTokenBalance(token.tokenId, {Ethereum: owner})).to.be.eq(0n);267 });268269 itEth('Can perform burnFromCross()', async ({helper}) => {270 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});271 272 const owner = bob;273 const spender = await helper.eth.createAccountWithBalance(donor, 100n);274275 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});276277 const address = helper.ethAddress.fromCollectionId(collection.collectionId);278 const contract = helper.ethNativeContract.collection(address, 'rft');279280 await token.repartition(owner, 15n);281 await token.approve(owner, {Ethereum: spender}, 15n);282283 {284 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);285 const result = await contract.methods.burnFromCross(ownerCross, token.tokenId).send({from: spender});286 const event = result.events.Transfer;287 expect(event).to.be.like({288 address: helper.ethAddress.fromCollectionId(collection.collectionId),289 event: 'Transfer',290 returnValues: {291 from: helper.address.substrateToEth(owner.address),292 to: '0x0000000000000000000000000000000000000000',293 tokenId: token.tokenId.toString(),294 },295 });296 }297298 expect(await collection.getTokenBalance(token.tokenId, {Substrate: owner.address})).to.be.eq(0n);299 });300301 itEth('Can perform transferFromCross()', async ({helper}) => {302 const collection = await helper.rft.mintCollection(minter, {name: 'A', description: 'B', tokenPrefix: 'C'});303304 const owner = bob;305 const spender = await helper.eth.createAccountWithBalance(donor, 100n);306 const receiver = charlie;307308 const token = await collection.mintToken(minter, 100n, {Substrate: owner.address});309310 const address = helper.ethAddress.fromCollectionId(collection.collectionId);311 const contract = helper.ethNativeContract.collection(address, 'rft');312313 await token.repartition(owner, 15n);314 await token.approve(owner, {Ethereum: spender}, 15n);315316 {317 const ownerCross = helper.ethCrossAccount.fromKeyringPair(owner);318 const recieverCross = helper.ethCrossAccount.fromKeyringPair(receiver);319 const result = await contract.methods.transferFromCross(ownerCross, recieverCross, token.tokenId).send({from: spender});320 const event = result.events.Transfer;321 expect(event).to.be.like({322 address: helper.ethAddress.fromCollectionId(collection.collectionId),323 event: 'Transfer',324 returnValues: {325 from: helper.address.substrateToEth(owner.address),326 to: helper.address.substrateToEth(receiver.address),327 tokenId: token.tokenId.toString(),328 },329 });330 }331332 expect(await token.getTop10Owners()).to.be.like([{Substrate: receiver.address}]);333 });334335 itEth('Can perform transfer()', async ({helper}) => {336 const caller = await helper.eth.createAccountWithBalance(donor);337 const receiver = helper.eth.createAccount();338 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry', '6', '6');339 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);340341 const result = await contract.methods.mint(caller).send();342 const tokenId = result.events.Transfer.returnValues.tokenId;343344 {345 const result = await contract.methods.transfer(receiver, tokenId).send();346347 const event = result.events.Transfer;348 expect(event.address).to.equal(collectionAddress);349 expect(event.returnValues.from).to.equal(caller);350 expect(event.returnValues.to).to.equal(receiver);351 expect(event.returnValues.tokenId).to.equal(tokenId.toString());352 }353354 {355 const balance = await contract.methods.balanceOf(caller).call();356 expect(+balance).to.equal(0);357 }358359 {360 const balance = await contract.methods.balanceOf(receiver).call();361 expect(+balance).to.equal(1);362 }363 });364 365 itEth('Can perform transferCross()', async ({helper}) => {366 const sender = await helper.eth.createAccountWithBalance(donor);367 const receiverEth = await helper.eth.createAccountWithBalance(donor);368 const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);369 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);370371 const collection = await helper.rft.mintCollection(minter, {});372 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);373 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);374375 const token = await collection.mintToken(minter, 50n, {Ethereum: sender});376377 {378 // Can transferCross to ethereum address:379 const result = await collectionEvm.methods.transferCross(receiverCrossEth, token.tokenId).send({from: sender});380 // Check events:381 const event = result.events.Transfer;382 expect(event.address).to.equal(collectionAddress);383 expect(event.returnValues.from).to.equal(sender);384 expect(event.returnValues.to).to.equal(receiverEth);385 expect(event.returnValues.tokenId).to.equal(token.tokenId.toString());386 // Sender's balance decreased:387 const senderBalance = await collectionEvm.methods.balanceOf(sender).call();388 expect(+senderBalance).to.equal(0);389 expect(await token.getBalance({Ethereum: sender})).to.eq(0n);390 // Receiver's balance increased:391 const receiverBalance = await collectionEvm.methods.balanceOf(receiverEth).call();392 expect(+receiverBalance).to.equal(1);393 expect(await token.getBalance({Ethereum: receiverEth})).to.eq(50n);394 }395 396 {397 // Can transferCross to substrate address:398 const substrateResult = await collectionEvm.methods.transferCross(receiverCrossSub, token.tokenId).send({from: receiverEth});399 // Check events:400 const event = substrateResult.events.Transfer;401 expect(event.address).to.be.equal(collectionAddress);402 expect(event.returnValues.from).to.be.equal(receiverEth);403 expect(event.returnValues.to).to.be.equal(helper.address.substrateToEth(minter.address));404 expect(event.returnValues.tokenId).to.be.equal(`${token.tokenId}`);405 // Sender's balance decreased:406 const senderBalance = await collectionEvm.methods.balanceOf(receiverEth).call();407 expect(+senderBalance).to.equal(0);408 expect(await token.getBalance({Ethereum: receiverEth})).to.eq(0n);409 // Receiver's balance increased:410 const receiverBalance = await helper.nft.getTokensByAddress(collection.collectionId, {Substrate: minter.address});411 expect(receiverBalance).to.contain(token.tokenId);412 expect(await token.getBalance({Substrate: minter.address})).to.eq(50n);413 }414 });415416 ['transfer', 'transferCross'].map(testCase => itEth(`Cannot ${testCase} non-owned token`, async ({helper}) => {417 const sender = await helper.eth.createAccountWithBalance(donor);418 const tokenOwner = await helper.eth.createAccountWithBalance(donor);419 const receiverSub = minter;420 const receiverCrossSub = helper.ethCrossAccount.fromKeyringPair(minter);421422 const collection = await helper.rft.mintCollection(minter, {});423 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);424 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', sender);425426 await collection.mintToken(minter, 50n, {Ethereum: sender});427 const nonSendersToken = await collection.mintToken(minter, 50n, {Ethereum: tokenOwner});428429 // Cannot transferCross someone else's token:430 const receiver = testCase === 'transfer' ? helper.address.substrateToEth(receiverSub.address) : receiverCrossSub;431 await expect(collectionEvm.methods[testCase](receiver, nonSendersToken.tokenId).send({from: sender})).to.be.rejected;432 // Cannot transfer token if it does not exist:433 await expect(collectionEvm.methods[testCase](receiver, 999999).send({from: sender})).to.be.rejected;434 }));435436 itEth('transfer event on transfer from partial ownership to full ownership', async ({helper}) => {437 const caller = await helper.eth.createAccountWithBalance(donor);438 const receiver = helper.eth.createAccount();439 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Partial-to-Full', '6', '6');440 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);441442 const result = await contract.methods.mint(caller).send();443 const tokenId = result.events.Transfer.returnValues.tokenId;444445 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);446447 await tokenContract.methods.repartition(2).send();448 await tokenContract.methods.transfer(receiver, 1).send();449450 const events: any = [];451 contract.events.allEvents((_: any, event: any) => {452 events.push(event);453 });454455 await tokenContract.methods.transfer(receiver, 1).send();456 if (events.length == 0) await helper.wait.newBlocks(1);457 const event = events[0];458459 expect(event.address).to.equal(collectionAddress);460 expect(event.returnValues.from).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');461 expect(event.returnValues.to).to.equal(receiver);462 expect(event.returnValues.tokenId).to.equal(tokenId.toString());463 });464465 itEth('transfer event on transfer from full ownership to partial ownership', async ({helper}) => {466 const caller = await helper.eth.createAccountWithBalance(donor);467 const receiver = helper.eth.createAccount();468 const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Transferry-Full-to-Partial', '6', '6');469 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);470471 const result = await contract.methods.mint(caller).send();472 const tokenId = result.events.Transfer.returnValues.tokenId;473474 const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller);475476 await tokenContract.methods.repartition(2).send();477478 const events: any = [];479 contract.events.allEvents((_: any, event: any) => {480 events.push(event);481 });482483 await tokenContract.methods.transfer(receiver, 1).send();484 if (events.length == 0) await helper.wait.newBlocks(1);485 const event = events[0];486487 expect(event.address).to.equal(collectionAddress);488 expect(event.returnValues.from).to.equal(caller);489 expect(event.returnValues.to).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF');490 expect(event.returnValues.tokenId).to.equal(tokenId.toString());491 });492});493494describe('RFT: Fees', () => {495 let donor: IKeyringPair;496497 before(async function() {498 await usingEthPlaygrounds(async (helper, privateKey) => {499 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);500501 donor = await privateKey({filename: __filename});502 });503 });504505 itEth('transferFrom() call fee is less than 0.2UNQ', async ({helper}) => {506 const caller = await helper.eth.createAccountWithBalance(donor);507 const receiver = helper.eth.createAccount();508 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer-From', '6', '6');509 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);510511 const result = await contract.methods.mint(caller).send();512 const tokenId = result.events.Transfer.returnValues.tokenId;513514 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transferFrom(caller, receiver, tokenId).send());515 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));516 expect(cost > 0n);517 });518519 itEth('transfer() call fee is less than 0.2UNQ', async ({helper}) => {520 const caller = await helper.eth.createAccountWithBalance(donor);521 const receiver = helper.eth.createAccount();522 const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'Feeful-Transfer', '6', '6');523 const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', caller);524525 const result = await contract.methods.mint(caller).send();526 const tokenId = result.events.Transfer.returnValues.tokenId;527528 const cost = await helper.eth.recordCallFee(caller, () => contract.methods.transfer(receiver, tokenId).send());529 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));530 expect(cost > 0n);531 });532});533534describe('Common metadata', () => {535 let donor: IKeyringPair;536 let alice: IKeyringPair;537538 before(async function() {539 await usingEthPlaygrounds(async (helper, privateKey) => {540 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);541542 donor = await privateKey({filename: __filename});543 [alice] = await helper.arrange.createAccounts([20n], donor);544 });545 });546547 itEth('Returns collection name', async ({helper}) => {548 const caller = helper.eth.createAccount();549 const tokenPropertyPermissions = [{550 key: 'URI',551 permission: {552 mutable: true,553 collectionAdmin: true,554 tokenOwner: false,555 },556 }];557 const collection = await helper.rft.mintCollection(558 alice,559 {560 name: 'Leviathan',561 tokenPrefix: '11',562 properties: [{key: 'ERC721Metadata', value: '1'}],563 tokenPropertyPermissions,564 },565 );566567 const contract = helper.ethNativeContract.collectionById(collection.collectionId, 'rft', caller);568 const name = await contract.methods.name().call();569 expect(name).to.equal('Leviathan');570 });571572 itEth('Returns symbol name', async ({helper}) => {573 const caller = await helper.eth.createAccountWithBalance(donor);574 const tokenPropertyPermissions = [{575 key: 'URI',576 permission: {577 mutable: true,578 collectionAdmin: true,579 tokenOwner: false,580 },581 }];582 const {collectionId} = await helper.rft.mintCollection(583 alice,584 {585 name: 'Leviathan',586 tokenPrefix: '12',587 properties: [{key: 'ERC721Metadata', value: '1'}],588 tokenPropertyPermissions,589 },590 );591592 const contract = helper.ethNativeContract.collectionById(collectionId, 'rft', caller);593 const symbol = await contract.methods.symbol().call();594 expect(symbol).to.equal('12');595 });596});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.tsdiffbeforeafterboth--- a/tests/src/refungible.test.ts
+++ b/tests/src/refungible.test.ts
@@ -255,3 +255,43 @@
});
});
+describe('Refungible 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.ReFungible]);
+
+ donor = await privateKey({filename: __filename});
+ [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);
+ });
+ });
+
+ itSub('Cannot transfer incorrect amount of token pieces', async ({helper}) => {
+ const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});
+ const tokenAlice = await collection.mintToken(alice, 10n, {Substrate: alice.address});
+ const tokenBob = await collection.mintToken(alice, 10n, {Substrate: bob.address});
+
+ // 1. Alice cannot transfer Bob's token:
+ await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 0n)).to.be.rejectedWith('common.TokenValueTooLow');
+ await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 1n)).to.be.rejectedWith('common.TokenValueTooLow');
+ await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 10n)).to.be.rejectedWith('common.TokenValueTooLow');
+ await expect(tokenBob.transfer(alice, {Substrate: charlie.address}, 100n)).to.be.rejectedWith('common.TokenValueTooLow');
+
+ // 2. Alice cannot transfer non-existing token:
+ await expect(collection.transferToken(alice, 100, {Substrate: charlie.address}, 0n)).to.be.rejectedWith('common.TokenValueTooLow');
+ await expect(collection.transferToken(alice, 100, {Substrate: charlie.address}, 1n)).to.be.rejectedWith('common.TokenValueTooLow');
+
+ // 3. Zero transfer allowed (EIP-20):
+ await tokenAlice.transfer(alice, {Substrate: charlie.address}, 0n);
+
+ expect(await tokenAlice.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);
+ expect(await tokenBob.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);
+ expect(await tokenAlice.getBalance({Substrate: alice.address})).to.eq(10n);
+ expect(await tokenBob.getBalance({Substrate: bob.address})).to.eq(10n);
+ expect(await tokenBob.getBalance({Substrate: charlie.address})).to.eq(0n);
+ });
+});
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});
+ });
});