Ethereum-Sign-Transactions

touqeer shah
Coinmonks
Published in
5 min readMar 28, 2023

--

This is a small application with ERC712 NFT to test what happens if we mint a single NFT and Redeem Multiple NFT in one function call how it is going to affect the cost of the transaction does if effect any significant price change.

Smart Contract

To test this scenario I write a small smart contract with openzeppelin ERC721 smart contract in which we have two main functions on which we are doing testing.

  1. createSingleNFT (which takes one voucher signed by the user and Mint one NFT in one Transaction.

2. redeem (which take an array of voucher and mint multiple NFTs in single transactions)

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

import "@openzeppelin/contracts/utils/Counters.sol";
import "./../interfaces/IUserIdentityNFT.sol";
import "./../libraries/UserIdentityNFT.sol";

contract UserIdentityNFT is ERC721URIStorage, ReentrancyGuard, EIP712, IUserIdentityNFT {
using Counters for Counters.Counter;
Counters.Counter private idCount;
address private figureprintOracle;
bytes32 public constant CLAME_USERID_VOUCHER =
keccak256("createUserId(string uri,bytes userId,bytes fingerPrint)");

// "NFTVoucher(uint256 tokenId,string uri,address currency,uint256 minPrice,bool isFixedPrice)"

constructor(
string memory name,
string memory symbol,
string memory signingDomain,
string memory signatureVersion
) ERC721(name, symbol) EIP712(signingDomain, signatureVersion) {}

// The functions below are overrides required by Solidity.
function createSingleNFT(UserIdVoucher calldata voucher) public nonReentrant {
address signer = verifySignature(voucher);
idCount.increment();
uint256 tokenId = idCount.current();
_mint(signer, tokenId);
_setTokenURI(tokenId, voucher.uri);
emit MintNft(signer, tokenId);
}

function _afterTokenTransfer(
address from,
address to,
uint256 firstTokenId,
uint256 batchSize
) internal override(ERC721) {
super._afterTokenTransfer(from, to, firstTokenId, batchSize);
}

/// @notice Redeems an NFTVoucher for an actual NFT, creating it in the process.

function redeem(UserIdVoucher[] calldata voucher) public nonReentrant {
for (uint32 i = 0; i < voucher.length; i++) {
address signer = verifySignature(voucher[i]);
idCount.increment();
uint256 tokenId = idCount.current();
_mint(msg.sender, tokenId);
_setTokenURI(tokenId, voucher[i].uri);
emit MintNft(signer, tokenId);
}
emit Redeem(msg.sender);
}

function _hash(UserIdVoucher calldata voucher) internal view returns (bytes32) {
return
_hashTypedDataV4(
keccak256(
abi.encode(
CLAME_USERID_VOUCHER,
keccak256(bytes(voucher.uri)),
keccak256(voucher.userId),
keccak256(voucher.fingerPrint)
)
)
);
}

/// @notice Verifies the signature for a given NFTVoucher, returning the address of the signer.
/// @dev Will revert if the signature is invalid. Does not verify that the signer is authorized to mint NFTs.
/// @param voucher An NFTVoucher describing an unminted NFT.
function verifySignature(UserIdVoucher calldata voucher) public view returns (address) {
bytes32 digest = _hash(voucher);
return ECDSA.recover(digest, voucher.signature);
}

function tokenURI(
uint256 tokenId
) public view override(ERC721URIStorage) returns (string memory) {
return super.tokenURI(tokenId);
}

function getIdCount() public view returns (uint256) {
return idCount.current();
}
}

Test Report

This simple test compares the price difference when creating a single NFT and multiple NFTs in one transaction.

Following Public Chain, we will use it for testing.

1. Ethereum Goerli Test Network

2. Polygon Test Network.

Tool :

1. Hardhat Gad Report (it is a well-known tool for testing transactions gas and generating a report).

2. Coin Market Cap API to get the current Price of the Token in the market to estimate Gas

Ethereum and Polygon Price:

First, compare with Ethereum and Polygon Price Differences following are the result of the Test.

Following are the result of we deployed and used Ethereum Network for our NFT Platform. There is a comparison of two function calls on a smart contract.

1. Create a Single NFT it will generate a single NFT of the User in one transaction.

The cost of one transaction is an average 4.10$ which means 10 Transactions will cost 41$ on Average (It will change based on the Ethereum price)

2. Redeem it will take an array of transaction Sign data and process all transactions in one single transaction to save the transaction fee.

The average cost of one transaction which processes 10 NFT creation Transactions will be 33.47$.

If we do simple math 41–33.47 = 7.53 dollars will be saved per Transaction 7.53$ save gas money.

Second, we compare polygon with some smart contracts; the results are below.

1. Create a Single NFT it will generate a single NFT of the User in one transaction.

The cost of one transaction is an average 0.07$ which means 10 Transactions will cost 0.7$ on Average (It will be changed based on the polygon price)

2. Redeem will take an array of transaction Sign data and process all transactions in one single transaction to save transaction fees.

The average cost of one transaction which processes 10 NFT creation Transactions will be 0.56$.

If we check the difference the 0.7–0.56 =0.14$ gas will save in the case.

Compare Price Difference:

Compare Polygon:

Here we test how much gas will consume on a different number of transactions on those functions.

Then Max transaction allows in one function call is 200 more than that will increase gas and not make sense.

Let us support that if we issue 1000 NFT per month, the total cost will be 3.28$ on average for 200 NFT Creations.

Total Price for 1000NFT = 3.28 * 5 = 16.4$

When we try with 40 Transactions in one call.

1. Create a single NFT have a constant price of 0.02 $.

2. Redeem will be 0.54$ to process 40 transactions at once.

When we try with 80 Transactions in one call.

1. Create a single NFT with a constant price of 0.02 $. (TOTAL cost 1.6$)

2. Redeem will be 1.08$ to process 80 transactions at once.

Result:

The final results are shocking when we increase the number of transactions and how the price difference is increased but after 200 Transactions in one call, I found that difference is not that much impressive.

We can use this approach in more real-time businesses like supply-chain etc. where we collect the transaction as signatures and push them in one transaction to Blockchain which makes Blockchain adoption more easily in real Business apart from only being used in Crypto and Finance.

Results are based on the current rate of the market so maybe when you run it may be some different results.

Linkdin: https://www.linkedin.com/in/touqeer-shah/

Source Code: https://github.com/touqeerShah/Ethereum-Sign-Transactions

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--