Skip to content

Commit

Permalink
fix: renamed error, move code to trigger revert
Browse files Browse the repository at this point in the history
  • Loading branch information
Maikol committed May 23, 2024
1 parent 8947c07 commit 3b39aa5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
22 changes: 12 additions & 10 deletions packages/horizon/contracts/payments/GraphPayments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract GraphPayments is Multicall, GraphDirectory, GraphPaymentsStorageV1Stora
error GraphPaymentsNotThawing();
error GraphPaymentsStillThawing(uint256 currentTimestamp, uint256 thawEndTimestamp);
error GraphPaymentsCollectorNotAuthorized(address sender, address dataService);
error GraphPaymentsCollectorInsufficientAmount(uint256 available, uint256 required);
error GraphPaymentsInsufficientAmount(uint256 available, uint256 required);

// -- Events --

Expand All @@ -54,25 +54,27 @@ contract GraphPayments is Multicall, GraphDirectory, GraphPaymentsStorageV1Stora
) external {
_graphToken().pullTokens(msg.sender, tokens);

// Pay protocol cut
// Calculate cuts
uint256 tokensProtocol = (tokens * PROTOCOL_PAYMENT_CUT) / MAX_PPM;
uint256 delegationFeeCut = _graphStaking().getDelegationFeeCut(receiver, dataService, uint8(paymentType));
uint256 tokensDelegationPool = (tokens * delegationFeeCut) / MAX_PPM;
uint256 totalCut = tokensProtocol + tokensDataService + tokensDelegationPool;
if (totalCut > tokens) {
revert GraphPaymentsInsufficientAmount(tokens, totalCut);
}

// Pay protocol cut
_graphToken().burnTokens(tokensProtocol);

// Pay data service cut
_graphToken().pushTokens(dataService, tokensDataService);

// Get delegation cut
uint256 delegationFeeCut = _graphStaking().getDelegationFeeCut(receiver, dataService, uint8(paymentType));
uint256 tokensDelegationPool = (tokens * delegationFeeCut) / MAX_PPM;
// Pay delegators
if (tokensDelegationPool > 0) {
_graphStaking().addToDelegationPool(receiver, dataService, tokensDelegationPool);
}

// Pay the rest to the receiver
uint256 totalCut = tokensProtocol + tokensDataService + tokensDelegationPool;
if (totalCut > tokens) {
revert GraphPaymentsCollectorInsufficientAmount(tokens, totalCut);
}
// Pay receiver
uint256 tokensReceiverRemaining = tokens - totalCut;
_graphToken().pushTokens(receiver, tokensReceiverRemaining);

Expand Down
28 changes: 27 additions & 1 deletion packages/horizon/test/payments/GraphPayments.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { HorizonStakingSharedTest } from "../shared/horizon-staking/HorizonStaki

contract GraphPaymentsTest is HorizonStakingSharedTest {

function testCollect(uint256 amount, uint256 tokensDataService) public useProvision(amount, 0, 0) useDelegationFeeCut(0, 100000) {
function testCollect(
uint256 amount,
uint256 tokensDataService
) public useProvision(amount, 0, 0) useDelegationFeeCut(0, delegationFeeCut) {
uint256 tokensProtocol = amount * protocolPaymentCut / MAX_PPM;
uint256 tokensDelegatoion = amount * delegationFeeCut / MAX_PPM;
vm.assume(tokensDataService < amount - tokensProtocol - tokensDelegatoion);
Expand All @@ -35,4 +38,27 @@ contract GraphPaymentsTest is HorizonStakingSharedTest {
uint256 delegatorBalance = staking.getDelegatedTokensAvailable(users.indexer, subgraphDataServiceAddress);
assertEq(delegatorBalance, tokensDelegatoion);
}

function testCollect_RevertWhen_InsufficientAmount(
uint256 amount,
uint256 tokensDataService
) public useProvision(amount, 0, 0) useDelegationFeeCut(0, delegationFeeCut) {
vm.assume(tokensDataService <= 10_000_000_000 ether);
vm.assume(tokensDataService > amount);

address escrowAddress = address(escrow);
mint(escrowAddress, amount);
vm.startPrank(escrowAddress);
approve(address(payments), amount);

bytes memory expectedError;
{
uint256 tokensProtocol = amount * protocolPaymentCut / MAX_PPM;
uint256 tokensDelegatoion = amount * delegationFeeCut / MAX_PPM;
uint256 requiredAmount = tokensDataService + tokensProtocol + tokensDelegatoion;
expectedError = abi.encodeWithSignature("GraphPaymentsInsufficientAmount(uint256,uint256)", amount, requiredAmount);
}
vm.expectRevert(expectedError);
payments.collect(IGraphPayments.PaymentTypes.QueryFee, users.indexer, amount, subgraphDataServiceAddress, tokensDataService);
}
}

0 comments on commit 3b39aa5

Please sign in to comment.