--- a/tests/src/eth/reFungibleToken.test.ts +++ b/tests/src/eth/reFungibleToken.test.ts @@ -239,10 +239,69 @@ expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0); expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200); - await contract.methods.repartition(150).send({from: receiver}); + const result = await contract.methods.repartition(150).send({from: receiver}); + console.log(result.events); await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected; expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150); }); + + itWeb3('Can repartition with increased amount', async ({web3, api, privateKeyWrapper}) => { + const alice = privateKeyWrapper('//Alice'); + + const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId; + + const owner = createEthAccount(web3); + await transferBalanceToEth(api, alice, owner); + + const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId; + + const address = tokenIdToAddress(collectionId, tokenId); + const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS}); + + const result = await contract.methods.repartition(200).send(); + const events = normalizeEvents(result.events); + + expect(events).to.include.deep.members([ + { + address, + event: 'Transfer', + args: { + from: '0x0000000000000000000000000000000000000000', + to: owner, + value: '100', + }, + }, + ]); + }); + + itWeb3('Can repartition with decreased amount', async ({web3, api, privateKeyWrapper}) => { + const alice = privateKeyWrapper('//Alice'); + + const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId; + + const owner = createEthAccount(web3); + await transferBalanceToEth(api, alice, owner); + + const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId; + + const address = tokenIdToAddress(collectionId, tokenId); + const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS}); + + const result = await contract.methods.repartition(50).send(); + const events = normalizeEvents(result.events); + + expect(events).to.include.deep.members([ + { + address, + event: 'Transfer', + args: { + from: owner, + to: '0x0000000000000000000000000000000000000000', + value: '50', + }, + }, + ]); + }); }); describe('Refungible: Fees', () => { --- a/tests/src/refungible.test.ts +++ b/tests/src/refungible.test.ts @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -import {default as usingApi, executeTransaction} from './substrate/substrate-api'; +import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api'; import {IKeyringPair} from '@polkadot/types/types'; import { createCollectionExpectSuccess, @@ -32,6 +32,8 @@ repartitionRFT, createCollectionWithPropsExpectSuccess, getDetailedCollectionInfo, + getCreateItemsResult, + getDestroyItemsResult, } from './util/helpers'; import chai from 'chai'; @@ -188,6 +190,46 @@ await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected; }); }); + + it('Repartition with increased amount', async () => { + await usingApi(async api => { + const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId; + const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId; + + const tx = api.tx.unique.repartition(collectionId, tokenId, 200n); + const events = await submitTransactionAsync(alice, tx); + const substrateEvents = getCreateItemsResult(events); + expect(substrateEvents).to.include.deep.members([ + { + success: true, + collectionId, + itemId: tokenId, + recipient: {Substrate: alice.address}, + amount: 100, + }, + ]); + }); + }); + + it('Repartition with decreased amount', async () => { + await usingApi(async api => { + const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId; + const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId; + + const tx = api.tx.unique.repartition(collectionId, tokenId, 50n); + const events = await submitTransactionAsync(alice, tx); + const substrateEvents = getDestroyItemsResult(events); + expect(substrateEvents).to.include.deep.members([ + { + success: true, + collectionId, + itemId: tokenId, + owner: {Substrate: alice.address}, + amount: 50, + }, + ]); + }); + }); }); describe('Test Refungible properties:', () => { --- a/tests/src/util/helpers.ts +++ b/tests/src/util/helpers.ts @@ -103,6 +103,15 @@ collectionId: number; itemId: number; recipient?: CrossAccountId; + amount?: number; +} + +interface DestroyItemResult { + success: boolean; + collectionId: number; + itemId: number; + owner: CrossAccountId; + amount: number; } interface TransferResult { @@ -220,12 +229,14 @@ const collectionId = parseInt(data[0].toString(), 10); const itemId = parseInt(data[1].toString(), 10); const recipient = normalizeAccountId(data[2].toJSON() as any); + const amount = parseInt(data[3].toString(), 10); const itemRes: CreateItemResult = { success: true, collectionId, itemId, recipient, + amount, }; results.push(itemRes); @@ -255,6 +266,31 @@ return result; } +export function getDestroyItemsResult(events: EventRecord[]): DestroyItemResult[] { + const results: DestroyItemResult[] = []; + + const genericResult = getGenericResult(events, 'common', 'ItemDestroyed', (data) => { + const collectionId = parseInt(data[0].toString(), 10); + const itemId = parseInt(data[1].toString(), 10); + const owner = normalizeAccountId(data[2].toJSON() as any); + const amount = parseInt(data[3].toString(), 10); + + const itemRes: DestroyItemResult = { + success: true, + collectionId, + itemId, + owner, + amount, + }; + + results.push(itemRes); + return results; + }); + + if (!genericResult.success) return []; + return results; +} + export function getTransferResult(api: ApiPromise, events: EventRecord[]): TransferResult { for (const {event} of events) { if (api.events.common.Transfer.is(event)) {