-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MultiSwapForge Contracts for GasEstimation #42
Conversation
@@ -107,7 +106,7 @@ contract FiberRouter is Ownable, TokenReceivable { | |||
@notice Sets the fund manager contract. | |||
@param _pool The fund manager | |||
*/ | |||
function setPool(address _pool) external onlyOwner { | |||
function setPool(address _pool) public virtual onlyOwner { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why public virtual
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the FiberRouter contract’s function access from external to public and introducing the virtual keyword is for inheritance. This is visible in MultiswapForge.sol, where functions like setPool are overridden. Without making the parent class functions public virtual, the compiler was giving errors while overriding in MultiswapForge, which is why I had to make this change in FiberRouter.
@@ -119,7 +118,7 @@ contract FiberRouter is Ownable, TokenReceivable { | |||
@notice Sets the gas wallet address. | |||
@param _gasWallet The wallet which pays for the funds on withdrawal | |||
*/ | |||
function setGasWallet(address payable _gasWallet) external onlyOwner { | |||
function setGasWallet(address payable _gasWallet) external virtual onlyOwner { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
@@ -132,7 +131,8 @@ contract FiberRouter is Ownable, TokenReceivable { | |||
@param _newRouterAddress The new Router Address of oneInch | |||
*/ | |||
function setOneInchAggregatorRouter(address _newRouterAddress) | |||
external | |||
public |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
@@ -412,7 +413,7 @@ function swapAndCrossOneInch( | |||
bytes32 salt, | |||
uint256 expiry, | |||
bytes memory multiSignature | |||
) external nonReentrant { | |||
) public virtual nonReentrant { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why public? also the same for all other methods
import "foundry-contracts/contracts/common/FerrumDeployer.sol"; | ||
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | ||
|
||
contract ForgeFundManager is SigCheckable, WithAdmin, TokenReceivable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ForgeFundManager is FundManager
This is the only thing needed. Similar to the router no need to rewrite everything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure let me make this adjustment
address targetAddress, | ||
bytes32 withdrawalData | ||
) external override payable nonReentrant { | ||
revert("Function is reverted"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe Not supported
error mesage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
} | ||
|
||
// Inherit withdrawSigned function | ||
function withdrawSigned( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename to withdrawSignedForGasEstimateion
and overwrite the withdrawSign
to revert.
Then create a new function called estimateGasForWithdrawSigned, where it will use address(this).call(...)
to call the above function. This function will not fail, because it is using .call
so you on the backend or frontend, you can use ethres.estimateGas(tx)
to estimate the gas correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me take a good look into this approach and test it again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@naiemk I have pushed the latest commit with the changes mentioned in this PR comments,
Moreover, the change specifically for estimation functions are implemented in a way that the original withdrawSigned
function overriden and reverted in Forge contract, however, I have renamed the withdrawSigned
to withdrawSignedForGasEstimateion
and called the super.withdrawSigned
to simulate the withdraw flow with a revert at the end. Then, I have created estimateGasForWithdrawSigned
that calls the withdrawSignedForGasEstimateion
using address(this).call()
. On backend (deployAndTest.js), I have called contract.estimateGas.estimateGasForWithdrawSigned
and now it is returning correct gasFee.
); | ||
|
||
// Calculate the gas used during the execution | ||
uint256 gasUsed = initialGas - gasleft(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused. no need
// Calculate the gas used during the execution | ||
uint256 gasUsed = initialGas - gasleft(); | ||
|
||
// Revert with a message indicating gas usage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why messge indicating gas use? No easy way to get this message in the client so this is useless
@naiemk changes have been made as per the suggestion. Changes introduced to the latest commit are mentioned below:
|
bytes32 salt, | ||
uint256 expiry, | ||
bytes memory multiSignature | ||
) external { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function does not revert. Can be used to extract funds.
revert("Not Supported"); | ||
} | ||
// This function is only used specifically for GasEstimation & Simulation of withdrawSigned | ||
function withdrawSignedForGasEstimation( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function must revert.
* @note PrivateKey of the Test Signer to be added here for Devs Reference | ||
*/ | ||
constructor() { | ||
super.addSigner(0x); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of adding 0x as signer. Add a real signer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly! I have requested a test wallet, and once it is received, I will include the test signer's address and private key (to be added in comments) and then proceed with the commit.
@@ -80,7 +79,7 @@ contract FundManager is SigCheckable, WithAdmin, TokenReceivable { | |||
router = _router; | |||
} | |||
|
|||
function addSigner(address _signer) external onlyOwner { | |||
function addSigner(address _signer) public onlyOwner { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I can create a dedicated setSigner function within the ForgeFundManager
contract, performing the same task of adding the test signer to the signers array. This approach allows the function to be conveniently invoked through the constructor.
function setSigner(address _signer) public onlyOwner {
require(_signer != address(0), "Bad signer");
signers[_signer] = true;
}
P.S: We cannot directly inherit an external function from a parent contract into a child contract unless the visibility of that function is modified to at least public
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nm my bad. this will be needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bytes4 selector = bytes4( | ||
keccak256( | ||
"withdrawSignedAndSwapOneInchForGasEstimation(address,uint256,uint256,address,address,bytes,bytes32,uint256,bytes)" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can use this.withdrawSignedAndSwapOneInchForGasEstimation.selector
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented.
Overview
This pull request introduces changes to the contract architecture to accommodate gas estimation functionality and separates the production contracts from gas estimation contracts. The main contracts involved are MultiSwapForgeContract, ForgeFundManager, FiberRouter, and FundManager.
Contracts Overview
Production Contracts:
Gas Estimation Contracts:
estimateWithdrawSigned
andestimateWithdrawSignedAndSwapOneInch
functions.withdrawSigned
andwithdrawSignedAndSwapOneInch
functions.Production Contracts Workflow
Gas Estimation Contracts Workflow
estimateGasForWithdrawSigned
inMultiSwapForgeContract
.MultiSwapForgeContract
'sestimateGasForWithdrawSigned
function invokes thewithdrawSigned
function ofForgeFundManager
.estimateGas.estimateGasForWithdrawSigned()
function returns the estimated gas for simulating the withdrawSigned function.These changes enhance the gas estimation capabilities of the system and segregate the gas estimation logic from the production transactions.
Changes
Additional Notes