-
Notifications
You must be signed in to change notification settings - Fork 29
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
Sponsor payment #326
base: master
Are you sure you want to change the base?
Sponsor payment #326
Conversation
* Supports Ether and ERC-20 token payments with enhanced security feature$. | ||
* @custom:storage-location erc7201:iden3.storage.SponsorPayment | ||
*/ | ||
contract SponsorPayment is ReentrancyGuardUpgradeable, EIP712Upgradeable, Ownable2StepUpgradeable { |
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 is a motivation to use nonReentrant functionality in this contract?
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.
to prevent nonReentrant attack
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 is only protection from internal calls,no?
contracts/payment/SponsorPayment.sol
Outdated
struct SponsorPaymentStorage { | ||
mapping(address => mapping(address => uint256)) balances; // sponsor => token => balance | ||
mapping(address => mapping(address => WithdrawalRequest)) withdrawalRequests; // sponsor => token => request | ||
mapping(bytes32 => bool) isWithdrawn; |
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 is not clear what is bytes32 without any comment
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.
fixed
contracts/payment/SponsorPayment.sol
Outdated
* @param addr Address to check | ||
*/ | ||
function _isContract(address addr) private view returns (bool) { | ||
uint256 size; |
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 guess it is the same check target.code.length > 0 (you can test)
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.
replaced this check
SponsorPaymentStorage storage $ = _getSponsorPaymentStorage(); | ||
|
||
if (msg.value == 0) revert InvalidDeposit("Invalid value amount"); | ||
$.balances[_msgSender()][address(0)] += msg.value; |
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.
do we deposit to 0 address for native token?
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.
Yes, I'm store native coin deposit per specific sponsor under address(0)
Any objection or suggestions?
function depositERC20(uint256 amount, address token) external nonReentrant validToken(token) { | ||
SponsorPaymentStorage storage $ = _getSponsorPaymentStorage(); | ||
if (amount == 0) revert InvalidDeposit("Invalid token amount"); | ||
IERC20(token).safeTransferFrom(_msgSender(), address(this), amount); |
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.
is allowance in token contract is given sepatelly?
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.
You can see the interaction with SC by reviewing unit tests
contracts/payment/SponsorPayment.sol
Outdated
emit ERC20Withdrawal(_msgSender(), token, amount); | ||
} | ||
|
||
function _claimPayment( |
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 method name confuses me.
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.
Please suggest the name that don't make you confuse
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.
discussion in the separate thread. I'm against of the usage the word 'claim' to avoid missunderstanding.
Pull Request Test Coverage Report for Build 12584161757Details
💛 - Coveralls |
contracts/payment/SponsorPayment.sol
Outdated
0x98fc76e32452055302f77aa95cd08aa0cf22c02a3ebdaee3e1411f6c47c2ef00; | ||
|
||
modifier validToken(address token) { | ||
if (token != address(0)) { |
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.
so if token == address(0) - it is valid ?
based on the modifier
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
token == address(0) || token.code.length == 0
-> then revert
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.
changed
contracts/payment/SponsorPayment.sol
Outdated
} | ||
|
||
/** | ||
* @dev Main storage $tructure for the contract |
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.
structure
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.
fixed
contracts/payment/SponsorPayment.sol
Outdated
* @dev Main storage $tructure for the contract | ||
*/ | ||
struct SponsorPaymentStorage { | ||
mapping(address => mapping(address => uint256)) balances; // sponsor => token => balance |
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.
Better use kind of "inline comments" for mappings mapping(address sponsor => mapping(address token => uint256 balance))
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.
fixed
contracts/payment/SponsorPayment.sol
Outdated
"ERC20SponsorPaymentInfo(address recipient,uint256 amount,address token,uint256 expiration,uint256 nonce,bytes metadata)" | ||
); | ||
|
||
bytes32 public constant PAYMENT_CLAIM_DATA_TYPE_HASH = |
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 not just SPONSOR_PAYMENT_INFO_TYPE_HASH for name consistency?
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.
renamed
contracts/payment/SponsorPayment.sol
Outdated
$.ownerPercentFee = ownerPercentFee; | ||
} | ||
|
||
function _tryRecoverSigner( |
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.
Please put all private and internal methods after public methods
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.
done
contracts/payment/SponsorPayment.sol
Outdated
struct WithdrawalRequest { | ||
uint256 amount; | ||
uint256 lockTime; | ||
bool exists; |
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 amount > 0 indicate that request exists to same 1 storage slot?
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.
Yes, refactored
contracts/payment/SponsorPayment.sol
Outdated
|
||
function _getWithdrawalAmount(address token) private view returns (uint256) { | ||
SponsorPaymentStorage storage $ = _getSponsorPaymentStorage(); | ||
WithdrawalRequest memory request = $.withdrawalRequests[_msgSender()][token]; |
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.
storage
but not memory
data allocation in such cases saves some gas.
/** | ||
* @notice Execute withdrawal after delay period | ||
*/ | ||
function withdraw() external 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.
we don't have any external calls here, so no need in nonReentrant
bytes32 public constant ERC_20_SPONSOR_PAYMENT_INFO_TYPE_HASH = | ||
keccak256( | ||
// solhint-disable-next-line max-line-length | ||
"ERC20SponsorPaymentInfo(address recipient,uint256 amount,address token,uint256 expiration,uint256 nonce,bytes metadata)" |
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 expiration to expirationDate, address token to tokenAddress ( for the purpose of the same naming)
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 type to Iden3PaymentPermitERC20V1
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.
put tokenAddress first, so maybe in future we can use single type for all erc20 encoded info
bytes32 public constant SPONSOR_PAYMENT_INFO_TYPE_HASH = | ||
keccak256( | ||
// solhint-disable-next-line max-line-length | ||
"ERC20SponsorPaymentInfo(address recipient,uint256 amount,uint256 expiration,uint256 nonce,bytes metadata)" |
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 expiration to expirationDate ( for the purpose of the same naming)
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 type to Iden3PaymentPermitV1
Some details: