-
Notifications
You must be signed in to change notification settings - Fork 14
Feature/paymaster #411
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
base: release/reworked-staking
Are you sure you want to change the base?
Feature/paymaster #411
Changes from all commits
dafe0cc
c6e753a
2a1128e
5af3554
b6580ff
edbf1eb
ce661ed
d8b8e4c
0a0cc46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,32 +7,39 @@ import {TokenLib} from "./libraries/TokenLib.sol"; | |
| import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
| import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
|
||
| contract Paymaster is Ownable(msg.sender) { | ||
| contract Paymaster is Ownable { | ||
| error NotAllowed(); | ||
|
|
||
| Hub public hub; | ||
| IERC20 public tokenContract; | ||
|
|
||
| mapping(address => bool) public allowedAddresses; | ||
|
|
||
| modifier onlyAllowed() { | ||
| if (!allowedAddresses[msg.sender]) { | ||
| event AllowedAddressAdded(address _address); | ||
| event AllowedAddressRemoved(address _address); | ||
| event FundsAdded(address sender, uint256 amount); | ||
| event WhitdrawalMade(address recipient, uint256 amount); | ||
|
|
||
| modifier onlyAllowed(address _originalSender) { | ||
| if (!allowedAddresses[_originalSender]) { | ||
| revert NotAllowed(); | ||
| } | ||
| _; | ||
| } | ||
|
|
||
| constructor(address hubAddress) { | ||
| constructor(address hubAddress, address initialOwner) Ownable(initialOwner) { | ||
| hub = Hub(hubAddress); | ||
| tokenContract = IERC20(hub.getContractAddress("Token")); | ||
| } | ||
|
|
||
| function addAllowedAddress(address _address) external onlyOwner { | ||
| allowedAddresses[_address] = true; | ||
| emit AllowedAddressAdded(_address); | ||
| } | ||
|
|
||
| function removeAllowedAddress(address _address) external onlyOwner { | ||
| allowedAddresses[_address] = false; | ||
| emit AllowedAddressRemoved(_address); | ||
| } | ||
|
|
||
| function fundPaymaster(uint256 amount) external { | ||
|
|
@@ -53,14 +60,23 @@ contract Paymaster is Ownable(msg.sender) { | |
| if (!tokenContract.transferFrom(msg.sender, address(this), amount)) { | ||
| revert TokenLib.TransferFailed(); | ||
| } | ||
|
|
||
| emit FundsAdded(msg.sender, amount); | ||
| } | ||
|
|
||
| function withdraw(address recipient, uint256 amount) external onlyOwner { | ||
| _transferTokens(recipient, amount); | ||
| emit WhitdrawalMade(recipient, amount); | ||
| } | ||
|
|
||
| function coverCost(uint256 amount) external onlyAllowed { | ||
| _transferTokens(hub.getContractAddress("KnowledgeCollection"), amount); | ||
| function coverCost(uint256 amount, address _originalSender) external onlyAllowed(_originalSender) { | ||
| address knowledgeCollectionAddress = hub.getContractAddress("KnowledgeCollection"); | ||
|
|
||
| if (msg.sender != knowledgeCollectionAddress) { | ||
| revert("Sender is not the KnowledgeCollection contract"); | ||
| } | ||
|
|
||
| _transferTokens(knowledgeCollectionAddress, amount); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we transfer tokens to the StakingStorage like we are doing in KnowledgeCollection.sol if paymaster is not valid? |
||
| } | ||
|
|
||
| function _transferTokens(address to, uint256 amount) internal { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ contract PaymasterManager is INamed, IVersioned, ContractStatus { | |
| } | ||
|
|
||
| function deployPaymaster() external { | ||
| address paymasterAddress = address(new Paymaster(address(hub))); | ||
| address paymasterAddress = address(new Paymaster(address(hub), msg.sender)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can set a paymaster at deployment and add it to validPaymasters and deployedPaymasters. Can we add a function to deactivate/remove them as well? Sounds like a good thing to have in case of compromise or if the owner decides to shut it down
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw what do we used deployedPaymasters for? Cannot seem to find the use case for it anywhere |
||
|
|
||
| validPaymasters[paymasterAddress] = true; | ||
| deployedPaymasters[msg.sender].push(paymasterAddress); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; | ||
| import { PaymasterManager } from '../../typechain'; | ||
|
|
||
| export async function createPaymaster( | ||
| paymasterCreator: SignerWithAddress, | ||
| paymasterManager: PaymasterManager | ||
| ) { | ||
| const tx = await paymasterManager.connect(paymasterCreator).deployPaymaster(); | ||
| const receipt = await tx.wait(); | ||
|
|
||
| const paymasterDeployedEvent = receipt!.logs.find( | ||
| log => log.topics[0] === paymasterManager.interface.getEvent('PaymasterDeployed').topicHash | ||
| ); | ||
|
|
||
| if (!paymasterDeployedEvent) { | ||
| throw new Error('PaymasterDeployed event not found in transaction logs'); | ||
| } | ||
|
|
||
| const parsedEvent = paymasterManager.interface.parseLog({ | ||
| topics: paymasterDeployedEvent.topics as string[], | ||
| data: paymasterDeployedEvent.data | ||
| }); | ||
|
|
||
| if (!parsedEvent) { | ||
| throw new Error('Failed to parse PaymasterDeployed event'); | ||
| } | ||
|
|
||
| const deployer = parsedEvent.args.deployer; | ||
| const paymasterAddress = parsedEvent.args.paymasterAddress; | ||
|
|
||
| return {deployer, paymasterAddress} | ||
| }; |
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.
It would be good to emit events when adding or removing from allowed addresses. Same for funding and withdrawing
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 think we should add the transfer ownership method to Paymaster