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

difftreelog

source

tests/src/eth/collectionSponsoring.test.ts13.6 KiBsourcehistory
1import {IKeyringPair} from '@polkadot/types/types';2import {usingPlaygrounds} from './../util/playgrounds/index';3import {itEth, expect} from '../eth/util/playgrounds';45describe('evm collection sponsoring', () => {6  let donor: IKeyringPair;7  let alice: IKeyringPair;8  let nominal: bigint;910  before(async () => {11    await usingPlaygrounds(async (helper, privateKey) => {12      donor = await privateKey({filename: __filename});13      [alice] = await helper.arrange.createAccounts([100n], donor);14      nominal = helper.balance.getOneTokenNominal();15    });16  });17  18  itEth('sponsors mint transactions', async ({helper}) => {19    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});20    await collection.setSponsor(alice, alice.address);21    await collection.confirmSponsorship(alice);2223    const minter = helper.eth.createAccount();24    expect(await helper.balance.getEthereum(minter)).to.equal(0n);2526    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);27    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);2829    await collection.addToAllowList(alice, {Ethereum: minter});3031    const nextTokenId = await contract.methods.nextTokenId().call();32    expect(nextTokenId).to.equal('1');33    const result = await contract.methods.mint(minter, nextTokenId).send();34    const events = helper.eth.normalizeEvents(result.events);35    expect(events).to.be.deep.equal([36      {37        address: collectionAddress,38        event: 'Transfer',39        args: {40          from: '0x0000000000000000000000000000000000000000',41          to: minter,42          tokenId: nextTokenId,43        },44      },45    ]);46  });4748  // TODO: Temprorary off. Need refactor49  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {50  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);51  //   const collectionHelpers = evmCollectionHelpers(web3, owner);52  //   let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();53  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);54  //   const sponsor = privateKeyWrapper('//Alice');55  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);5657  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;58  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});59  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;60    61  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);62  //   await submitTransactionAsync(sponsor, confirmTx);63  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;64    65  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});66  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);67  // });6869  itEth('Remove sponsor', async ({helper}) => {70    const owner = await helper.eth.createAccountWithBalance(donor);71    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);7273    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});74    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);75    const sponsor = await helper.eth.createAccountWithBalance(donor);76    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);7778    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;79    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});80    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;81    82    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});83    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;84    85    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});86    87    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});88    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');89  });9091  itEth('Sponsoring collection from evm address via access list', async ({helper}) => {92    const owner = await helper.eth.createAccountWithBalance(donor);93    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);9495    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});96    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);97    const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);98    const collection = helper.nft.getCollectionObject(collectionId);99    const sponsor = await helper.eth.createAccountWithBalance(donor);100    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);101102    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});103    let collectionData = (await collection.getData())!;104    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));105    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');106107    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});108    collectionData = (await collection.getData())!;109    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));110111    const user = helper.eth.createAccount();112    const nextTokenId = await collectionEvm.methods.nextTokenId().call();113    expect(nextTokenId).to.be.equal('1');114115    const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();116    expect(oldPermissions.mintMode).to.be.false;117    expect(oldPermissions.access).to.be.equal('Normal');118119    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});120    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});121    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});122123    const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();124    expect(newPermissions.mintMode).to.be.true;125    expect(newPermissions.access).to.be.equal('AllowList');126127    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));128    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));129130    {131      const nextTokenId = await collectionEvm.methods.nextTokenId().call();132      expect(nextTokenId).to.be.equal('1');133      const result = await collectionEvm.methods.mintWithTokenURI(134        user,135        nextTokenId,136        'Test URI',137      ).send({from: user});138      const events = helper.eth.normalizeEvents(result.events);139140      expect(events).to.be.deep.equal([141        {142          address: collectionIdAddress,143          event: 'Transfer',144          args: {145            from: '0x0000000000000000000000000000000000000000',146            to: user,147            tokenId: nextTokenId,148          },149        },150      ]);151152      const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));153      const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));154155      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');156      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);157      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;158    }159  });160161  // TODO: Temprorary off. Need refactor162  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {163  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);164  //   const collectionHelpers = evmCollectionHelpers(web3, owner);165  //   const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();166  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);167  //   const sponsor = privateKeyWrapper('//Alice');168  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);169170  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});171    172  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);173  //   await submitTransactionAsync(sponsor, confirmTx);174    175  //   const user = createEthAccount(web3);176  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();177  //   expect(nextTokenId).to.be.equal('1');178179  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});180  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});181  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});182183  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);184  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];185186  //   {187  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();188  //     expect(nextTokenId).to.be.equal('1');189  //     const result = await collectionEvm.methods.mintWithTokenURI(190  //       user,191  //       nextTokenId,192  //       'Test URI',193  //     ).send({from: user});194  //     const events = normalizeEvents(result.events);195196  //     expect(events).to.be.deep.equal([197  //       {198  //         address: collectionIdAddress,199  //         event: 'Transfer',200  //         args: {201  //           from: '0x0000000000000000000000000000000000000000',202  //           to: user,203  //           tokenId: nextTokenId,204  //         },205  //       },206  //     ]);207208  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);209  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];210211  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');212  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);213  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;214  //   }215  // });216217  itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {218    const owner = await helper.eth.createAccountWithBalance(donor);219    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);220221    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});222    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);223    const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);224    const collection = helper.nft.getCollectionObject(collectionId);225    const sponsor = await helper.eth.createAccountWithBalance(donor);226    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);227228    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();229    let collectionData = (await collection.getData())!;230    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));231    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');232233    const sponsorCollection = helper.ethNativeContract.collection(collectionIdAddress, 'nft', sponsor);234    await sponsorCollection.methods.confirmCollectionSponsorship().send();235    collectionData = (await collection.getData())!;236    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));237238    const user = helper.eth.createAccount();239    await collectionEvm.methods.addCollectionAdmin(user).send();240    241    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));242    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));243244    const userCollectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', user);245    const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();246    expect(nextTokenId).to.be.equal('1');247    result = await userCollectionEvm.methods.mintWithTokenURI(248      user,249      nextTokenId,250      'Test URI',251    ).send();252253    const events = helper.eth.normalizeEvents(result.events);254    const address = helper.ethAddress.fromCollectionId(collectionId);255256    expect(events).to.be.deep.equal([257      {258        address,259        event: 'Transfer',260        args: {261          from: '0x0000000000000000000000000000000000000000',262          to: user,263          tokenId: nextTokenId,264        },265      },266    ]);267    expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');268  269    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));270    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);271    const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));272    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;273  });274});