Skip to content

Commit

Permalink
Gda get flow rate (#271)
Browse files Browse the repository at this point in the history
* pessimistic estimations of GDA

* update query

* add PK to model (upsert)

* save flowDistribution events to DB

* remove PK

* insert each event a new row

---------

Co-authored-by: Didi <[email protected]>
  • Loading branch information
ngmachado and d10r authored Feb 2, 2024
1 parent 1777c36 commit fc58015
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/database/businessRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ class BusinessRepository {
}

async getGDAOutFlowRate(token, distributor) {
const sqlquery = `SELECT SUM(sumNewDistributorToPoolFlowRate) as aggrDistributorFlowRate FROM (SELECT SUM(newDistributorToPoolFlowRate) as sumNewDistributorToPoolFlowRate, pool from flowdistributionupdateds
where distributor = :distributor and superToken = :token
GROUP BY pool
HAVING MAX(blockNumber)) AS P`;
const sqlquery = `SELECT SUM(newDistributorToPoolFlowRate) AS aggrDistributorFlowRate FROM (
SELECT newDistributorToPoolFlowRate, pool FROM flowdistributionupdateds
WHERE distributor = :distributor and superToken = :token
GROUP BY pool
HAVING MAX(blockNumber)
) AS P`;
return this.app.db.SQLRepository.executeSQLSelect(sqlquery, { distributor: distributor, token: token });
}

Expand Down
20 changes: 19 additions & 1 deletion src/protocol/queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Queues {
"FlowUpdated",
task.self.app.protocol.getCFAAgreementEvents
);
let flowDistributionUpdatedEvents = await task.self.app.queues._handleAgreementEvents(
const flowDistributionUpdatedEvents = await task.self.app.queues._handleAgreementEvents(
task,
senderFilerGDA,
"GDA",
Expand Down Expand Up @@ -256,6 +256,7 @@ class Queues {

async _processEvents(task, events) {
for (const event of events) {
// we always need to save the agreement
await task.self.app.db.models.AgreementModel.upsert({
agreementId: event.agreementId,
superToken: event.token,
Expand All @@ -265,6 +266,23 @@ class Queues {
blockNumber: event.blockNumber,
source: event.source
});
// if GDA save the flow distribution
if (event.source === "GDA") {
await task.self.app.db.models.FlowDistributionModel.create({
agreementId: event.agreementId,
superToken: event.token,
pool: event.pool,
distributor: event.distributor,
operator: event.operator,
oldFlowRate: event.oldFlowRate,
newDistributorToPoolFlowRate: event.newDistributorToPoolFlowRate,
newPoolToDistributorFlowRate: event.newPoolToDistributorFlowRate,
newTotalDistributionFlowRate: event.newTotalDistributionFlowRate,
adjustmentFlowRate: event.adjustmentFlowRate,
blockNumber: event.blockNumber
});
}
// add estimation task
if (["CFA", "GDA"].includes(event.source)) {
const accounts = event.source === "CFA" ? [event.sender, event.receiver] : [event.distributor, event.pool];
accounts.forEach(account => {
Expand Down

0 comments on commit fc58015

Please sign in to comment.