Developer Hub
🔮 For applications
Guides & Tutorials
How to calculate affiliate's gaming revenue

Get bets made on specific Frontend (Affiliate)


This documentation provides step-by-step instructions on how to calculate Pool Revenue per user for a specific affiliate using a GraphQL query. We will explain the GraphQL query and variables, introduce the Pool Revenue formula, and detail how to calculate it for each user based on this formula.

GraphQL Query

The GraphQL query used to retrieve the necessary data for calculating Pool Revenue on a specific frontend per user is as follows:

query Bets {
  bets(
    where: {
      status_in: [Resolved, Canceled],
      affiliate: $affiliate
    }
    first: 1000,
    skip: $skip
  ) {
    bettor
    odds
    amount
    payout
    _isFreebet
  }
}

Explaining Variables:

  • $affiliate: The affiliate ID for which pool revenue is being calculated.
  • $skip: Used for pagination to skip a certain number of records when fetching data.

Pool Revenue

Formula:

The formula to calculate pool revenue (PoolRev aka ggr) is:

PoolRev = totalBetAmount - totalWonAmount

Where:

  • totalBetAmount is the sum of all bet amounts placed by users.
  • totalWonAmount is the sum of all amounts won by users.

How to Calculate totalBetAmount:

To calculate the totalBetAmount, sum up the amount of each bet retrieved from the GraphQL query:

totalBetAmount = sum(bet.amount)

How to Calculate totalWonAmount:

To calculate the totalWonAmount we use two formulas depending on _isFreebet status of bet:

  • If bet._isFreebet == false :
totalWonAmount = sum(bet.payout)
  • If bet._isFreebet == true :
totalWonAmount = sum(bet.amount * (bet.odds - 1))

Calculating Affiliate's Pool Revenue

To calculate the Pool Revenue for a specific affiliate:

  • Execute the GraphQL query with the affiliate's identifier.
  • Apply the Pool Revenue formula as described above.
 
. . .
// Sum up bet amounts
const totalBetAmount = bets.reduce((e: number, {amount}) => e+Number(amount), 0);
 
// Sum up won amount based on bet status
const totalWonAmount = bets.reduce((e: number, bet) => {
    if (bet._isFreebet) {
        e += Number(bet.amount) * (Number(bet.odds) - 1)
    } else {
        e += Number(bet.payout)
    }
 
    return e
}, 0);

Calculating Pool Revenue for a Specific Period of Time

where: {
      status_in: [Resolved, Canceled],
      resolvedBlockTimestamp_gte: $since,
      resolvedBlockTimestamp_lte: $until,
      affiliate: $affiliate
}
  • $since: The start date for the date range within which bets should be considered.
  • $until: The end date for the date range within which bets should be considered.

Calculating Pool Revenue for Each User

To calculate the Pool Revenue for each user based on the above formulas, follow these steps:

  1. Execute the GraphQL query to retrieve bets for the specific affiliate ($affiliate) within the specified date range ($since to $until).
  2. Loop through the retrieved bets:
    • For each bet, identify the bettoramount , odds , _isFreebet and payout.
  3. Calculate Pool Revenue for each user:
    • Initialize an empty dictionary or data structure to store the calculated Pool Revenue for each user.
    • For each bet, determine the user who placed the bet.
    • Calculate the Pool Revenue for that user using the PoolRev formula:
PoolRev = totalBetAmount - totalWonAmount

4.  Continue this process until you have processed all the retrieved bets.

5.  The dictionary or data structure will now contain the calculated PoolRev for each user. You can access these PoolRev values for the specific affiliate and perform further analysis or reporting. By following these steps, you can successfully calculate the Pool Revenue per user for a specific affiliate based on their betting activity within the specified date range. The provided formula helps you understand how Pool Revenue is derived from the total bet and won amounts.

Code example

import {request, gql} from "graphql-request";
 
interface BetSpec {
  bettor: string;
  amount: Number;
  payout: Number;
  odds: Number;
  _isFreebet: boolean;
}
 
// Initialize an empty dictionary to store PoolRev (aka ggr) per user
const ggrPerUser: { [userId: string]: number } = {};
 
// Set initial skip value to 0 for pagination
let skip = 0;
 
while (true) {
  // Execute the GraphQL query with the appropriate variables
  const graphQuery = gql`...`; // Insert the GraphQL query here
 
  // Make the request and retrieve the data
  const graphRes = await request(graphUrl, graphQuery);
 
  // Process each bet
  for (const bet of graphRes.bets as BetSpec[]) {
    const { bettor, amount, payout, odds, _isFreebet } = bet;
 
    // Calculate PoolRev (aka ggr) for each bet
    if (_isFreebet) {
      const ggr = amount - amount * (odds - 1);
    } else {
      const ggr = amount - payout;
    }
 
    // Add PoolRev (aka ggr) to the user's total
    if (!ggrPerUser[bettor]) {
      ggrPerUser[bettor] = 0;
    }
    ggrPerUser[bettor] += ggr;
 
    // Continue processing until all bets are retrieved
  }
 
  // Increment the skip value for pagination
  skip += 1000;
 
  // Check if there are more bets to fetch
  if (graphRes.bets.length < 1000) {
    break;
  }
}