difftreelog
Merge pull request #410 from UniqueNetwork/fix/admin-transfer
in: master
Fix/admin transfer
7 files changed
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -379,11 +379,7 @@
) -> DispatchResult {
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
- ensure!(
- &token_data.owner == sender
- || (collection.limits.owner_can_transfer() && collection.is_owner_or_admin(sender)),
- <CommonError<T>>::NoPermission
- );
+ ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);
if collection.permissions.access() == AccessMode::AllowList {
collection.check_allowlist(sender)?;
@@ -665,12 +661,7 @@
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
- // TODO: require sender to be token, owner, require admins to go through transfer_from
- ensure!(
- &token_data.owner == from
- || (collection.limits.owner_can_transfer() && collection.is_owner_or_admin(from)),
- <CommonError<T>>::NoPermission
- );
+ ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);
if collection.permissions.access() == AccessMode::AllowList {
collection.check_allowlist(from)?;
@@ -976,8 +967,12 @@
// `from`, `to` checked in [`transfer`]
collection.check_allowlist(spender)?;
}
+
+ if collection.limits.owner_can_transfer() && collection.is_owner_or_admin(spender) {
+ return Ok(());
+ }
+
if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
- // TODO: should collection owner be allowed to perform this transfer?
ensure!(
<PalletStructure<T>>::check_indirectly_owned(
spender.clone(),
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -60,6 +60,7 @@
"testAddToContractAllowList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractAllowList.test.ts",
"testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",
"testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",
+ "testAdminTransferAndBurn": "mocha --timeout 9999999 -r ts-node/register ./**/adminTransferAndBurn.test.ts",
"testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",
"testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
"testContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/contractSponsoring.test.ts",
tests/src/adminTransferAndBurn.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/adminTransferAndBurn.test.ts
@@ -0,0 +1,72 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+import {default as usingApi} from './substrate/substrate-api';
+import {
+ createCollectionExpectSuccess,
+ createItemExpectSuccess,
+ transferExpectFailure,
+ transferFromExpectSuccess,
+ burnItemExpectFailure,
+ burnFromExpectSuccess,
+ setCollectionLimitsExpectSuccess,
+} from './util/helpers';
+
+chai.use(chaiAsPromised);
+const expect = chai.expect;
+
+describe('Integration Test: ownerCanTransfer allows admins to use only transferFrom/burnFrom:', () => {
+ let alice: IKeyringPair;
+ let bob: IKeyringPair;
+ let charlie: IKeyringPair;
+
+ before(async () => {
+ await usingApi(async (api, privateKeyWrapper) => {
+ alice = privateKeyWrapper('//Alice');
+ bob = privateKeyWrapper('//Bob');
+ charlie = privateKeyWrapper('//Charlie');
+ });
+ });
+
+ it('admin transfers other user\'s token', async () => {
+ await usingApi(async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});
+
+ const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', {Substrate: bob.address});
+
+ await transferExpectFailure(collectionId, tokenId, alice, charlie);
+
+ await transferFromExpectSuccess(collectionId, tokenId, alice, bob, charlie, 1);
+ });
+ });
+
+ it('admin burns other user\'s token', async () => {
+ await usingApi(async () => {
+ const collectionId = await createCollectionExpectSuccess();
+ await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});
+
+ const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', {Substrate: bob.address});
+
+ await burnItemExpectFailure(alice, collectionId, tokenId);
+
+ await burnFromExpectSuccess(alice, bob, collectionId, tokenId);
+ });
+ });
+});
tests/src/burnItem.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 {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20 createCollectionExpectSuccess,21 createItemExpectSuccess,22 getGenericResult,23 normalizeAccountId,24 addCollectionAdminExpectSuccess,25 getBalance,26 setCollectionLimitsExpectSuccess,27 isTokenExists,28} from './util/helpers';2930import chai from 'chai';31import chaiAsPromised from 'chai-as-promised';32chai.use(chaiAsPromised);33const expect = chai.expect;3435let alice: IKeyringPair;36let bob: IKeyringPair;3738describe('integration test: ext. burnItem():', () => {39 before(async () => {40 await usingApi(async (api, privateKeyWrapper) => {41 alice = privateKeyWrapper('//Alice');42 bob = privateKeyWrapper('//Bob');43 });44 });4546 it('Burn item in NFT collection', async () => {47 const createMode = 'NFT';48 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});49 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);5051 await usingApi(async (api) => {52 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);53 const events = await submitTransactionAsync(alice, tx);54 const result = getGenericResult(events);5556 expect(result.success).to.be.true;57 // Get the item58 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;59 });60 });6162 it('Burn item in Fungible collection', async () => {63 const createMode = 'Fungible';64 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});65 await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens66 const tokenId = 0; // ignored6768 await usingApi(async (api) => {69 // Destroy 1 of 1070 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);71 const events = await submitTransactionAsync(alice, tx);72 const result = getGenericResult(events);7374 // Get alice balance75 const balance = await getBalance(api, collectionId, alice.address, 0);7677 // What to expect78 expect(result.success).to.be.true;79 expect(balance).to.be.equal(9n);80 });81 });8283 it('Burn item in ReFungible collection', async () => {84 const createMode = 'ReFungible';85 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});86 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);8788 await usingApi(async (api) => {89 const tx = api.tx.unique.burnItem(collectionId, tokenId, 100);90 const events = await submitTransactionAsync(alice, tx);91 const result = getGenericResult(events);9293 // Get alice balance94 const balance = await getBalance(api, collectionId, alice.address, tokenId);9596 // What to expect97 expect(result.success).to.be.true;98 expect(balance).to.be.equal(0n);99 });100 });101102 it('Burn owned portion of item in ReFungible collection', async () => {103 const createMode = 'ReFungible';104 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});105 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);106107 await usingApi(async (api) => {108 // Transfer 1/100 of the token to Bob109 const transfertx = api.tx.unique.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);110 const events1 = await submitTransactionAsync(alice, transfertx);111 const result1 = getGenericResult(events1);112113 // Get balances114 const bobBalanceBefore = await getBalance(api, collectionId, bob.address, tokenId);115 const aliceBalanceBefore = await getBalance(api, collectionId, alice.address, tokenId);116117 // Bob burns his portion118 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);119 const events2 = await submitTransactionAsync(bob, tx);120 const result2 = getGenericResult(events2);121122 // Get balances123 const bobBalanceAfter = await getBalance(api, collectionId, bob.address, tokenId);124 const aliceBalanceAfter = await getBalance(api, collectionId, alice.address, tokenId);125 // console.log(balance);126127 // What to expect before burning128 expect(result1.success).to.be.true;129 expect(aliceBalanceBefore).to.be.equal(99n);130 expect(bobBalanceBefore).to.be.equal(1n);131132 // What to expect after burning133 expect(result2.success).to.be.true;134 expect(aliceBalanceAfter).to.be.equal(99n);135 expect(bobBalanceAfter).to.be.equal(0n);136 });137138 });139140});141142describe('integration test: ext. burnItem() with admin permissions:', () => {143 before(async () => {144 await usingApi(async (api, privateKeyWrapper) => {145 alice = privateKeyWrapper('//Alice');146 bob = privateKeyWrapper('//Bob');147 });148 });149150 it('Burn item in NFT collection', async () => {151 const createMode = 'NFT';152 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});153 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});154 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);155 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);156157 await usingApi(async (api) => {158 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);159 const events = await submitTransactionAsync(bob, tx);160 const result = getGenericResult(events);161162 expect(result.success).to.be.true;163 // Get the item164 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;165 });166 });167168 // TODO: burnFrom169 it('Burn item in Fungible collection', async () => {170 const createMode = 'Fungible';171 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});172 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});173 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens174 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);175176 await usingApi(async (api) => {177 // Destroy 1 of 10178 const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);179 const events = await submitTransactionAsync(bob, tx);180 const result = getGenericResult(events);181182 // Get alice balance183 const balance = await getBalance(api, collectionId, alice.address, 0);184185 // What to expect186 expect(result.success).to.be.true;187 expect(balance).to.be.equal(9n);188 });189 });190191 // TODO: burnFrom192 it('Burn item in ReFungible collection', async () => {193 const createMode = 'ReFungible';194 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});195 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});196 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);197 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);198199 await usingApi(async (api) => {200 const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);201 const events = await submitTransactionAsync(bob, tx);202 const result = getGenericResult(events);203 // Get alice balance204 expect(result.success).to.be.true;205 // Get the item206 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;207 });208 });209});210211describe('Negative integration test: ext. burnItem():', () => {212 before(async () => {213 await usingApi(async (api, privateKeyWrapper) => {214 alice = privateKeyWrapper('//Alice');215 bob = privateKeyWrapper('//Bob');216 });217 });218219 it('Burn a token that was never created', async () => {220 const createMode = 'NFT';221 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});222 const tokenId = 10;223224 await usingApi(async (api) => {225 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);226 const badTransaction = async function () {227 await submitTransactionExpectFailAsync(alice, tx);228 };229 await expect(badTransaction()).to.be.rejected;230 });231232 });233234 it('Burn a token using the address that does not own it', async () => {235 const createMode = 'NFT';236 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});237 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);238239 await usingApi(async (api) => {240 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);241 const badTransaction = async function () {242 await submitTransactionExpectFailAsync(bob, tx);243 };244 await expect(badTransaction()).to.be.rejected;245 });246247 });248249 it('Transfer a burned a token', async () => {250 const createMode = 'NFT';251 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});252 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);253254 await usingApi(async (api) => {255256 const burntx = api.tx.unique.burnItem(collectionId, tokenId, 1);257 const events1 = await submitTransactionAsync(alice, burntx);258 const result1 = getGenericResult(events1);259 expect(result1.success).to.be.true;260261 const tx = api.tx.unique.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);262 const badTransaction = async function () {263 await submitTransactionExpectFailAsync(alice, tx);264 };265 await expect(badTransaction()).to.be.rejected;266 });267268 });269270 it('Burn more than owned in Fungible collection', async () => {271 const createMode = 'Fungible';272 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});273 // Helper creates 10 fungible tokens274 await createItemExpectSuccess(alice, collectionId, createMode);275 const tokenId = 0; // ignored276277 await usingApi(async (api) => {278 // Destroy 11 of 10279 const tx = api.tx.unique.burnItem(collectionId, tokenId, 11);280 const badTransaction = async function () {281 await submitTransactionExpectFailAsync(alice, tx);282 };283 await expect(badTransaction()).to.be.rejected;284285 // Get alice balance286 const balance = await getBalance(api, collectionId, alice.address, 0);287288 // What to expect289 expect(balance).to.be.equal(10n);290 });291292 });293294});tests/src/nesting/graphs.test.tsdiffbeforeafterboth--- a/tests/src/nesting/graphs.test.ts
+++ b/tests/src/nesting/graphs.test.ts
@@ -36,7 +36,7 @@
await usingApi(async (api, privateKeyWrapper) => {
const alice = privateKeyWrapper('//Alice');
const collection = await buildComplexObjectGraph(api, alice);
- await setCollectionLimitsExpectSuccess(alice, collection, {ownerCanTransfer: true});
+ const tokenTwoParent = tokenIdToCross(collection, 1);
// to self
await expect(
@@ -49,7 +49,7 @@
'second transaction',
).to.be.rejectedWith(/structure\.OuroborosDetected/);
await expect(
- executeTransaction(api, alice, api.tx.unique.transfer(tokenIdToCross(collection, 8), collection, 2, 1)),
+ executeTransaction(api, alice, api.tx.unique.transferFrom(tokenTwoParent, tokenIdToCross(collection, 8), collection, 2, 1)),
'third transaction',
).to.be.rejectedWith(/structure\.OuroborosDetected/);
});
tests/src/nesting/nest.test.tsdiffbeforeafterboth--- a/tests/src/nesting/nest.test.ts
+++ b/tests/src/nesting/nest.test.ts
@@ -123,7 +123,7 @@
], 'Children contents check at nesting #2');
// Move token B to a different user outside the nesting tree
- await transferExpectSuccess(collectionA, tokenB, alice, bob);
+ await transferFromExpectSuccess(collectionA, tokenB, alice, targetAddress, bob);
children = await getTokenChildren(api, collectionA, targetToken);
expect(children.length).to.be.equal(1, 'Children length check at unnesting');
expect(children).to.be.have.deep.members([
@@ -470,7 +470,7 @@
await expect(executeTransaction(
api,
alice,
- api.tx.unique.transfer(targetAddress, collection, newToken, 1),
+ api.tx.unique.transferFrom(targetAddress, {Substrate: bob.address}, collection, newToken, 1),
), 'while nesting another\'s token token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);
expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -830,6 +830,25 @@
});
}
+export async function burnItemExpectFailure(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {
+ await usingApi(async (api) => {
+ const tx = api.tx.unique.burnItem(collectionId, tokenId, value);
+
+ const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;
+ const result = getCreateCollectionResult(events);
+ // tslint:disable-next-line:no-unused-expression
+ expect(result.success).to.be.false;
+ });
+}
+
+export async function burnFromExpectSuccess(sender: IKeyringPair, from: IKeyringPair | CrossAccountId, collectionId: number, tokenId: number, value: number | bigint = 1) {
+ await usingApi(async (api) => {
+ const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(from), tokenId, value);
+ const events = await submitTransactionAsync(sender, tx);
+ return getGenericResult(events).success;
+ });
+}
+
export async function
approve(
api: ApiPromise,