git.delta.rocks / unique-network / refs/commits / 4906885c14fb

difftreelog

source

tests/src/eth/collectionSponsoring.test.ts12.7 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 = privateKey('//Alice');13      nominal = helper.balance.getOneTokenNominal();14    });15  });1617  beforeEach(async () => {18    await usingPlaygrounds(async (helper) => {19      [alice] = await helper.arrange.createAccounts([1000n], donor);20    });21  });2223  itEth('sponsors mint transactions', async ({helper}) => {24    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});25    await collection.setSponsor(alice, alice.address);26    await collection.confirmSponsorship(alice);2728    const minter = helper.eth.createAccount();29    expect(await helper.balance.getEthereum(minter)).to.equal(0n);3031    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);32    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);3334    await collection.addToAllowList(alice, {Ethereum: minter});3536    const result = await contract.methods.mint(minter).send();3738    const events = helper.eth.normalizeEvents(result.events);39    expect(events).to.be.deep.equal([40      {41        address: collectionAddress,42        event: 'Transfer',43        args: {44          from: '0x0000000000000000000000000000000000000000',45          to: minter,46          tokenId: '1',47        },48      },49    ]);50  });5152  // TODO: Temprorary off. Need refactor53  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {54  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);55  //   const collectionHelpers = evmCollectionHelpers(web3, owner);56  //   let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send();57  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);58  //   const sponsor = privateKeyWrapper('//Alice');59  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);6061  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;62  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});63  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;6465  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);66  //   await submitTransactionAsync(sponsor, confirmTx);67  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;6869  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});70  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);71  // });7273  itEth('Remove sponsor', async ({helper}) => {74    const owner = await helper.eth.createAccountWithBalance(donor);75    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);7677    let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});78    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);79    const sponsor = await helper.eth.createAccountWithBalance(donor);80    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);8182    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;83    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});84    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;8586    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});87    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;8889    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});9091    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});92    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');93  });9495  itEth('Sponsoring collection from evm address via access list', async ({helper}) => {96    const owner = await helper.eth.createAccountWithBalance(donor);9798    const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');99100    const collection = helper.nft.getCollectionObject(collectionId);101    const sponsor = await helper.eth.createAccountWithBalance(donor);102    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);103104    await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});105    let collectionData = (await collection.getData())!;106    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));107    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');108109    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});110    collectionData = (await collection.getData())!;111    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));112113    const user = helper.eth.createAccount();114    const nextTokenId = await collectionEvm.methods.nextTokenId().call();115    expect(nextTokenId).to.be.equal('1');116117    const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();118    expect(oldPermissions.mintMode).to.be.false;119    expect(oldPermissions.access).to.be.equal('Normal');120121    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});122    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});123    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});124125    const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();126    expect(newPermissions.mintMode).to.be.true;127    expect(newPermissions.access).to.be.equal('AllowList');128129    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));130    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));131132    {133      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});134      const events = helper.eth.normalizeEvents(result.events);135136      expect(events).to.be.deep.equal([137        {138          address: collectionAddress,139          event: 'Transfer',140          args: {141            from: '0x0000000000000000000000000000000000000000',142            to: user,143            tokenId: '1',144          },145        },146      ]);147148      const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));149      const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));150151      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');152      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);153      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;154    }155  });156157  // TODO: Temprorary off. Need refactor158  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {159  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);160  //   const collectionHelpers = evmCollectionHelpers(web3, owner);161  //   const result = await collectionHelpers.methods.createERC721MetadataCompatibleNFTCollection('Sponsor collection', '1', '1', '').send();162  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);163  //   const sponsor = privateKeyWrapper('//Alice');164  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);165166  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});167168  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);169  //   await submitTransactionAsync(sponsor, confirmTx);170171  //   const user = createEthAccount(web3);172  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();173  //   expect(nextTokenId).to.be.equal('1');174175  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});176  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});177  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});178179  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);180  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];181182  //   {183  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();184  //     expect(nextTokenId).to.be.equal('1');185  //     const result = await collectionEvm.methods.mintWithTokenURI(186  //       user,187  //       nextTokenId,188  //       'Test URI',189  //     ).send({from: user});190  //     const events = normalizeEvents(result.events);191192  //     expect(events).to.be.deep.equal([193  //       {194  //         address: collectionIdAddress,195  //         event: 'Transfer',196  //         args: {197  //           from: '0x0000000000000000000000000000000000000000',198  //           to: user,199  //           tokenId: nextTokenId,200  //         },201  //       },202  //     ]);203204  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);205  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];206207  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');208  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);209  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;210  //   }211  // });212213  itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {214    const owner = await helper.eth.createAccountWithBalance(donor);215216    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');217    const collection = helper.nft.getCollectionObject(collectionId);218    const sponsor = await helper.eth.createAccountWithBalance(donor);219    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);220221    await collectionEvm.methods.setCollectionSponsor(sponsor).send();222    let collectionData = (await collection.getData())!;223    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));224    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');225226    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);227    await sponsorCollection.methods.confirmCollectionSponsorship().send();228    collectionData = (await collection.getData())!;229    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));230231    const user = helper.eth.createAccount();232    await collectionEvm.methods.addCollectionAdmin(user).send();233234    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));235    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));236237    const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user);238239    let result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI',).send();240    const tokenId = result.events.Transfer.returnValues.tokenId;241242    const events = helper.eth.normalizeEvents(result.events);243    const address = helper.ethAddress.fromCollectionId(collectionId);244245    expect(events).to.be.deep.equal([246      {247        address,248        event: 'Transfer',249        args: {250          from: '0x0000000000000000000000000000000000000000',251          to: user,252          tokenId: '1',253        },254      },255    ]);256    expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');257258    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));259    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);260    const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));261    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;262  });263});