-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ether to smart wallet payment (#780)
- Loading branch information
Showing
7 changed files
with
313 additions
and
86 deletions.
There are no files selected for viewing
125 changes: 61 additions & 64 deletions
125
packages/smart-contracts/src/contracts/EthereumFeeProxy.sol
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 |
---|---|---|
@@ -1,78 +1,75 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | ||
import '@openzeppelin/contracts/security/ReentrancyGuard.sol'; | ||
|
||
/** | ||
* @title EthereumFeeProxy | ||
* @notice This contract performs an Ethereum transfer with a Fee sent to a third address and stores a reference | ||
*/ | ||
contract EthereumFeeProxy is ReentrancyGuard{ | ||
// Event to declare a transfer with a reference | ||
event TransferWithReferenceAndFee( | ||
address to, | ||
uint256 amount, | ||
bytes indexed paymentReference, | ||
uint256 feeAmount, | ||
address feeAddress | ||
); | ||
contract EthereumFeeProxy is ReentrancyGuard { | ||
// Event to declare a transfer with a reference | ||
event TransferWithReferenceAndFee( | ||
address to, | ||
uint256 amount, | ||
bytes indexed paymentReference, | ||
uint256 feeAmount, | ||
address feeAddress | ||
); | ||
|
||
// Fallback function returns funds to the sender | ||
receive() external payable { | ||
revert("not payable receive"); | ||
} | ||
// Fallback function returns funds to the sender | ||
receive() external payable { | ||
revert('not payable receive'); | ||
} | ||
|
||
/** | ||
* @notice Performs an Ethereum transfer with a reference | ||
* @param _to Transfer recipient | ||
* @param _paymentReference Reference of the payment related | ||
* @param _feeAmount The amount of the payment fee (part of the msg.value) | ||
* @param _feeAddress The fee recipient | ||
*/ | ||
function transferWithReferenceAndFee( | ||
address payable _to, | ||
bytes calldata _paymentReference, | ||
uint256 _feeAmount, | ||
address payable _feeAddress | ||
) external payable { | ||
transferExactEthWithReferenceAndFee( | ||
_to, | ||
msg.value - _feeAmount, | ||
_paymentReference, | ||
_feeAmount, | ||
_feeAddress | ||
); | ||
} | ||
|
||
/** | ||
* @notice Performs an Ethereum transfer with a reference | ||
* @param _to Transfer recipient | ||
* @param _paymentReference Reference of the payment related | ||
* @param _feeAmount The amount of the payment fee (part of the msg.value) | ||
* @param _feeAddress The fee recipient | ||
*/ | ||
function transferWithReferenceAndFee( | ||
address payable _to, | ||
bytes calldata _paymentReference, | ||
uint256 _feeAmount, | ||
address payable _feeAddress | ||
) | ||
external | ||
payable | ||
{ | ||
transferExactEthWithReferenceAndFee( | ||
_to, | ||
msg.value - _feeAmount, | ||
_paymentReference, | ||
_feeAmount, | ||
_feeAddress | ||
); | ||
} | ||
/** | ||
* @notice Performs an Ethereum transfer with a reference with an exact amount of eth | ||
* @param _to Transfer recipient | ||
* @param _amount Amount to transfer | ||
* @param _paymentReference Reference of the payment related | ||
* @param _feeAmount The amount of the payment fee (part of the msg.value) | ||
* @param _feeAddress The fee recipient | ||
*/ | ||
function transferExactEthWithReferenceAndFee( | ||
address payable _to, | ||
uint256 _amount, | ||
bytes calldata _paymentReference, | ||
uint256 _feeAmount, | ||
address payable _feeAddress | ||
) public payable nonReentrant { | ||
(bool sendSuccess, ) = _to.call{value: _amount}(''); | ||
require(sendSuccess, 'Could not pay the recipient'); | ||
|
||
_feeAddress.transfer(_feeAmount); | ||
|
||
/** | ||
* @notice Performs an Ethereum transfer with a reference with an exact amount of eth | ||
* @param _to Transfer recipient | ||
* @param _amount Amount to transfer | ||
* @param _paymentReference Reference of the payment related | ||
* @param _feeAmount The amount of the payment fee (part of the msg.value) | ||
* @param _feeAddress The fee recipient | ||
*/ | ||
function transferExactEthWithReferenceAndFee( | ||
address payable _to, | ||
uint256 _amount, | ||
bytes calldata _paymentReference, | ||
uint256 _feeAmount, | ||
address payable _feeAddress | ||
) | ||
nonReentrant | ||
public | ||
payable | ||
{ | ||
_to.transfer(_amount); | ||
_feeAddress.transfer(_feeAmount); | ||
// transfer the remaining ethers to the sender | ||
payable(msg.sender).transfer(msg.value - _amount - _feeAmount); | ||
// transfer the remaining ethers to the sender | ||
(bool sendBackSuccess, ) = payable(msg.sender).call{ | ||
value: msg.value - _amount - _feeAmount | ||
}(''); | ||
require(sendBackSuccess, 'Could not send remaining funds to the payer'); | ||
|
||
emit TransferWithReferenceAndFee(_to, _amount, _paymentReference, _feeAmount, _feeAddress); | ||
} | ||
} | ||
emit TransferWithReferenceAndFee(_to, _amount, _paymentReference, _feeAmount, _feeAddress); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,30 +1,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
|
||
/** | ||
* @title EthereumProxy | ||
* @notice This contract performs an Ethereum transfer and stores a reference | ||
*/ | ||
contract EthereumProxy { | ||
// Event to declare a transfer with a reference | ||
event TransferWithReference(address to, uint256 amount, bytes indexed paymentReference); | ||
// Event to declare a transfer with a reference | ||
event TransferWithReference(address to, uint256 amount, bytes indexed paymentReference); | ||
|
||
// Fallback function returns funds to the sender | ||
receive() external payable { | ||
revert("not payable receive"); | ||
} | ||
// Fallback function returns funds to the sender | ||
receive() external payable { | ||
revert('not payable receive'); | ||
} | ||
|
||
/** | ||
* @notice Performs an Ethereum transfer with a reference | ||
* @param _to Transfer recipient | ||
* @param _paymentReference Reference of the payment related | ||
*/ | ||
function transferWithReference(address payable _to, bytes calldata _paymentReference) | ||
external | ||
payable | ||
{ | ||
_to.transfer(msg.value); | ||
emit TransferWithReference(_to, msg.value, _paymentReference); | ||
} | ||
/** | ||
* @notice Performs an Ethereum transfer with a reference | ||
* @param _to Transfer recipient | ||
* @param _paymentReference Reference of the payment related | ||
*/ | ||
function transferWithReference(address payable _to, bytes calldata _paymentReference) | ||
external | ||
payable | ||
{ | ||
(bool success, ) = _to.call{value: msg.value}(''); | ||
require(success, 'Could not pay the recipient'); | ||
emit TransferWithReference(_to, msg.value, _paymentReference); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/smart-contracts/src/contracts/test/EtherFallbackPayment.sol
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,15 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity >=0.7.0 <0.9.0; | ||
|
||
/// @title EtherPaymentFallback - A contract that has a fallback to accept ether payments | ||
/// @author Richard Meissner - <richard@gnosis.pm> | ||
contract EtherPaymentFallback { | ||
event SafeReceived(address indexed sender, uint256 value); | ||
|
||
/// @dev Fallback function accepts Ether transactions. | ||
receive() external payable { | ||
emit SafeReceived(msg.sender, msg.value); | ||
} | ||
|
||
constructor() {} | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/smart-contracts/src/contracts/test/GnosisProxy.sol
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,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
contract GnosisSafeProxy { | ||
address internal singleton; | ||
|
||
/// @dev Constructor function sets address of singleton contract. | ||
/// @param _singleton Singleton address. | ||
constructor(address _singleton) { | ||
require(_singleton != address(0), 'Invalid singleton address provided'); | ||
singleton = _singleton; | ||
} | ||
|
||
/// @dev Fallback function forwards all transactions and returns all received return data. | ||
fallback() external payable { | ||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
let _singleton := and(sload(0), 0xffffffffffffffffffffffffffffffffffffffff) | ||
// 0xa619486e == keccak("masterCopy()"). The value is right padded to 32-bytes with 0s | ||
if eq( | ||
calldataload(0), | ||
0xa619486e00000000000000000000000000000000000000000000000000000000 | ||
) { | ||
mstore(0, _singleton) | ||
return(0, 0x20) | ||
} | ||
calldatacopy(0, 0, calldatasize()) | ||
let success := delegatecall(gas(), _singleton, 0, calldatasize(), 0, 0) | ||
returndatacopy(0, 0, returndatasize()) | ||
if eq(success, 0) { | ||
revert(0, returndatasize()) | ||
} | ||
return(0, returndatasize()) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.