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.tsdiffbeforeafterboth--- 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 <http://www.gnu.org/licenses/>.
-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:', () => {
tests/src/util/helpers.tsdiffbeforeafterboth103 collectionId: number;103 collectionId: number;104 itemId: number;104 itemId: number;105 recipient?: CrossAccountId;105 recipient?: CrossAccountId;106 amount?: number;106}107}108109interface DestroyItemResult {110 success: boolean;111 collectionId: number;112 itemId: number;113 owner: CrossAccountId;114 amount: number;115}107116108interface TransferResult {117interface TransferResult {109 collectionId: number;118 collectionId: number;220 const collectionId = parseInt(data[0].toString(), 10);229 const collectionId = parseInt(data[0].toString(), 10);221 const itemId = parseInt(data[1].toString(), 10);230 const itemId = parseInt(data[1].toString(), 10);222 const recipient = normalizeAccountId(data[2].toJSON() as any);231 const recipient = normalizeAccountId(data[2].toJSON() as any);232 const amount = parseInt(data[3].toString(), 10);223233224 const itemRes: CreateItemResult = {234 const itemRes: CreateItemResult = {225 success: true,235 success: true,226 collectionId,236 collectionId,227 itemId,237 itemId,228 recipient,238 recipient,239 amount,229 };240 };230241231 results.push(itemRes);242 results.push(itemRes);255 return result;266 return result;256}267}268269export function getDestroyItemsResult(events: EventRecord[]): DestroyItemResult[] {270 const results: DestroyItemResult[] = [];271 272 const genericResult = getGenericResult<DestroyItemResult[]>(events, 'common', 'ItemDestroyed', (data) => {273 const collectionId = parseInt(data[0].toString(), 10);274 const itemId = parseInt(data[1].toString(), 10);275 const owner = normalizeAccountId(data[2].toJSON() as any);276 const amount = parseInt(data[3].toString(), 10);277278 const itemRes: DestroyItemResult = {279 success: true,280 collectionId,281 itemId,282 owner,283 amount,284 };285286 results.push(itemRes);287 return results;288 });289290 if (!genericResult.success) return [];291 return results;292}257293258export function getTransferResult(api: ApiPromise, events: EventRecord[]): TransferResult {294export function getTransferResult(api: ApiPromise, events: EventRecord[]): TransferResult {259 for (const {event} of events) {295 for (const {event} of events) {