-
Notifications
You must be signed in to change notification settings - Fork 11
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
ReimbursementPool on Lynx #323
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1b525b5
Add ReimbursementPool contract
cygnusv 9475b59
Adapt ReimbursementPool contract to our codebase
cygnusv 9670d34
Adapt Reimbursement pool to OpenZeppelin v5
cygnusv 5e75990
ReimbursementPool deployment script and params for Lynx
cygnusv 3462100
Add ReimbursementPool deployment to Lynx registry
cygnusv 53e9bfb
Make linter happy
cygnusv 969dff2
Credit original source of ReimbursementPool
cygnusv f76a026
Script to set ReimbursementPool contract on Lynx
cygnusv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
// TODO: What about license? Original is GPL but ours is AGPL | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; | ||
import "./IReimbursementPool.sol"; | ||
|
||
/** | ||
* @title ReimbursementPool contract | ||
* @dev Original source: https://github.com/keep-network/keep-core/blob/main/solidity/random-beacon/contracts/ReimbursementPool.sol | ||
*/ | ||
contract ReimbursementPool is Ownable, ReentrancyGuard, IReimbursementPool { | ||
/// @notice Authorized contracts that can interact with the reimbursment pool. | ||
/// Authorization can be granted and removed by the owner. | ||
mapping(address => bool) public isAuthorized; | ||
|
||
/// @notice Static gas includes: | ||
/// - cost of the refund function | ||
/// - base transaction cost | ||
uint256 public staticGas; | ||
|
||
/// @notice Max gas price used to reimburse a transaction submitter. Protects | ||
/// against malicious operator-miners. | ||
uint256 public maxGasPrice; | ||
|
||
event StaticGasUpdated(uint256 newStaticGas); | ||
|
||
event MaxGasPriceUpdated(uint256 newMaxGasPrice); | ||
|
||
event SendingEtherFailed(uint256 refundAmount, address receiver); | ||
|
||
event AuthorizedContract(address thirdPartyContract); | ||
|
||
event UnauthorizedContract(address thirdPartyContract); | ||
|
||
event FundsWithdrawn(uint256 withdrawnAmount, address receiver); | ||
|
||
constructor(uint256 _staticGas, uint256 _maxGasPrice) Ownable(msg.sender) { | ||
staticGas = _staticGas; | ||
maxGasPrice = _maxGasPrice; | ||
} | ||
|
||
/// @notice Receive ETH | ||
receive() external payable {} | ||
|
||
/// @notice Refunds ETH to a spender for executing specific transactions. | ||
/// @dev Ignoring the result of sending ETH to a receiver is made on purpose. | ||
/// For EOA receiving ETH should always work. If a receiver is a smart | ||
/// contract, then we do not want to fail a transaction, because in some | ||
/// cases the refund is done at the very end of multiple calls where all | ||
/// the previous calls were already paid off. It is a receiver's smart | ||
/// contract resposibility to make sure it can receive ETH. | ||
/// @dev Only authorized contracts are allowed calling this function. | ||
/// @param gasSpent Gas spent on a transaction that needs to be reimbursed. | ||
/// @param receiver Address where the reimbursment is sent. | ||
function refund(uint256 gasSpent, address receiver) external nonReentrant { | ||
require(isAuthorized[msg.sender], "Contract is not authorized for a refund"); | ||
require(receiver != address(0), "Receiver's address cannot be zero"); | ||
|
||
uint256 gasPrice = tx.gasprice < maxGasPrice ? tx.gasprice : maxGasPrice; | ||
|
||
uint256 refundAmount = (gasSpent + staticGas) * gasPrice; | ||
|
||
/* solhint-disable avoid-low-level-calls */ | ||
// slither-disable-next-line low-level-calls,unchecked-lowlevel | ||
(bool sent, ) = receiver.call{value: refundAmount}(""); | ||
/* solhint-enable avoid-low-level-calls */ | ||
if (!sent) { | ||
// slither-disable-next-line reentrancy-events | ||
emit SendingEtherFailed(refundAmount, receiver); | ||
} | ||
} | ||
|
||
/// @notice Authorize a contract that can interact with this reimbursment pool. | ||
/// Can be authorized by the owner only. | ||
/// @param _contract Authorized contract. | ||
function authorize(address _contract) external onlyOwner { | ||
isAuthorized[_contract] = true; | ||
|
||
emit AuthorizedContract(_contract); | ||
} | ||
|
||
/// @notice Unauthorize a contract that was previously authorized to interact | ||
/// with this reimbursment pool. Can be unauthorized by the | ||
/// owner only. | ||
/// @param _contract Authorized contract. | ||
function unauthorize(address _contract) external onlyOwner { | ||
delete isAuthorized[_contract]; | ||
|
||
emit UnauthorizedContract(_contract); | ||
} | ||
|
||
/// @notice Setting a static gas cost for executing a transaction. Can be set | ||
/// by the owner only. | ||
/// @param _staticGas Static gas cost. | ||
function setStaticGas(uint256 _staticGas) external onlyOwner { | ||
staticGas = _staticGas; | ||
|
||
emit StaticGasUpdated(_staticGas); | ||
} | ||
|
||
/// @notice Setting a max gas price for transactions. Can be set by the | ||
/// owner only. | ||
/// @param _maxGasPrice Max gas price used to reimburse tx submitters. | ||
function setMaxGasPrice(uint256 _maxGasPrice) external onlyOwner { | ||
maxGasPrice = _maxGasPrice; | ||
|
||
emit MaxGasPriceUpdated(_maxGasPrice); | ||
} | ||
|
||
/// @notice Withdraws all ETH from this pool which are sent to a given | ||
/// address. Can be set by the owner only. | ||
/// @param receiver An address where ETH is sent. | ||
function withdrawAll(address receiver) external onlyOwner { | ||
withdraw(address(this).balance, receiver); | ||
} | ||
|
||
/// @notice Withdraws ETH amount from this pool which are sent to a given | ||
/// address. Can be set by the owner only. | ||
/// @param amount Amount to withdraw from the pool. | ||
/// @param receiver An address where ETH is sent. | ||
function withdraw(uint256 amount, address receiver) public onlyOwner { | ||
require(address(this).balance >= amount, "Insufficient contract balance"); | ||
require(receiver != address(0), "Receiver's address cannot be zero"); | ||
|
||
emit FundsWithdrawn(amount, receiver); | ||
|
||
/* solhint-disable avoid-low-level-calls */ | ||
// slither-disable-next-line low-level-calls,arbitrary-send | ||
(bool sent, ) = receiver.call{value: amount}(""); | ||
/* solhint-enable avoid-low-level-calls */ | ||
require(sent, "Failed to send Ether"); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bit of a nitpick. I know this was copied from a Keep contract, but should this be
deauthorize
? We've used theauthorize/deauthorize
parlance in other areas.Same comment for the event.
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.
I agree that there's a divergence wrt to staking contracts but this contract is not related to staking, and I'd prefer if we limit changes unless we really need them.
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.
Definitely not a "need" 😄