Skip to content

Commit

Permalink
add test cases for ticketing contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick95550 committed Jul 3, 2024
1 parent bf00ab0 commit f28c9ed
Show file tree
Hide file tree
Showing 6 changed files with 1,725 additions and 45 deletions.
10 changes: 10 additions & 0 deletions common/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const DeployedContractNames = {
authorizedAccounts: 'AuthorizedAccounts',
rewardsManager: 'RewardsManager',
ticketing: 'Ticketing',
futurepass: 'FuturepassRegistrar',
};

export const ContractNames = {
Expand All @@ -33,6 +34,7 @@ export type SyloContracts = {
rewardsManager: factories.contracts.payments.RewardsManager;
deposits: factories.contracts.payments.Deposits;
ticketing: factories.contracts.payments.Ticketing;
futurepassRegistrar: factories.contracts.mocks.TestFuturepassRegistrar;
};

export type ContractAddresses = {
Expand All @@ -47,6 +49,7 @@ export type ContractAddresses = {
deposits: string;
ticketing: string;
rewardsManager: string;
futurepassRegistrar: string;
};

export function connectContracts(
Expand Down Expand Up @@ -108,6 +111,12 @@ export function connectContracts(
provider,
);

const futurepassRegistrar =
factories.TestFuturepassRegistrar__factory.connect(
contracts.futurepassRegistrar,
provider,
);

return {
syloToken,
syloStakingManager,
Expand All @@ -120,5 +129,6 @@ export function connectContracts(
deposits,
ticketing,
rewardsManager,
futurepassRegistrar,
};
}
6 changes: 0 additions & 6 deletions contracts/payments/RewardsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ contract RewardsManager is IRewardsManager, Initializable, AccessControl {
*/
Registries public registries;

/**
* @notice Ticketing contract
*/
Ticketing public ticketing;

/**
* @notice Tracks claims from staker accounts
*/
Expand Down Expand Up @@ -59,7 +54,6 @@ contract RewardsManager is IRewardsManager, Initializable, AccessControl {
}

registries = _registries;
ticketing = _ticketing;

_grantRole(onlyTicketing, address(_ticketing));
}
Expand Down
39 changes: 12 additions & 27 deletions contracts/payments/Ticketing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
);

error FaceValueCannotBeZero();
error MultiReceiverFaceValueCannotBeZero();
error TicketDurationCannotBeZero();
error InvalidSigningPermission();
error SenderCannotUseAttachedAuthorizedAccount();
Expand All @@ -127,6 +128,7 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
error TicketAlreadyUsed();
error TicketEpochNotFound();
error TicketAlreadyRedeemed();
error MultiReceiverTicketAlreadyRedeemed();
error RedeemerCommitMismatch();
error InvalidSignature();
error TokenAddressCannotBeNil();
Expand Down Expand Up @@ -194,7 +196,7 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165

function setMultiReceiverFaceValue(uint256 _multiReceiverFaceValue) external onlyOwner {
if (_multiReceiverFaceValue == 0) {
revert FaceValueCannotBeZero();
revert MultiReceiverFaceValueCannotBeZero();
}

multiReceiverFaceValue = _multiReceiverFaceValue;
Expand Down Expand Up @@ -336,15 +338,8 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
);
}

function _redeemMultiReceiver(
MultiReceiverTicket calldata ticket,
address receiver
) internal {
uint256 rewardAmount = rewardRedeemer(
ticket.cycle,
ticket.sender,
ticket.redeemer
);
function _redeemMultiReceiver(MultiReceiverTicket calldata ticket, address receiver) internal {
uint256 rewardAmount = rewardRedeemer(ticket.cycle, ticket.sender, ticket.redeemer);

emit MultiReceiverRedemption(
ticket.cycle,
Expand All @@ -367,19 +362,13 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165

if (faceValue > deposit.escrow) {
amount = deposit.escrow;
incrementRewardPool(redeemer, cycle, deposit, amount);
SafeERC20.safeTransferFrom(
_token,
address(_deposits),
address(0x000000000000000000000000000000000000dEaD),
deposit.penalty
);
_deposits.removePenalty(sender);
incrementRewardPool(sender, redeemer, cycle, amount);

delete deposit.penalty;
emit SenderPenaltyBurnt(sender);
} else {
amount = faceValue;
incrementRewardPool(redeemer, cycle, deposit, amount);
incrementRewardPool(sender, redeemer, cycle, amount);
}

return amount;
Expand Down Expand Up @@ -496,7 +485,7 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
ticketHash = getMultiReceiverTicketHash(ticket);
ticketReceiverHash = keccak256(abi.encodePacked(ticketHash, futurepassAccount));
if (usedTickets[ticketReceiverHash]) {
revert TicketAlreadyRedeemed();
revert MultiReceiverTicketAlreadyRedeemed();
}

// validate the redeemer has knowledge of the redeemer rand
Expand Down Expand Up @@ -608,9 +597,7 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
* on the decay rate parameter of the epoch the ticket was generated in.
* @param generationBlock The generationBlock of the ticket.
*/
function calculateWinningProbability(
uint256 generationBlock
) public view returns (uint128) {
function calculateWinningProbability(uint256 generationBlock) public view returns (uint128) {
uint256 elapsedDuration = block.number - generationBlock;

// Ticket has completely expired
Expand Down Expand Up @@ -671,14 +658,12 @@ contract Ticketing is ITicketing, Initializable, Ownable2StepUpgradeable, ERC165
}

function incrementRewardPool(
address sender,
address stakee,
uint256 cycle,
IDeposits.Deposit memory deposit,
uint256 amount
) internal {
deposit.escrow = deposit.escrow - amount;

SafeERC20.safeTransferFrom(_token, address(_deposits), address(_rewardsManager), amount);
_deposits.spendEscrow(sender, amount);
_rewardsManager.incrementRewardPool(stakee, cycle, amount);
}

Expand Down
3 changes: 3 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const config: HardhatUserConfig = {
optimizer: {
enabled: true,
runs: 200,
details: {
yul: true,
},
},
},
},
Expand Down
Loading

0 comments on commit f28c9ed

Please sign in to comment.