Skip to content

Commit

Permalink
chg: Modify the RegisterHelper contract to support transferring eth t…
Browse files Browse the repository at this point in the history
…o the contract
  • Loading branch information
twygod committed Jan 26, 2024
1 parent 9b7aa18 commit 5eedb08
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ forge create --rpc-url <your_rpc_url> \
**Optimism-Goerli:**

- Referral: [0xbae830e2871E339D48912Fb5547808E9e0EE1aaD](https://goerli-optimism.etherscan.io/address/0xbae830e2871E339D48912Fb5547808E9e0EE1aaD)
- RegisterHelper: [0x5F7c58fB44f7C94c68EF4102780cab913C16b302](https://goerli-optimism.etherscan.io/address/0x5F7c58fB44f7C94c68EF4102780cab913C16b302)
- RegisterHelper: [0xC38C2Bb8eD28fb87649DF1e5f02217441b12d49D](https://goerli-optimism.etherscan.io/address/0xC38C2Bb8eD28fb87649DF1e5f02217441b12d49D)
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "union-referral",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "forge test -vvv",
"compile": "forge build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/unioncredit/union-referral.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/unioncredit/union-referral/issues"
},
"homepage": "https://github.com/unioncredit/union-referral#readme"
"name": "union-referral",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "forge test -vv",
"compile": "forge build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/unioncredit/union-referral.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/unioncredit/union-referral/issues"
},
"homepage": "https://github.com/unioncredit/union-referral#readme"
}
15 changes: 10 additions & 5 deletions src/RegisterHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract RegisterHelper is AccessControl {
uint _rebate,
uint _regFee
) AccessControl(_admin) {
if (_regFeeRecipient == address(0)) revert NullAddress();
if (_regFeeRecipient == payable(address(0))) revert NullAddress();
union = _union;
userManager = _userManager;
referral = _referral;
Expand Down Expand Up @@ -116,12 +116,12 @@ contract RegisterHelper is AccessControl {

uint diff = msg.value - regFee - rebate;
if(referrer != address(0)) {
if(regFee > 0)regFeeRecipient.transfer(regFee);
if(rebate > 0)referrer.transfer(rebate);
if(regFee > 0)sendCall(regFeeRecipient, regFee);
if(rebate > 0)sendCall(referrer, rebate);
} else {
if(regFee > 0 || rebate > 0)regFeeRecipient.transfer(regFee + rebate);
if(regFee > 0 || rebate > 0)sendCall(regFeeRecipient,regFee + rebate);
}
if(diff > 0)payable(msg.sender).transfer(diff);
if(diff > 0)sendCall(payable(msg.sender), diff);

IUserManager(userManager).registerMember(newUser);
emit Register(newUser, regFeeRecipient, referrer, msg.value, regFee, rebate);
Expand All @@ -133,4 +133,9 @@ contract RegisterHelper is AccessControl {
uint256 balance = token.balanceOf(address(this));
token.transfer(recipient, balance);
}

function sendCall(address payable to, uint amount) public payable {
(bool sent, bytes memory data) = to.call{value: amount}("");
require(sent, "Failed to send Ether");
}
}
48 changes: 48 additions & 0 deletions src/mocks/GnosisSafeProxyMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*Submitted for verification at Optimistic.Etherscan.io on 2021-12-21
*/

// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity 0.8.19;

/// @title IProxy - Helper interface to access masterCopy of the Proxy on-chain
/// @author Richard Meissner - <[email protected]>
interface IProxy {
function masterCopy() external view returns (address);
}

/// @title GnosisSafeProxy - Generic proxy contract allows to execute all transactions applying the code of a master contract.
/// @author Stefan George - <[email protected]>
/// @author Richard Meissner - <[email protected]>
contract GnosisSafeProxyMock {
// singleton always needs to be first declared variable, to ensure that it is at the same location in the contracts to which calls are delegated.
// To reduce deployment costs this variable is internal and needs to be retrieved via `getStorageAt`
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())
}
}
}
6 changes: 4 additions & 2 deletions test/TestRegisterMemberUseRegHelp.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.19;

import {TestWrapper} from "./TestWrapper.sol";
import "forge-std/console.sol";
import {Referral} from "src/Referral.sol";
import {RegisterHelper} from "src/RegisterHelper.sol";

Expand All @@ -16,14 +17,15 @@ contract TestRegisterMemberUseRegHelp is TestWrapper {
uint public regFee;

function setUp() public virtual {
deployMocks();

admin = address(1);
comp = address(2);
regFeeRecipient = payable(address(3));
regFeeRecipient = payable(address(gnosisSafeProxyMock));
sender = payable(address(4));//The default sender for testing is a contract and cannot accept eth, so we need to change the address.
vm.deal(sender, 100 ether);
rebate = 5e17;
regFee = 1e18;
deployMocks();
referral = new Referral(admin, comp);
registerHelper = new RegisterHelper(
admin,
Expand Down
3 changes: 3 additions & 0 deletions test/TestWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ pragma solidity 0.8.19;
import "forge-std/Test.sol";
import {UnionTokenMock} from "src/mocks/UnionTokenMock.sol";
import {UserManagerMock} from "src/mocks/UserManagerMock.sol";
import {GnosisSafeProxyMock} from "src/mocks/GnosisSafeProxyMock.sol";

contract TestWrapper is Test {
UnionTokenMock public unionTokenMock;
UserManagerMock public userManagerMock;
GnosisSafeProxyMock public gnosisSafeProxyMock;

function deployMocks() public {
unionTokenMock = new UnionTokenMock();
userManagerMock = new UserManagerMock(address(unionTokenMock));
gnosisSafeProxyMock = new GnosisSafeProxyMock(address(11));
}
}

0 comments on commit 5eedb08

Please sign in to comment.