difftreelog
test(refungible-pallet) add tests for repartition events
in: master
3 files changed
tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth239 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);239 expect(+await contract.methods.balanceOf(owner).call()).to.be.equal(0);240 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);240 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(200);241241242 await contract.methods.repartition(150).send({from: receiver});242 const result = await contract.methods.repartition(150).send({from: receiver});243 console.log(result.events);243 await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;244 await expect(contract.methods.transfer(owner, 160).send({from: receiver})).to.eventually.be.rejected;244 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);245 expect(+await contract.methods.balanceOf(receiver).call()).to.be.equal(150);245 });246 });247248 itWeb3('Can repartition with increased amount', async ({web3, api, privateKeyWrapper}) => {249 const alice = privateKeyWrapper('//Alice');250251 const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;252253 const owner = createEthAccount(web3);254 await transferBalanceToEth(api, alice, owner);255256 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;257258 const address = tokenIdToAddress(collectionId, tokenId);259 const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});260261 const result = await contract.methods.repartition(200).send();262 const events = normalizeEvents(result.events);263264 expect(events).to.include.deep.members([265 {266 address,267 event: 'Transfer',268 args: {269 from: '0x0000000000000000000000000000000000000000',270 to: owner,271 value: '100',272 },273 },274 ]);275 });276277 itWeb3('Can repartition with decreased amount', async ({web3, api, privateKeyWrapper}) => {278 const alice = privateKeyWrapper('//Alice');279280 const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;281282 const owner = createEthAccount(web3);283 await transferBalanceToEth(api, alice, owner);284285 const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n, {Ethereum: owner})).itemId;286287 const address = tokenIdToAddress(collectionId, tokenId);288 const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: owner, ...GAS_ARGS});289290 const result = await contract.methods.repartition(50).send();291 const events = normalizeEvents(result.events);292293 expect(events).to.include.deep.members([294 {295 address,296 event: 'Transfer',297 args: {298 from: owner,299 to: '0x0000000000000000000000000000000000000000',300 value: '50',301 },302 },303 ]);304 });246});305});247306248describe('Refungible: Fees', () => {307describe('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.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) {