git.delta.rocks / unique-network / refs/commits / e899eeedd0be

difftreelog

source

tests/src/eth/collectionSponsoring.test.ts13.5 KiBsourcehistory
1import {addToAllowListExpectSuccess, bigIntToSub, confirmSponsorshipExpectSuccess, createCollectionExpectSuccess, enablePublicMintingExpectSuccess, getDetailedCollectionInfo, setCollectionSponsorExpectSuccess} from '../util/helpers';2import {itWeb3, createEthAccount, collectionIdToAddress, GAS_ARGS, normalizeEvents, createEthAccountWithBalance, evmCollectionHelpers, getCollectionAddressFromResult, evmCollection, ethBalanceViaSub} from './util/helpers';3import nonFungibleAbi from './nonFungibleAbi.json';4import {expect} from 'chai';5import {evmToAddress} from '@polkadot/util-crypto';6import {submitTransactionAsync} from '../substrate/substrate-api';7import getBalance from '../substrate/get-balance';89describe('evm collection sponsoring', () => {10  itWeb3('sponsors mint transactions', async ({web3, privateKeyWrapper}) => {11    const alice = privateKeyWrapper('//Alice');1213    const collection = await createCollectionExpectSuccess();14    await setCollectionSponsorExpectSuccess(collection, alice.address);15    await confirmSponsorshipExpectSuccess(collection);1617    const minter = createEthAccount(web3);18    expect(await web3.eth.getBalance(minter)).to.equal('0');1920    const address = collectionIdToAddress(collection);21    const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection), {from: minter, ...GAS_ARGS});2223    await enablePublicMintingExpectSuccess(alice, collection);24    await addToAllowListExpectSuccess(alice, collection, {Ethereum: minter});2526    const nextTokenId = await contract.methods.nextTokenId().call();27    expect(nextTokenId).to.equal('1');28    const result = await contract.methods.mint(minter, nextTokenId).send();29    const events = normalizeEvents(result.events);30    expect(events).to.be.deep.equal([31      {32        address,33        event: 'Transfer',34        args: {35          from: '0x0000000000000000000000000000000000000000',36          to: minter,37          tokenId: nextTokenId,38        },39      },40    ]);41  });4243  // TODO: Temprorary off. Need refactor44  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {45  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);46  //   const collectionHelpers = evmCollectionHelpers(web3, owner);47  //   let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();48  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);49  //   const sponsor = privateKeyWrapper('//Alice');50  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);5152  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;53  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});54  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;55    56  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);57  //   await submitTransactionAsync(sponsor, confirmTx);58  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;59    60  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});61  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);62  // });6364  itWeb3('Remove sponsor', async ({api, web3, privateKeyWrapper}) => {65    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);66    const collectionHelpers = evmCollectionHelpers(web3, owner);67    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();68    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);69    const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);70    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);7172    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;73    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});74    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;75    76    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});77    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;78    79    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});80    81    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});82    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');83  });8485  itWeb3('Sponsoring collection from evm address via access list', async ({api, web3, privateKeyWrapper}) => {86    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);87    const collectionHelpers = evmCollectionHelpers(web3, owner);88    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();89    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);90    const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);91    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);92    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});93    let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;94    const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;95    expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;96    expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));97    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');9899    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});100    collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;101    expect(collectionSub.sponsorship.isConfirmed).to.be.true;102    expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));103104    const user = createEthAccount(web3);105    const nextTokenId = await collectionEvm.methods.nextTokenId().call();106    expect(nextTokenId).to.be.equal('1');107108    const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();109    expect(oldPermissions.mintMode).to.be.false;110    expect(oldPermissions.access).to.be.equal('Normal');111112    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});113    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});114    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});115116    const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();117    expect(newPermissions.mintMode).to.be.true;118    expect(newPermissions.access).to.be.equal('AllowList');119120    const ownerBalanceBefore = await ethBalanceViaSub(api, owner);121    const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);122123    {124      const nextTokenId = await collectionEvm.methods.nextTokenId().call();125      expect(nextTokenId).to.be.equal('1');126      const result = await collectionEvm.methods.mintWithTokenURI(127        user,128        nextTokenId,129        'Test URI',130      ).send({from: user});131      const events = normalizeEvents(result.events);132133      expect(events).to.be.deep.equal([134        {135          address: collectionIdAddress,136          event: 'Transfer',137          args: {138            from: '0x0000000000000000000000000000000000000000',139            to: user,140            tokenId: nextTokenId,141          },142        },143      ]);144145      const ownerBalanceAfter = await ethBalanceViaSub(api, owner);146      const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);147148      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');149      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);150      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;151    }152  });153154  // TODO: Temprorary off. Need refactor155  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {156  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);157  //   const collectionHelpers = evmCollectionHelpers(web3, owner);158  //   const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();159  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);160  //   const sponsor = privateKeyWrapper('//Alice');161  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);162163  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});164    165  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);166  //   await submitTransactionAsync(sponsor, confirmTx);167    168  //   const user = createEthAccount(web3);169  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();170  //   expect(nextTokenId).to.be.equal('1');171172  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});173  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});174  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});175176  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);177  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];178179  //   {180  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();181  //     expect(nextTokenId).to.be.equal('1');182  //     const result = await collectionEvm.methods.mintWithTokenURI(183  //       user,184  //       nextTokenId,185  //       'Test URI',186  //     ).send({from: user});187  //     const events = normalizeEvents(result.events);188189  //     expect(events).to.be.deep.equal([190  //       {191  //         address: collectionIdAddress,192  //         event: 'Transfer',193  //         args: {194  //           from: '0x0000000000000000000000000000000000000000',195  //           to: user,196  //           tokenId: nextTokenId,197  //         },198  //       },199  //     ]);200201  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);202  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];203204  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');205  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);206  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;207  //   }208  // });209210  itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3, privateKeyWrapper}) => {211    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);212    const collectionHelpers = evmCollectionHelpers(web3, owner);213    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();214    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);215    const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);216    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);217    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();218    let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;219    const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;220    expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;221    expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));222    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');223    const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);224    await sponsorCollection.methods.confirmCollectionSponsorship().send();225    collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;226    expect(collectionSub.sponsorship.isConfirmed).to.be.true;227    expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));228229    const user = createEthAccount(web3);230    await collectionEvm.methods.addCollectionAdmin(user).send();231    232    const ownerBalanceBefore = await ethBalanceViaSub(api, owner);233    const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);234    235  236    const userCollectionEvm = evmCollection(web3, user, collectionIdAddress);237    const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();238    expect(nextTokenId).to.be.equal('1');239    result = await userCollectionEvm.methods.mintWithTokenURI(240      user,241      nextTokenId,242      'Test URI',243    ).send();244245    const events = normalizeEvents(result.events);246    const address = collectionIdToAddress(collectionId);247248    expect(events).to.be.deep.equal([249      {250        address,251        event: 'Transfer',252        args: {253          from: '0x0000000000000000000000000000000000000000',254          to: user,255          tokenId: nextTokenId,256        },257      },258    ]);259    expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');260  261    const ownerBalanceAfter = await ethBalanceViaSub(api, owner);262    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);263    const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);264    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;265  });266});