-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from layerx-labs/dev
BEPRO 2.28
- Loading branch information
Showing
8 changed files
with
227 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,7 @@ | |
border: 3px solid #4250e4; | ||
border-radius: 50%; | ||
height: 250px; | ||
width: 250px; | ||
} | ||
header h1, h3 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Bounty } from "@taikai/dappkit"; | ||
import { Op, Sequelize } from "sequelize"; | ||
|
||
import models from "src/db"; | ||
import { issuesAttributes } from "src/db/models/issues"; | ||
import { usersAttributes } from "src/db/models/users"; | ||
|
||
import { savePointEvent } from "src/modules/points-system/save-point-event"; | ||
import { removePointEntry } from "src/modules/points-system/remove-point-event"; | ||
import { getOrUpdateLastTokenPrice } from "src/modules/tokens"; | ||
|
||
import logger from "src/utils/logger-handler"; | ||
|
||
interface HandleFundedFundingPointsProps { | ||
bounty: Bounty; | ||
issue: issuesAttributes; | ||
} | ||
|
||
type FundingEventInfo = { | ||
taskId: number, | ||
benefactorId: number, | ||
amount: string; | ||
tokenPrice: number; | ||
} | ||
|
||
const { | ||
NEXT_PUBLIC_CURRENCY_MAIN: currency = "eur", | ||
} = process.env; | ||
|
||
export async function handleFundedFundingPoints({ | ||
bounty, | ||
issue, | ||
}: HandleFundedFundingPointsProps) { | ||
try { | ||
const pointsEventsOfIssue = await models.points_events.findAll({ | ||
where: { | ||
actionName: "funded_funding_request", | ||
info: { | ||
taskId: { | ||
[Op.eq]: issue.id | ||
} | ||
} | ||
} | ||
}); | ||
|
||
const users: usersAttributes[] = []; | ||
const findUser = (address: string) => users.find(user => user?.address?.toLowerCase() === address.toLowerCase()); | ||
|
||
const benefactors = bounty.funding.map((funding, id) => ({ id, ...funding })); | ||
for (const benefactor of benefactors) { | ||
let user = findUser(benefactor.benefactor); | ||
|
||
if (!user) { | ||
const found = await models.users.findOne({ | ||
where: Sequelize.where( Sequelize.fn("LOWER", Sequelize.col("address")), | ||
"=", | ||
benefactor.benefactor.toLowerCase()) | ||
}); | ||
|
||
if (!found) | ||
continue; | ||
|
||
user = found; | ||
users.push(found); | ||
} | ||
|
||
const hasPointEvent = pointsEventsOfIssue | ||
.find(({ userId, info }) => (info as FundingEventInfo)?.benefactorId === benefactor.id && userId === user?.id); | ||
|
||
if (!hasPointEvent && +benefactor.amount > 0) { | ||
const tokenPrice = await getOrUpdateLastTokenPrice(issue.transactionalTokenId!, currency); | ||
await savePointEvent("funded_funding_request", user.address!, { | ||
taskId: issue.id, | ||
benefactorId: benefactor.id, | ||
amount: benefactor.amount, | ||
tokenPrice, | ||
currency | ||
}, (pointsPerAction, scalingFactor) => pointsPerAction * scalingFactor * +benefactor.amount * tokenPrice); | ||
|
||
logger.info(`handleFundedFundingPoints: point saved`, { | ||
taskId: issue.id, | ||
benefactorId: benefactor.id, | ||
amount: benefactor.amount, | ||
tokenPrice, | ||
currency | ||
}); | ||
} else if (hasPointEvent && +benefactor.amount === 0) | ||
await removePointEntry(hasPointEvent.id); | ||
logger.info(`handleFundedFundingPoints: point removed because fund was retracted`, { | ||
taskId: issue.id, | ||
benefactorId: benefactor.id, | ||
amount: benefactor.amount, | ||
currency, | ||
}); | ||
} | ||
} catch(error) { | ||
logger.error(`handleFundedFundingPoints: failed to handle funded funding points`, { error, bounty, issue }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import models from "src/db"; | ||
import logger from "src/utils/logger-handler"; | ||
|
||
export async function removePointEntry(pointEntryId: number) { | ||
const pointEntry = await models.points_events.findOne({ | ||
where: { | ||
id: pointEntryId | ||
} | ||
}); | ||
|
||
if (!pointEntry) { | ||
logger.warn(`removePointEntry: Entry with id ${pointEntryId} not found on points_events`); | ||
return; | ||
} | ||
|
||
if (pointEntry.pointsCounted) { | ||
const user = await models.users.findOne({ | ||
where: { | ||
id: pointEntry.userId | ||
} | ||
}); | ||
|
||
if (!user) { | ||
logger.warn(`removePointEntry: User with id ${pointEntry.userId} not found`); | ||
return; | ||
} | ||
|
||
user.totalPoints = user.totalPoints! - pointEntry.pointsWon; | ||
|
||
await user.save(); | ||
} | ||
|
||
await pointEntry.destroy(); | ||
|
||
logger.info(`PointsEvents ${pointEntryId} removed ${pointEntry.pointsCounted ? "and user totalPoints updated" : ""}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters