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

difftreelog

source

tests/src/eth/evmToSubstrate/coderTest.ts6.3 KiBsourcehistory
1import {EthUniqueHelper, usingEthPlaygrounds} from '../util';2import {readFile} from 'fs/promises';3import {ContractImports} from '../util/playgrounds/types';4import Web3 from 'web3';5import {6  IProperty,7  ITokenPropertyPermission,8} from '../../util/playgrounds/types';9import {addressToEvm, decodeAddress} from '@polkadot/util-crypto';10import nonFungibleAbi from '../nonFungibleAbi.json';11import {IKeyringPair} from '@polkadot/types/types';1213enum SponsoringMode {14	Disabled = 0,15	Allowlisted = 1,16	Generous = 2,17}1819const WS_ENDPOINT = 'wss://ws-rc.unique.network';20const CONTRACT_IMPORT: ContractImports[] = [21  {22    fsPath: `${__dirname}/../api/CollectionHelpers.sol`,23    solPath: 'api/CollectionHelpers.sol',24  },25  {26    fsPath: `${__dirname}/../api/ContractHelpers.sol`,27    solPath: 'api/ContractHelpers.sol',28  },29  {30    fsPath: `${__dirname}/../api/UniqueRefungibleToken.sol`,31    solPath: 'api/UniqueRefungibleToken.sol',32  },33  {34    fsPath: `${__dirname}/../api/UniqueRefungible.sol`,35    solPath: 'api/UniqueRefungible.sol',36  },37  {38    fsPath: `${__dirname}/../api/UniqueNFT.sol`,39    solPath: 'api/UniqueNFT.sol',40  },41];4243const main = async () => {44  await usingEthPlaygrounds(async (helper, privateKey) => {45    const contract_source = (46      await readFile(`${__dirname}/EvmToSubstrateHelper.sol`)47    ).toString();4849    const donor = await privateKey('//Alice'); // Seed from account with balance on this network50    const myAccount = await privateKey('//Bob'); // replace with account from polkadot extension51    const signer = await helper.eth.createAccountWithBalance(donor, 100n);52    console.log(`signer data: ${Uint8Array.from(Buffer.from(signer.slice(2), 'hex'))}`);53    const collection = await helper.nft.mintCollection(donor, {54      name: 'test mintToSubstrate',55      description: 'EVMHelpers',56      tokenPrefix: 'ap',57      tokenPropertyPermissions: [58        {59          key: 'url',60          permission: {61            tokenOwner: true,62            collectionAdmin: true,63            mutable: true,64          },65        },66      ],67      limits: {sponsorTransferTimeout: 0, sponsorApproveTimeout: 0},68      permissions: {mintMode: true},69    });7071    await collection.addToAllowList(donor, {72      Ethereum: helper.address.substrateToEth(donor.address),73    });74    await collection.addToAllowList(donor, {Substrate: donor.address});75    await collection.addAdmin(donor, {Ethereum: signer});76    await collection.addAdmin(donor, {77      Ethereum: helper.address.substrateToEth(donor.address),78    });7980    console.log('collection admin(s)): ', await collection.getAdmins());81    console.log('collection owner(s)): ', await collection.getAllowList());8283    const collectionEthAddress = helper.ethAddress.fromCollectionId(collection.collectionId);8485    const collectionEthContract = helper.ethNativeContract.collection(86      collectionEthAddress,87      'nft',88    );8990    const receiverEthAddress = helper.address.substrateToEth(myAccount.address);91			92    const contract = await helper.ethContract.deployByCode(93      signer,94      'EvmToSubstrate',95      contract_source,96      CONTRACT_IMPORT,97    );98    console.log(`contract has been deployed\naddres: ${contract.options.address} || ${Uint8Array.from(Buffer.from(contract.options.address.slice(2), 'hex'))}`);99100    await helper.eth.transferBalanceFromSubstrate(101      donor,102      contract.options.address,103      100n,104    );105106    console.log('transfer has been completed');107108    await collection.addToAllowList(donor, {109      Ethereum: contract.options.address,110    });111    await collection.addAdmin(donor, {Ethereum: contract.options.address});112113    console.log('setup has been completed');114115    console.log('\t\t\t *** Properties Fees ***\n');116117    const propertiesNumber = 20;118119    const properties = Array(40)120      .fill(0)121      .map((_, i) => {122        return {123          key: `key_${i}`,124          value: Uint8Array.from(Buffer.from(`value_${i}`)),125        };126      });127128    const permissions: ITokenPropertyPermission[] = properties.map((p) => {129      return {130        key: p.key,131        permission: {132          tokenOwner: true,133          collectionAdmin: true,134          mutable: true,135        },136      };137    });138139    //    *** ProxyContract Bulk ***140141    const token = await collection.mintToken(donor, {Ethereum: signer});142143    const mintWithBulkPropProxyContractFee = await helper.arrange.calculcateFee(144      {Ethereum: signer},145      async () => {146        await contract.methods147          .proxyProperties(148            collectionEthContract.options.address,149            token.tokenId,150            properties.slice(0, propertiesNumber).map((p) => {151              return {field_0: p.key, field_1: p.value};152            }),153          )154          .send({from: signer, gas: 20_000_000});155      },156    );157    console.log(`token mint from contract(with bulk prop.) to the Substrate Id: ${mintWithBulkPropProxyContractFee}`);158159    console.log('All done');160  });161};162163main()164  .then(() => process.exit(0))165  .catch((e) => {166    console.log(e);167    process.exit(1);168  });169170async function createCollectionForPropertiesBenchmarks(171  helper: EthUniqueHelper,172  privateKey: (seed: string) => Promise<IKeyringPair>,173  ethSigner: string,174  proxyContract: string,175  permissions: ITokenPropertyPermission[],176) {177  const donor = await privateKey('//Alice'); // Seed from account with balance on this network178179  const collection = await helper.nft.mintCollection(donor, {180    name: 'test mintToSubstrate',181    description: 'EVMHelpers',182    tokenPrefix: 'ap',183    tokenPropertyPermissions: [184      {185        key: 'url',186        permission: {187          tokenOwner: true,188          collectionAdmin: true,189          mutable: true,190        },191      },192    ],193    limits: {sponsorTransferTimeout: 0, sponsorApproveTimeout: 0},194    permissions: {mintMode: true},195  });196197  await collection.addToAllowList(donor, {198    Ethereum: helper.address.substrateToEth(donor.address),199  });200  await collection.addToAllowList(donor, {Substrate: donor.address});201  await collection.addAdmin(donor, {Ethereum: ethSigner});202  await collection.addAdmin(donor, {203    Ethereum: helper.address.substrateToEth(donor.address),204  });205  await collection.addToAllowList(donor, {Ethereum: proxyContract});206  await collection.addAdmin(donor, {Ethereum: proxyContract});207  await collection.setTokenPropertyPermissions(donor, permissions);208209  return collection;210}