git.delta.rocks / unique-network / refs/commits / 6d1c84661df9

difftreelog

source

js-packages/scripts/benchmarks/mintFee/index.ts11.0 KiBsourcehistory
1import {usingEthPlaygrounds} from '@unique/tests/eth/util/index.js';2import {EthUniqueHelper} from '@unique/tests/eth/util/playgrounds/unique.dev.js';3import {readFile} from 'fs/promises';4import type {ICrossAccountId} from '@unique/playgrounds/types.js';5import type {IKeyringPair} from '@polkadot/types/types';6import {UniqueNFTCollection} from '@unique/playgrounds/unique.js';7import {Contract} from 'web3-eth-contract';8import {createObjectCsvWriter} from 'csv-writer';9import {convertToTokens, createCollectionForBenchmarks, PERMISSIONS, PROPERTIES} from '../utils/common.js';10import {makeNames} from '@unique/tests/util/index.js';11import type {ContractImports} from '@unique/tests/eth/util/playgrounds/types.js';1213const {dirname} = makeNames(import.meta.url);1415export const CONTRACT_IMPORT: ContractImports[] = [16  {17    fsPath: `${dirname}/../../../tests/eth/api/CollectionHelpers.sol`,18    solPath: 'eth/api/CollectionHelpers.sol',19  },20  {21    fsPath: `${dirname}/../../../tests/eth/api/ContractHelpers.sol`,22    solPath: 'eth/api/ContractHelpers.sol',23  },24  {25    fsPath: `${dirname}/../../../tests/eth/api/UniqueRefungibleToken.sol`,26    solPath: 'eth/api/UniqueRefungibleToken.sol',27  },28  {29    fsPath: `${dirname}/../../../tests/eth/api/UniqueRefungible.sol`,30    solPath: 'eth/api/UniqueRefungible.sol',31  },32  {33    fsPath: `${dirname}/../../../tests/eth/api/UniqueNFT.sol`,34    solPath: 'eth/api/UniqueNFT.sol',35  },36];3738interface IBenchmarkResultForProp {39	propertiesNumber: number;40	substrateFee: number;41	ethFee: number;42	ethBulkFee: number;43  ethMintCrossFee: number;44	evmProxyContractFee: number;45	evmProxyContractBulkFee: number;46}4748const main = async () => {49  const benchmarks = [50    'substrateFee',51    'ethFee',52    'ethBulkFee',53    'ethMintCrossFee',54    'evmProxyContractFee',55    'evmProxyContractBulkFee',56  ];57  const headers = [58    'propertiesNumber',59    ...benchmarks,60  ];616263  const csvWriter = createObjectCsvWriter({64    path: 'properties.csv',65    header: headers,66  });6768  await usingEthPlaygrounds(async (helper, privateKey) => {69    const CONTRACT_SOURCE = (70      await readFile(`${dirname}/proxyContract.sol`)71    ).toString();7273    const donor = await privateKey('//Alice'); // Seed from account with balance on this network74    const ethSigner = await helper.eth.createAccountWithBalance(donor);7576    const contract = await helper.ethContract.deployByCode(77      ethSigner,78      'ProxyMint',79      CONTRACT_SOURCE,80      CONTRACT_IMPORT,81    );8283    const fees = await benchMintFee(helper, privateKey, contract);84    console.log('Minting without properties');85    console.table(fees);8687    const result: IBenchmarkResultForProp[] = [];88    const csvResult: IBenchmarkResultForProp[] = [];8990    for(let i = 1; i <= 20; i++) {91      const benchResult = await benchMintWithProperties(helper, privateKey, contract, {92        propertiesNumber: i,93      }) as any;9495      csvResult.push(benchResult);9697      const minFee = Math.min(...(benchmarks.map(x => benchResult[x])));98      for(const key of benchmarks) {99        const keyPercent = Math.round((benchResult[key] / minFee) * 100);100        benchResult[key] = `${benchResult[key]} (${keyPercent}%)`;101      }102103      result.push(benchResult);104    }105106    await csvWriter.writeRecords(csvResult);107108    console.log('Minting with properties');109    console.table(result, headers);110  });111};112113main()114  .then(() => process.exit(0))115  .catch((e) => {116    console.log(e);117    process.exit(1);118  });119120121122async function benchMintFee(123  helper: EthUniqueHelper,124  privateKey: (seed: string) => Promise<IKeyringPair>,125  proxyContract: Contract,126): Promise<{127	substrateFee: number;128	ethFee: number;129	evmProxyContractFee: number;130}> {131  const donor = await privateKey('//Alice');132  const substrateReceiver = await privateKey('//Bob');133  const ethSigner = await helper.eth.createAccountWithBalance(donor);134135  const nominal = helper.balance.getOneTokenNominal();136137  await helper.eth.transferBalanceFromSubstrate(138    donor,139    proxyContract.options.address,140    100n,141  );142143  const collection = (await createCollectionForBenchmarks(144    'nft',145    helper,146    privateKey,147    ethSigner,148    proxyContract.options.address,149    PERMISSIONS,150  )) as UniqueNFTCollection;151152  const substrateFee = await helper.arrange.calculcateFee(153    {Substrate: donor.address},154    () => collection.mintToken(donor, {Substrate: substrateReceiver.address}),155  );156157  const collectionEthAddress = helper.ethAddress.fromCollectionId(collection.collectionId);158  const collectionContract = await helper.ethNativeContract.collection(159    collectionEthAddress,160    'nft',161  );162163  const receiverEthAddress = helper.address.substrateToEth(substrateReceiver.address);164165  const encodedCall = collectionContract.methods166    .mint(receiverEthAddress)167    .encodeABI();168169  const ethFee = await helper.arrange.calculcateFee(170    {Substrate: donor.address},171    async () => {172      await helper.eth.sendEVM(173        donor,174        collectionContract.options.address,175        encodedCall,176        '0',177      );178    },179  );180181  const evmProxyContractFee = await helper.arrange.calculcateFee(182    {Ethereum: ethSigner},183    async () => {184      await proxyContract.methods185        .mintToSubstrate(186          helper.ethAddress.fromCollectionId(collection.collectionId),187          substrateReceiver.addressRaw,188        )189        .send({from: ethSigner});190    },191  );192193  return {194    substrateFee: convertToTokens(substrateFee, nominal),195    ethFee: convertToTokens(ethFee, nominal),196    evmProxyContractFee: convertToTokens(evmProxyContractFee, nominal),197  };198}199200async function benchMintWithProperties(201  helper: EthUniqueHelper,202  privateKey: (seed: string) => Promise<IKeyringPair>,203  proxyContract: Contract,204  setup: { propertiesNumber: number },205): Promise<IBenchmarkResultForProp> {206  const donor = await privateKey('//Alice'); // Seed from account with balance on this network207  const ethSigner = await helper.eth.createAccountWithBalance(donor);208209  const susbstrateReceiver = await privateKey('//Bob');210  const receiverEthAddress = helper.address.substrateToEth(susbstrateReceiver.address);211212  const nominal = helper.balance.getOneTokenNominal();213214  const substrateFee = await calculateFeeNftMintWithProperties(215    helper,216    privateKey,217    {Substrate: donor.address},218    ethSigner,219    proxyContract.options.address,220    async (collection) => {221      await collection.mintToken(222        donor,223        {Substrate: susbstrateReceiver.address},224        PROPERTIES.slice(0, setup.propertiesNumber).map((p) => ({key: p.key, value: Buffer.from(p.value).toString()})),225      );226    },227  );228229  const ethFee = await calculateFeeNftMintWithProperties(230    helper,231    privateKey,232    {Substrate: donor.address},233    ethSigner,234    proxyContract.options.address,235    async (collection) => {236      const evmContract = await helper.ethNativeContract.collection(237        helper.ethAddress.fromCollectionId(collection.collectionId),238        'nft',239        undefined,240        true,241      );242243      const subTokenId = await evmContract.methods.nextTokenId().call();244245      let encodedCall = evmContract.methods246        .mint(receiverEthAddress)247        .encodeABI();248249      await helper.eth.sendEVM(250        donor,251        evmContract.options.address,252        encodedCall,253        '0',254      );255256      for(const val of PROPERTIES.slice(0, setup.propertiesNumber)) {257        encodedCall = await evmContract.methods258          .setProperty(subTokenId, val.key, Buffer.from(val.value))259          .encodeABI();260261        await helper.eth.sendEVM(262          donor,263          evmContract.options.address,264          encodedCall,265          '0',266        );267      }268    },269  );270271  const ethBulkFee = await calculateFeeNftMintWithProperties(272    helper,273    privateKey,274    {Substrate: donor.address},275    ethSigner,276    proxyContract.options.address,277    async (collection) => {278      const evmContract = await helper.ethNativeContract.collection(279        helper.ethAddress.fromCollectionId(collection.collectionId),280        'nft',281      );282283      const subTokenId = await evmContract.methods.nextTokenId().call();284285      let encodedCall = evmContract.methods286        .mint(receiverEthAddress)287        .encodeABI();288289      await helper.eth.sendEVM(290        donor,291        evmContract.options.address,292        encodedCall,293        '0',294      );295296      encodedCall = await evmContract.methods297        .setProperties(298          subTokenId,299          PROPERTIES.slice(0, setup.propertiesNumber),300        )301        .encodeABI();302303      await helper.eth.sendEVM(304        donor,305        evmContract.options.address,306        encodedCall,307        '0',308      );309    },310  );311312  const ethMintCrossFee = await calculateFeeNftMintWithProperties(313    helper,314    privateKey,315    {Ethereum: ethSigner},316    ethSigner,317    proxyContract.options.address,318    async (collection) => {319      const evmContract = await helper.ethNativeContract.collection(320        helper.ethAddress.fromCollectionId(collection.collectionId),321        'nft',322      );323324      await evmContract.methods.mintCross(325        helper.ethCrossAccount.fromAddress(receiverEthAddress),326        PROPERTIES.slice(0, setup.propertiesNumber),327      )328        .send({from: ethSigner});329    },330  );331332  const proxyContractFee = await calculateFeeNftMintWithProperties(333    helper,334    privateKey,335    {Ethereum: ethSigner},336    ethSigner,337    proxyContract.options.address,338    async (collection) => {339      await proxyContract.methods340        .mintToSubstrateWithProperty(341          helper.ethAddress.fromCollectionId(collection.collectionId),342          susbstrateReceiver.addressRaw,343          PROPERTIES.slice(0, setup.propertiesNumber),344        )345        .send({from: ethSigner});346    },347  );348349  const proxyContractBulkFee = await calculateFeeNftMintWithProperties(350    helper,351    privateKey,352    {Ethereum: ethSigner},353    ethSigner,354    proxyContract.options.address,355    async (collection) => {356      await proxyContract.methods357        .mintToSubstrateBulkProperty(358          helper.ethAddress.fromCollectionId(collection.collectionId),359          susbstrateReceiver.addressRaw,360          PROPERTIES.slice(0, setup.propertiesNumber),361        )362        .send({from: ethSigner});363    },364  );365366  return {367    propertiesNumber: setup.propertiesNumber,368    substrateFee: convertToTokens(substrateFee, nominal),369    ethFee: convertToTokens(ethFee, nominal),370    ethBulkFee: convertToTokens(ethBulkFee, nominal),371    ethMintCrossFee: convertToTokens(ethMintCrossFee, nominal),372    evmProxyContractFee: convertToTokens(proxyContractFee, nominal),373    evmProxyContractBulkFee: convertToTokens(proxyContractBulkFee, nominal),374  };375}376377async function calculateFeeNftMintWithProperties(378  helper: EthUniqueHelper,379  privateKey: (seed: string) => Promise<IKeyringPair>,380  payer: ICrossAccountId,381  ethSigner: string,382  proxyContractAddress: string,383  calculatedCall: (collection: UniqueNFTCollection) => Promise<any>,384): Promise<bigint> {385  const collection = (await createCollectionForBenchmarks(386    'nft',387    helper,388    privateKey,389    ethSigner,390    proxyContractAddress,391    PERMISSIONS,392  )) as UniqueNFTCollection;393  return helper.arrange.calculcateFee(payer, async () => {394    await calculatedCall(collection);395  });396}397398399