Skip to content

Commit

Permalink
refactor: wrong amount naming
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChin committed Nov 11, 2024
1 parent 010ecd7 commit 3321cc9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 56 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ yarn coverage

Configure the `.env` variables.

### Private Key management

Deployment private keys are encripted using ERC2335. Run:

```bash
cast wallet import nameOfAccountGoesHere --interactive
```

And you will be able to deploy with our scripts

### Sepolia

```bash
Expand Down
60 changes: 30 additions & 30 deletions src/contracts/Grateful.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ contract Grateful is IGrateful, Ownable2Step {
address _sender,
address _merchant,
address _token,
uint256 _AMOUNT_USDC
uint256 _amount
) public view returns (uint256) {
return uint256(keccak256(abi.encodePacked(_sender, _merchant, _token, _AMOUNT_USDC, block.timestamp)));
return uint256(keccak256(abi.encodePacked(_sender, _merchant, _token, _amount, block.timestamp)));
}

/// @inheritdoc IGrateful
function applyFee(address _merchant, uint256 _AMOUNT_USDC) public view returns (uint256) {
function applyFee(address _merchant, uint256 _amount) public view returns (uint256) {
uint256 feePercentage = fee;
if (customFees[_merchant].isSet) {
feePercentage = customFees[_merchant].fee;
}
uint256 feeAmount = (_AMOUNT_USDC * feePercentage) / 10_000;
return _AMOUNT_USDC - feeAmount;
uint256 feeAmount = (_amount * feePercentage) / 10_000;
return _amount - feeAmount;
}

/// @inheritdoc IGrateful
Expand Down Expand Up @@ -148,29 +148,29 @@ contract Grateful is IGrateful, Ownable2Step {
function pay(
address _merchant,
address _token,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _id
) external onlyWhenTokenWhitelisted(_token) {
_processPayment(msg.sender, _merchant, _token, _AMOUNT_USDC, _id, new address[](0), new uint256[](0));
_processPayment(msg.sender, _merchant, _token, _amount, _id, new address[](0), new uint256[](0));
}

/// @inheritdoc IGrateful
function pay(
address _merchant,
address _token,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _id,
address[] calldata _recipients,
uint256[] calldata _percentages
) external onlyWhenTokenWhitelisted(_token) {
_processPayment(msg.sender, _merchant, _token, _AMOUNT_USDC, _id, _recipients, _percentages);
_processPayment(msg.sender, _merchant, _token, _amount, _id, _recipients, _percentages);
}

/// @inheritdoc IGrateful
function createOneTimePayment(
address _merchant,
address[] memory _tokens,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _salt,
uint256 _paymentId,
address precomputed,
Expand All @@ -179,39 +179,39 @@ contract Grateful is IGrateful, Ownable2Step {
) external onlyWhenTokensWhitelisted(_tokens) returns (OneTime oneTime) {
oneTimePayments[precomputed] = true;
oneTime = new OneTime{salt: bytes32(_salt)}(
IGrateful(address(this)), _tokens, _merchant, _AMOUNT_USDC, _paymentId, _recipients, _percentages
IGrateful(address(this)), _tokens, _merchant, _amount, _paymentId, _recipients, _percentages
);
emit OneTimePaymentCreated(_merchant, _tokens, _AMOUNT_USDC);
emit OneTimePaymentCreated(_merchant, _tokens, _amount);
}

/// @inheritdoc IGrateful
function receiveOneTimePayment(
address _merchant,
address _token,
uint256 _paymentId,
uint256 _AMOUNT_USDC,
uint256 _amount,
address[] calldata _recipients,
uint256[] calldata _percentages
) external {
if (!oneTimePayments[msg.sender]) {
revert Grateful_OneTimeNotFound();
}
_processPayment(msg.sender, _merchant, _token, _AMOUNT_USDC, _paymentId, _recipients, _percentages);
_processPayment(msg.sender, _merchant, _token, _amount, _paymentId, _recipients, _percentages);
}

/// @inheritdoc IGrateful
function computeOneTimeAddress(
address _merchant,
address[] memory _tokens,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _salt,
uint256 _paymentId,
address[] calldata _recipients,
uint256[] calldata _percentages
) external view returns (OneTime oneTime) {
bytes memory bytecode = abi.encodePacked(
type(OneTime).creationCode,
abi.encode(address(this), _tokens, _merchant, _AMOUNT_USDC, _paymentId, _recipients, _percentages)
abi.encode(address(this), _tokens, _merchant, _amount, _paymentId, _recipients, _percentages)
);
bytes32 bytecodeHash = keccak256(bytecode);
bytes32 addressHash = keccak256(abi.encodePacked(bytes1(0xff), address(this), bytes32(_salt), bytecodeHash));
Expand All @@ -223,32 +223,32 @@ contract Grateful is IGrateful, Ownable2Step {
function createOneTimePayment(
address _merchant,
address[] memory _tokens,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _salt,
uint256 _paymentId,
address precomputed
) external onlyWhenTokensWhitelisted(_tokens) returns (OneTime oneTime) {
oneTimePayments[precomputed] = true;
oneTime = new OneTime{salt: bytes32(_salt)}(
IGrateful(address(this)), _tokens, _merchant, _AMOUNT_USDC, _paymentId, new address[](0), new uint256[](0)
IGrateful(address(this)), _tokens, _merchant, _amount, _paymentId, new address[](0), new uint256[](0)
);
emit OneTimePaymentCreated(_merchant, _tokens, _AMOUNT_USDC);
emit OneTimePaymentCreated(_merchant, _tokens, _amount);
}

/// @inheritdoc IGrateful
function receiveOneTimePayment(
address _merchant,
IERC20[] memory _tokens,
uint256 _paymentId,
uint256 _AMOUNT_USDC
uint256 _amount
) external {
if (!oneTimePayments[msg.sender]) {
revert Grateful_OneTimeNotFound();
}
for (uint256 i = 0; i < _tokens.length; i++) {
if (_tokens[i].balanceOf(msg.sender) >= _AMOUNT_USDC) {
if (_tokens[i].balanceOf(msg.sender) >= _amount) {
_processPayment(
msg.sender, _merchant, address(_tokens[i]), _AMOUNT_USDC, _paymentId, new address[](0), new uint256[](0)
msg.sender, _merchant, address(_tokens[i]), _amount, _paymentId, new address[](0), new uint256[](0)
);
}
}
Expand All @@ -258,13 +258,13 @@ contract Grateful is IGrateful, Ownable2Step {
function computeOneTimeAddress(
address _merchant,
address[] memory _tokens,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _salt,
uint256 _paymentId
) external view returns (OneTime oneTime) {
bytes memory bytecode = abi.encodePacked(
type(OneTime).creationCode,
abi.encode(address(this), _tokens, _merchant, _AMOUNT_USDC, _paymentId, new address[](0), new uint256[](0))
abi.encode(address(this), _tokens, _merchant, _amount, _paymentId, new address[](0), new uint256[](0))
);
bytes32 bytecodeHash = keccak256(bytecode);
bytes32 addressHash = keccak256(abi.encodePacked(bytes1(0xff), address(this), bytes32(_salt), bytecodeHash));
Expand Down Expand Up @@ -378,7 +378,7 @@ contract Grateful is IGrateful, Ownable2Step {
* @param _sender Address of the sender.
* @param _merchant Address of the merchant.
* @param _token Address of the token.
* @param _AMOUNT_USDC Amount of the token.
* @param _amount Amount of the token.
* @param _paymentId ID of the payment.
* @param _recipients List of recipients for payment splitting.
* @param _percentages Corresponding percentages for each recipient.
Expand All @@ -387,24 +387,24 @@ contract Grateful is IGrateful, Ownable2Step {
address _sender,
address _merchant,
address _token,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _paymentId,
address[] memory _recipients,
uint256[] memory _percentages
) internal {
// Transfer the full amount from the sender to this contract
IERC20(_token).safeTransferFrom(_sender, address(this), _AMOUNT_USDC);
IERC20(_token).safeTransferFrom(_sender, address(this), _amount);

// Check payment id
if (paymentIds[_paymentId]) {
revert Grateful_PaymentIdAlreadyUsed();
}

// Apply the fee
uint256 amountWithFee = applyFee(_merchant, _AMOUNT_USDC);
uint256 amountWithFee = applyFee(_merchant, _amount);

// Transfer fee to owner
IERC20(_token).safeTransfer(owner(), _AMOUNT_USDC - amountWithFee);
IERC20(_token).safeTransfer(owner(), _amount - amountWithFee);

// If payment splitting is requested
if (_recipients.length > 0) {
Expand Down Expand Up @@ -455,6 +455,6 @@ contract Grateful is IGrateful, Ownable2Step {

paymentIds[_paymentId] = true;

emit PaymentProcessed(_sender, _merchant, _token, _AMOUNT_USDC, yieldingFunds[_merchant], _paymentId);
emit PaymentProcessed(_sender, _merchant, _token, _amount, yieldingFunds[_merchant], _paymentId);
}
}
12 changes: 6 additions & 6 deletions src/contracts/OneTime.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract OneTime {
IGrateful _grateful,
address[] memory _tokens,
address _merchant,
uint256 _AMOUNT_USDC,
uint256 _amount,
uint256 _paymentId,
address[] memory _recipients,
uint256[] memory _percentages
Expand All @@ -26,18 +26,18 @@ contract OneTime {

for (uint256 i = 0; i < _tokens.length; i++) {
IERC20 token = IERC20(_tokens[i]);
if (token.balanceOf(address(this)) >= _AMOUNT_USDC) {
token.safeIncreaseAllowance(address(_grateful), _AMOUNT_USDC);
_grateful.receiveOneTimePayment(_merchant, address(token), _paymentId, _AMOUNT_USDC, _recipients, _percentages);
if (token.balanceOf(address(this)) >= _amount) {
token.safeIncreaseAllowance(address(_grateful), _amount);
_grateful.receiveOneTimePayment(_merchant, address(token), _paymentId, _amount, _recipients, _percentages);
}
}
}

function rescueFunds(IERC20 _token, address _receiver, uint256 _AMOUNT_USDC) external {
function rescueFunds(IERC20 _token, address _receiver, uint256 _amount) external {
if (msg.sender != grateful.owner()) {
revert Ownable.OwnableUnauthorizedAccount(msg.sender);
}

IERC20(_token).safeTransfer(_receiver, _AMOUNT_USDC);
IERC20(_token).safeTransfer(_receiver, _amount);
}
}
Loading

0 comments on commit 3321cc9

Please sign in to comment.