From 9dfb3ef3792ff27d3b835fe2fce5a225c130da64 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Thu, 30 Mar 2023 19:28:20 -0300 Subject: [PATCH 01/15] set up hardhat gas reporter and get gas cost benchmark --- gas-report.txt | 27 +++++++++++++++++++++++++++ hardhat.config.ts | 10 +++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 gas-report.txt diff --git a/gas-report.txt b/gas-report.txt new file mode 100644 index 0000000..f0254ef --- /dev/null +++ b/gas-report.txt @@ -0,0 +1,27 @@ +·-----------------------------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· +| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ +······································································································|···························|··············|······························ +| Methods · 30 gwei/gas · 1790.71 usd/eth │ +·········································································|····························|·············|·············|··············|···············|·············· +| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithERC20Tokens · 61264 · 61276 · 61268 · 3 · 3.29 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithNativeTokens · - · - · 41388 · 3 · 2.22 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · setBot · - · - · 33895 · 1 · 1.82 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawERC20 · - · - · 45658 · 2 · 2.45 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawNative · 35533 · 35605 · 35547 · 5 · 1.91 │ +·········································································|····························|·············|·············|··············|···············|·············· +| ERC20Sample · mint · 68200 · 68212 · 68211 · 24 · 3.66 │ +·········································································|····························|·············|·············|··············|···············|·············· +| ERC20Sample · transfer · 51366 · 51378 · 51369 · 7 · 2.76 │ +·········································································|····························|·············|·············|··············|···············|·············· +| Deployments · · % of limit · │ +······································································································|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · - · - · 1481683 · 4.9 % · 79.60 │ +······································································································|·············|·············|··············|···············|·············· +| ERC20Sample · - · - · 669119 · 2.2 % · 35.95 │ +·-----------------------------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file diff --git a/hardhat.config.ts b/hardhat.config.ts index 7ac7778..f73ad35 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -18,6 +18,7 @@ import "./tasks/v-testnet-launch/deploy-grtPool"; import "./tasks/v-testnet-launch/deploy-grtLiquidityWallet"; import "./tasks/v-testnet-launch/update-grtLiquidityWallet"; import "./tasks/v-testnet-launch/update-grtPool"; +require("hardhat-gas-reporter") let protocolVersion = "-testnet-launch"; @@ -161,7 +162,14 @@ const config: HardhatUserConfig = { spacing: 2, format: "json", }, - + gasReporter: { + outputFile: "gas-report.txt", + enabled: process.env.REPORT_GAS !== undefined, + currency: "USD", + noColors: true, + coinmarketcap: process.env.COIN_MARKETCAP_API_KEY || "", + token: "ETH" + } // deterministicDeployment: () => { // return { // factory: contractAddress, From 4fe8c0cf0fb6e5d4837f5af5664fb05a50ef1dcc Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Fri, 31 Mar 2023 12:45:32 -0300 Subject: [PATCH 02/15] grt liquidity wallet gas optimization --- abis/GrtLiquidityWallet.json | 15 ++++++++ .../v-testnet-launch/GrtLiquidityWallet.sol | 35 +++++++++---------- gas-report-first.txt | 27 ++++++++++++++ gas-report.txt | 20 +++++------ test/v-testnet-launch/GrtLiquidityWallet.ts | 6 ++-- 5 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 gas-report-first.txt diff --git a/abis/GrtLiquidityWallet.json b/abis/GrtLiquidityWallet.json index 009d180..aa8cc28 100644 --- a/abis/GrtLiquidityWallet.json +++ b/abis/GrtLiquidityWallet.json @@ -1,4 +1,19 @@ [ + { + "inputs": [], + "name": "FailedToSendNativeTokens", + "type": "error" + }, + { + "inputs": [], + "name": "InsufficientBalance", + "type": "error" + }, + { + "inputs": [], + "name": "NotAllowedToPayTheOffer", + "type": "error" + }, { "anonymous": false, "inputs": [ diff --git a/contracts/v-testnet-launch/GrtLiquidityWallet.sol b/contracts/v-testnet-launch/GrtLiquidityWallet.sol index b059d64..819e96e 100644 --- a/contracts/v-testnet-launch/GrtLiquidityWallet.sol +++ b/contracts/v-testnet-launch/GrtLiquidityWallet.sol @@ -38,26 +38,28 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { return IERC20(token).safeTransfer(msg.sender, amount); } + error InsufficientBalance(); + error FailedToSendNativeTokens(); + function withdrawNative(uint256 amount) external onlyOwner returns (bool) { - require( - address(this).balance >= amount, - "Grindery wallet: insufficient balance." - ); + if(address(this).balance < amount) + revert InsufficientBalance(); (bool sent, ) = msg.sender.call{value: amount}(""); - require(sent, "Grindery wallet: failed to send native tokens."); + if(!sent) + revert FailedToSendNativeTokens(); return sent; } + error NotAllowedToPayTheOffer(); + function payOfferWithERC20Tokens( bytes32 offerId, address token, address to, uint256 amount ) external returns (bool) { - require( - msg.sender == owner() || msg.sender == _bot, - "Grindery wallet: not allowed to pay the offer." - ); + if(msg.sender != owner() || msg.sender == _bot) + revert NotAllowedToPayTheOffer(); IERC20(token).safeTransfer(to, amount); emit LogOfferPaid(offerId, token, to, amount); return true; @@ -68,16 +70,13 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { address to, uint256 amount ) external returns (bool) { - require( - msg.sender == owner() || msg.sender == _bot, - "Grindery wallet: not allowed to pay the offer." - ); - require( - address(this).balance >= amount, - "Grindery wallet: insufficient balance." - ); + if(msg.sender != owner() || msg.sender == _bot) + revert NotAllowedToPayTheOffer(); + if(address(this).balance > amount) + revert InsufficientBalance(); (bool sent, ) = to.call{value: amount}(""); - require(sent, "Grindery wallet: failed to send native tokens."); + if(!sent) + revert FailedToSendNativeTokens(); emit LogOfferPaid(offerId, address(0), to, amount); return true; } diff --git a/gas-report-first.txt b/gas-report-first.txt new file mode 100644 index 0000000..f0254ef --- /dev/null +++ b/gas-report-first.txt @@ -0,0 +1,27 @@ +·-----------------------------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· +| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ +······································································································|···························|··············|······························ +| Methods · 30 gwei/gas · 1790.71 usd/eth │ +·········································································|····························|·············|·············|··············|···············|·············· +| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithERC20Tokens · 61264 · 61276 · 61268 · 3 · 3.29 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithNativeTokens · - · - · 41388 · 3 · 2.22 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · setBot · - · - · 33895 · 1 · 1.82 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawERC20 · - · - · 45658 · 2 · 2.45 │ +·········································································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawNative · 35533 · 35605 · 35547 · 5 · 1.91 │ +·········································································|····························|·············|·············|··············|···············|·············· +| ERC20Sample · mint · 68200 · 68212 · 68211 · 24 · 3.66 │ +·········································································|····························|·············|·············|··············|···············|·············· +| ERC20Sample · transfer · 51366 · 51378 · 51369 · 7 · 2.76 │ +·········································································|····························|·············|·············|··············|···············|·············· +| Deployments · · % of limit · │ +······································································································|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · - · - · 1481683 · 4.9 % · 79.60 │ +······································································································|·············|·············|··············|···············|·············· +| ERC20Sample · - · - · 669119 · 2.2 % · 35.95 │ +·-----------------------------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file diff --git a/gas-report.txt b/gas-report.txt index f0254ef..e76da8e 100644 --- a/gas-report.txt +++ b/gas-report.txt @@ -1,27 +1,27 @@ ·-----------------------------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· | Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ ······································································································|···························|··············|······························ -| Methods · 30 gwei/gas · 1790.71 usd/eth │ +| Methods · 27 gwei/gas · 1793.31 usd/eth │ ·········································································|····························|·············|·············|··············|···············|·············· | Contract · Method · Min · Max · Avg · # calls · usd (avg) │ ·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithERC20Tokens · 61264 · 61276 · 61268 · 3 · 3.29 │ +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithERC20Tokens · 63398 · 63410 · 63402 · 3 · 3.07 │ ·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithNativeTokens · - · - · 41388 · 3 · 2.22 │ +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithNativeTokens · - · - · 43522 · 3 · 2.11 │ ·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · setBot · - · - · 33895 · 1 · 1.82 │ +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · setBot · - · - · 33895 · 1 · 1.64 │ ·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawERC20 · - · - · 45658 · 2 · 2.45 │ +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawERC20 · - · - · 45658 · 2 · 2.21 │ ·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawNative · 35533 · 35605 · 35547 · 5 · 1.91 │ +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawNative · 35533 · 35605 · 35547 · 5 · 1.72 │ ·········································································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 68200 · 68212 · 68211 · 24 · 3.66 │ +| ERC20Sample · mint · 68200 · 68212 · 68211 · 24 · 3.30 │ ·········································································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · transfer · 51366 · 51378 · 51369 · 7 · 2.76 │ +| ERC20Sample · transfer · 51366 · 51378 · 51369 · 7 · 2.49 │ ·········································································|····························|·············|·············|··············|···············|·············· | Deployments · · % of limit · │ ······································································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · - · - · 1481683 · 4.9 % · 79.60 │ +| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · - · - · 1383152 · 4.6 % · 66.97 │ ······································································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 35.95 │ +| ERC20Sample · - · - · 669119 · 2.2 % · 32.40 │ ·-----------------------------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file diff --git a/test/v-testnet-launch/GrtLiquidityWallet.ts b/test/v-testnet-launch/GrtLiquidityWallet.ts index 3d6d348..1a8b9fa 100644 --- a/test/v-testnet-launch/GrtLiquidityWallet.ts +++ b/test/v-testnet-launch/GrtLiquidityWallet.ts @@ -228,7 +228,7 @@ describe("Grindery Liquidity Wallet", () => { grtLiquidityWallet .connect(owner) .withdrawNative(ethers.utils.parseEther("200")) - ).to.be.revertedWith("Grindery wallet: insufficient balance."); + ).to.be.revertedWithCustomError(grtLiquidityWallet, "InsufficientBalance"); }); }); @@ -243,7 +243,7 @@ describe("Grindery Liquidity Wallet", () => { owner.address, 100 ) - ).to.be.revertedWith("Grindery wallet: not allowed to pay the offer."); + ).to.be.revertedWithCustomError(grtLiquidityWallet, "NotAllowedToPayTheOffer"); }); it("Should transfer ERC20 funds to user", async () => { @@ -281,7 +281,7 @@ describe("Grindery Liquidity Wallet", () => { grtLiquidityWallet .connect(user2) .payOfferWithNativeTokens(offerId, owner.address, 100) - ).to.be.revertedWith("Grindery wallet: not allowed to pay the offer."); + ).to.be.revertedWithCustomError(grtLiquidityWallet, "NotAllowedToPayTheOffer"); }); it("Should transfer Native funds to user", async () => { From 0656d67699a8f0668c4be934c6d93462b8ebd3d0 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Fri, 31 Mar 2023 12:53:03 -0300 Subject: [PATCH 03/15] gas report liquidity pool before optimization --- gas-report.txt | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/gas-report.txt b/gas-report.txt index e76da8e..eebe87b 100644 --- a/gas-report.txt +++ b/gas-report.txt @@ -1,27 +1,23 @@ -·-----------------------------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· -| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ -······································································································|···························|··············|······························ -| Methods · 27 gwei/gas · 1793.31 usd/eth │ -·········································································|····························|·············|·············|··············|···············|·············· -| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithERC20Tokens · 63398 · 63410 · 63402 · 3 · 3.07 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithNativeTokens · - · - · 43522 · 3 · 2.11 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · setBot · - · - · 33895 · 1 · 1.64 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawERC20 · - · - · 45658 · 2 · 2.21 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawNative · 35533 · 35605 · 35547 · 5 · 1.72 │ -·········································································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 68200 · 68212 · 68211 · 24 · 3.30 │ -·········································································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · transfer · 51366 · 51378 · 51369 · 7 · 2.49 │ -·········································································|····························|·············|·············|··············|···············|·············· -| Deployments · · % of limit · │ -······································································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · - · - · 1383152 · 4.6 % · 66.97 │ -······································································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 32.40 │ -·-----------------------------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file +·---------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· +| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ +··················································································|···························|··············|······························ +| Methods · 37 gwei/gas · 1828.93 usd/eth │ +·····················································|····························|·············|·············|··············|···············|·············· +| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ +·····················································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33808 · 3 · 2.29 │ +·····················································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165769 · 165781 · 165779 · 13 · 11.22 │ +·····················································|····························|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtPool.sol:GrtPool · depositETHAndAcceptOffer · 157933 · 175033 · 173806 · 14 · 11.76 │ +·····················································|····························|·············|·············|··············|···············|·············· +| ERC20Sample · approve · 46223 · 46235 · 46234 · 26 · 3.13 │ +·····················································|····························|·············|·············|··············|···············|·············· +| ERC20Sample · mint · 51100 · 68212 · 59656 · 26 · 4.04 │ +·····················································|····························|·············|·············|··············|···············|·············· +| Deployments · · % of limit · │ +··················································································|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1938633 · 6.5 % · 131.19 │ +··················································································|·············|·············|··············|···············|·············· +| ERC20Sample · - · - · 669119 · 2.2 % · 45.28 │ +·---------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file From 40a079c36d54c4677efebf671e3fc7dbc4566b8f Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Fri, 31 Mar 2023 14:01:20 -0300 Subject: [PATCH 04/15] gas optimization grt pool --- abis/GrtPool.json | 20 ++++++++++++++ contracts/v-testnet-launch/GrtPool.sol | 37 ++++++++++---------------- gas-report.txt | 16 +++++------ test/v-testnet-launch/GrtPool.ts | 11 ++++---- 4 files changed, 47 insertions(+), 37 deletions(-) diff --git a/abis/GrtPool.json b/abis/GrtPool.json index f2fc7b6..356336d 100644 --- a/abis/GrtPool.json +++ b/abis/GrtPool.json @@ -1,4 +1,24 @@ [ + { + "inputs": [], + "name": "FailedToSendNativeTokens", + "type": "error" + }, + { + "inputs": [], + "name": "OfferInactive", + "type": "error" + }, + { + "inputs": [], + "name": "TransferedAmountMustBePositive", + "type": "error" + }, + { + "inputs": [], + "name": "ZeroAddressIsNotAllowed", + "type": "error" + }, { "anonymous": false, "inputs": [ diff --git a/contracts/v-testnet-launch/GrtPool.sol b/contracts/v-testnet-launch/GrtPool.sol index e71eec9..c20bbba 100644 --- a/contracts/v-testnet-launch/GrtPool.sol +++ b/contracts/v-testnet-launch/GrtPool.sol @@ -36,34 +36,31 @@ contract GrtPool is OwnableUpgradeable, GrtOffer, UUPSUpgradeable { function _authorizeUpgrade(address) internal override onlyOwner onlyProxy {} - receive() external payable {} + error TransferedAmountMustBePositive(); + error ZeroAddressIsNotAllowed(); + error OfferInactive(); + error FailedToSendNativeTokens(); function depositETHAndAcceptOffer( bytes32 offerId, address destAddr ) external payable returns (bytes32) { - require( - msg.value > 0, - "Grindery Pool: transfered amount must be positive." - ); - require( - destAddr != address(0), - "Grindery Pool: zero address as destination address is not allowed." - ); - require( - _offers[offerId].isActive, - "Grindery Pool: the offer is inactive." - ); - + if(msg.value <= 0) + revert TransferedAmountMustBePositive(); + if(destAddr == address(0)) + revert ZeroAddressIsNotAllowed(); + if(!_offers[offerId].isActive) + revert OfferInactive(); (bool sent, ) = address(this).call{value: msg.value}(""); - require(sent, "Grindery Pool: failed to send native tokens."); + if(!sent) + revert FailedToSendNativeTokens(); bytes32 tradeId = keccak256( abi.encodePacked(msg.sender, _noncesDeposit[msg.sender]) ); Trade storage trade = _trades[tradeId]; trade.userAddr = msg.sender; trade.destAddr = destAddr; - trade.deposit = setTokenInfo(address(0), msg.value, block.chainid); + trade.deposit = TokenInfo(address(0), msg.value, block.chainid); trade.offerId = offerId; _noncesDeposit[msg.sender]++; emit LogTrade(tradeId, address(0), msg.value, offerId); @@ -100,11 +97,5 @@ contract GrtPool is OwnableUpgradeable, GrtOffer, UUPSUpgradeable { return _trades[tradeId].deposit.chainId; } - function setTokenInfo( - address token, - uint256 amount, - uint256 chainId - ) internal pure returns (TokenInfo memory) { - return TokenInfo(token, amount, chainId); - } + receive() external payable {} } diff --git a/gas-report.txt b/gas-report.txt index eebe87b..b756b74 100644 --- a/gas-report.txt +++ b/gas-report.txt @@ -1,23 +1,23 @@ ·---------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· | Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ ··················································································|···························|··············|······························ -| Methods · 37 gwei/gas · 1828.93 usd/eth │ +| Methods · 33 gwei/gas · 1829.70 usd/eth │ ·····················································|····························|·············|·············|··············|···············|·············· | Contract · Method · Min · Max · Avg · # calls · usd (avg) │ ·····················································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33808 · 3 · 2.29 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33808 · 3 · 2.04 │ ·····················································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165769 · 165781 · 165779 · 13 · 11.22 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165769 · 165781 · 165779 · 13 · 10.01 │ ·····················································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtPool.sol:GrtPool · depositETHAndAcceptOffer · 157933 · 175033 · 173806 · 14 · 11.76 │ +| contracts/v-testnet-launch/GrtPool.sol:GrtPool · depositETHAndAcceptOffer · 157693 · 174793 · 173566 · 14 · 10.48 │ ·····················································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · approve · 46223 · 46235 · 46234 · 26 · 3.13 │ +| ERC20Sample · approve · 46223 · 46235 · 46234 · 26 · 2.79 │ ·····················································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 51100 · 68212 · 59656 · 26 · 4.04 │ +| ERC20Sample · mint · 51100 · 68212 · 59656 · 26 · 3.60 │ ·····················································|····························|·············|·············|··············|···············|·············· | Deployments · · % of limit · │ ··················································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1938633 · 6.5 % · 131.19 │ +| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1855389 · 6.2 % · 112.03 │ ··················································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 45.28 │ +| ERC20Sample · - · - · 669119 · 2.2 % · 40.40 │ ·---------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file diff --git a/test/v-testnet-launch/GrtPool.ts b/test/v-testnet-launch/GrtPool.ts index 6c0c4ce..197a53a 100644 --- a/test/v-testnet-launch/GrtPool.ts +++ b/test/v-testnet-launch/GrtPool.ts @@ -76,9 +76,7 @@ describe("Grindery Offer testings", function () { .depositETHAndAcceptOffer(offerId, user3.address, { value: 0, }) - ).to.be.revertedWith( - "Grindery Pool: transfered amount must be positive." - ); + ).to.be.revertedWithCustomError(grtPool,"TransferedAmountMustBePositive"); }); it("Should fail if the offer is inactive", async function () { @@ -89,7 +87,7 @@ describe("Grindery Offer testings", function () { .depositETHAndAcceptOffer(offerId, user3.address, { value: 100, }) - ).to.be.revertedWith("Grindery Pool: the offer is inactive."); + ).to.be.revertedWithCustomError(grtPool, "OfferInactive"); }); it("Should fail if the destination address is the zero address", async function () { @@ -100,8 +98,9 @@ describe("Grindery Offer testings", function () { .depositETHAndAcceptOffer(offerId, ethers.constants.AddressZero, { value: 100, }) - ).to.be.revertedWith( - "Grindery Pool: zero address as destination address is not allowed." + ).to.be.revertedWithCustomError( + grtPool, + "ZeroAddressIsNotAllowed" ); }); From 41da6b61f29e6f8ae4d70dd29770a2012286c94e Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Fri, 31 Mar 2023 14:05:50 -0300 Subject: [PATCH 05/15] remove file --- gas-report-first.txt | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 gas-report-first.txt diff --git a/gas-report-first.txt b/gas-report-first.txt deleted file mode 100644 index f0254ef..0000000 --- a/gas-report-first.txt +++ /dev/null @@ -1,27 +0,0 @@ -·-----------------------------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· -| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ -······································································································|···························|··············|······························ -| Methods · 30 gwei/gas · 1790.71 usd/eth │ -·········································································|····························|·············|·············|··············|···············|·············· -| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithERC20Tokens · 61264 · 61276 · 61268 · 3 · 3.29 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · payOfferWithNativeTokens · - · - · 41388 · 3 · 2.22 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · setBot · - · - · 33895 · 1 · 1.82 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawERC20 · - · - · 45658 · 2 · 2.45 │ -·········································································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · withdrawNative · 35533 · 35605 · 35547 · 5 · 1.91 │ -·········································································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 68200 · 68212 · 68211 · 24 · 3.66 │ -·········································································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · transfer · 51366 · 51378 · 51369 · 7 · 2.76 │ -·········································································|····························|·············|·············|··············|···············|·············· -| Deployments · · % of limit · │ -······································································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtLiquidityWallet.sol:GrtLiquidityWallet · - · - · 1481683 · 4.9 % · 79.60 │ -······································································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 35.95 │ -·-----------------------------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file From 47c121b9546a6561dfe11046ae95487f858d4c59 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Mon, 3 Apr 2023 09:44:16 -0300 Subject: [PATCH 06/15] gas optimization report grt offer before changes --- gas-report.txt | 52 ++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/gas-report.txt b/gas-report.txt index b756b74..9fdca2e 100644 --- a/gas-report.txt +++ b/gas-report.txt @@ -1,23 +1,29 @@ -·---------------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· -| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ -··················································································|···························|··············|······························ -| Methods · 33 gwei/gas · 1829.70 usd/eth │ -·····················································|····························|·············|·············|··············|···············|·············· -| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ -·····················································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33808 · 3 · 2.04 │ -·····················································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165769 · 165781 · 165779 · 13 · 10.01 │ -·····················································|····························|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtPool.sol:GrtPool · depositETHAndAcceptOffer · 157693 · 174793 · 173566 · 14 · 10.48 │ -·····················································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · approve · 46223 · 46235 · 46234 · 26 · 2.79 │ -·····················································|····························|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 51100 · 68212 · 59656 · 26 · 3.60 │ -·····················································|····························|·············|·············|··············|···············|·············· -| Deployments · · % of limit · │ -··················································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1855389 · 6.2 % · 112.03 │ -··················································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 40.40 │ -·---------------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file +·-------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· +| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ +··········································································|···························|··············|······························ +| Methods · 20 gwei/gas · 1803.02 usd/eth │ +·····················································|····················|·············|·············|··············|···············|·············· +| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ +·····················································|····················|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setChainIdOffer · - · - · 35787 · 3 · 1.29 │ +·····················································|····················|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33808 · 3 · 1.22 │ +·····················································|····················|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMaxPriceLimit · - · - · 37303 · 3 · 1.35 │ +·····················································|····················|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMinPriceLimit · - · - · 37344 · 3 · 1.35 │ +·····················································|····················|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165769 · 165781 · 165780 · 28 · 5.98 │ +·····················································|····················|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setTokenOffer · - · - · 36136 · 3 · 1.30 │ +·····················································|····················|·············|·············|··············|···············|·············· +| ERC20Sample · approve · 26323 · 46235 · 39127 · 42 · 1.41 │ +·····················································|····················|·············|·············|··············|···············|·············· +| ERC20Sample · mint · 34012 · 68212 · 55998 · 42 · 2.02 │ +·····················································|····················|·············|·············|··············|···············|·············· +| Deployments · · % of limit · │ +··········································································|·············|·············|··············|···············|·············· +| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1855389 · 6.2 % · 66.91 │ +··········································································|·············|·············|··············|···············|·············· +| ERC20Sample · - · - · 669119 · 2.2 % · 24.13 │ +·-------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file From fadaf1adcad8f1df5fc6079b7caf3b9c4a5870e2 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Mon, 3 Apr 2023 10:39:27 -0300 Subject: [PATCH 07/15] gas optimization grt offer --- abis/GrtPool.json | 10 ++++ contracts/v-testnet-launch/GrtOffer.sol | 67 ++++++++++++------------- test/v-testnet-launch/GrtOffer.ts | 22 +++----- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/abis/GrtPool.json b/abis/GrtPool.json index 356336d..b2fcfeb 100644 --- a/abis/GrtPool.json +++ b/abis/GrtPool.json @@ -4,6 +4,11 @@ "name": "FailedToSendNativeTokens", "type": "error" }, + { + "inputs": [], + "name": "NotAllowedToModifyOffer", + "type": "error" + }, { "inputs": [], "name": "OfferInactive", @@ -19,6 +24,11 @@ "name": "ZeroAddressIsNotAllowed", "type": "error" }, + { + "inputs": [], + "name": "ZeroAddressNotAllowed", + "type": "error" + }, { "anonymous": false, "inputs": [ diff --git a/contracts/v-testnet-launch/GrtOffer.sol b/contracts/v-testnet-launch/GrtOffer.sol index 3f0dbb2..c217581 100644 --- a/contracts/v-testnet-launch/GrtOffer.sol +++ b/contracts/v-testnet-launch/GrtOffer.sol @@ -25,21 +25,22 @@ contract GrtOffer is GrtOfferUtils { ); event LogSetStatusOffer(bytes32 indexed _idOffer, bool indexed _isActive); + error NotAllowedToModifyOffer(); + error ZeroAddressNotAllowed(); + function setChainIdOffer(bytes32 offerId, uint256 chainId) external { - require( - msg.sender == _offers[offerId].user, - "Grindery offer: you are not allowed to modify this offer." - ); - _offers[offerId].chainId = chainId; + Offer storage offer = _offers[offerId]; + if(msg.sender != offer.user) + revert NotAllowedToModifyOffer(); + offer.chainId = chainId; emit LogSetChainIdOffer(offerId, chainId); } function setTokenOffer(bytes32 offerId, address token) external { - require( - msg.sender == _offers[offerId].user, - "Grindery offer: you are not allowed to modify this offer." - ); - _offers[offerId].token = token; + Offer storage offer = _offers[offerId]; + if(msg.sender != offer.user) + revert NotAllowedToModifyOffer(); + offer.token = token; emit LogSetTokenOffer(offerId, token); } @@ -47,12 +48,11 @@ contract GrtOffer is GrtOfferUtils { bytes32 offerId, bytes calldata minPriceLimit ) external { - require( - msg.sender == _offers[offerId].user, - "Grindery offer: you are not allowed to modify this offer." - ); + Offer storage offer = _offers[offerId]; + if(msg.sender != offer.user) + revert NotAllowedToModifyOffer(); bytes32 priceLimit = keccak256(abi.encodePacked(minPriceLimit)); - _offers[offerId].minPriceLimit = priceLimit; + offer.minPriceLimit = priceLimit; emit LogSetMinPriceLimit(offerId, priceLimit); } @@ -60,21 +60,19 @@ contract GrtOffer is GrtOfferUtils { bytes32 offerId, bytes calldata maxPriceLimit ) external { - require( - msg.sender == _offers[offerId].user, - "Grindery offer: you are not allowed to modify this offer." - ); + Offer storage offer = _offers[offerId]; + if(msg.sender != offer.user) + revert NotAllowedToModifyOffer(); bytes32 priceLimit = keccak256(abi.encodePacked(maxPriceLimit)); - _offers[offerId].maxPriceLimit = priceLimit; + offer.maxPriceLimit = priceLimit; emit LogSetMaxPriceLimit(offerId, priceLimit); } function setIsActive(bytes32 offerId, bool isActive) external { - require( - msg.sender == _offers[offerId].user, - "Grindery offer: you are not allowed to modify this offer." - ); - _offers[offerId].isActive = isActive; + Offer storage offer = _offers[offerId]; + if(msg.sender != offer.user) + revert NotAllowedToModifyOffer(); + offer.isActive = isActive; emit LogSetStatusOffer(offerId, isActive); } @@ -84,21 +82,20 @@ contract GrtOffer is GrtOfferUtils { bytes calldata minPriceLimit, bytes calldata maxPriceLimit ) external returns (bytes32) { - require( - msg.sender != address(0), - "Grindery offer: setOffer from zero address is not allowed" - ); + if(msg.sender == address(0)) + revert ZeroAddressNotAllowed(); bytes32 offerId = keccak256( abi.encodePacked(msg.sender, _noncesOffer[msg.sender]) ); - _offers[offerId].user = msg.sender; - _offers[offerId].isActive = true; - _offers[offerId].chainId = chainId; - _offers[offerId].token = token; - _offers[offerId].minPriceLimit = keccak256( + Offer storage offer = _offers[offerId]; + offer.user = msg.sender; + offer.isActive = true; + offer.chainId = chainId; + offer.token = token; + offer.minPriceLimit = keccak256( abi.encodePacked(minPriceLimit) ); - _offers[offerId].maxPriceLimit = keccak256( + offer.maxPriceLimit = keccak256( abi.encodePacked(maxPriceLimit) ); emit LogNewOffer(offerId, token, chainId); diff --git a/test/v-testnet-launch/GrtOffer.ts b/test/v-testnet-launch/GrtOffer.ts index 125240e..f01bf84 100644 --- a/test/v-testnet-launch/GrtOffer.ts +++ b/test/v-testnet-launch/GrtOffer.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import { ethers, upgrades } from "hardhat"; import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"; -import { Contract } from "ethers"; +import { Contract, constants } from "ethers"; const protocolVersion = "v-testnet-launch"; @@ -213,8 +213,8 @@ describe("Grindery Offer testings", function () { it("Should fail if the sender is not the creator of the offer", async function () { await expect( grtOffer.connect(user2).setChainIdOffer(offerId, 34) - ).to.be.revertedWith( - "Grindery offer: you are not allowed to modify this offer." + ).to.be.revertedWithCustomError(grtOffer, + "NotAllowedToModifyOffer" ); }); @@ -236,9 +236,7 @@ describe("Grindery Offer testings", function () { it("Should fail if the sender is not the creator of the offer", async function () { await expect( grtOffer.connect(user2).setTokenOffer(offerId, token1.address) - ).to.be.revertedWith( - "Grindery offer: you are not allowed to modify this offer." - ); + ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") }); it("Should modify the token address", async function () { @@ -269,9 +267,7 @@ describe("Grindery Offer testings", function () { ["FIRA", "50"] ) ) - ).to.be.revertedWith( - "Grindery offer: you are not allowed to modify this offer." - ); + ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") }); it("Should modify the Min price limit options", async function () { @@ -341,9 +337,7 @@ describe("Grindery Offer testings", function () { ["FIRA", "2000"] ) ) - ).to.be.revertedWith( - "Grindery offer: you are not allowed to modify this offer." - ); + ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") }); it("Should modify the Max price limit options", async function () { @@ -405,9 +399,7 @@ describe("Grindery Offer testings", function () { it("Should fail if the sender is not the creator of the offer", async function () { await expect( grtOffer.connect(user2).setIsActive(offerId, false) - ).to.be.revertedWith( - "Grindery offer: you are not allowed to modify this offer." - ); + ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") }); it("Should modify the status", async function () { From 9aeda1552ac5c3cd2b831da2389921490f256560 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Mon, 3 Apr 2023 10:39:50 -0300 Subject: [PATCH 08/15] gas optimization report after grt offer changes --- gas-report.txt | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/gas-report.txt b/gas-report.txt index 9fdca2e..f136ec0 100644 --- a/gas-report.txt +++ b/gas-report.txt @@ -1,29 +1,29 @@ ·-------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· | Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ ··········································································|···························|··············|······························ -| Methods · 20 gwei/gas · 1803.02 usd/eth │ +| Methods · 39 gwei/gas · 1805.95 usd/eth │ ·····················································|····················|·············|·············|··············|···············|·············· | Contract · Method · Min · Max · Avg · # calls · usd (avg) │ ·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setChainIdOffer · - · - · 35787 · 3 · 1.29 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setChainIdOffer · - · - · 35729 · 3 · 2.52 │ ·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33808 · 3 · 1.22 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33735 · 3 · 2.38 │ ·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMaxPriceLimit · - · - · 37303 · 3 · 1.35 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMaxPriceLimit · - · - · 37245 · 3 · 2.62 │ ·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMinPriceLimit · - · - · 37344 · 3 · 1.35 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMinPriceLimit · - · - · 37286 · 3 · 2.63 │ ·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165769 · 165781 · 165780 · 28 · 5.98 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165618 · 165630 · 165629 · 28 · 11.67 │ ·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setTokenOffer · - · - · 36136 · 3 · 1.30 │ +| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setTokenOffer · - · - · 36069 · 3 · 2.54 │ ·····················································|····················|·············|·············|··············|···············|·············· -| ERC20Sample · approve · 26323 · 46235 · 39127 · 42 · 1.41 │ +| ERC20Sample · approve · 26323 · 46235 · 39127 · 42 · 2.76 │ ·····················································|····················|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 34012 · 68212 · 55998 · 42 · 2.02 │ +| ERC20Sample · mint · 34012 · 68212 · 55998 · 42 · 3.94 │ ·····················································|····················|·············|·············|··············|···············|·············· | Deployments · · % of limit · │ ··········································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1855389 · 6.2 % · 66.91 │ +| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1734279 · 5.8 % · 122.15 │ ··········································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 24.13 │ +| ERC20Sample · - · - · 669119 · 2.2 % · 47.13 │ ·-------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file From e0f34233f11afcabb1b1a3ae76ab25d45bac1230 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Tue, 4 Apr 2023 16:28:50 -0300 Subject: [PATCH 09/15] gas optimization liquidity wallet --- contracts/v-testnet-launch/GrtLiquidityWallet.sol | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/contracts/v-testnet-launch/GrtLiquidityWallet.sol b/contracts/v-testnet-launch/GrtLiquidityWallet.sol index 819e96e..31cd06e 100644 --- a/contracts/v-testnet-launch/GrtLiquidityWallet.sol +++ b/contracts/v-testnet-launch/GrtLiquidityWallet.sol @@ -58,7 +58,9 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { address to, uint256 amount ) external returns (bool) { - if(msg.sender != owner() || msg.sender == _bot) + if(msg.sender != owner()) + revert NotAllowedToPayTheOffer(); + if(msg.sender == _bot) revert NotAllowedToPayTheOffer(); IERC20(token).safeTransfer(to, amount); emit LogOfferPaid(offerId, token, to, amount); @@ -70,7 +72,9 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { address to, uint256 amount ) external returns (bool) { - if(msg.sender != owner() || msg.sender == _bot) + if(msg.sender != owner()) + revert NotAllowedToPayTheOffer(); + if(msg.sender == _bot) revert NotAllowedToPayTheOffer(); if(address(this).balance > amount) revert InsufficientBalance(); From 7e09d66d84d4b3ea3c67d4c73f453be00d5ecf5a Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Tue, 4 Apr 2023 16:29:23 -0300 Subject: [PATCH 10/15] gas reporter config --- gas-report.txt | 29 ----------------------------- hardhat.config.ts | 4 ++-- 2 files changed, 2 insertions(+), 31 deletions(-) delete mode 100644 gas-report.txt diff --git a/gas-report.txt b/gas-report.txt deleted file mode 100644 index f136ec0..0000000 --- a/gas-report.txt +++ /dev/null @@ -1,29 +0,0 @@ -·-------------------------------------------------------------------------|---------------------------|--------------|-----------------------------· -| Solc version: 0.8.17 · Optimizer enabled: true · Runs: 1000 · Block limit: 30000000 gas │ -··········································································|···························|··············|······························ -| Methods · 39 gwei/gas · 1805.95 usd/eth │ -·····················································|····················|·············|·············|··············|···············|·············· -| Contract · Method · Min · Max · Avg · # calls · usd (avg) │ -·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setChainIdOffer · - · - · 35729 · 3 · 2.52 │ -·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setIsActive · - · - · 33735 · 3 · 2.38 │ -·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMaxPriceLimit · - · - · 37245 · 3 · 2.62 │ -·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setMinPriceLimit · - · - · 37286 · 3 · 2.63 │ -·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setOffer · 165618 · 165630 · 165629 · 28 · 11.67 │ -·····················································|····················|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtOffer.sol:GrtOffer · setTokenOffer · - · - · 36069 · 3 · 2.54 │ -·····················································|····················|·············|·············|··············|···············|·············· -| ERC20Sample · approve · 26323 · 46235 · 39127 · 42 · 2.76 │ -·····················································|····················|·············|·············|··············|···············|·············· -| ERC20Sample · mint · 34012 · 68212 · 55998 · 42 · 3.94 │ -·····················································|····················|·············|·············|··············|···············|·············· -| Deployments · · % of limit · │ -··········································································|·············|·············|··············|···············|·············· -| contracts/v-testnet-launch/GrtPool.sol:GrtPool · - · - · 1734279 · 5.8 % · 122.15 │ -··········································································|·············|·············|··············|···············|·············· -| ERC20Sample · - · - · 669119 · 2.2 % · 47.13 │ -·-------------------------------------------------------------------------|-------------|-------------|--------------|---------------|-------------· \ No newline at end of file diff --git a/hardhat.config.ts b/hardhat.config.ts index f73ad35..05e7542 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -163,11 +163,11 @@ const config: HardhatUserConfig = { format: "json", }, gasReporter: { - outputFile: "gas-report.txt", + //outputFile: "gas-report.txt", enabled: process.env.REPORT_GAS !== undefined, currency: "USD", noColors: true, - coinmarketcap: process.env.COIN_MARKETCAP_API_KEY || "", + // coinmarketcap: process.env.COIN_MARKETCAP_API_KEY || "", token: "ETH" } // deterministicDeployment: () => { From 03b74b3b712c522b981bbf52f9b51e9ae88da05a Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Wed, 5 Apr 2023 11:09:16 -0300 Subject: [PATCH 11/15] use import statement instead of require --- hardhat.config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardhat.config.ts b/hardhat.config.ts index 05e7542..3d97acb 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -18,7 +18,7 @@ import "./tasks/v-testnet-launch/deploy-grtPool"; import "./tasks/v-testnet-launch/deploy-grtLiquidityWallet"; import "./tasks/v-testnet-launch/update-grtLiquidityWallet"; import "./tasks/v-testnet-launch/update-grtPool"; -require("hardhat-gas-reporter") +import "hardhat-gas-reporter"; let protocolVersion = "-testnet-launch"; From 2885f4b1e77c9753fa1b56ceaf4ee7a1af4f3c3b Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Mon, 17 Apr 2023 12:53:17 -0300 Subject: [PATCH 12/15] add modifier to check msg sender with offer user --- contracts/v-testnet-launch/GrtOffer.sol | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/contracts/v-testnet-launch/GrtOffer.sol b/contracts/v-testnet-launch/GrtOffer.sol index c217581..a06658a 100644 --- a/contracts/v-testnet-launch/GrtOffer.sol +++ b/contracts/v-testnet-launch/GrtOffer.sol @@ -28,18 +28,20 @@ contract GrtOffer is GrtOfferUtils { error NotAllowedToModifyOffer(); error ZeroAddressNotAllowed(); - function setChainIdOffer(bytes32 offerId, uint256 chainId) external { - Offer storage offer = _offers[offerId]; + modifier isOwner(Offer memory offer) { if(msg.sender != offer.user) revert NotAllowedToModifyOffer(); + _; + } + + function setChainIdOffer(bytes32 offerId, uint256 chainId) external isOwner(_offers[offerId]) { + Offer storage offer = _offers[offerId]; offer.chainId = chainId; emit LogSetChainIdOffer(offerId, chainId); } - function setTokenOffer(bytes32 offerId, address token) external { + function setTokenOffer(bytes32 offerId, address token) external isOwner(_offers[offerId]) { Offer storage offer = _offers[offerId]; - if(msg.sender != offer.user) - revert NotAllowedToModifyOffer(); offer.token = token; emit LogSetTokenOffer(offerId, token); } @@ -47,10 +49,8 @@ contract GrtOffer is GrtOfferUtils { function setMinPriceLimit( bytes32 offerId, bytes calldata minPriceLimit - ) external { + ) external isOwner(_offers[offerId]) { Offer storage offer = _offers[offerId]; - if(msg.sender != offer.user) - revert NotAllowedToModifyOffer(); bytes32 priceLimit = keccak256(abi.encodePacked(minPriceLimit)); offer.minPriceLimit = priceLimit; emit LogSetMinPriceLimit(offerId, priceLimit); @@ -59,19 +59,15 @@ contract GrtOffer is GrtOfferUtils { function setMaxPriceLimit( bytes32 offerId, bytes calldata maxPriceLimit - ) external { + ) external isOwner(_offers[offerId]) { Offer storage offer = _offers[offerId]; - if(msg.sender != offer.user) - revert NotAllowedToModifyOffer(); bytes32 priceLimit = keccak256(abi.encodePacked(maxPriceLimit)); offer.maxPriceLimit = priceLimit; emit LogSetMaxPriceLimit(offerId, priceLimit); } - function setIsActive(bytes32 offerId, bool isActive) external { + function setIsActive(bytes32 offerId, bool isActive) external isOwner(_offers[offerId]) { Offer storage offer = _offers[offerId]; - if(msg.sender != offer.user) - revert NotAllowedToModifyOffer(); offer.isActive = isActive; emit LogSetStatusOffer(offerId, isActive); } From a9f95d08960a4ff558a25b188feb8f8d29cb3fd2 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Mon, 17 Apr 2023 13:04:47 -0300 Subject: [PATCH 13/15] add validation for zero address --- contracts/v-testnet-launch/GrtOffer.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/v-testnet-launch/GrtOffer.sol b/contracts/v-testnet-launch/GrtOffer.sol index a06658a..6288415 100644 --- a/contracts/v-testnet-launch/GrtOffer.sol +++ b/contracts/v-testnet-launch/GrtOffer.sol @@ -41,6 +41,8 @@ contract GrtOffer is GrtOfferUtils { } function setTokenOffer(bytes32 offerId, address token) external isOwner(_offers[offerId]) { + if(token == address(0)) + revert ZeroAddressNotAllowed(); Offer storage offer = _offers[offerId]; offer.token = token; emit LogSetTokenOffer(offerId, token); From 9376d32ffc1c8fc44eae9b9472c63819e0ae6b59 Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Mon, 17 Apr 2023 13:09:00 -0300 Subject: [PATCH 14/15] move errors to top --- contracts/v-testnet-launch/GrtLiquidityWallet.sol | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/contracts/v-testnet-launch/GrtLiquidityWallet.sol b/contracts/v-testnet-launch/GrtLiquidityWallet.sol index 31cd06e..142506a 100644 --- a/contracts/v-testnet-launch/GrtLiquidityWallet.sol +++ b/contracts/v-testnet-launch/GrtLiquidityWallet.sol @@ -17,6 +17,10 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { uint256 _amount ); + error InsufficientBalance(); + error FailedToSendNativeTokens(); + error NotAllowedToPayTheOffer(); + function initialize(address bot) external initializer { __Ownable_init(); _bot = bot; @@ -38,9 +42,6 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { return IERC20(token).safeTransfer(msg.sender, amount); } - error InsufficientBalance(); - error FailedToSendNativeTokens(); - function withdrawNative(uint256 amount) external onlyOwner returns (bool) { if(address(this).balance < amount) revert InsufficientBalance(); @@ -50,8 +51,6 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { return sent; } - error NotAllowedToPayTheOffer(); - function payOfferWithERC20Tokens( bytes32 offerId, address token, From 6b9a6eda3d977cedf645a8a4fd0a513d76ec477b Mon Sep 17 00:00:00 2001 From: Mauricio Junior Date: Tue, 18 Apr 2023 20:18:42 -0300 Subject: [PATCH 15/15] refactor to use require statement --- .../v-testnet-launch/GrtLiquidityWallet.sol | 38 +++++++++---------- contracts/v-testnet-launch/GrtOffer.sol | 21 +++++----- contracts/v-testnet-launch/GrtPool.sol | 26 ++++++------- test/v-testnet-launch/GrtLiquidityWallet.ts | 6 +-- test/v-testnet-launch/GrtOffer.ts | 12 +++--- test/v-testnet-launch/GrtPool.ts | 9 ++--- 6 files changed, 55 insertions(+), 57 deletions(-) diff --git a/contracts/v-testnet-launch/GrtLiquidityWallet.sol b/contracts/v-testnet-launch/GrtLiquidityWallet.sol index 142506a..b059d64 100644 --- a/contracts/v-testnet-launch/GrtLiquidityWallet.sol +++ b/contracts/v-testnet-launch/GrtLiquidityWallet.sol @@ -17,10 +17,6 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { uint256 _amount ); - error InsufficientBalance(); - error FailedToSendNativeTokens(); - error NotAllowedToPayTheOffer(); - function initialize(address bot) external initializer { __Ownable_init(); _bot = bot; @@ -43,11 +39,12 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { } function withdrawNative(uint256 amount) external onlyOwner returns (bool) { - if(address(this).balance < amount) - revert InsufficientBalance(); + require( + address(this).balance >= amount, + "Grindery wallet: insufficient balance." + ); (bool sent, ) = msg.sender.call{value: amount}(""); - if(!sent) - revert FailedToSendNativeTokens(); + require(sent, "Grindery wallet: failed to send native tokens."); return sent; } @@ -57,10 +54,10 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { address to, uint256 amount ) external returns (bool) { - if(msg.sender != owner()) - revert NotAllowedToPayTheOffer(); - if(msg.sender == _bot) - revert NotAllowedToPayTheOffer(); + require( + msg.sender == owner() || msg.sender == _bot, + "Grindery wallet: not allowed to pay the offer." + ); IERC20(token).safeTransfer(to, amount); emit LogOfferPaid(offerId, token, to, amount); return true; @@ -71,15 +68,16 @@ contract GrtLiquidityWallet is OwnableUpgradeable, UUPSUpgradeable { address to, uint256 amount ) external returns (bool) { - if(msg.sender != owner()) - revert NotAllowedToPayTheOffer(); - if(msg.sender == _bot) - revert NotAllowedToPayTheOffer(); - if(address(this).balance > amount) - revert InsufficientBalance(); + require( + msg.sender == owner() || msg.sender == _bot, + "Grindery wallet: not allowed to pay the offer." + ); + require( + address(this).balance >= amount, + "Grindery wallet: insufficient balance." + ); (bool sent, ) = to.call{value: amount}(""); - if(!sent) - revert FailedToSendNativeTokens(); + require(sent, "Grindery wallet: failed to send native tokens."); emit LogOfferPaid(offerId, address(0), to, amount); return true; } diff --git a/contracts/v-testnet-launch/GrtOffer.sol b/contracts/v-testnet-launch/GrtOffer.sol index 6288415..21e8a49 100644 --- a/contracts/v-testnet-launch/GrtOffer.sol +++ b/contracts/v-testnet-launch/GrtOffer.sol @@ -25,12 +25,11 @@ contract GrtOffer is GrtOfferUtils { ); event LogSetStatusOffer(bytes32 indexed _idOffer, bool indexed _isActive); - error NotAllowedToModifyOffer(); - error ZeroAddressNotAllowed(); - modifier isOwner(Offer memory offer) { - if(msg.sender != offer.user) - revert NotAllowedToModifyOffer(); + require( + msg.sender == offer.user, + "Grindery offer: you are not allowed to modify this offer." + ); _; } @@ -41,8 +40,10 @@ contract GrtOffer is GrtOfferUtils { } function setTokenOffer(bytes32 offerId, address token) external isOwner(_offers[offerId]) { - if(token == address(0)) - revert ZeroAddressNotAllowed(); + require( + msg.sender == _offers[offerId].user, + "Grindery offer: you are not allowed to modify this offer." + ); Offer storage offer = _offers[offerId]; offer.token = token; emit LogSetTokenOffer(offerId, token); @@ -80,8 +81,10 @@ contract GrtOffer is GrtOfferUtils { bytes calldata minPriceLimit, bytes calldata maxPriceLimit ) external returns (bytes32) { - if(msg.sender == address(0)) - revert ZeroAddressNotAllowed(); + require( + msg.sender != address(0), + "Grindery offer: setOffer from zero address is not allowed" + ); bytes32 offerId = keccak256( abi.encodePacked(msg.sender, _noncesOffer[msg.sender]) ); diff --git a/contracts/v-testnet-launch/GrtPool.sol b/contracts/v-testnet-launch/GrtPool.sol index c20bbba..acc53e2 100644 --- a/contracts/v-testnet-launch/GrtPool.sol +++ b/contracts/v-testnet-launch/GrtPool.sol @@ -36,24 +36,24 @@ contract GrtPool is OwnableUpgradeable, GrtOffer, UUPSUpgradeable { function _authorizeUpgrade(address) internal override onlyOwner onlyProxy {} - error TransferedAmountMustBePositive(); - error ZeroAddressIsNotAllowed(); - error OfferInactive(); - error FailedToSendNativeTokens(); - function depositETHAndAcceptOffer( bytes32 offerId, address destAddr ) external payable returns (bytes32) { - if(msg.value <= 0) - revert TransferedAmountMustBePositive(); - if(destAddr == address(0)) - revert ZeroAddressIsNotAllowed(); - if(!_offers[offerId].isActive) - revert OfferInactive(); + require( + msg.value > 0, + "Grindery Pool: transfered amount must be positive." + ); + require( + destAddr != address(0), + "Grindery Pool: zero address as destination address is not allowed." + ); + require( + _offers[offerId].isActive, + "Grindery Pool: the offer is inactive." + ); (bool sent, ) = address(this).call{value: msg.value}(""); - if(!sent) - revert FailedToSendNativeTokens(); + require(sent, "Grindery Pool: failed to send native tokens."); bytes32 tradeId = keccak256( abi.encodePacked(msg.sender, _noncesDeposit[msg.sender]) ); diff --git a/test/v-testnet-launch/GrtLiquidityWallet.ts b/test/v-testnet-launch/GrtLiquidityWallet.ts index 1a8b9fa..3d6d348 100644 --- a/test/v-testnet-launch/GrtLiquidityWallet.ts +++ b/test/v-testnet-launch/GrtLiquidityWallet.ts @@ -228,7 +228,7 @@ describe("Grindery Liquidity Wallet", () => { grtLiquidityWallet .connect(owner) .withdrawNative(ethers.utils.parseEther("200")) - ).to.be.revertedWithCustomError(grtLiquidityWallet, "InsufficientBalance"); + ).to.be.revertedWith("Grindery wallet: insufficient balance."); }); }); @@ -243,7 +243,7 @@ describe("Grindery Liquidity Wallet", () => { owner.address, 100 ) - ).to.be.revertedWithCustomError(grtLiquidityWallet, "NotAllowedToPayTheOffer"); + ).to.be.revertedWith("Grindery wallet: not allowed to pay the offer."); }); it("Should transfer ERC20 funds to user", async () => { @@ -281,7 +281,7 @@ describe("Grindery Liquidity Wallet", () => { grtLiquidityWallet .connect(user2) .payOfferWithNativeTokens(offerId, owner.address, 100) - ).to.be.revertedWithCustomError(grtLiquidityWallet, "NotAllowedToPayTheOffer"); + ).to.be.revertedWith("Grindery wallet: not allowed to pay the offer."); }); it("Should transfer Native funds to user", async () => { diff --git a/test/v-testnet-launch/GrtOffer.ts b/test/v-testnet-launch/GrtOffer.ts index f01bf84..6207337 100644 --- a/test/v-testnet-launch/GrtOffer.ts +++ b/test/v-testnet-launch/GrtOffer.ts @@ -213,9 +213,7 @@ describe("Grindery Offer testings", function () { it("Should fail if the sender is not the creator of the offer", async function () { await expect( grtOffer.connect(user2).setChainIdOffer(offerId, 34) - ).to.be.revertedWithCustomError(grtOffer, - "NotAllowedToModifyOffer" - ); + ).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.") }); it("Should modify the chainID", async function () { @@ -236,7 +234,7 @@ describe("Grindery Offer testings", function () { it("Should fail if the sender is not the creator of the offer", async function () { await expect( grtOffer.connect(user2).setTokenOffer(offerId, token1.address) - ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") + ).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.") }); it("Should modify the token address", async function () { @@ -267,7 +265,7 @@ describe("Grindery Offer testings", function () { ["FIRA", "50"] ) ) - ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") + ).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.") }); it("Should modify the Min price limit options", async function () { @@ -337,7 +335,7 @@ describe("Grindery Offer testings", function () { ["FIRA", "2000"] ) ) - ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") + ).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.") }); it("Should modify the Max price limit options", async function () { @@ -399,7 +397,7 @@ describe("Grindery Offer testings", function () { it("Should fail if the sender is not the creator of the offer", async function () { await expect( grtOffer.connect(user2).setIsActive(offerId, false) - ).to.be.revertedWithCustomError(grtOffer, "NotAllowedToModifyOffer") + ).to.be.revertedWith("Grindery offer: you are not allowed to modify this offer.") }); it("Should modify the status", async function () { diff --git a/test/v-testnet-launch/GrtPool.ts b/test/v-testnet-launch/GrtPool.ts index 197a53a..560ee55 100644 --- a/test/v-testnet-launch/GrtPool.ts +++ b/test/v-testnet-launch/GrtPool.ts @@ -76,7 +76,7 @@ describe("Grindery Offer testings", function () { .depositETHAndAcceptOffer(offerId, user3.address, { value: 0, }) - ).to.be.revertedWithCustomError(grtPool,"TransferedAmountMustBePositive"); + ).to.be.revertedWith("Grindery Pool: transfered amount must be positive."); }); it("Should fail if the offer is inactive", async function () { @@ -87,7 +87,7 @@ describe("Grindery Offer testings", function () { .depositETHAndAcceptOffer(offerId, user3.address, { value: 100, }) - ).to.be.revertedWithCustomError(grtPool, "OfferInactive"); + ).to.be.revertedWith("Grindery Pool: the offer is inactive."); }); it("Should fail if the destination address is the zero address", async function () { @@ -98,9 +98,8 @@ describe("Grindery Offer testings", function () { .depositETHAndAcceptOffer(offerId, ethers.constants.AddressZero, { value: 100, }) - ).to.be.revertedWithCustomError( - grtPool, - "ZeroAddressIsNotAllowed" + ).to.be.revertedWith( + "Grindery Pool: zero address as destination address is not allowed." ); });