From d996a74e54debfc3591790d5c26c7b75de32b1b9 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 18 Jan 2023 11:19:29 +0000 Subject: [PATCH] Merge pull request #835 from UniqueNetwork/tests/generalization --- --- a/tests/src/apiConsts.test.ts +++ b/tests/src/apiConsts.test.ts @@ -15,7 +15,7 @@ // along with Unique Network. If not, see . import {ApiPromise} from '@polkadot/api'; -import {usingPlaygrounds, itSub, expect} from './util'; +import {usingPlaygrounds, itSub, expect, COLLECTION_HELPER, CONTRACT_HELPER} from './util'; const MAX_COLLECTION_DESCRIPTION_LENGTH = 256n; @@ -40,9 +40,6 @@ ownerCanDestroy: true, transfersEnabled: true, }; - -const EVM_COLLECTION_HELPERS_ADDRESS = '0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f'; -const HELPERS_CONTRACT_ADDRESS = '0x842899ECF380553E8a4de75bF534cdf6fBF64049'; describe('integration test: API UNIQUE consts', () => { let api: ApiPromise; @@ -106,11 +103,11 @@ }); itSub('HELPERS_CONTRACT_ADDRESS', () => { - expect(api.consts.evmContractHelpers.contractAddress.toString().toLowerCase()).to.be.equal(HELPERS_CONTRACT_ADDRESS.toLowerCase()); + expect(api.consts.evmContractHelpers.contractAddress.toString().toLowerCase()).to.be.equal(CONTRACT_HELPER.toLowerCase()); }); itSub('EVM_COLLECTION_HELPERS_ADDRESS', () => { - expect(api.consts.common.contractAddress.toString().toLowerCase()).to.be.equal(EVM_COLLECTION_HELPERS_ADDRESS.toLowerCase()); + expect(api.consts.common.contractAddress.toString().toLowerCase()).to.be.equal(COLLECTION_HELPER.toLowerCase()); }); }); --- a/tests/src/burnItem.test.ts +++ b/tests/src/burnItem.test.ts @@ -45,32 +45,6 @@ await collection.burnTokens(alice, 1n); expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n); }); - - itSub.ifWithPallets('Burn item in ReFungible collection', [Pallets.ReFungible], async function({helper}) { - const collection = await helper.rft.mintCollection(alice); - const token = await collection.mintToken(alice, 100n); - - await token.burn(alice, 90n); - expect(await token.getBalance({Substrate: alice.address})).to.eq(10n); - - await token.burn(alice, 10n); - expect(await token.getBalance({Substrate: alice.address})).to.eq(0n); - }); - - itSub.ifWithPallets('Burn owned portion of item in ReFungible collection', [Pallets.ReFungible], async function({helper}) { - const collection = await helper.rft.mintCollection(alice); - const token = await collection.mintToken(alice, 100n); - - await token.transfer(alice, {Substrate: bob.address}, 1n); - - expect(await token.getBalance({Substrate: alice.address})).to.eq(99n); - expect(await token.getBalance({Substrate: bob.address})).to.eq(1n); - - await token.burn(bob, 1n); - - expect(await token.getBalance({Substrate: alice.address})).to.eq(99n); - expect(await token.getBalance({Substrate: bob.address})).to.eq(0n); - }); }); describe('integration test: ext. burnItem() with admin permissions:', () => { @@ -104,16 +78,6 @@ await collection.burnTokensFrom(bob, {Substrate: alice.address}, 1n); expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n); }); - - itSub.ifWithPallets('Burn item in ReFungible collection', [Pallets.ReFungible], async function({helper}) { - const collection = await helper.rft.mintCollection(alice); - await collection.setLimits(alice, {ownerCanTransfer: true}); - await collection.addAdmin(alice, {Substrate: bob.address}); - const token = await collection.mintToken(alice, 100n); - - await token.burnFrom(bob, {Substrate: alice.address}, 100n); - expect(await token.doesExist()).to.be.false; - }); }); describe('Negative integration test: ext. burnItem():', () => { @@ -138,31 +102,6 @@ const token = await collection.mintToken(alice); 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}) => { --- a/tests/src/eth/collectionHelperAddress.test.ts +++ b/tests/src/eth/collectionHelperAddress.test.ts @@ -16,9 +16,7 @@ import {itEth, usingEthPlaygrounds, expect} from './util'; import {IKeyringPair} from '@polkadot/types/types'; -import {Pallets} from '../util'; - -const EVM_COLLECTION_HELPERS_ADDRESS = '0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f'; +import {COLLECTION_HELPER, Pallets} from '../util'; describe('[eth]CollectionHelperAddress test: ERC20/ERC721 ', () => { let donor: IKeyringPair; @@ -36,7 +34,7 @@ const nftCollection = await helper.ethNativeContract.collection(nftCollectionAddress, 'nft', owner); expect((await nftCollection.methods.collectionHelperAddress().call()) - .toString().toLowerCase()).to.be.equal(EVM_COLLECTION_HELPERS_ADDRESS); + .toString().toLowerCase()).to.be.equal(COLLECTION_HELPER); }); itEth.ifWithPallets('RFT ', [Pallets.ReFungible], async ({helper}) => { @@ -46,7 +44,7 @@ const rftCollection = await helper.ethNativeContract.collection(rftCollectionAddress, 'rft', owner); expect((await rftCollection.methods.collectionHelperAddress().call()) - .toString().toLowerCase()).to.be.equal(EVM_COLLECTION_HELPERS_ADDRESS); + .toString().toLowerCase()).to.be.equal(COLLECTION_HELPER); }); itEth('FT', async ({helper}) => { @@ -56,7 +54,7 @@ const collection = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner); expect((await collection.methods.collectionHelperAddress().call()) - .toString().toLowerCase()).to.be.equal(EVM_COLLECTION_HELPERS_ADDRESS); + .toString().toLowerCase()).to.be.equal(COLLECTION_HELPER); }); itEth('[collectionHelpers] convert collectionId into address', async ({helper}) => { --- a/tests/src/eth/createNFTCollection.test.ts +++ b/tests/src/eth/createNFTCollection.test.ts @@ -18,6 +18,7 @@ import {IKeyringPair} from '@polkadot/types/types'; import {expect, itEth, usingEthPlaygrounds} from './util'; import {CollectionLimitField} from './util/playgrounds/types'; +import {COLLECTION_HELPER} from '../util'; describe('Create NFT collection from EVM', () => { --- a/tests/src/eth/fungible.test.ts +++ b/tests/src/eth/fungible.test.ts @@ -17,38 +17,6 @@ import {expect, itEth, usingEthPlaygrounds} from './util'; import {IKeyringPair} from '@polkadot/types/types'; -describe('Fungible: Information getting', () => { - let donor: IKeyringPair; - let alice: IKeyringPair; - - before(async function() { - await usingEthPlaygrounds(async (helper, privateKey) => { - donor = await privateKey({filename: __filename}); - [alice] = await helper.arrange.createAccounts([20n], donor); - }); - }); - - itEth('totalSupply', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const collection = await helper.ft.mintCollection(alice); - await collection.mint(alice, 200n); - - const contract = await helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller); - const totalSupply = await contract.methods.totalSupply().call(); - expect(totalSupply).to.equal('200'); - }); - - itEth('balanceOf', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const collection = await helper.ft.mintCollection(alice); - await collection.mint(alice, 200n, {Ethereum: caller}); - - const contract = await helper.ethNativeContract.collectionById(collection.collectionId, 'ft', caller); - const balance = await contract.methods.balanceOf(caller).call(); - expect(balance).to.equal('200'); - }); -}); - describe('Fungible: Plain calls', () => { let donor: IKeyringPair; let alice: IKeyringPair; @@ -59,24 +27,6 @@ donor = await privateKey({filename: __filename}); [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor); }); - }); - - itEth('Can perform mint()', async ({helper}) => { - const owner = await helper.eth.createAccountWithBalance(donor); - const receiver = helper.eth.createAccount(); - const collection = await helper.ft.mintCollection(alice); - await collection.addAdmin(alice, {Ethereum: owner}); - - const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner); - - const result = await contract.methods.mint(receiver, 100).send(); - - const event = result.events.Transfer; - expect(event.address).to.equal(collectionAddress); - expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000'); - expect(event.returnValues.to).to.equal(receiver); - expect(event.returnValues.value).to.equal('100'); }); [ --- /dev/null +++ b/tests/src/eth/getCode.test.ts @@ -0,0 +1,53 @@ +// 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 . + +import {expect, itEth, usingEthPlaygrounds} from './util'; +import {IKeyringPair} from '@polkadot/types/types'; +import {COLLECTION_HELPER, CONTRACT_HELPER} from '../util'; + +describe('RPC eth_getCode', () => { + let donor: IKeyringPair; + + before(async function() { + await usingEthPlaygrounds(async (helper, privateKey) => { + donor = await privateKey({filename: __filename}); + }); + }); + + [ + {address: COLLECTION_HELPER}, + {address: CONTRACT_HELPER}, + ].map(testCase => { + itEth(`returns value for native contract: ${testCase.address}`, async ({helper}) => { + const contractCodeSub = (await helper.callRpc('api.rpc.eth.getCode', [testCase.address])).toJSON(); + const contractCodeEth = (await helper.getWeb3().eth.getCode(testCase.address)); + + expect(contractCodeSub).to.has.length.greaterThan(4); + expect(contractCodeEth).to.has.length.greaterThan(4); + }); + }); + + itEth('returns value for custom contract', async ({helper}) => { + const signer = await helper.eth.createAccountWithBalance(donor); + const flipper = await helper.eth.deployFlipper(signer); + + const contractCodeSub = (await helper.callRpc('api.rpc.eth.getCode', [flipper.options.address])).toJSON(); + const contractCodeEth = (await helper.getWeb3().eth.getCode(flipper.options.address)); + + expect(contractCodeSub).to.has.length.greaterThan(4); + expect(contractCodeEth).to.has.length.greaterThan(4); + }); +}); --- a/tests/src/eth/nonFungible.test.ts +++ b/tests/src/eth/nonFungible.test.ts @@ -19,68 +19,6 @@ import {Contract} from 'web3-eth-contract'; import {ITokenPropertyPermission} from '../util/playgrounds/types'; - -describe('NFT: Information getting', () => { - let donor: IKeyringPair; - let alice: IKeyringPair; - - before(async function() { - await usingEthPlaygrounds(async (helper, privateKey) => { - donor = await privateKey({filename: __filename}); - [alice] = await helper.arrange.createAccounts([10n], donor); - }); - }); - - itEth('totalSupply', async ({helper}) => { - const collection = await helper.nft.mintCollection(alice, {}); - await collection.mintToken(alice); - - const caller = await helper.eth.createAccountWithBalance(donor); - - const contract = await helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller); - const totalSupply = await contract.methods.totalSupply().call(); - - expect(totalSupply).to.equal('1'); - }); - - itEth('balanceOf', async ({helper}) => { - const collection = await helper.nft.mintCollection(alice, {}); - const caller = await helper.eth.createAccountWithBalance(donor); - - await collection.mintToken(alice, {Ethereum: caller}); - await collection.mintToken(alice, {Ethereum: caller}); - await collection.mintToken(alice, {Ethereum: caller}); - - const contract = await helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller); - const balance = await contract.methods.balanceOf(caller).call(); - - expect(balance).to.equal('3'); - }); - - itEth('ownerOf', async ({helper}) => { - const collection = await helper.nft.mintCollection(alice, {}); - const caller = await helper.eth.createAccountWithBalance(donor); - - const token = await collection.mintToken(alice, {Ethereum: caller}); - - const contract = await helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller); - - const owner = await contract.methods.ownerOf(token.tokenId).call(); - - expect(owner).to.equal(caller); - }); - - itEth('name/symbol is available regardless of ERC721Metadata support', async ({helper}) => { - const collection = await helper.nft.mintCollection(alice, {name: 'test', tokenPrefix: 'TEST'}); - const caller = helper.eth.createAccount(); - - const contract = await helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller); - - expect(await contract.methods.name().call()).to.equal('test'); - expect(await contract.methods.symbol().call()).to.equal('TEST'); - }); -}); - describe('Check ERC721 token URI for NFT', () => { let donor: IKeyringPair; @@ -148,30 +86,6 @@ donor = await privateKey({filename: __filename}); [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor); }); - }); - - itEth('Can perform mint() & get crossOwner()', async ({helper}) => { - const owner = await helper.eth.createAccountWithBalance(donor); - const receiver = helper.eth.createAccount(); - - const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', ''); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner); - - const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send(); - const tokenId = result.events.Transfer.returnValues.tokenId; - expect(tokenId).to.be.equal('1'); - - const event = result.events.Transfer; - expect(event.address).to.be.equal(collectionAddress); - expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000'); - expect(event.returnValues.to).to.be.equal(receiver); - - expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI'); - expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']); - // TODO: this wont work right now, need release 919000 first - // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send(); - // const tokenUri = await contract.methods.tokenURI(nextTokenId).call(); - // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`); }); // TODO combine all minting tests in one place --- a/tests/src/eth/reFungible.test.ts +++ b/tests/src/eth/reFungible.test.ts @@ -19,90 +19,6 @@ import {IKeyringPair} from '@polkadot/types/types'; import {ITokenPropertyPermission} from '../util/playgrounds/types'; -describe('Refungible: Information getting', () => { - let donor: IKeyringPair; - - before(async function() { - await usingEthPlaygrounds(async (helper, privateKey) => { - requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); - - donor = await privateKey({filename: __filename}); - }); - }); - - itEth('totalSupply', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'TotalSupply', '6', '6'); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller); - - await contract.methods.mint(caller).send(); - - const totalSupply = await contract.methods.totalSupply().call(); - expect(totalSupply).to.equal('1'); - }); - - itEth('balanceOf', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'BalanceOf', '6', '6'); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller); - - await contract.methods.mint(caller).send(); - await contract.methods.mint(caller).send(); - await contract.methods.mint(caller).send(); - - const balance = await contract.methods.balanceOf(caller).call(); - expect(balance).to.equal('3'); - }); - - itEth('ownerOf', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const {collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf', '6', '6'); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller); - - const result = await contract.methods.mint(caller).send(); - const tokenId = result.events.Transfer.returnValues.tokenId; - - const owner = await contract.methods.ownerOf(tokenId).call(); - expect(owner).to.equal(caller); - }); - - itEth('ownerOf after burn', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const receiver = helper.eth.createAccount(); - const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'OwnerOf-AfterBurn', '6', '6'); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller); - - const result = await contract.methods.mint(caller).send(); - const tokenId = result.events.Transfer.returnValues.tokenId; - const tokenContract = await helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller, true); - - await tokenContract.methods.repartition(2).send(); - await tokenContract.methods.transfer(receiver, 1).send(); - - await tokenContract.methods.burnFrom(caller, 1).send(); - - const owner = await contract.methods.ownerOf(tokenId).call(); - expect(owner).to.equal(receiver); - }); - - itEth('ownerOf for partial ownership', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const receiver = helper.eth.createAccount(); - const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Partial-OwnerOf', '6', '6'); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller); - - const result = await contract.methods.mint(caller).send(); - const tokenId = result.events.Transfer.returnValues.tokenId; - const tokenContract = await helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller); - - await tokenContract.methods.repartition(2).send(); - await tokenContract.methods.transfer(receiver, 1).send(); - - const owner = await contract.methods.ownerOf(tokenId).call(); - expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'); - }); -}); - describe('Refungible: Plain calls', () => { let donor: IKeyringPair; let minter: IKeyringPair; @@ -116,25 +32,6 @@ donor = await privateKey({filename: __filename}); [minter, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor); }); - }); - - itEth('Can perform mint() & crossOwnerOf()', async ({helper}) => { - const owner = await helper.eth.createAccountWithBalance(donor); - const receiver = helper.eth.createAccount(); - const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Minty', '6', '6', ''); - const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner); - - const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send(); - - const event = result.events.Transfer; - expect(event.address).to.equal(collectionAddress); - expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000'); - expect(event.returnValues.to).to.equal(receiver); - const tokenId = event.returnValues.tokenId; - expect(tokenId).to.be.equal('1'); - - expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']); - expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI'); }); [ --- a/tests/src/eth/reFungibleToken.test.ts +++ b/tests/src/eth/reFungibleToken.test.ts @@ -19,51 +19,6 @@ import {IKeyringPair} from '@polkadot/types/types'; import {Contract} from 'web3-eth-contract'; - -describe('Refungible token: Information getting', () => { - let donor: IKeyringPair; - let alice: IKeyringPair; - - before(async function() { - await usingEthPlaygrounds(async (helper, privateKey) => { - requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); - - donor = await privateKey({filename: __filename}); - [alice] = await helper.arrange.createAccounts([20n], donor); - }); - }); - - itEth('totalSupply', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'MUON'}); - const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: caller}); - - const contract = await helper.ethNativeContract.rftTokenById(collection.collectionId, tokenId, caller); - const totalSupply = await contract.methods.totalSupply().call(); - expect(totalSupply).to.equal('200'); - }); - - itEth('balanceOf', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'MUON'}); - const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: caller}); - - const contract = await helper.ethNativeContract.rftTokenById(collection.collectionId, tokenId, caller); - const balance = await contract.methods.balanceOf(caller).call(); - expect(balance).to.equal('200'); - }); - - itEth('decimals', async ({helper}) => { - const caller = await helper.eth.createAccountWithBalance(donor); - const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'MUON'}); - const {tokenId} = await collection.mintToken(alice, 200n, {Ethereum: caller}); - - const contract = await helper.ethNativeContract.rftTokenById(collection.collectionId, tokenId, caller); - const decimals = await contract.methods.decimals().call(); - expect(decimals).to.equal('0'); - }); -}); - // FIXME: Need erc721 for ReFubgible. describe('Check ERC721 token URI for ReFungible', () => { let donor: IKeyringPair; --- /dev/null +++ b/tests/src/eth/tokens/callMethodsERC20.test.ts @@ -0,0 +1,91 @@ +// 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 . + +import {Pallets, requirePalletsOrSkip} from '../../util'; +import {expect, itEth, usingEthPlaygrounds} from '../util'; +import {IKeyringPair} from '@polkadot/types/types'; + +[ + {mode: 'ft' as const, requiredPallets: []}, + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, +].map(testCase => { + describe(`${testCase.mode.toUpperCase()}: ERC-20 call methods`, () => { + let donor: IKeyringPair; + + before(async function() { + await usingEthPlaygrounds(async (helper, privateKey) => { + requirePalletsOrSkip(this, helper, testCase.requiredPallets); + donor = await privateKey({filename: __filename}); + }); + }); + + itEth('totalSupply', async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + const mintingParams = testCase.mode === 'ft' ? [caller, 200n] : [caller]; + + const {collection, collectionId} = await helper.eth.createCollection(testCase.mode, caller, 'TotalSupply', '6', '6'); + if (testCase.mode === 'rft') await collection.methods.mint(caller).send({from: caller}); + + // Use collection contract for FT or token contract for RFT: + const contract = testCase.mode === 'ft' + ? collection + : await helper.ethNativeContract.rftTokenById(collectionId, 1, caller); + + // Mint tokens: + testCase.mode === 'ft' + ? await contract.methods.mint(...mintingParams).send({from: caller}) + : await contract.methods.repartition(200).send({from: caller}); + + const totalSupply = await contract.methods.totalSupply().call(); + expect(totalSupply).to.equal('200'); + }); + + itEth('balanceOf', async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + const mintingParams = testCase.mode === 'ft' ? [caller, 200n] : [caller]; + + const {collection, collectionId} = await helper.eth.createCollection(testCase.mode, caller, 'BalanceOf', 'Descroption', 'Prefix'); + if (testCase.mode === 'rft') await collection.methods.mint(caller).send({from: caller}); + + // Use collection contract for FT or token contract for RFT: + const contract = testCase.mode === 'ft' + ? collection + : await helper.ethNativeContract.rftTokenById(collectionId, 1, caller); + + // Mint tokens: + testCase.mode === 'ft' + ? await contract.methods.mint(...mintingParams).send({from: caller}) + : await contract.methods.repartition(200).send({from: caller}); + + const balance = await contract.methods.balanceOf(caller).call(); + expect(balance).to.equal('200'); + }); + + itEth('decimals', async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + const {collection, collectionId} = await helper.eth.createCollection(testCase.mode, caller, 'BalanceOf', 'Descroption', 'Prefix'); + if (testCase.mode === 'rft') await collection.methods.mint(caller).send({from: caller}); + + // Use collection contract for FT or token contract for RFT: + const contract = testCase.mode === 'ft' + ? collection + : await helper.ethNativeContract.rftTokenById(collectionId, 1, caller); + + const decimals = await contract.methods.decimals().call(); + expect(decimals).to.equal(testCase.mode === 'rft' ? '0' : '18'); + }); + }); +}); \ No newline at end of file --- /dev/null +++ b/tests/src/eth/tokens/callMethodsERC721.test.ts @@ -0,0 +1,143 @@ +// 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 . + +import {Pallets} from '../../util'; +import {expect, itEth, usingEthPlaygrounds} from '../util'; +import {IKeyringPair} from '@polkadot/types/types'; + + +describe('ERC-721 call methods', () => { + let donor: IKeyringPair; + + before(async function() { + await usingEthPlaygrounds(async (helper, privateKey) => { + donor = await privateKey({filename: __filename}); + }); + }); + + [ + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'nft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: name/symbol/description`, testCase.requiredPallets, async ({helper}) => { + const callerEth = await helper.eth.createAccountWithBalance(donor); + const [callerSub] = await helper.arrange.createAccounts([100n], donor); + const [name, description, tokenPrefix] = ['Name', 'Description', 'Symbol']; + + const {collection: collectionEth} = await helper.eth.createCollection(testCase.mode, callerEth, name, description, tokenPrefix); + await collectionEth.methods.mint(callerEth).send({from: callerEth}); + const {collectionId} = await helper[testCase.mode].mintCollection(callerSub, {name, description, tokenPrefix}); + const collectionSub = await helper.ethNativeContract.collectionById(collectionId, testCase.mode, callerEth); + + // Can get name/symbol/description for Eth collection + expect(await collectionEth.methods.name().call()).to.eq(name); + expect(await collectionEth.methods.symbol().call()).to.eq(tokenPrefix); + expect(await collectionEth.methods.description().call()).to.eq(description); + // Can get name/symbol/description for Sub collection + expect(await collectionSub.methods.name().call()).to.eq(name); + expect(await collectionSub.methods.symbol().call()).to.eq(tokenPrefix); + expect(await collectionSub.methods.description().call()).to.eq(description); + }); + }); + + [ + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'nft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: totalSupply`, testCase.requiredPallets, async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + + const {collection} = await helper.eth.createCollection(testCase.mode, caller, 'TotalSupply', '6', '6'); + await collection.methods.mint(caller).send({from: caller}); + + const totalSupply = await collection.methods.totalSupply().call(); + expect(totalSupply).to.equal('1'); + }); + }); + + [ + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'nft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: balanceOf`, testCase.requiredPallets, async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + + const {collection} = await helper.eth.createCollection(testCase.mode, caller, 'BalanceOf', 'Descroption', 'Prefix'); + await collection.methods.mint(caller).send({from: caller}); + await collection.methods.mint(caller).send({from: caller}); + await collection.methods.mint(caller).send({from: caller}); + + const balance = await collection.methods.balanceOf(caller).call(); + expect(balance).to.equal('3'); + }); + }); + + [ + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'nft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: ownerOf`, testCase.requiredPallets, async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + const {collection} = await helper.eth.createCollection(testCase.mode, caller, 'OwnerOf', '6', '6'); + + const result = await collection.methods.mint(caller).send(); + const tokenId = result.events.Transfer.returnValues.tokenId; + + const owner = await collection.methods.ownerOf(tokenId).call(); + expect(owner).to.equal(caller); + }); + }); + + [ + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + // TODO {mode: 'nft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: ownerOf after burn`, testCase.requiredPallets, async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + const receiver = helper.eth.createAccount(); + const {collection, collectionId} = await helper.eth.createCollection(testCase.mode, caller, 'OwnerOf-AfterBurn', '6', '6'); + + const result = await collection.methods.mint(caller).send(); + const tokenId = result.events.Transfer.returnValues.tokenId; + const tokenContract = await helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller, true); + + await tokenContract.methods.repartition(2).send(); + await tokenContract.methods.transfer(receiver, 1).send(); + + await tokenContract.methods.burnFrom(caller, 1).send(); + + const owner = await collection.methods.ownerOf(tokenId).call(); + expect(owner).to.equal(receiver); + }); + }); + + itEth.ifWithPallets('RFT: ownerOf for partial ownership', [Pallets.ReFungible], async ({helper}) => { + const caller = await helper.eth.createAccountWithBalance(donor); + const receiver = helper.eth.createAccount(); + const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(caller, 'Partial-OwnerOf', '6', '6'); + const contract = await helper.ethNativeContract.collection(collectionAddress, 'rft', caller); + + const result = await contract.methods.mint(caller).send(); + const tokenId = result.events.Transfer.returnValues.tokenId; + const tokenContract = await helper.ethNativeContract.rftTokenById(collectionId, tokenId, caller); + + await tokenContract.methods.repartition(2).send(); + await tokenContract.methods.transfer(receiver, 1).send(); + + const owner = await contract.methods.ownerOf(tokenId).call(); + expect(owner).to.equal('0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'); + }); +}); --- /dev/null +++ b/tests/src/eth/tokens/minting.test.ts @@ -0,0 +1,167 @@ +// 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 . + +import {IKeyringPair} from '@polkadot/types/types'; +import {Pallets} from '../../util'; +import {expect, itEth, usingEthPlaygrounds} from '../util'; + + +describe('Minting tokens', () => { + let donor: IKeyringPair; + let alice: IKeyringPair; + + before(async function() { + await usingEthPlaygrounds(async (helper, privateKey) => { + donor = await privateKey({filename: __filename}); + [alice] = await helper.arrange.createAccounts([30n, 20n], donor); + }); + }); + + [ + {mode: 'nft' as const, requiredPallets: []}, + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'ft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Substrate collection`, testCase.requiredPallets, async ({helper}) => { + const owner = await helper.eth.createAccountWithBalance(donor); + const receiver = helper.eth.createAccount(); + const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver]; + + const collection = await helper[testCase.mode].mintCollection(alice); + await collection.addAdmin(alice, {Ethereum: owner}); + + const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId); + const contract = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner); + + const result = await contract.methods.mint(...mintingParams).send({from: owner}); + + // Check events: + const event = result.events.Transfer; + expect(event.address).to.equal(collectionAddress); + expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000'); + expect(event.returnValues.to).to.equal(receiver); + if (testCase.mode === 'ft') + expect(event.returnValues.value).to.equal('100'); + + // Check token exist: + if(testCase.mode === 'ft') { + expect(await helper.ft.getBalance(collection.collectionId, {Ethereum: receiver})).to.eq(100n); + } else { + const tokenId = event.returnValues.tokenId; + expect(tokenId).to.be.equal('1'); + expect(await helper.collection.getLastTokenId(collection.collectionId)).to.eq(1); + expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']); + } + }); + }); + + [ + {mode: 'nft' as const, requiredPallets: []}, + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'ft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => { + const owner = await helper.eth.createAccountWithBalance(donor); + const receiver = helper.eth.createAccount(); + const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver]; + + const {collection, collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'Name', 'Desc', 'Prefix'); + + const result = await collection.methods.mint(...mintingParams).send({from: owner}); + + // Check events: + const event = result.events.Transfer; + expect(event.address).to.equal(collectionAddress); + expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000'); + expect(event.returnValues.to).to.equal(receiver); + if (testCase.mode === 'ft') + expect(event.returnValues.value).to.equal('100'); + + // Check token exist: + if(testCase.mode === 'ft') { + expect(await helper.ft.getBalance(collectionId, {Ethereum: receiver})).to.eq(100n); + } else { + const tokenId = event.returnValues.tokenId; + expect(tokenId).to.be.equal('1'); + expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1); + expect(await collection.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']); + } + }); + }); + + [ + {mode: 'nft' as const, requiredPallets: []}, + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + {mode: 'ft' as const, requiredPallets: []}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mint() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => { + const owner = await helper.eth.createAccountWithBalance(donor); + const receiver = helper.eth.createAccount(); + const mintingParams = testCase.mode === 'ft' ? [receiver, 100] : [receiver]; + + const {collection, collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'Name', 'Desc', 'Prefix'); + + const result = await collection.methods.mint(...mintingParams).send({from: owner}); + + // Check events: + const event = result.events.Transfer; + expect(event.address).to.equal(collectionAddress); + expect(event.returnValues.from).to.equal('0x0000000000000000000000000000000000000000'); + expect(event.returnValues.to).to.equal(receiver); + if (testCase.mode === 'ft') + expect(event.returnValues.value).to.equal('100'); + + // Check token exist: + if(testCase.mode === 'ft') { + expect(await helper.ft.getBalance(collectionId, {Ethereum: receiver})).to.eq(100n); + } else { + const tokenId = event.returnValues.tokenId; + expect(tokenId).to.be.equal('1'); + expect(await helper.collection.getLastTokenId(collectionId)).to.eq(1); + expect(await collection.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']); + } + }); + }); + + [ + {mode: 'nft' as const, requiredPallets: []}, + {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]}, + ].map(testCase => { + itEth.ifWithPallets(`${testCase.mode.toUpperCase()}: Can mintWithTokenURI() for Ethereum collection`, testCase.requiredPallets, async ({helper}) => { + const owner = await helper.eth.createAccountWithBalance(donor); + const receiver = helper.eth.createAccount(); + + const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Mint collection', '6', '6', ''); + const contract = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner); + + const result = await contract.methods.mintWithTokenURI(receiver, 'Test URI').send(); + const tokenId = result.events.Transfer.returnValues.tokenId; + expect(tokenId).to.be.equal('1'); + + const event = result.events.Transfer; + expect(event.address).to.be.equal(collectionAddress); + expect(event.returnValues.from).to.be.equal('0x0000000000000000000000000000000000000000'); + expect(event.returnValues.to).to.be.equal(receiver); + + expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI'); + expect(await contract.methods.crossOwnerOf(tokenId).call()).to.be.like([receiver, '0']); + // TODO: this wont work right now, need release 919000 first + // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send(); + // const tokenUri = await contract.methods.tokenURI(nextTokenId).call(); + // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`); + }); + }); +}); \ No newline at end of file --- a/tests/src/eth/util/playgrounds/unique.dev.ts +++ b/tests/src/eth/util/playgrounds/unique.dev.ts @@ -257,7 +257,7 @@ } } - async createCollection(mode: TCollectionMode, signer: string, name: string, description: string, tokenPrefix: string, decimals = 18): Promise<{ collectionId: number, collectionAddress: string, events: NormalizedEvent[] }> { + async createCollection(mode: TCollectionMode, signer: string, name: string, description: string, tokenPrefix: string, decimals = 18, mergeDeprecated = false): Promise<{ collectionId: number, collectionAddress: string, events: NormalizedEvent[], collection: Contract }> { const collectionCreationPrice = this.helper.balance.getCollectionCreationPrice(); const collectionHelper = await this.helper.ethNativeContract.collectionHelpers(signer); const functionName: string = this.createCollectionMethodName(mode); @@ -268,8 +268,9 @@ const collectionAddress = this.helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId); const collectionId = this.helper.ethAddress.extractCollectionId(collectionAddress); const events = this.helper.eth.normalizeEvents(result.events); + const collection = await this.helper.ethNativeContract.collectionById(collectionId, mode, signer, mergeDeprecated); - return {collectionId, collectionAddress, events}; + return {collectionId, collectionAddress, events, collection}; } createNFTCollection(signer: string, name: string, description: string, tokenPrefix: string): Promise<{ collectionId: number, collectionAddress: string, events: NormalizedEvent[] }> { --- a/tests/src/nesting/nest.test.ts +++ b/tests/src/nesting/nest.test.ts @@ -15,7 +15,7 @@ // along with Unique Network. If not, see . import {IKeyringPair} from '@polkadot/types/types'; -import {expect, itSub, Pallets, usingPlaygrounds} from '../util'; +import {expect, itSub, usingPlaygrounds} from '../util'; describe('Integration Test: Composite nesting tests', () => { let alice: IKeyringPair; @@ -287,54 +287,7 @@ await collectionFT.mint(charlie, 5n); await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n); expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n); - }); - - // ---------- Re-Fungible ---------- - - itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token', [Pallets.ReFungible], async ({helper}) => { - const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}}); - const collectionRFT = await helper.rft.mintCollection(alice); - const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address}); - - await collectionNFT.addToAllowList(alice, {Substrate: charlie.address}); - await collectionNFT.addToAllowList(alice, targetToken.nestingAccount()); - - await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true}); - await collectionRFT.addToAllowList(alice, {Substrate: charlie.address}); - await collectionRFT.addToAllowList(alice, targetToken.nestingAccount()); - - // Create an immediately nested token - const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount()); - expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n); - - // Create a token to be nested and nest - const newToken = await collectionRFT.mintToken(charlie, 5n); - await newToken.transfer(charlie, targetToken.nestingAccount(), 2n); - expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n); }); - - itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => { - const collectionNFT = await helper.nft.mintCollection(alice); - const collectionRFT = await helper.rft.mintCollection(alice); - const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address}); - - await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionRFT.collectionId]}}); - await collectionNFT.addToAllowList(alice, {Substrate: charlie.address}); - await collectionNFT.addToAllowList(alice, targetToken.nestingAccount()); - - await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true}); - await collectionRFT.addToAllowList(alice, {Substrate: charlie.address}); - await collectionRFT.addToAllowList(alice, targetToken.nestingAccount()); - - // Create an immediately nested token - const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount()); - expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n); - - // Create a token to be nested and nest - const newToken = await collectionRFT.mintToken(charlie, 5n); - await newToken.transfer(charlie, targetToken.nestingAccount(), 2n); - expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n); - }); }); describe('Negative Test: Nesting', () => { @@ -579,89 +532,5 @@ expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n); expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n); - }); - - // ---------- Re-Fungible ---------- - - itSub.ifWithPallets('ReFungible: disallows to nest token if nesting is disabled', [Pallets.ReFungible], async ({helper}) => { - const collectionNFT = await helper.nft.mintCollection(alice); - const collectionRFT = await helper.rft.mintCollection(alice); - const targetToken = await collectionNFT.mintToken(alice); - - // Try to create an immediately nested token - await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount())) - .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/); - - // Try to create a token to be nested and nest - const token = await collectionRFT.mintToken(alice, 5n); - await expect(token.transfer(alice, targetToken.nestingAccount(), 2n)) - .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/); - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n); - }); - - itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token', [Pallets.ReFungible], async ({helper}) => { - const collectionNFT = await helper.nft.mintCollection(alice); - const collectionRFT = await helper.rft.mintCollection(alice); - const targetToken = await collectionNFT.mintToken(alice); - - await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}); - await collectionNFT.addToAllowList(alice, {Substrate: bob.address}); - await collectionNFT.addToAllowList(alice, targetToken.nestingAccount()); - - // Try to create a token to be nested and nest - const newToken = await collectionRFT.mintToken(alice); - await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/); - - expect(await targetToken.getChildren()).to.be.length(0); - expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n); - - // Nest some tokens as Alice into Bob's token - await newToken.transfer(alice, targetToken.nestingAccount()); - - // Try to pull it out - await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n)) - .to.be.rejectedWith(/common\.ApprovedValueTooLow/); - expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n); - }); - - itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => { - const collectionNFT = await helper.nft.mintCollection(alice); - const collectionRFT = await helper.rft.mintCollection(alice); - const targetToken = await collectionNFT.mintToken(alice); - - await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: [collectionRFT.collectionId]}}); - await collectionNFT.addToAllowList(alice, {Substrate: bob.address}); - await collectionNFT.addToAllowList(alice, targetToken.nestingAccount()); - - // Try to create a token to be nested and nest - const newToken = await collectionRFT.mintToken(alice); - await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/); - - expect(await targetToken.getChildren()).to.be.length(0); - expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n); - - // Nest some tokens as Alice into Bob's token - await newToken.transfer(alice, targetToken.nestingAccount()); - - // Try to pull it out - await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n)) - .to.be.rejectedWith(/common\.ApprovedValueTooLow/); - expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n); - }); - - itSub.ifWithPallets('ReFungible: disallows to nest token to an unlisted collection', [Pallets.ReFungible], async ({helper}) => { - const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}}); - const collectionRFT = await helper.rft.mintCollection(alice); - const targetToken = await collectionNFT.mintToken(alice); - - // Try to create an immediately nested token - await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount())) - .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/); - - // Try to create a token to be nested and nest - const token = await collectionRFT.mintToken(alice, 5n); - await expect(token.transfer(alice, targetToken.nestingAccount(), 2n)) - .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/); - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n); }); }); --- a/tests/src/refungible.test.ts +++ b/tests/src/refungible.test.ts @@ -88,20 +88,6 @@ expect((await token.getTop10Owners()).length).to.be.equal(10); }); - itSub('Transfer token pieces', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n); - expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true; - - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n); - expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n); - - await expect(token.transfer(alice, {Substrate: bob.address}, 41n)) - .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/); - }); - itSub('Create multiple tokens', async ({helper}) => { const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); // TODO: fix mintMultipleTokens @@ -118,56 +104,8 @@ const lastTokenId = await collection.getLastTokenId(); expect(lastTokenId).to.be.equal(3); expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n); - }); - - itSub('Burn some pieces', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - expect(await collection.doesTokenExist(token.tokenId)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n); - expect(await token.burn(alice, 99n)).to.be.true; - expect(await collection.doesTokenExist(token.tokenId)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n); }); - itSub('Burn all pieces', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - - expect(await collection.doesTokenExist(token.tokenId)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n); - - expect(await token.burn(alice, 100n)).to.be.true; - expect(await collection.doesTokenExist(token.tokenId)).to.be.false; - }); - - itSub('Burn some pieces for multiple users', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - - expect(await collection.doesTokenExist(token.tokenId)).to.be.true; - - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n); - expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true; - - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n); - expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n); - - expect(await token.burn(alice, 40n)).to.be.true; - - expect(await collection.doesTokenExist(token.tokenId)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n); - - expect(await token.burn(bob, 59n)).to.be.true; - - expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n); - expect(await collection.doesTokenExist(token.tokenId)).to.be.true; - - expect(await token.burn(bob, 1n)).to.be.true; - - expect(await collection.doesTokenExist(token.tokenId)).to.be.false; - }); - itSub('Set allowance for token', async ({helper}) => { const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); const token = await collection.mintToken(alice, 100n); @@ -183,68 +121,6 @@ expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n); }); - itSub('Repartition', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - - expect(await token.repartition(alice, 200n)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n); - expect(await token.getTotalPieces()).to.be.equal(200n); - - expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n); - expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n); - - await expect(token.repartition(alice, 80n)) - .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/); - - expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true; - expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n); - expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n); - - expect(await token.repartition(bob, 150n)).to.be.true; - await expect(token.transfer(bob, {Substrate: alice.address}, 160n)) - .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/); - }); - - itSub('Repartition with increased amount', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - await token.repartition(alice, 200n); - const chainEvents = helper.chainLog.slice(-1)[0].events; - const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemCreated'); - expect(event).to.deep.include({ - section: 'common', - method: 'ItemCreated', - index: [66, 2], - data: [ - collection.collectionId, - token.tokenId, - {substrate: alice.address}, - 100n, - ], - }); - }); - - itSub('Repartition with decreased amount', async ({helper}) => { - const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); - const token = await collection.mintToken(alice, 100n); - await token.repartition(alice, 50n); - const chainEvents = helper.chainLog.slice(-1)[0].events; - const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemDestroyed'); - expect(event).to.deep.include({ - section: 'common', - method: 'ItemDestroyed', - index: [66, 3], - data: [ - collection.collectionId, - token.tokenId, - {substrate: alice.address}, - 50n, - ], - }); - }); - itSub('Create new collection with properties', async ({helper}) => { const properties = [{key: 'key1', value: 'val1'}]; const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}]; @@ -252,46 +128,5 @@ const info = await collection.getData(); expect(info?.raw.properties).to.be.deep.equal(properties); expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions); - }); -}); - -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); }); }); --- /dev/null +++ b/tests/src/sub/refungible/burn.test.ts @@ -0,0 +1,127 @@ +// 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 . + +import {IKeyringPair} from '@polkadot/types/types'; +import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from '../../util'; + +describe('Refungible: burn', () => { + let donor: IKeyringPair; + let alice: IKeyringPair; + let bob: IKeyringPair; + + before(async function() { + await usingPlaygrounds(async (helper, privateKey) => { + requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); + + donor = await privateKey({filename: __filename}); + [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor); + }); + }); + + itSub('can burn some pieces', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + expect(await collection.doesTokenExist(token.tokenId)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n); + await token.burn(alice, 99n); + expect(await collection.doesTokenExist(token.tokenId)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n); + }); + + itSub('can burn all pieces', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + + expect(await collection.doesTokenExist(token.tokenId)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n); + + await token.burn(alice, 100n); + expect(await collection.doesTokenExist(token.tokenId)).to.be.false; + }); + + itSub('burn pieces for multiple users', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + + await token.transfer(alice, {Substrate: bob.address}, 60n); + + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n); + expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n); + + await token.burn(alice, 40n); + + expect(await collection.doesTokenExist(token.tokenId)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n); + + await token.burn(bob, 59n); + + expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n); + expect(await collection.doesTokenExist(token.tokenId)).to.be.true; + + await token.burn(bob, 1n); + + expect(await collection.doesTokenExist(token.tokenId)).to.be.false; + }); + + itSub('burn pieces by admin', async function({helper}) { + const collection = await helper.rft.mintCollection(alice); + await collection.setLimits(alice, {ownerCanTransfer: true}); + await collection.addAdmin(alice, {Substrate: bob.address}); + const token = await collection.mintToken(alice, 100n); + + await token.burnFrom(bob, {Substrate: alice.address}, 100n); + expect(await token.doesExist()).to.be.false; + }); +}); + +describe('Refungible: burn negative tests', () => { + let donor: IKeyringPair; + let alice: IKeyringPair; + let bob: IKeyringPair; + + before(async function() { + await usingPlaygrounds(async (helper, privateKey) => { + requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); + donor = await privateKey({filename: __filename}); + [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor); + }); + }); + + itSub('cannot burn non-owned token pieces', 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}]); + }); +}); --- /dev/null +++ b/tests/src/sub/refungible/nesting.test.ts @@ -0,0 +1,180 @@ +// 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 . + +import {IKeyringPair} from '@polkadot/types/types'; +import {expect, itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from '../../util'; + +describe('Refungible nesting', () => { + let alice: IKeyringPair; + let charlie: IKeyringPair; + + before(async function() { + await usingPlaygrounds(async (helper, privateKey) => { + requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); + const donor = await privateKey({filename: __filename}); + [alice, charlie] = await helper.arrange.createAccounts([50n, 10n], donor); + }); + }); + + [ + {restrictedMode: true}, + {restrictedMode: false}, + ].map(testCase => { + itSub(`Owner can nest their token${testCase.restrictedMode ? ': Restricted mode' : ''}`, async ({helper}) => { + const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}}); + const collectionRFT = await helper.rft.mintCollection(alice); + const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address}); + + await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionRFT.collectionId] : null}}); + await collectionNFT.addToAllowList(alice, {Substrate: charlie.address}); + await collectionNFT.addToAllowList(alice, targetToken.nestingAccount()); + + await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true}); + await collectionRFT.addToAllowList(alice, {Substrate: charlie.address}); + await collectionRFT.addToAllowList(alice, targetToken.nestingAccount()); + + // Create an immediately nested token + const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount()); + expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n); + + // Create a token to be nested and nest + const newToken = await collectionRFT.mintToken(charlie, 5n); + await newToken.transfer(charlie, targetToken.nestingAccount(), 2n); + expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n); + expect(await newToken.getBalance({Substrate: charlie.address})).to.be.equal(3n); + }); + }); + + itSub('Owner can unnest nested token', async ({helper}) => { + const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}}); + const targetToken = await collection.mintToken(alice); + + // Owner mints nested RFT token: + const collectionRFT = await helper.rft.mintCollection(alice); + const token = await collectionRFT.mintToken(alice, 10n, targetToken.nestingAccount()); + + // 1.1 Owner can partially unnest token pieces with transferFrom: + await token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 9n); + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(9n); + expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(1n); + + // 1.2 Owner can unnest all pieces: + await token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 1n); + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(10n); + expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n); + }); + + itSub('Owner can burn nested token pieces', async ({helper}) => { + const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}}); + const targetToken = await collection.mintToken(alice); + + // Owner mints nested RFT token: + const collectionRFT = await helper.rft.mintCollection(alice); + const token = await collectionRFT.mintToken(alice, 100n, {Substrate: alice.address}); + await token.transfer(alice, targetToken.nestingAccount(), 30n); + + // 1.1 Owner can partially burnFrom nested pieces: + await token.burnFrom(alice, targetToken.nestingAccount(), 10n); + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(70n); + expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(20n); + expect(await targetToken.getChildren()).to.has.length(1); + + // 1.1 Owner can burnFrom all nested pieces: + await token.burnFrom(alice, targetToken.nestingAccount(), 20n); + expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n); + expect(await targetToken.getChildren()).to.has.length(0); + expect(await token.doesExist()).to.be.true; + + // 2. Target token does not contain any pieces and can be burnt: + await targetToken.burn(alice); + expect(await targetToken.doesExist()).to.be.false; + }); +}); + +describe('Refungible nesting negative tests', () => { + let alice: IKeyringPair; + let bob: IKeyringPair; + + before(async function() { + await usingPlaygrounds(async (helper, privateKey) => { + requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); + const donor = await privateKey({filename: __filename}); + [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor); + }); + }); + + itSub('cannot nest token if nesting is disabled', async ({helper}) => { + const collectionNFT = await helper.nft.mintCollection(alice); + const collectionRFT = await helper.rft.mintCollection(alice); + const targetToken = await collectionNFT.mintToken(alice); + + // Try to create an immediately nested token + await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount())) + .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/); + + // Try to create a token to be nested and nest + const token = await collectionRFT.mintToken(alice, 5n); + await expect(token.transfer(alice, targetToken.nestingAccount(), 2n)) + .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/); + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n); + }); + + [ + {restrictedMode: true}, + {restrictedMode: false}, + ].map(testCase => { + itSub(`non-Owner cannot nest someone else's token${testCase.restrictedMode ? ': Restricted mode' : ''}`, async ({helper}) => { + const collectionNFT = await helper.nft.mintCollection(alice); + const collectionRFT = await helper.rft.mintCollection(alice); + const targetToken = await collectionNFT.mintToken(alice); + + await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionRFT.collectionId] : null}}); + await collectionNFT.addToAllowList(alice, {Substrate: bob.address}); + await collectionNFT.addToAllowList(alice, targetToken.nestingAccount()); + + // Try to create a token to be nested and nest + const newToken = await collectionRFT.mintToken(alice); + await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/); + + expect(await targetToken.getChildren()).to.be.length(0); + expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n); + + // Nest some tokens as Alice into Bob's token + await newToken.transfer(alice, targetToken.nestingAccount()); + + // Try to pull it out + await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n)) + .to.be.rejectedWith(/common\.ApprovedValueTooLow/); + expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n); + }); + }); + + itSub('ReFungible: disallows to nest token to an unlisted collection', async ({helper}) => { + const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}}); + const collectionRFT = await helper.rft.mintCollection(alice); + const targetToken = await collectionNFT.mintToken(alice); + + // Try to create an immediately nested token + await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount())) + .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/); + + // Try to create a token to be nested and nest + const token = await collectionRFT.mintToken(alice, 5n); + await expect(token.transfer(alice, targetToken.nestingAccount(), 2n)) + .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/); + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n); + }); +}); --- /dev/null +++ b/tests/src/sub/refungible/repartition.test.ts @@ -0,0 +1,96 @@ +// 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 . + +import {IKeyringPair} from '@polkadot/types/types'; +import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from '../../util'; + +describe('integration test: Refungible functionality:', () => { + let donor: IKeyringPair; + let alice: IKeyringPair; + let bob: IKeyringPair; + + before(async function() { + await usingPlaygrounds(async (helper, privateKey) => { + requirePalletsOrSkip(this, helper, [Pallets.ReFungible]); + + donor = await privateKey({filename: __filename}); + [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor); + }); + }); + + itSub('Repartition', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + + expect(await token.repartition(alice, 200n)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n); + expect(await token.getTotalPieces()).to.be.equal(200n); + + expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n); + expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n); + + await expect(token.repartition(alice, 80n)) + .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/); + + expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true; + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n); + expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n); + + expect(await token.repartition(bob, 150n)).to.be.true; + await expect(token.transfer(bob, {Substrate: alice.address}, 160n)) + .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/); + }); + + itSub('Repartition with increased amount', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + await token.repartition(alice, 200n); + const chainEvents = helper.chainLog.slice(-1)[0].events; + const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemCreated'); + expect(event).to.deep.include({ + section: 'common', + method: 'ItemCreated', + index: [66, 2], + data: [ + collection.collectionId, + token.tokenId, + {substrate: alice.address}, + 100n, + ], + }); + }); + + itSub('Repartition with decreased amount', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + await token.repartition(alice, 50n); + const chainEvents = helper.chainLog.slice(-1)[0].events; + const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemDestroyed'); + expect(event).to.deep.include({ + section: 'common', + method: 'ItemDestroyed', + index: [66, 3], + data: [ + collection.collectionId, + token.tokenId, + {substrate: alice.address}, + 50n, + ], + }); + }); +}); + --- /dev/null +++ b/tests/src/sub/refungible/transfer.test.ts @@ -0,0 +1,73 @@ +// 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 . + +import {IKeyringPair} from '@polkadot/types/types'; +import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from '../../util'; + +describe('Refungible transfer 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('Can transfer token pieces', async ({helper}) => { + const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}); + const token = await collection.mintToken(alice, 100n); + + expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true; + // 1. Can transfer less or equal than have: + expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n); + expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n); + }); + + 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. Cannot transfer more than have: + await expect(tokenAlice.transfer(alice, {Substrate: bob.address}, 11n)) + .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/); + + // 4. 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); + }); +}); \ No newline at end of file --- a/tests/src/util/index.ts +++ b/tests/src/util/index.ts @@ -100,6 +100,9 @@ export const LOCKING_PERIOD = 12n; // 12 blocks of relay export const UNLOCKING_PERIOD = 6n; // 6 blocks of parachain +// Native contracts +export const COLLECTION_HELPER = '0x6c4e9fe1ae37a41e93cee429e8e1881abdcbb54f'; +export const CONTRACT_HELPER = '0x842899ECF380553E8a4de75bF534cdf6fBF64049'; export enum Pallets { Inflation = 'inflation', -- gitstuff