difftreelog
Style fixes
in: master
6 files changed
tests/.gitignorediffbeforeafterboth--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1 +1,2 @@
/node_modules/
+properties.csv
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -101,6 +101,7 @@
"testXcmTransferAcala": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferAcala.test.ts acalaId=2000 uniqueId=5000",
"testXcmTransferStatemine": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferStatemine.test.ts statemineId=1000 uniqueId=5000",
"testXcmTransferMoonbeam": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferMoonbeam.test.ts",
+ "benchMintingFee": "ts-node src/benchmarks/mintFee/benchmark.ts",
"load": "mocha --timeout 9999999 -r ts-node/register './**/*.load.ts'",
"loadTransfer": "ts-node src/transfer.nload.ts",
"polkadot-types-fetch-metadata": "curl -H 'Content-Type: application/json' -d '{\"id\":\"1\", \"jsonrpc\":\"2.0\", \"method\": \"state_getMetadata\", \"params\":[]}' http://localhost:9933 > src/interfaces/metadata.json",
tests/src/benchmarks/mintFee/benchmark.tsdiffbeforeafterboth1import {EthUniqueHelper, usingEthPlaygrounds} from '../../eth/util';2import {readFile} from 'fs/promises';3import {ContractImports} from '../../eth/util/playgrounds/types';4import {5 ICrossAccountId,6 ITokenPropertyPermission,7} from '../../util/playgrounds/types';8import {IKeyringPair} from '@polkadot/types/types';9import {UniqueNFTCollection} from '../../util/playgrounds/unique';10import {Contract} from 'web3-eth-contract';11import {createObjectCsvWriter} from 'csv-writer';1213const CONTRACT_IMPORT: ContractImports[] = [14 {15 fsPath: `${__dirname}/../../eth/api/CollectionHelpers.sol`,16 solPath: 'eth/api/CollectionHelpers.sol',17 },18 {19 fsPath: `${__dirname}/../../eth/api/ContractHelpers.sol`,20 solPath: 'eth/api/ContractHelpers.sol',21 },22 {23 fsPath: `${__dirname}/../../eth/api/UniqueRefungibleToken.sol`,24 solPath: 'eth/api/UniqueRefungibleToken.sol',25 },26 {27 fsPath: `${__dirname}/../../eth/api/UniqueRefungible.sol`,28 solPath: 'eth/api/UniqueRefungible.sol',29 },30 {31 fsPath: `${__dirname}/../../eth/api/UniqueNFT.sol`,32 solPath: 'eth/api/UniqueNFT.sol',33 },34];3536const PROPERTIES = Array(40)37 .fill(0)38 .map((_, i) => {39 return {40 key: `key_${i}`,41 value: Uint8Array.from(Buffer.from(`value_${i}`)),42 };43 });4445const PERMISSIONS: ITokenPropertyPermission[] = PROPERTIES.map((p) => {46 return {47 key: p.key,48 permission: {49 tokenOwner: true,50 collectionAdmin: true,51 mutable: true,52 },53 };54});5556interface IBenchmarkResultForProp {57 propertiesNumber: number;58 substrateFee: number;59 ethFee: number;60 ethBulkFee: number;61 evmProxyContractFee: number;62 evmProxyContractBulkFee: number;63}6465const main = async () => {66 const benchmarks = [67 'substrateFee',68 'ethFee',69 'ethBulkFee',70 'evmProxyContractFee',71 'evmProxyContractBulkFee',72 ];73 const headers = [74 'propertiesNumber',75 ...benchmarks,76 ];777879 const csvWriter = createObjectCsvWriter({80 path: 'properties.csv',81 header: headers,82 });8384 await usingEthPlaygrounds(async (helper, privateKey) => {85 const CONTRACT_SOURCE = (86 await readFile(`${__dirname}/proxyContract.sol`)87 ).toString();8889 const donor = await privateKey('//Alice'); // Seed from account with balance on this network90 const ethSigner = await helper.eth.createAccountWithBalance(donor, 100n);9192 const contract = await helper.ethContract.deployByCode(93 ethSigner,94 'ProxyMint',95 CONTRACT_SOURCE,96 CONTRACT_IMPORT,97 );9899 const fees = await benchMintFee(helper, privateKey, contract);100 console.log('Minting without properties');101 console.table(fees);102103 const result: IBenchmarkResultForProp[] = [];104 const csvResult: IBenchmarkResultForProp[] = [];105106 for (let i = 1; i <= 20; i++) {107 const benchResult = await benchMintWithProperties(helper, privateKey, contract, {108 propertiesNumber: i,109 }) as any;110111 csvResult.push(benchResult);112113 const minFee = Math.min(...(benchmarks.map(x => benchResult[x])));114 for(const key of benchmarks) {115 const keyPercent = Math.round((benchResult[key] / minFee) * 100);116 benchResult[key] = `${benchResult[key]} (${keyPercent}%)`;117 }118119 result.push(benchResult);120 }121122 await csvWriter.writeRecords(csvResult);123124 console.log('Minting with properties');125 console.table(result, headers);126 });127};128129main()130 .then(() => process.exit(0))131 .catch((e) => {132 console.log(e);133 process.exit(1);134 });135136async function createCollectionForBenchmarks(137 helper: EthUniqueHelper,138 privateKey: (seed: string) => Promise<IKeyringPair>,139 ethSigner: string,140 proxyContract: string,141 permissions: ITokenPropertyPermission[],142) {143 const donor = await privateKey('//Alice');144145 const collection = await helper.nft.mintCollection(donor, {146 name: 'test mintToSubstrate',147 description: 'EVMHelpers',148 tokenPrefix: 'ap',149 tokenPropertyPermissions: [150 {151 key: 'url',152 permission: {153 tokenOwner: true,154 collectionAdmin: true,155 mutable: true,156 },157 },158 ],159 limits: {sponsorTransferTimeout: 0, sponsorApproveTimeout: 0},160 permissions: {mintMode: true},161 });162163 await collection.addToAllowList(donor, {164 Ethereum: helper.address.substrateToEth(donor.address),165 });166 await collection.addToAllowList(donor, {Substrate: donor.address});167 await collection.addAdmin(donor, {Ethereum: ethSigner});168 await collection.addAdmin(donor, {169 Ethereum: helper.address.substrateToEth(donor.address),170 });171 await collection.addToAllowList(donor, {Ethereum: proxyContract});172 await collection.addAdmin(donor, {Ethereum: proxyContract});173 await collection.setTokenPropertyPermissions(donor, permissions);174175 return collection;176}177178async function benchMintFee(179 helper: EthUniqueHelper,180 privateKey: (seed: string) => Promise<IKeyringPair>,181 proxyContract: Contract,182): Promise<{183 substrateFee: number;184 ethFee: number;185 evmProxyContractFee: number;186}> {187 const donor = await privateKey('//Alice');188 const substrateReceiver = await privateKey('//Bob');189 const ethSigner = await helper.eth.createAccountWithBalance(donor, 100n);190191 const nominal = helper.balance.getOneTokenNominal();192193 await helper.eth.transferBalanceFromSubstrate(194 donor,195 proxyContract.options.address,196 100n,197 );198199 const collection = await createCollectionForBenchmarks(200 helper,201 privateKey,202 ethSigner,203 proxyContract.options.address,204 PERMISSIONS,205 );206207 const substrateFee = await helper.arrange.calculcateFee(208 {Substrate: donor.address},209 () => collection.mintToken(donor, {Substrate: substrateReceiver.address}),210 );211212 const collectionEthAddress = helper.ethAddress.fromCollectionId(collection.collectionId);213 const collectionContract = helper.ethNativeContract.collection(214 collectionEthAddress,215 'nft',216 );217218 const receiverEthAddress = helper.address.substrateToEth(substrateReceiver.address);219220 const encodedCall = collectionContract.methods221 .mint(receiverEthAddress)222 .encodeABI();223224 const ethFee = await helper.arrange.calculcateFee(225 {Substrate: donor.address},226 async () => {227 await helper.eth.sendEVM(228 donor,229 collectionContract.options.address,230 encodedCall,231 '0',232 );233 },234 );235236 const evmProxyContractFee = await helper.arrange.calculcateFee(237 {Ethereum: ethSigner},238 async () => {239 await proxyContract.methods240 .mintToSubstrate(241 helper.ethAddress.fromCollectionId(collection.collectionId),242 substrateReceiver.addressRaw,243 )244 .send({from: ethSigner});245 },246 );247248 return {249 substrateFee: convertToTokens(substrateFee, nominal),250 ethFee: convertToTokens(ethFee, nominal),251 evmProxyContractFee: convertToTokens(evmProxyContractFee, nominal),252 };253}254255async function benchMintWithProperties(256 helper: EthUniqueHelper,257 privateKey: (seed: string) => Promise<IKeyringPair>,258 proxyContract: Contract,259 setup: { propertiesNumber: number },260): Promise<IBenchmarkResultForProp> {261 const donor = await privateKey('//Alice'); // Seed from account with balance on this network262 const ethSigner = await helper.eth.createAccountWithBalance(donor, 100n);263264 const susbstrateReceiver = await privateKey('//Bob');265 const receiverEthAddress = helper.address.substrateToEth(susbstrateReceiver.address);266267 const nominal = helper.balance.getOneTokenNominal();268269 const substrateFee = await calculateFeeNftMintWithProperties(270 helper,271 privateKey,272 {Substrate: donor.address},273 ethSigner,274 proxyContract.options.address,275 async (collection) => {276 await collection.mintToken(277 donor,278 {Substrate: susbstrateReceiver.address},279 PROPERTIES.slice(0, setup.propertiesNumber).map((p) => {280 return {key: p.key, value: Buffer.from(p.value).toString()};281 }),282 );283 },284 );285286 const ethFee = await calculateFeeNftMintWithProperties(287 helper,288 privateKey,289 {Substrate: donor.address},290 ethSigner,291 proxyContract.options.address,292 async (collection) => {293 const evmContract = helper.ethNativeContract.collection(294 helper.ethAddress.fromCollectionId(collection.collectionId),295 'nft',296 );297298 const subTokenId = await evmContract.methods.nextTokenId().call();299300 let encodedCall = evmContract.methods301 .mint(receiverEthAddress)302 .encodeABI();303304 await helper.eth.sendEVM(305 donor,306 evmContract.options.address,307 encodedCall,308 '0',309 );310311 for (const val of PROPERTIES.slice(0, setup.propertiesNumber)) {312 encodedCall = await evmContract.methods313 .setProperty(subTokenId, val.key, Buffer.from(val.value))314 .encodeABI();315316 await helper.eth.sendEVM(317 donor,318 evmContract.options.address,319 encodedCall,320 '0',321 );322 }323 },324 );325326 const ethBulkFee = await calculateFeeNftMintWithProperties(327 helper,328 privateKey,329 {Substrate: donor.address},330 ethSigner,331 proxyContract.options.address,332 async (collection) => {333 const evmContract = helper.ethNativeContract.collection(334 helper.ethAddress.fromCollectionId(collection.collectionId),335 'nft',336 );337338 const subTokenId = await evmContract.methods.nextTokenId().call();339340 let encodedCall = evmContract.methods341 .mint(receiverEthAddress)342 .encodeABI();343344 await helper.eth.sendEVM(345 donor,346 evmContract.options.address,347 encodedCall,348 '0',349 );350351 encodedCall = await evmContract.methods352 .setProperties(353 subTokenId,354 PROPERTIES.slice(0, setup.propertiesNumber).map((p) => {355 return {field_0: p.key, field_1: p.value};356 }),357 )358 .encodeABI();359360 await helper.eth.sendEVM(361 donor,362 evmContract.options.address,363 encodedCall,364 '0',365 );366 },367 );368369 const proxyContractFee = await calculateFeeNftMintWithProperties(370 helper,371 privateKey,372 {Ethereum: ethSigner},373 ethSigner,374 proxyContract.options.address,375 async (collection) => {376 await proxyContract.methods377 .mintToSubstrateWithProperty(378 helper.ethAddress.fromCollectionId(collection.collectionId),379 susbstrateReceiver.addressRaw,380 PROPERTIES.slice(0, setup.propertiesNumber),381 )382 .send({from: ethSigner});383 },384 );385386 const proxyContractBulkFee = await calculateFeeNftMintWithProperties(387 helper,388 privateKey,389 {Ethereum: ethSigner},390 ethSigner,391 proxyContract.options.address,392 async (collection) => {393 await proxyContract.methods394 .mintToSubstrateBulkProperty(395 helper.ethAddress.fromCollectionId(collection.collectionId),396 susbstrateReceiver.addressRaw,397 PROPERTIES.slice(0, setup.propertiesNumber).map((p) => {398 return {field_0: p.key, field_1: p.value};399 }),400 )401 .send({from: ethSigner, gas: 25_000_000});402 },403 );404405 return {406 propertiesNumber: setup.propertiesNumber,407 substrateFee: convertToTokens(substrateFee, nominal),408 ethFee: convertToTokens(ethFee, nominal),409 ethBulkFee: convertToTokens(ethBulkFee, nominal),410 evmProxyContractFee: convertToTokens(proxyContractFee, nominal),411 evmProxyContractBulkFee: convertToTokens(proxyContractBulkFee, nominal),412 };413}414415async function calculateFeeNftMintWithProperties(416 helper: EthUniqueHelper,417 privateKey: (seed: string) => Promise<IKeyringPair>,418 payer: ICrossAccountId,419 ethSigner: string,420 proxyContractAddress: string,421 calculatedCall: (collection: UniqueNFTCollection) => Promise<any>,422): Promise<bigint> {423 const collection = await createCollectionForBenchmarks(424 helper,425 privateKey,426 ethSigner,427 proxyContractAddress,428 PERMISSIONS,429 );430 return helper.arrange.calculcateFee(payer, async () => {431 await calculatedCall(collection);432 });433}434function convertToTokens(value: bigint, nominal: bigint): number {435 return Number((value * 1000n) / nominal) / 1000;436}tests/src/benchmarks/mintFee/proxyContract.soldiffbeforeafterboth--- /dev/null
+++ b/tests/src/benchmarks/mintFee/proxyContract.sol
@@ -0,0 +1,129 @@
+// SPDX-License-Identifier: Apache License
+pragma solidity >=0.8.0;
+import {CollectionHelpers} from "../../eth/api/CollectionHelpers.sol";
+import {ContractHelpers} from "../../eth/api/ContractHelpers.sol";
+import {UniqueRefungibleToken} from "../../eth/api/UniqueRefungibleToken.sol";
+import {UniqueRefungible, Collection, EthCrossAccount as RftCrossAccountId, Tuple20 as RftProperties} from "../../eth/api/UniqueRefungible.sol";
+import {UniqueNFT, EthCrossAccount as NftCrossAccountId, Tuple21 as NftProperty, TokenProperties} from "../../eth/api/UniqueNFT.sol";
+
+struct Property {
+ string key;
+ bytes value;
+}
+
+contract ProxyMint {
+ bytes32 constant REFUNGIBLE_COLLECTION_TYPE = keccak256(bytes("ReFungible"));
+ bytes32 constant NONFUNGIBLE_COLLECTION_TYPE = keccak256(bytes("NFT"));
+
+ modifier checkRestrictions(address _collection) {
+ Collection commonContract = Collection(_collection);
+ require(commonContract.isOwnerOrAdmin(msg.sender), "Only collection admin/owner can call this method");
+ _;
+ }
+
+ /// @dev This emits when a mint to a substrate address has been made.
+ event MintToSub(address _toEth, uint256 _toSub, address _collection, uint256 _tokenId);
+
+ function mintToSubstrate(address _collection, uint256 _substrateReceiver) external checkRestrictions(_collection) {
+ Collection commonContract = Collection(_collection);
+ bytes32 collectionType = keccak256(bytes(commonContract.uniqueCollectionType()));
+ uint256 tokenId;
+
+ if (collectionType == REFUNGIBLE_COLLECTION_TYPE) {
+ UniqueRefungible rftCollection = UniqueRefungible(_collection);
+
+ tokenId = rftCollection.mint(address(this));
+
+ rftCollection.transferFromCross(
+ RftCrossAccountId(address(this), 0),
+ RftCrossAccountId(address(0), _substrateReceiver),
+ tokenId
+ );
+ } else if (collectionType == NONFUNGIBLE_COLLECTION_TYPE) {
+ UniqueNFT nftCollection = UniqueNFT(_collection);
+ tokenId = nftCollection.mint(address(this));
+
+ nftCollection.transferFromCross(
+ NftCrossAccountId(address(this), 0),
+ NftCrossAccountId(address(0), _substrateReceiver),
+ tokenId
+ );
+ } else {
+ revert("Wrong collection type. Works only with NFT or RFT");
+ }
+
+ emit MintToSub(address(0), _substrateReceiver, _collection, tokenId);
+ }
+
+ function mintToSubstrateWithProperty(
+ address _collection,
+ uint256 _substrateReceiver,
+ Property[] calldata properties
+ ) external checkRestrictions(_collection) {
+ uint256 propertiesLength = properties.length;
+ require(propertiesLength > 0, "Properies is empty");
+
+ Collection commonContract = Collection(_collection);
+ bytes32 collectionType = keccak256(bytes(commonContract.uniqueCollectionType()));
+ uint256 tokenId;
+
+ if (collectionType == REFUNGIBLE_COLLECTION_TYPE) {
+ UniqueRefungible rftCollection = UniqueRefungible(_collection);
+ tokenId = rftCollection.nextTokenId();
+ rftCollection.mint(address(this));
+ for (uint256 i = 0; i < propertiesLength; ++i) {
+ rftCollection.setProperty(tokenId, properties[i].key, properties[i].value);
+ }
+ rftCollection.transferFromCross(
+ RftCrossAccountId(address(this), 0),
+ RftCrossAccountId(address(0), _substrateReceiver),
+ tokenId
+ );
+ } else if (collectionType == NONFUNGIBLE_COLLECTION_TYPE) {
+ UniqueNFT nftCollection = UniqueNFT(_collection);
+ tokenId = nftCollection.mint(address(this));
+ for (uint256 i = 0; i < propertiesLength; ++i) {
+ nftCollection.setProperty(tokenId, properties[i].key, properties[i].value);
+ }
+ nftCollection.transferFromCross(
+ NftCrossAccountId(address(this), 0),
+ NftCrossAccountId(address(0), _substrateReceiver),
+ tokenId
+ );
+ } else {
+ revert("Wrong collection type. Works only with NFT or RFT");
+ }
+
+ emit MintToSub(address(0), _substrateReceiver, _collection, tokenId);
+ }
+
+ function mintToSubstrateBulkProperty(
+ address _collection,
+ uint256 _substrateReceiver,
+ NftProperty[] calldata _properties
+ ) external checkRestrictions(_collection) {
+ uint256 propertiesLength = _properties.length;
+ require(propertiesLength > 0, "Properies is empty");
+
+ Collection commonContract = Collection(_collection);
+ bytes32 collectionType = keccak256(bytes(commonContract.uniqueCollectionType()));
+ uint256 tokenId;
+
+ if (collectionType == REFUNGIBLE_COLLECTION_TYPE) {} else if (collectionType == NONFUNGIBLE_COLLECTION_TYPE) {
+ UniqueNFT nftCollection = UniqueNFT(_collection);
+ tokenId = nftCollection.mint(address(this));
+
+ nftCollection.setProperties(tokenId, _properties);
+
+ nftCollection.transferFromCross(
+ NftCrossAccountId(address(this), 0),
+ NftCrossAccountId(address(0), _substrateReceiver),
+ tokenId
+ );
+ } else {
+ revert("Wrong collection type. Works only with NFT or RFT");
+ }
+
+ emit MintToSub(address(0), _substrateReceiver, _collection, tokenId);
+ }
+}
tests/src/benchmarks/mintFeeBench/feeBench.tsdiffbeforeafterboth--- a/tests/src/benchmarks/mintFeeBench/feeBench.ts
+++ /dev/null
@@ -1,418 +0,0 @@
-import {EthUniqueHelper, usingEthPlaygrounds} from '../../eth/util';
-import {readFile} from 'fs/promises';
-import {ContractImports} from '../../eth/util/playgrounds/types';
-import {
- ICrossAccountId,
- ITokenPropertyPermission,
-} from '../../util/playgrounds/types';
-import {IKeyringPair} from '@polkadot/types/types';
-import {UniqueNFTCollection} from '../../util/playgrounds/unique';
-import {Contract} from 'web3-eth-contract';
-import {createObjectCsvWriter} from 'csv-writer';
-
-const CONTRACT_IMPORT: ContractImports[] = [
- {
- fsPath: `${__dirname}/../../eth/api/CollectionHelpers.sol`,
- solPath: 'eth/api/CollectionHelpers.sol',
- },
- {
- fsPath: `${__dirname}/../../eth/api/ContractHelpers.sol`,
- solPath: 'eth/api/ContractHelpers.sol',
- },
- {
- fsPath: `${__dirname}/../../eth/api/UniqueRefungibleToken.sol`,
- solPath: 'eth/api/UniqueRefungibleToken.sol',
- },
- {
- fsPath: `${__dirname}/../../eth/api/UniqueRefungible.sol`,
- solPath: 'eth/api/UniqueRefungible.sol',
- },
- {
- fsPath: `${__dirname}/../../eth/api/UniqueNFT.sol`,
- solPath: 'eth/api/UniqueNFT.sol',
- },
-];
-
-const PROPERTIES = Array(40)
- .fill(0)
- .map((_, i) => {
- return {
- key: `key_${i}`,
- value: Uint8Array.from(Buffer.from(`value_${i}`)),
- };
- });
-
-const PERMISSIONS: ITokenPropertyPermission[] = PROPERTIES.map((p) => {
- return {
- key: p.key,
- permission: {
- tokenOwner: true,
- collectionAdmin: true,
- mutable: true,
- },
- };
-});
-
-interface IBenchmarkResultForProp {
- propertiesNumber: number;
- substrateFee: number;
- ethFee: number;
- ethBulkFee: number;
- evmProxyContractFee: number;
- evmProxyContractBulkFee: number;
-}
-
-const main = async () => {
- const csvWriter = createObjectCsvWriter({
- path: 'properties.csv',
- header: [
- 'propertiesNumber',
- 'substrateFee',
- 'ethFee',
- 'ethBulkFee',
- 'evmProxyContractFee',
- 'evmProxyContractBulkFee',
- ],
- });
-
- await usingEthPlaygrounds(async (helper, privateKey) => {
- const CONTRACT_SOURCE = (
- await readFile(`${__dirname}/proxyContract.sol`)
- ).toString();
-
- const donor = await privateKey('//Alice'); // Seed from account with balance on this network
- const myAccount = await privateKey('//Bob'); // replace with account from polkadot extension
- const ethSigner = await helper.eth.createAccountWithBalance(donor, 100n);
-
- const contract = await helper.ethContract.deployByCode(
- ethSigner,
- 'ProxyMint',
- CONTRACT_SOURCE,
- CONTRACT_IMPORT,
- );
-
- const fees = await benchMintFee(helper, privateKey, contract);
- console.log(fees);
-
- const result: IBenchmarkResultForProp[] = [];
-
- for (let i = 1; i <= 20; i++) {
- result.push(await benchMintWithProperties(helper, privateKey, contract, {
- propertiesNumber: i,
- }));
- }
-
- await csvWriter.writeRecords(result);
-
- console.table(result);
- });
-};
-
-main()
- .then(() => process.exit(0))
- .catch((e) => {
- console.log(e);
- process.exit(1);
- });
-
-async function createCollectionForBenchmarks(
- helper: EthUniqueHelper,
- privateKey: (seed: string) => Promise<IKeyringPair>,
- ethSigner: string,
- proxyContract: string,
- permissions: ITokenPropertyPermission[],
-) {
- const donor = await privateKey('//Alice');
-
- const collection = await helper.nft.mintCollection(donor, {
- name: 'test mintToSubstrate',
- description: 'EVMHelpers',
- tokenPrefix: 'ap',
- tokenPropertyPermissions: [
- {
- key: 'url',
- permission: {
- tokenOwner: true,
- collectionAdmin: true,
- mutable: true,
- },
- },
- ],
- limits: {sponsorTransferTimeout: 0, sponsorApproveTimeout: 0},
- permissions: {mintMode: true},
- });
-
- await collection.addToAllowList(donor, {
- Ethereum: helper.address.substrateToEth(donor.address),
- });
- await collection.addToAllowList(donor, {Substrate: donor.address});
- await collection.addAdmin(donor, {Ethereum: ethSigner});
- await collection.addAdmin(donor, {
- Ethereum: helper.address.substrateToEth(donor.address),
- });
- await collection.addToAllowList(donor, {Ethereum: proxyContract});
- await collection.addAdmin(donor, {Ethereum: proxyContract});
- await collection.setTokenPropertyPermissions(donor, permissions);
-
- return collection;
-}
-
-async function benchMintFee(
- helper: EthUniqueHelper,
- privateKey: (seed: string) => Promise<IKeyringPair>,
- proxyContract: Contract,
-): Promise<{
- substrateFee: number;
- ethFee: number;
- evmProxyContractFee: number;
-}> {
- const donor = await privateKey('//Alice');
- const substrateReceiver = await privateKey('//Bob');
- const ethSigner = await helper.eth.createAccountWithBalance(donor, 100n);
-
- const nominal = helper.balance.getOneTokenNominal();
-
- await helper.eth.transferBalanceFromSubstrate(
- donor,
- proxyContract.options.address,
- 100n,
- );
-
- const collection = await createCollectionForBenchmarks(
- helper,
- privateKey,
- ethSigner,
- proxyContract.options.address,
- PERMISSIONS,
- );
-
- const substrateFee = await helper.arrange.calculcateFee(
- {Substrate: donor.address},
- () => collection.mintToken(donor, {Substrate: substrateReceiver.address}),
- );
-
- const collectionEthAddress = helper.ethAddress.fromCollectionId(collection.collectionId);
- const collectionContract = helper.ethNativeContract.collection(
- collectionEthAddress,
- 'nft',
- );
-
- const receiverEthAddress = helper.address.substrateToEth(substrateReceiver.address);
-
- const encodedCall = collectionContract.methods
- .mint(receiverEthAddress)
- .encodeABI();
-
- const ethFee = await helper.arrange.calculcateFee(
- {Substrate: donor.address},
- async () => {
- await helper.eth.sendEVM(
- donor,
- collectionContract.options.address,
- encodedCall,
- '0',
- );
- },
- );
-
- const evmProxyContractFee = await helper.arrange.calculcateFee(
- {Ethereum: ethSigner},
- async () => {
- await proxyContract.methods
- .mintToSubstrate(
- helper.ethAddress.fromCollectionId(collection.collectionId),
- substrateReceiver.addressRaw,
- )
- .send({from: ethSigner});
- },
- );
-
- return {
- substrateFee: convertToTokens(substrateFee, nominal),
- ethFee: convertToTokens(ethFee, nominal),
- evmProxyContractFee: convertToTokens(evmProxyContractFee, nominal),
- };
-}
-
-async function benchMintWithProperties(
- helper: EthUniqueHelper,
- privateKey: (seed: string) => Promise<IKeyringPair>,
- proxyContract: Contract,
- setup: { propertiesNumber: number },
-): Promise<IBenchmarkResultForProp> {
- const donor = await privateKey('//Alice'); // Seed from account with balance on this network
- const ethSigner = await helper.eth.createAccountWithBalance(donor, 100n);
-
- const susbstrateReceiver = await privateKey('//Bob');
- const receiverEthAddress = helper.address.substrateToEth(susbstrateReceiver.address);
-
- const nominal = helper.balance.getOneTokenNominal();
-
- const substrateFee = await calculateFeeNftMintWithProperties(
- helper,
- privateKey,
- {Substrate: donor.address},
- ethSigner,
- proxyContract.options.address,
- async (collection) => {
- await collection.mintToken(
- donor,
- {Substrate: susbstrateReceiver.address},
- PROPERTIES.slice(0, setup.propertiesNumber).map((p) => {
- return {key: p.key, value: Buffer.from(p.value).toString()};
- }),
- );
- },
- );
-
- const ethFee = await calculateFeeNftMintWithProperties(
- helper,
- privateKey,
- {Substrate: donor.address},
- ethSigner,
- proxyContract.options.address,
- async (collection) => {
- const evmContract = helper.ethNativeContract.collection(
- helper.ethAddress.fromCollectionId(collection.collectionId),
- 'nft',
- );
-
- const subTokenId = await evmContract.methods.nextTokenId().call();
-
- let encodedCall = evmContract.methods
- .mint(receiverEthAddress)
- .encodeABI();
-
- await helper.eth.sendEVM(
- donor,
- evmContract.options.address,
- encodedCall,
- '0',
- );
-
- for (const val of PROPERTIES.slice(0, setup.propertiesNumber)) {
- encodedCall = await evmContract.methods
- .setProperty(subTokenId, val.key, Buffer.from(val.value))
- .encodeABI();
-
- await helper.eth.sendEVM(
- donor,
- evmContract.options.address,
- encodedCall,
- '0',
- );
- }
- },
- );
-
- const ethBulkFee = await calculateFeeNftMintWithProperties(
- helper,
- privateKey,
- {Substrate: donor.address},
- ethSigner,
- proxyContract.options.address,
- async (collection) => {
- const evmContract = helper.ethNativeContract.collection(
- helper.ethAddress.fromCollectionId(collection.collectionId),
- 'nft',
- );
-
- const subTokenId = await evmContract.methods.nextTokenId().call();
-
- let encodedCall = evmContract.methods
- .mint(receiverEthAddress)
- .encodeABI();
-
- await helper.eth.sendEVM(
- donor,
- evmContract.options.address,
- encodedCall,
- '0',
- );
-
- encodedCall = await evmContract.methods
- .setProperties(
- subTokenId,
- PROPERTIES.slice(0, setup.propertiesNumber).map((p) => {
- return {field_0: p.key, field_1: p.value};
- }),
- )
- .encodeABI();
-
- await helper.eth.sendEVM(
- donor,
- evmContract.options.address,
- encodedCall,
- '0',
- );
- },
- );
-
- const proxyContractFee = await calculateFeeNftMintWithProperties(
- helper,
- privateKey,
- {Ethereum: ethSigner},
- ethSigner,
- proxyContract.options.address,
- async (collection) => {
- await proxyContract.methods
- .mintToSubstrateWithProperty(
- helper.ethAddress.fromCollectionId(collection.collectionId),
- susbstrateReceiver.addressRaw,
- PROPERTIES.slice(0, setup.propertiesNumber),
- )
- .send({from: ethSigner});
- },
- );
-
- const proxyContractBulkFee = await calculateFeeNftMintWithProperties(
- helper,
- privateKey,
- {Ethereum: ethSigner},
- ethSigner,
- proxyContract.options.address,
- async (collection) => {
- await proxyContract.methods
- .mintToSubstrateBulkProperty(
- helper.ethAddress.fromCollectionId(collection.collectionId),
- susbstrateReceiver.addressRaw,
- PROPERTIES.slice(0, setup.propertiesNumber).map((p) => {
- return {field_0: p.key, field_1: p.value};
- }),
- )
- .send({from: ethSigner, gas: 25_000_000});
- },
- );
-
- return {
- propertiesNumber: setup.propertiesNumber,
- substrateFee: convertToTokens(substrateFee, nominal),
- ethFee: convertToTokens(ethFee, nominal),
- ethBulkFee: convertToTokens(ethBulkFee, nominal),
- evmProxyContractFee: convertToTokens(proxyContractFee, nominal),
- evmProxyContractBulkFee: convertToTokens(proxyContractBulkFee, nominal),
- };
-}
-
-async function calculateFeeNftMintWithProperties(
- helper: EthUniqueHelper,
- privateKey: (seed: string) => Promise<IKeyringPair>,
- payer: ICrossAccountId,
- ethSigner: string,
- proxyContractAddress: string,
- calculatedCall: (collection: UniqueNFTCollection) => Promise<any>,
-): Promise<bigint> {
- const collection = await createCollectionForBenchmarks(
- helper,
- privateKey,
- ethSigner,
- proxyContractAddress,
- PERMISSIONS,
- );
- return helper.arrange.calculcateFee(payer, async () => {
- await calculatedCall(collection);
- });
-}
-function convertToTokens(value: bigint, nominal: bigint): number {
- return Number((value * 1000n) / nominal) / 1000;
-}
tests/src/benchmarks/mintFeeBench/proxyContract.soldiffbeforeafterboth--- a/tests/src/benchmarks/mintFeeBench/proxyContract.sol
+++ /dev/null
@@ -1,129 +0,0 @@
-// SPDX-License-Identifier: Apache License
-pragma solidity >=0.8.0;
-import {CollectionHelpers} from "../../eth/api/CollectionHelpers.sol";
-import {ContractHelpers} from "../../eth/api/ContractHelpers.sol";
-import {UniqueRefungibleToken} from "../../eth/api/UniqueRefungibleToken.sol";
-import {UniqueRefungible, Collection, EthCrossAccount as RftCrossAccountId, Tuple20 as RftProperties} from "../../eth/api/UniqueRefungible.sol";
-import {UniqueNFT, EthCrossAccount as NftCrossAccountId, Tuple21 as NftProperty, TokenProperties} from "../../eth/api/UniqueNFT.sol";
-
-struct Property {
- string key;
- bytes value;
-}
-
-contract ProxyMint {
- bytes32 constant REFUNGIBLE_COLLECTION_TYPE = keccak256(bytes("ReFungible"));
- bytes32 constant NONFUNGIBLE_COLLECTION_TYPE = keccak256(bytes("NFT"));
-
- modifier checkRestrictions(address _collection) {
- Collection commonContract = Collection(_collection);
- require(commonContract.isOwnerOrAdmin(msg.sender), "Only collection admin/owner can call this method");
- _;
- }
-
- /// @dev This emits when a mint to a substrate address has been made.
- event MintToSub(address _toEth, uint256 _toSub, address _collection, uint256 _tokenId);
-
- function mintToSubstrate(address _collection, uint256 _substrateReceiver) external checkRestrictions(_collection) {
- Collection commonContract = Collection(_collection);
- bytes32 collectionType = keccak256(bytes(commonContract.uniqueCollectionType()));
- uint256 tokenId;
-
- if (collectionType == REFUNGIBLE_COLLECTION_TYPE) {
- UniqueRefungible rftCollection = UniqueRefungible(_collection);
-
- tokenId = rftCollection.mint(address(this));
-
- rftCollection.transferFromCross(
- RftCrossAccountId(address(this), 0),
- RftCrossAccountId(address(0), _substrateReceiver),
- tokenId
- );
- } else if (collectionType == NONFUNGIBLE_COLLECTION_TYPE) {
- UniqueNFT nftCollection = UniqueNFT(_collection);
- tokenId = nftCollection.mint(address(this));
-
- nftCollection.transferFromCross(
- NftCrossAccountId(address(this), 0),
- NftCrossAccountId(address(0), _substrateReceiver),
- tokenId
- );
- } else {
- revert("Wrong collection type. Works only with NFT or RFT");
- }
-
- emit MintToSub(address(0), _substrateReceiver, _collection, tokenId);
- }
-
- function mintToSubstrateWithProperty(
- address _collection,
- uint256 _substrateReceiver,
- Property[] calldata properties
- ) external checkRestrictions(_collection) {
- uint256 propertiesLength = properties.length;
- require(propertiesLength > 0, "Properies is empty");
-
- Collection commonContract = Collection(_collection);
- bytes32 collectionType = keccak256(bytes(commonContract.uniqueCollectionType()));
- uint256 tokenId;
-
- if (collectionType == REFUNGIBLE_COLLECTION_TYPE) {
- UniqueRefungible rftCollection = UniqueRefungible(_collection);
- tokenId = rftCollection.nextTokenId();
- rftCollection.mint(address(this));
- for (uint256 i = 0; i < propertiesLength; ++i) {
- rftCollection.setProperty(tokenId, properties[i].key, properties[i].value);
- }
- rftCollection.transferFromCross(
- RftCrossAccountId(address(this), 0),
- RftCrossAccountId(address(0), _substrateReceiver),
- tokenId
- );
- } else if (collectionType == NONFUNGIBLE_COLLECTION_TYPE) {
- UniqueNFT nftCollection = UniqueNFT(_collection);
- tokenId = nftCollection.mint(address(this));
- for (uint256 i = 0; i < propertiesLength; ++i) {
- nftCollection.setProperty(tokenId, properties[i].key, properties[i].value);
- }
- nftCollection.transferFromCross(
- NftCrossAccountId(address(this), 0),
- NftCrossAccountId(address(0), _substrateReceiver),
- tokenId
- );
- } else {
- revert("Wrong collection type. Works only with NFT or RFT");
- }
-
- emit MintToSub(address(0), _substrateReceiver, _collection, tokenId);
- }
-
- function mintToSubstrateBulkProperty(
- address _collection,
- uint256 _substrateReceiver,
- NftProperty[] calldata _properties
- ) external checkRestrictions(_collection) {
- uint256 propertiesLength = _properties.length;
- require(propertiesLength > 0, "Properies is empty");
-
- Collection commonContract = Collection(_collection);
- bytes32 collectionType = keccak256(bytes(commonContract.uniqueCollectionType()));
- uint256 tokenId;
-
- if (collectionType == REFUNGIBLE_COLLECTION_TYPE) {} else if (collectionType == NONFUNGIBLE_COLLECTION_TYPE) {
- UniqueNFT nftCollection = UniqueNFT(_collection);
- tokenId = nftCollection.mint(address(this));
-
- nftCollection.setProperties(tokenId, _properties);
-
- nftCollection.transferFromCross(
- NftCrossAccountId(address(this), 0),
- NftCrossAccountId(address(0), _substrateReceiver),
- tokenId
- );
- } else {
- revert("Wrong collection type. Works only with NFT or RFT");
- }
-
- emit MintToSub(address(0), _substrateReceiver, _collection, tokenId);
- }
-}