Skip to content

Commit

Permalink
Merge pull request #56 from 0xProject/feat/add-member-zrx-staked-to-p…
Browse files Browse the repository at this point in the history
…ool-epoch-rewards

Adding member_zrx_staked to epoch pool rewards query
  • Loading branch information
sui414 authored Mar 31, 2021
2 parents ca0d864 + 7b5e278 commit 3cdc149
Show file tree
Hide file tree
Showing 7 changed files with 529 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ lib
.env.kovan
package-lock.json
yarn-error.log
.DS_Store
3 changes: 3 additions & 0 deletions staking-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"build": "yarn tsc -b",
"clean": "rm -rf lib",
"dev": "nodemon -r dotenv/config src/index.ts | pino-pretty",
"lint": "tslint --project . --format stylish && yarn prettier:ci",
"prettier": "prettier --write '**/*.{ts,tsx,json,md}' --config ../.prettierrc --ignore-path ../../.prettierignore",
"prettier:ci": "prettier --list-different '**/*.{ts,tsx,json,md}' --config ../.prettierrc --ignore-path ../../.prettierignore",
Expand All @@ -29,6 +30,8 @@
"mocha": "^6.2.2",
"prettier": "^1.18.2",
"source-map-support": "^0.5.19",
"nodemon": "^1.19.4",
"pino-pretty": "^3.2.2",
"supertest": "^4.0.2",
"tslint": "^6.1.3"
},
Expand Down
39 changes: 39 additions & 0 deletions staking-api/src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ export const stakingPoolsQuery = `SELECT * FROM staking.pool_info;`;

export const stakingPoolByIdQuery = `SELECT * FROM staking.pool_info WHERE pool_id = $1;`;

export const allTimePoolStakedAmountsQuery = `
SELECT
rpe.pool_id AS pool_id
, e.epoch_id AS epoch_id
, COALESCE(esps.member_zrx_delegated, 0.00) AS member_zrx_staked
FROM events.rewards_paid_events rpe
FULL JOIN staking.epochs e ON e.epoch_id = (rpe.epoch_id - 1)
LEFT JOIN staking.epoch_start_pool_status esps ON (esps.epoch_id = e.epoch_id AND esps.pool_id = rpe.pool_id)
WHERE rpe.pool_id IS NOT NULL
ORDER BY rpe.pool_id::int ASC, epoch_id;
`;

export const poolSevenDayProtocolFeesGeneratedQuery = `
WITH pool_7d_fills AS (
SELECT
Expand Down Expand Up @@ -337,6 +349,33 @@ export const poolTotalProtocolFeesGeneratedQuery = `
`;

export const poolEpochRewardsQuery = `
SELECT
$1::text AS pool_id
, e.epoch_id AS epoch_id
, e.starting_block_timestamp
, e.starting_block_number
, e.starting_transaction_index
, e.ending_timestamp
, e.ending_block_number
, e.ending_transaction_hash
, COALESCE(esps.member_zrx_delegated, 0) AS member_zrx_staked
, COALESCE(rpe.operator_reward / 1e18,0) AS operator_reward
, COALESCE(rpe.members_reward / 1e18,0) AS members_reward
, COALESCE((rpe.operator_reward + rpe.members_reward) / 1e18,0) AS total_reward
FROM events.rewards_paid_events rpe
FULL JOIN staking.epochs e ON e.epoch_id = (rpe.epoch_id - 1)
LEFT JOIN staking.epoch_start_pool_status esps ON (esps.epoch_id = e.epoch_id AND esps.pool_id = rpe.pool_id)
LEFT JOIN staking.current_epoch ce ON ce.epoch_id = e.epoch_id
WHERE
(
rpe.pool_id = $1
OR rpe.pool_id IS NULL
)
AND ce.epoch_id IS NULL
ORDER BY epoch_id ASC;
`;

export const poolEpochRewardsOldQuery = `
SELECT
$1::text AS pool_id
, e.epoch_id AS epoch_id
Expand Down
52 changes: 51 additions & 1 deletion staking-api/src/query_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
AllTimeDelegatorPoolStats,
AllTimeDelegatorStats,
AllTimePoolStats,
AllTimePoolStakedAmount,
AllTimePoolStakedAmounts,
AllTimeStakingStats,
DelegatorEvent,
Epoch,
Expand All @@ -20,6 +22,7 @@ import {
PoolWithStats,
RawAllTimeDelegatorPoolsStats,
RawAllTimePoolRewards,
RawAllTimePoolStakedAmount,
RawAllTimeStakingStats,
RawDelegatorDeposited,
RawDelegatorEvent,
Expand Down Expand Up @@ -118,7 +121,7 @@ export class QueryRunner {

public async getStakingPoolEpochRewardsAsync(poolId: string): Promise<PoolEpochRewards[]> {
const rawPoolEpochRewards: RawPoolEpochRewards[] = await (await getDbAsync()).query(
queries.poolEpochRewardsQuery,
queries.poolEpochRewardsOldQuery,
[poolId],
);
const poolEpochRewards = stakingUtils.getPoolEpochRewardsFromRaw(rawPoolEpochRewards);
Expand Down Expand Up @@ -184,13 +187,37 @@ export class QueryRunner {
}

public async getStakingPoolsWithStatsAsync(): Promise<PoolWithStats[]> {
const start = process.hrtime();
const pools = await this.getStakingPoolsAsync();
let end = process.hrtime(start);
let timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
console.log(`getStakingPoolsAsync time elapsed:${timeInMs}`);
const rawCurrentEpochPoolStats = await (await getDbAsync()).query(queries.currentEpochPoolsStatsQuery);
end = process.hrtime(start);
timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
console.log(`rawCurrentEpochPoolStats time elapsed:${timeInMs}`);
const rawNextEpochPoolStats = await (await getDbAsync()).query(queries.nextEpochPoolsStatsQuery);
end = process.hrtime(start);
timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
console.log(`rawNextEpochPoolStats time elapsed:${timeInMs}`);
const rawPoolSevenDayProtocolFeesGenerated = await (await getDbAsync()).query(
queries.sevenDayProtocolFeesGeneratedQuery,
);
end = process.hrtime(start);
timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
console.log(`rawPoolSevenDayProtocolFeesGenerated time elapsed:${timeInMs}`);

const rawPoolsAvgRewards = await (await getDbAsync()).query(queries.poolsAvgRewardsQuery);
end = process.hrtime(start);
timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
console.log(`rawPoolsAvgRewards time elapsed:${timeInMs}`);

const rawAllTimePoolStakedAmounts = await (await getDbAsync()).query(queries.allTimePoolStakedAmountsQuery);
end = process.hrtime(start);
timeInMs = (end[0] * 1000000000 + end[1]) / 1000000;
console.log(`allTimePoolStakedAmounts time elapsed:${timeInMs}`);

const allTimePoolStakedAmounts = stakingUtils.getAllTimePoolStakedAmountsFromRaw(rawAllTimePoolStakedAmounts);
const currentEpochPoolStats = stakingUtils.getEpochPoolsStatsFromRaw(rawCurrentEpochPoolStats);
const nextEpochPoolStats = stakingUtils.getEpochPoolsStatsFromRaw(rawNextEpochPoolStats);
const poolProtocolFeesGenerated = stakingUtils.getPoolsProtocolFeesGeneratedFromRaw(
Expand All @@ -201,6 +228,7 @@ export class QueryRunner {
const nextEpochPoolStatsMap = arrayToMapWithId(nextEpochPoolStats, 'poolId');
const poolProtocolFeesGeneratedMap = arrayToMapWithId(poolProtocolFeesGenerated, 'poolId');
const poolAvgRewardsMap = arrayToMapWithId(poolAvgRewards, 'poolId');

return pools.map(pool => ({
...pool,
sevenDayProtocolFeesGeneratedInEth:
Expand All @@ -210,6 +238,7 @@ export class QueryRunner {
avgMemberRewardEthPerZrx: poolAvgRewardsMap[pool.poolId].avgMemberRewardEthPerZrx,
currentEpochStats: currentEpochPoolStatsMap[pool.poolId],
nextEpochStats: nextEpochPoolStatsMap[pool.poolId],
allTimeStakedAmounts: allTimePoolStakedAmounts[pool.poolId],
}));
}

Expand Down Expand Up @@ -411,6 +440,7 @@ export const stakingUtils = {
epochEndTimestamp: epochReward.ending_timestamp,
operatorRewardsPaidInEth: Number(epochReward.operator_reward || 0),
membersRewardsPaidInEth: Number(epochReward.members_reward || 0),
memberZrxStaked: Number(epochReward.member_zrx_staked || 0),
totalRewardsPaidInEth: Number(epochReward.total_reward || 0),
}));
},
Expand Down Expand Up @@ -508,4 +538,24 @@ export const stakingUtils = {
totalRewardsPaidInEth: Number(total_rewards_paid || 0),
};
},
getAllTimePoolStakedAmountsFromRaw: (
rawAllTimePoolStakedAmounts: RawAllTimePoolStakedAmount[],
): AllTimePoolStakedAmounts => {
const allTimePoolStakedAmounts: AllTimePoolStakedAmounts = {};

rawAllTimePoolStakedAmounts.forEach(poolStakedAmountForEpoch => {
const poolId = poolStakedAmountForEpoch.pool_id;
const poolStakedAmountForEpochData = {
epochId: Number(poolStakedAmountForEpoch.epoch_id),
memberZrxStaked: Number(poolStakedAmountForEpoch.member_zrx_staked || 0),
};
if (allTimePoolStakedAmounts[poolId]) {
allTimePoolStakedAmounts[poolId].push(poolStakedAmountForEpochData);
} else {
allTimePoolStakedAmounts[poolId] = [poolStakedAmountForEpochData];
}
});

return allTimePoolStakedAmounts;
},
};
16 changes: 16 additions & 0 deletions staking-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface RawPoolEpochRewards {
operator_reward: string;
members_reward: string;
total_reward: string;
member_zrx_staked: string;
// Fields below are available but not used in response
starting_block_timestamp: string;
starting_block_number: string;
Expand Down Expand Up @@ -273,3 +274,18 @@ export interface DelegatorEvent {
transactionHash: string | null;
eventArgs: object;
}

export interface RawAllTimePoolStakedAmount {
pool_id: string;
epoch_id: string;
member_zrx_staked: string;
}

export interface AllTimePoolStakedAmount {
epochId: number;
memberZrxStaked: number;
}

export interface AllTimePoolStakedAmounts {
[key: string]: AllTimePoolStakedAmount[];
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"strict": true,
// These settings are required for TypeScript project references
"declaration": true,
"declarationMap": true,
// "declarationMap": true,
"sourceMap": true
},
// The root of the project is just a list of references and does not contain
Expand Down
Loading

0 comments on commit 3cdc149

Please sign in to comment.