Skip to content

Commit

Permalink
contracts: bonus -> rewards (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBigBoss authored Jul 18, 2024
1 parent 411cc52 commit 6c83097
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 34 deletions.
20 changes: 10 additions & 10 deletions packages/contracts/src/SendtagCheckout.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ contract SendtagCheckout is Ownable {
/// @notice When false, the contract is paused and checkouts are not allowed.
bool public open;

/// @notice The event emitted when a referrer is paid.
event ReferralBonus(address indexed referrer, address referred, uint256 amount);
/// @notice The event emitted when a referrer is paid a reward.
event ReferralReward(address indexed referrer, address referred, uint256 amount);

/// @notice The event emitted when the contract is toggled.
event Toggled(bool open);
Expand All @@ -37,25 +37,25 @@ contract SendtagCheckout is Ownable {
/// @notice Sends the funds to the Send Multisig and remits funds to a referrer.
/// @param amount The amount of tokens to pay.
/// @param referrer The address of the referrer.
/// @param bonus The amount to pay to the referrer.
/// @param reward The amount to pay to the referrer.
/// @dev Does not check if amount is valid or not. That is done offchain.
function checkout(uint256 amount, address referrer, uint256 bonus) external {
require(open, "Not open");
function checkout(uint256 amount, address referrer, uint256 reward) external {
require(open, "Closed");
require(amount > 0, "Invalid amount");

// First, collect the amount to this contract
SafeERC20.safeTransferFrom(token, msg.sender, address(this), amount);

// Then, pay the referrer...
if (bonus > 0) {
if (reward > 0) {
require(referrer != address(0), "Invalid referrer address");
require(bonus <= amount, "Invalid referrer bonus");
SafeERC20.safeTransfer(token, referrer, bonus);
emit ReferralBonus(referrer, msg.sender, bonus);
require(reward <= amount, "Invalid referrer reward");
SafeERC20.safeTransfer(token, referrer, reward);
emit ReferralReward(referrer, msg.sender, reward);
}

// Finally, send the funds to the Send Multisig
SafeERC20.safeTransfer(token, multisig, amount - bonus);
SafeERC20.safeTransfer(token, multisig, amount - reward);
}

/// @notice Toggle the contract.
Expand Down
26 changes: 13 additions & 13 deletions packages/contracts/test/SendtagCheckout.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,36 @@ contract SendtagCheckoutTest is Test {

/// @notice Test the checkout function with a referrer.
/// Bob wants a sendtag, he just found send.app because of his friend Alice and uses her referral code
function testFuzzCheckoutReferrer(uint256 amount, uint256 bonus) public {
function testFuzzCheckoutReferrer(uint256 amount, uint256 rewards) public {
vm.assume(amount > 0);
vm.assume(bonus <= amount);
vm.assume(rewards <= amount);
address sender = address(0xb0b);
address referrer = address(0xa71ce);
vm.startPrank(sender);
token.mint(amount);
token.approve(address(checkout), amount);
if (bonus > 0) {
if (rewards > 0) {
vm.expectEmit(true, true, true, true);
emit SendtagCheckout.ReferralBonus(referrer, sender, bonus);
emit SendtagCheckout.ReferralReward(referrer, sender, rewards);
}
checkout.checkout(amount, referrer, bonus);
checkout.checkout(amount, referrer, rewards);
vm.stopPrank();
assertEq(token.balanceOf(multisig), amount - bonus);
assertEq(token.balanceOf(multisig), amount - rewards);
assertEq(token.balanceOf(sender), 0);
assertEq(token.balanceOf(referrer), bonus);
assertEq(token.balanceOf(referrer), rewards);
}

/// @notice Test the checkout function with a bonus and invalid referrer.
function testCheckoutInvalidReferrer(uint256 amount, uint256 bonus) public {
/// @notice Test the checkout function with a rewards and invalid referrer.
function testCheckoutInvalidReferrer(uint256 amount, uint256 rewards) public {
vm.assume(amount > 0);
vm.assume(bonus > 0);
vm.assume(bonus <= amount);
vm.assume(rewards > 0);
vm.assume(rewards <= amount);
address sender = address(0xb0b);
vm.startPrank(sender);
token.mint(amount);
token.approve(address(checkout), amount);
vm.expectRevert("Invalid referrer address");
checkout.checkout(amount, address(0), bonus);
checkout.checkout(amount, address(0), rewards);
vm.stopPrank();
}

Expand All @@ -93,7 +93,7 @@ contract SendtagCheckoutTest is Test {
uint256 amount = 100;
token.mint(amount);
token.approve(address(checkout), amount);
vm.expectRevert("Not open");
vm.expectRevert("Closed");
checkout.checkout(amount, address(0), 0);
vm.stopPrank();

Expand Down
20 changes: 9 additions & 11 deletions packages/wagmi/src/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3210,7 +3210,7 @@ export const sendtagCheckoutAbi = [
inputs: [
{ name: 'amount', internalType: 'uint256', type: 'uint256' },
{ name: 'referrer', internalType: 'address', type: 'address' },
{ name: 'bonus', internalType: 'uint256', type: 'uint256' },
{ name: 'reward', internalType: 'uint256', type: 'uint256' },
],
name: 'checkout',
outputs: [],
Expand Down Expand Up @@ -3287,7 +3287,7 @@ export const sendtagCheckoutAbi = [
{ name: 'referred', internalType: 'address', type: 'address', indexed: false },
{ name: 'amount', internalType: 'uint256', type: 'uint256', indexed: false },
],
name: 'ReferralBonus',
name: 'ReferralReward',
},
{
type: 'event',
Expand Down Expand Up @@ -7671,14 +7671,14 @@ export const watchSendtagCheckoutOwnershipTransferredEvent = /*#__PURE__*/ creat
)

/**
* Wraps __{@link watchContractEvent}__ with `abi` set to __{@link sendtagCheckoutAbi}__ and `eventName` set to `"ReferralBonus"`
* Wraps __{@link watchContractEvent}__ with `abi` set to __{@link sendtagCheckoutAbi}__ and `eventName` set to `"ReferralReward"`
*
*
*/
export const watchSendtagCheckoutReferralBonusEvent = /*#__PURE__*/ createWatchContractEvent({
export const watchSendtagCheckoutReferralRewardEvent = /*#__PURE__*/ createWatchContractEvent({
abi: sendtagCheckoutAbi,
address: sendtagCheckoutAddress,
eventName: 'ReferralBonus',
eventName: 'ReferralReward',
})

/**
Expand Down Expand Up @@ -12347,15 +12347,13 @@ export const useWatchSendtagCheckoutOwnershipTransferredEvent =
})

/**
* Wraps __{@link useWatchContractEvent}__ with `abi` set to __{@link sendtagCheckoutAbi}__ and `eventName` set to `"ReferralBonus"`
* Wraps __{@link useWatchContractEvent}__ with `abi` set to __{@link sendtagCheckoutAbi}__ and `eventName` set to `"ReferralReward"`
*
*
*/
export const useWatchSendtagCheckoutReferralBonusEvent = /*#__PURE__*/ createUseWatchContractEvent({
abi: sendtagCheckoutAbi,
address: sendtagCheckoutAddress,
eventName: 'ReferralBonus',
})
export const useWatchSendtagCheckoutReferralRewardEvent = /*#__PURE__*/ createUseWatchContractEvent(
{ abi: sendtagCheckoutAbi, address: sendtagCheckoutAddress, eventName: 'ReferralReward' }
)

/**
* Wraps __{@link useWatchContractEvent}__ with `abi` set to __{@link sendtagCheckoutAbi}__ and `eventName` set to `"Toggled"`
Expand Down

0 comments on commit 6c83097

Please sign in to comment.