Skip to content
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

chore: comments refferal #112

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions contracts/ReferralRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ contract ReferralRegistry is UUPSHelper {
address _paymentToken
) external payable {
if (referralPrograms[key].owner != address(0)) revert Errors.KeyAlreadyUsed();
if (msg.value != costReferralProgram) revert Errors.NotEnoughPayment();
require(
_cost == 0 || (_cost > 0 && _requiresRefererToBeSet),
"Cost must be set if requiresRefererToBeSet is true"
);
if (msg.value < costReferralProgram) revert Errors.NotEnoughPayment();
if (_cost != 0 && !_requiresRefererToBeSet) revert Errors.InvalidParam();
referralKeys.push(key);
referralPrograms[key] = ReferralProgram({
owner: _owner,
Expand All @@ -91,9 +88,9 @@ contract ReferralRegistry is UUPSHelper {
});
if (costReferralProgram > 0) {
(bool sent, ) = feeRecipient.call{ value: msg.value }("");
require(sent, "Failed to send Ether");
if (!sent) revert Errors.NotEnoughPayment();
}
emit ReferralKeyAdded(key);
emit ReferralKeyAdded(key, referralPrograms[key]);
}

/// @notice Edits the parameters of a referral program
Expand All @@ -110,6 +107,8 @@ contract ReferralRegistry is UUPSHelper {
address newPaymentToken
) external {
if (referralPrograms[key].owner != msg.sender) revert Errors.NotAllowed();
if (newCost != 0 && !newRequiresRefererToBeSet) revert Errors.InvalidParam();

referralPrograms[key] = ReferralProgram({
owner: referralPrograms[key].owner,
requiresAuthorization: newRequiresAuthorization,
Expand Down Expand Up @@ -140,23 +139,25 @@ contract ReferralRegistry is UUPSHelper {
/// @param referrerCode The code of the referrer
function becomeReferrer(string calldata key, string calldata referrerCode) external payable {
if (referralPrograms[key].owner == address(0)) revert Errors.NotAllowed();
require(codeToReferrer[key][referrerCode] == address(0), "Referrer code already in use");
if (codeToReferrer[key][referrerCode] != address(0)) revert Errors.KeyAlreadyUsed();
ReferralProgram storage program = referralPrograms[key];
if (program.requiresAuthorization) {
if (refererStatus[key][msg.sender] != ReferralStatus.Allowed) revert Errors.NotAllowed();
}
refererStatus[key][msg.sender] = ReferralStatus.Set;
referrerCodeMapping[key][msg.sender] = referrerCode;
codeToReferrer[key][referrerCode] = msg.sender;
if (program.cost > 0) {
if (address(program.paymentToken) == address(0)) {
if (msg.value < program.cost) revert Errors.NotEnoughPayment();
// Are we sure this is safe? Like couldn't it loop by adding code to `program.owner` while only paying
// once `program.cost`
(bool sent, ) = program.owner.call{ value: msg.value }("");
require(sent, "Failed to send Ether");
} else {
IERC20(program.paymentToken).safeTransferFrom(msg.sender, program.owner, program.cost);
}
}
if (program.requiresAuthorization) {
if (refererStatus[key][msg.sender] != ReferralStatus.Allowed) revert Errors.NotAllowed();
}
refererStatus[key][msg.sender] = ReferralStatus.Set;
referrerCodeMapping[key][msg.sender] = referrerCode;
codeToReferrer[key][referrerCode] = msg.sender;
emit ReferrerAdded(key, msg.sender);
}

Expand All @@ -176,7 +177,7 @@ contract ReferralRegistry is UUPSHelper {
}
}
if (referralPrograms[key].requiresRefererToBeSet) {
require(refererStatus[key][referrer] == ReferralStatus.Set, "Referrer has not created a referral link");
if (refererStatus[key][referrer] != ReferralStatus.Set) revert Errors.RefererNotSet();
}
keyToUserToReferrer[key][msg.sender] = referrer;
keyToReferred[key][referrer].push(msg.sender);
Expand All @@ -186,8 +187,11 @@ contract ReferralRegistry is UUPSHelper {
/// @notice Allows a user to acknowledge that they are referred by a referrer using a referrer code
/// @param key The referral key for which the user is acknowledging the referrer
/// @param referrerCode The code of the referrer
/// TODO: This function doesn't work when `referralPrograms[key].requiresRefererToBeSet` is false and referrerCode is
/// not set
function acknowledgeReferrerByKey(string calldata key, string calldata referrerCode) external {
address referrer = codeToReferrer[key][referrerCode];
if (referrer == address(0)) revert Errors.NotAllowed();
acknowledgeReferrer(key, referrer);
}

Expand Down Expand Up @@ -216,7 +220,7 @@ contract ReferralRegistry is UUPSHelper {
bool newRequiresRefererToBeSet,
address newPaymentToken
);
event ReferralKeyAdded(string indexed key);
event ReferralKeyAdded(string indexed key, ReferralProgram program);
event ReferralKeyRemoved(uint256 index);
event UpgradeabilityRevoked();

Expand Down Expand Up @@ -273,6 +277,9 @@ contract ReferralRegistry is UUPSHelper {
function getReferralKeys() external view returns (string[] memory) {
return referralKeys;
}

// Why did we add all the below getters?

/// @notice Gets the details of a referral program
/// @param key The referral key to get details for
/// @return The details of the referral program
Expand Down
1 change: 1 addition & 0 deletions contracts/utils/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ library Errors {
error ReentrantCall();
error WithdrawalFailed();
error InvalidClaim();
error RefererNotSet();
}
2 changes: 1 addition & 1 deletion scripts/deployReferralRegistry.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract DeployReferralRegistry is BaseScript {
console.log("ReferralRegistry Implementation:", implementation);

// Deploy proxy
ERC1967Proxy proxy = new ERC1967Proxy(implementation, "");
ERC1967Proxy proxy = new ERC1967Proxy{salt: vm.envBytes32("DEPLOY_SALT_2")}(implementation, "");
console.log("ReferralRegistry Proxy:", address(proxy));

// Initialize
Expand Down
2 changes: 1 addition & 1 deletion test/unit/ReferralRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ contract ReferralRegistryTest is Test {
referralRegistry.addReferralKey{value: fee}(referralKey, cost, requiresRefererToBeSet, owner, requiresAuthorization, paymentToken);

uint256 newCost = 2000;
bool newRequiresRefererToBeSet = false;
bool newRequiresRefererToBeSet = true;
bool newRequiresAuthorization = false;
address newPaymentToken = address(new MockERC20());

Expand Down