difftreelog
test(refungible-pallet) add tests for repartition events
in: master
3 files changed
tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth--- 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', () => {
tests/src/refungible.test.tsdiffbeforeafterboth14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617import {default as usingApi, executeTransaction} from './substrate/substrate-api';17import {default as usingApi, submitTransactionAsync} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';18import {IKeyringPair} from '@polkadot/types/types';19import {19import {20 createCollectionExpectSuccess,20 createCollectionExpectSuccess,32 repartitionRFT,32 repartitionRFT,33 createCollectionWithPropsExpectSuccess,33 createCollectionWithPropsExpectSuccess,34 getDetailedCollectionInfo,34 getDetailedCollectionInfo,35 getCreateItemsResult,36 getDestroyItemsResult,35} from './util/helpers';37} from './util/helpers';363837import chai from 'chai';39import chai from 'chai';189 });191 });190 });192 });193194 it('Repartition with increased amount', async () => {195 await usingApi(async api => {196 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;197 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;198199 const tx = api.tx.unique.repartition(collectionId, tokenId, 200n);200 const events = await submitTransactionAsync(alice, tx);201 const substrateEvents = getCreateItemsResult(events);202 expect(substrateEvents).to.include.deep.members([203 {204 success: true,205 collectionId,206 itemId: tokenId,207 recipient: {Substrate: alice.address},208 amount: 100,209 },210 ]);211 });212 });213214 it('Repartition with decreased amount', async () => {215 await usingApi(async api => {216 const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;217 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;218219 const tx = api.tx.unique.repartition(collectionId, tokenId, 50n);220 const events = await submitTransactionAsync(alice, tx);221 const substrateEvents = getDestroyItemsResult(events);222 expect(substrateEvents).to.include.deep.members([223 {224 success: true,225 collectionId,226 itemId: tokenId,227 owner: {Substrate: alice.address},228 amount: 50,229 },230 ]);231 });232 });191});233});192234193describe('Test Refungible properties:', () => {235describe('Test Refungible properties:', () => {tests/src/util/helpers.tsdiffbeforeafterboth--- 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<DestroyItemResult[]>(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)) {