Skip to content

Commit 228d7ef

Browse files
authored
Add info column to points events (#204)
* update models * save info * save info * add more info * save tokens prices too
1 parent 4460ceb commit 228d7ef

File tree

8 files changed

+58
-17
lines changed

8 files changed

+58
-17
lines changed

src/actions/get-bounty-closed-event.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,19 @@ export async function action(block: DecodedLog, query?: EventsQuery): Promise<Ev
115115
updateLeaderboardProposals("accepted");
116116
updateBountiesHeader();
117117
updateSeoCardBounty(dbBounty.id, name);
118-
savePointEvent("accepted_proposal", transaction.from);
119-
savePointEvent("created_proposal", dbProposal.creator!);
120-
savePointEvent("created_deliverable", deliverable.user.address!);
118+
savePointEvent("accepted_proposal", transaction.from, {
119+
taskId: dbBounty.id,
120+
proposalId: dbProposal.id,
121+
merger: transaction.from
122+
});
123+
savePointEvent("created_proposal", dbProposal.creator!, {
124+
taskId: dbBounty.id,
125+
proposalId: dbProposal.id,
126+
});
127+
savePointEvent("created_deliverable", deliverable.user.address!, {
128+
taskId: dbBounty.id,
129+
deliverableId: deliverable.id,
130+
});
121131

122132
eventsProcessed[network.name!] = {
123133
[dbBounty.id!.toString()]: {bounty: dbBounty, eventBlock: parseLogWithContext(block)}

src/actions/get-bounty-moved-to-open.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ export async function action(query?: EventsQuery): Promise<EventsProcessed> {
8888
const tokenPrice = await getOrUpdateLastTokenPrice(dbBounty.transactionalTokenId!, currency);
8989

9090
await savePointEvent( "created_task",
91-
dbBounty.user.address!,
91+
dbBounty.user.address!,
92+
{ taskId: dbBounty.id, taskAmount: dbBounty.amount, tokenPrice, currency },
9293
(pointsPerAction, scalingFactor) => pointsPerAction * scalingFactor * +dbBounty.amount! * tokenPrice);
9394

9495
eventsProcessed[networkName!] = {

src/actions/get-network-registered-events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function action(block: DecodedLog<NetworkCreatedEvent['returnValues
4949

5050
await network.save();
5151

52-
savePointEvent("created_marketplace", creator);
52+
savePointEvent("created_marketplace", creator, { networkId: network.id });
5353

5454
updateNumberOfNetworkHeader();
5555

src/actions/points-system/save-delegated-tokens-events.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,33 @@ export async function action() {
1717
const delegations = await db.delegations.findAll({
1818
include: [
1919
{
20-
association: "network"
20+
association: "network",
21+
include: [
22+
{ association: "network_token_token" }
23+
]
2124
}
2225
],
2326
});
2427

2528
const totalConvertedByCurator = {};
29+
const tokensPrices = {};
2630

2731
for (const delegation of delegations) {
2832
const tokenPrice = await getOrUpdateLastTokenPrice(delegation.network.network_token_id!);
2933
const convertedAmount = +delegation.amount! * tokenPrice;
34+
tokensPrices[delegation.network.network_token_token.symbol] = tokenPrice;
3035
totalConvertedByCurator[delegation.from] = (totalConvertedByCurator[delegation.from] || 0) + convertedAmount;
3136
}
3237

3338
for (const address in totalConvertedByCurator) {
3439
await savePointEvent( "delegated",
3540
address,
41+
{
42+
tokensPrices,
43+
delegations: delegations
44+
.filter(c => c.from.toLowerCase() === address.toLowerCase())
45+
.map(({ from, to, amount, networkId }) => ({ from, to, amount, networkId }))
46+
},
3647
(pointsPerAction, scalingFActor) => pointsPerAction * scalingFActor * totalConvertedByCurator[address]);
3748
}
3849

src/actions/points-system/save-locked-tokens-events.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,33 @@ export async function action() {
2222
},
2323
include: [
2424
{
25-
association: "network"
25+
association: "network",
26+
include: [
27+
{ association: "network_token_token" }
28+
]
2629
}
2730
],
2831
});
2932

3033
const totalConvertedByCurator = {};
34+
const tokensPrices = {};
3135

3236
for (const curator of curators) {
3337
const tokenPrice = await getOrUpdateLastTokenPrice(curator.network.network_token_id!);
3438
const convertedAmount = +curator.tokensLocked! * tokenPrice;
39+
tokensPrices[curator.network.network_token_token.symbol] = tokenPrice;
3540
totalConvertedByCurator[curator.address] = (totalConvertedByCurator[curator.address] || 0) + convertedAmount;
3641
}
3742

3843
for (const address in totalConvertedByCurator) {
3944
await savePointEvent( "locked",
4045
address,
46+
{
47+
tokensPrices,
48+
curators: curators
49+
.filter(c => c.address.toLowerCase() === address.toLowerCase())
50+
.map(({ tokensLocked, networkId }) => ({ tokensLocked, networkId }))
51+
},
4152
(pointsPerAction, scalingFActor) => pointsPerAction * scalingFActor * totalConvertedByCurator[address]);
4253
}
4354

src/db/models/points_events.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ export interface points_eventsAttributes {
1111
pointsCounted?: boolean;
1212
createdAt: Date;
1313
updatedAt: Date;
14+
info?: object;
1415
}
1516

1617
export type points_eventsPk = "id";
1718
export type points_eventsId = points_events[points_eventsPk];
18-
export type points_eventsOptionalAttributes = "id" | "pointsCounted" | "createdAt" | "updatedAt";
19+
export type points_eventsOptionalAttributes = "id" | "pointsCounted" | "createdAt" | "updatedAt" | "info";
1920
export type points_eventsCreationAttributes = Optional<points_eventsAttributes, points_eventsOptionalAttributes>;
2021

2122
export class points_events extends Model<points_eventsAttributes, points_eventsCreationAttributes> implements points_eventsAttributes {
@@ -26,6 +27,7 @@ export class points_events extends Model<points_eventsAttributes, points_eventsC
2627
pointsCounted?: boolean;
2728
createdAt!: Date;
2829
updatedAt!: Date;
30+
info?: object;
2931

3032
// points_events belongsTo points_base via actionName
3133
actionName_points_base!: points_base;
@@ -70,6 +72,10 @@ export class points_events extends Model<points_eventsAttributes, points_eventsC
7072
type: DataTypes.BOOLEAN,
7173
allowNull: true,
7274
defaultValue: false
75+
},
76+
info: {
77+
type: DataTypes.JSON,
78+
allowNull: true
7379
}
7480
}, {
7581
tableName: 'points_events',

src/db/models/users.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export interface usersAttributes {
2121
isEmailConfirmed?: boolean;
2222
emailVerificationCode?: string;
2323
emailVerificationSentAt?: Date;
24+
totalPoints?: number;
2425
githubLink?: string;
2526
linkedInLink?: string;
26-
totalPoints?: number;
2727
}
2828

2929
export type usersPk = "id";
3030
export type usersId = users[usersPk];
31-
export type usersOptionalAttributes = "id" | "address" | "createdAt" | "updatedAt" | "handle" | "resetedAt" | "email" | "isEmailConfirmed" | "emailVerificationCode" | "emailVerificationSentAt" | "githubLink" | "linkedInLink" | "totalPoints";
31+
export type usersOptionalAttributes = "id" | "address" | "createdAt" | "updatedAt" | "handle" | "resetedAt" | "email" | "isEmailConfirmed" | "emailVerificationCode" | "emailVerificationSentAt" | "totalPoints" | "githubLink" | "linkedInLink";
3232
export type usersCreationAttributes = Optional<usersAttributes, usersOptionalAttributes>;
3333

3434
export class users extends Model<usersAttributes, usersCreationAttributes> implements usersAttributes {
@@ -42,9 +42,9 @@ export class users extends Model<usersAttributes, usersCreationAttributes> imple
4242
isEmailConfirmed?: boolean;
4343
emailVerificationCode?: string;
4444
emailVerificationSentAt?: Date;
45+
totalPoints?: number;
4546
githubLink?: string;
4647
linkedInLink?: string;
47-
totalPoints?: number;
4848

4949
// users hasMany comments via userId
5050
comments!: comments[];
@@ -195,18 +195,18 @@ export class users extends Model<usersAttributes, usersCreationAttributes> imple
195195
type: DataTypes.DATE,
196196
allowNull: true
197197
},
198+
totalPoints: {
199+
type: DataTypes.DOUBLE,
200+
allowNull: true,
201+
defaultValue: 0
202+
},
198203
githubLink: {
199204
type: DataTypes.STRING(255),
200205
allowNull: true
201206
},
202207
linkedInLink: {
203208
type: DataTypes.STRING(255),
204209
allowNull: true
205-
},
206-
totalPoints: {
207-
type: DataTypes.DOUBLE,
208-
allowNull: true,
209-
defaultValue: 0
210210
}
211211
}, {
212212
tableName: 'users',

src/modules/points-system/save-point-event.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ type PointsEvents = "locked" | "delegated" | "created_marketplace" | "created_ta
77
"created_proposal" | "accepted_proposal";
88

99
export async function savePointEvent( event: PointsEvents,
10-
participantAddress: string,
10+
participantAddress: string,
11+
info = {},
1112
calculateFunction = (pointsPerAction: number, scalingFactor: number) => pointsPerAction * scalingFactor ) {
1213
const pointsBase = await db.points_base.findOne({
1314
where: {
@@ -52,6 +53,7 @@ export async function savePointEvent( event: PointsEvents,
5253
userId: user.id,
5354
actionName: pointsBase.actionName,
5455
pointsWon: calculateFunction(pointsBase.pointsPerAction, pointsBase.scalingFactor || 1),
56+
info,
5557
};
5658

5759
await db.points_events.create(newEvent);

0 commit comments

Comments
 (0)