Skip to content

Commit

Permalink
fix: make ids unique
Browse files Browse the repository at this point in the history
  • Loading branch information
royvardhan committed Mar 20, 2024
1 parent d7aae42 commit a47db38
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 70 deletions.
4 changes: 2 additions & 2 deletions ponder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const generateLMSeasonEnum = (numSeasons: number) => {
* - "liquid_mining_twelve_seasons": Indexes twelve seasons in Liquid Mining (1000 * 10^6 points)
* - ...generateLMSeasonEnum(120): Indexes first wallet in a Liquid Mining new season (500 * 10^6 points)
*/
const PointsSource = [
const pointsSource = [
"stratosphere_enrollment",
"dex_aggregator_swap",
"dex_aggregator_1k_swaps",
Expand All @@ -55,7 +55,7 @@ const PointsSource = [
];

export default createSchema((p) => ({
PointsSource: p.createEnum(PointsSource),
PointsSource: p.createEnum(pointsSource),
Points: p.createTable({
id: p.string(),
// Use enum for points source
Expand Down
141 changes: 73 additions & 68 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ import {
BIGINT_THOUSAND,
MINIMUM_POINTS,
assets,
chainId,
pointsMap,
} from "./config/constants";

ponder.on("Stratosphere:Transfer", async ({ event, context }) => {
const { Points } = context.db;
const { to: userAddress, tokenId } = event.args;
const { hash } = event.transaction;
const { chainId } = context.network;

await getOrCreateUserData(context, tokenId, userAddress);

await Points.create({
id: hash,
id: `${hash}-stratosphere-enrollment`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "stratosphere_enrollment",
points: pointsMap.Enrollment,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});
Expand All @@ -33,6 +35,7 @@ ponder.on("Stratosphere:Transfer", async ({ event, context }) => {
ponder.on("LiquidMining:Deposit", async ({ event, context }) => {
const { Points, UserHistory, LiquidMining } = context.db;
const { hash } = event.transaction;
const { chainId } = context.network;
const { seasonId, user: userAddress, amount } = event.args;
const tokenId = await getTokenId(userAddress, context);

Expand All @@ -49,46 +52,47 @@ ponder.on("LiquidMining:Deposit", async ({ event, context }) => {
});

await Points.create({
id: hash,
id: `${hash}-liquid-mining-first-wallet`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: `liquid_mining_first_wallet_season_${Number(seasonId)}`,
points: pointsMap.FirstWalletInVPNDLM,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});
}

const userData = await getOrCreateUserData(context, tokenId, userAddress);

if (userData.LMSeasons.length === 0) {
await Points.create({
id: hash,
id: `${hash}-liquid-mining-first-deposit`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "liquid-mining_first_deposit",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "liquid_mining_first_deposit",
points: pointsMap.FirstDepositInVPNDLM,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await Points.create({
id: hash,
id: `${hash}-liquid-mining-one-season`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "liquid-mining_one_season",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "liquid_mining_one_season",
points: pointsMap.OneSeasonVPNDLMLock,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
LMSeasons: [seasonId],
LMOneSeasonPointsClaimed: true,
Expand All @@ -98,7 +102,7 @@ ponder.on("LiquidMining:Deposit", async ({ event, context }) => {

if (!userData.LMSeasons.includes(seasonId)) {
await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: ({ current }) => ({
LMSeasons: [...current.LMSeasons, seasonId],
}),
Expand All @@ -110,19 +114,19 @@ ponder.on("LiquidMining:Deposit", async ({ event, context }) => {
!userData.LMThreeSeasonsPointsClaimed
) {
await Points.create({
id: hash,
id: `${hash}-liquid-mining-three-seasons`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "liquid-mining_three_seasons",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "liquid_mining_three_seasons",
points: pointsMap.ThreeSeasonVPNDLMLock,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
LMThreeSeasonsPointsClaimed: true,
},
Expand All @@ -131,19 +135,19 @@ ponder.on("LiquidMining:Deposit", async ({ event, context }) => {

if (userData.LMSeasons.length === 6 && !userData.LMSixSeasonsPointsClaimed) {
await Points.create({
id: hash,
id: `${hash}-liquid-mining-six-seasons`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "liquid-mining_six_seasons",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "liquid_mining_six_seasons",
points: pointsMap.SixSeasonVPNDLMLock,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
LMSixSeasonsPointsClaimed: true,
},
Expand All @@ -152,19 +156,19 @@ ponder.on("LiquidMining:Deposit", async ({ event, context }) => {

if (userData.LMSeasons.length === 12 && !userData.LMOneYearPointsClaimed) {
await Points.create({
id: hash,
id: `${hash}-liquid-mining-twelve-seasons`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "liquid-mining_twelve_seasons",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "liquid_mining_twelve_seasons",
points: pointsMap.OneYearVPNDLMLock,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
LMOneYearPointsClaimed: true,
},
Expand All @@ -175,6 +179,7 @@ ponder.on("LiquidMining:Deposit", async ({ event, context }) => {
ponder.on("VapeStaking:Deposit", async ({ event, context }) => {
const { Points, UserHistory, VapeStaking } = context.db;
const { hash } = event.transaction;
const { chainId } = context.network;
const { user: userAddress } = event.args;
const tokenId = await getTokenId(userAddress, context);

Expand All @@ -192,13 +197,13 @@ ponder.on("VapeStaking:Deposit", async ({ event, context }) => {
});

await Points.create({
id: hash,
id: `${hash}-vape-staking-first-wallet`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "vape-staking_first_wallet",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "vape_staking_first_wallet",
points: pointsMap.FirstWalletInVAPELM,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});
Expand All @@ -208,19 +213,19 @@ ponder.on("VapeStaking:Deposit", async ({ event, context }) => {

if (!userData.depositInVS) {
await Points.create({
id: hash,
id: `${hash}-liquid-mining-first-deposit`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "vape-staking_first_deposit",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "vape_staking_first_deposit",
points: pointsMap.FirstDepositInVAPELM,
chainId: context.network.chainId,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
depositInVS: true,
},
Expand Down Expand Up @@ -251,11 +256,11 @@ ponder.on("DexAggregator:RouterSwap", async ({ event, context }) => {
// If we are unable to find a path, usdValueOfTrade is Zero and we don't want to index that
if (usdValueOfTrade >= MINIMUM_POINTS) {
await Points.create({
id: hash,
id: `${hash}-dex-aggregator-swap`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "dex-aggregator_swap",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "dex_aggregator_swap",
points: usdValueOfTrade,
chainId: chainId,
timestamp: event.block.timestamp,
Expand All @@ -281,19 +286,19 @@ ponder.on("DexAggregator:RouterSwap", async ({ event, context }) => {
!userData.first1kSwaps
) {
await Points.create({
id: hash,
id: `${hash}-dex-aggregator-1k-swaps`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "dex-aggregator_1k_swaps",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "dex_aggregator_1k_swaps",
points: pointsMap.ThousandSwaps,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
first1kSwaps: true,
},
Expand All @@ -306,19 +311,19 @@ ponder.on("DexAggregator:RouterSwap", async ({ event, context }) => {
!userData.first10kSwaps
) {
await Points.create({
id: hash,
id: `${hash}-dex-aggregator-10k-swaps`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "dex-aggregator_10k_swaps",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "dex_aggregator_10k_swaps",
points: pointsMap.TenThousandSwaps,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
first10kSwaps: true,
},
Expand All @@ -331,19 +336,19 @@ ponder.on("DexAggregator:RouterSwap", async ({ event, context }) => {
!userData.first100kSwaps
) {
await Points.create({
id: hash,
id: `${hash}-dex-aggregator-100k-swaps`,
data: {
userDataId: `${userAddress}-${context.network.chainId}`,
userHistoryId: `${userAddress}-${context.network.chainId}`,
pointsSource: "dex-aggregator_100k_swaps",
userDataId: `${userAddress}-${chainId}`,
userHistoryId: `${userAddress}-${chainId}`,
pointsSource: "dex_aggregator_100k_swaps",
points: pointsMap.HundredThousandSwaps,
chainId: chainId,
timestamp: event.block.timestamp,
},
});

await UserHistory.update({
id: userAddress,
id: `${userAddress}-${chainId}`,
data: {
first100kSwaps: true,
},
Expand Down

0 comments on commit a47db38

Please sign in to comment.