-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[IPCT1-486] - replace community state #147
Changes from 10 commits
3ad8ff3
71f1592
44a671f
8a493b7
5d47525
18ce9b1
f4c9fdf
2cb6767
037e829
8e48e69
27c7461
ed7a6d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line no-undef | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
if (process.env.NODE_ENV === 'test') { | ||
return; | ||
} | ||
|
||
await queryInterface.sequelize.query(` | ||
CREATE OR REPLACE FUNCTION update_inflow_community_states() | ||
RETURNS TRIGGER AS $$ | ||
declare | ||
-- state_raised numeric(29); | ||
-- state_daily_raised numeric(29); | ||
n_backer bigint; | ||
community_id integer; | ||
BEGIN | ||
SELECT id INTO community_id FROM community where "contractAddress"=NEW."contractAddress"; | ||
|
||
IF community_id is null THEN | ||
return new; | ||
end if; | ||
-- if this address never donated, it's a new backer | ||
SELECT count(*) INTO n_backer FROM inflow WHERE "from" = NEW."from" AND "contractAddress"=NEW."contractAddress"; | ||
IF n_backer = 0 THEN | ||
UPDATE ubi_community_state SET backers = backers + 1 WHERE "communityId"=community_id; | ||
end if; | ||
-- update total raised | ||
-- SELECT SUM(raised + NEW.amount) INTO state_raised FROM ubi_community_state WHERE "communityId"=community_id; | ||
-- UPDATE ubi_community_state SET raised = state_raised WHERE "communityId"=community_id; | ||
-- SELECT SUM(raised + NEW.amount) INTO state_daily_raised FROM ubi_community_daily_state WHERE "communityId"=community_id AND date=DATE(NEW."txAt"); | ||
-- UPDATE ubi_community_daily_state SET raised = state_daily_raised WHERE "communityId"=community_id AND date=DATE(NEW."txAt"); | ||
return NEW; | ||
END; | ||
$$ LANGUAGE plpgsql;`); | ||
|
||
await queryInterface.sequelize.query(` | ||
CREATE OR REPLACE FUNCTION update_claim_states() | ||
RETURNS TRIGGER AS $$ | ||
declare | ||
-- state_claimed numeric(29); | ||
-- state_daily_claimed numeric(29); | ||
beneficiary_claimed numeric(22); | ||
beneficiary_last_claim_at timestamp with time zone; | ||
community_public_id uuid; | ||
BEGIN | ||
SELECT "publicId" INTO community_public_id FROM community where id=NEW."communityId"; | ||
-- update claims | ||
UPDATE ubi_community_state SET claims = claims + 1 WHERE "communityId"=NEW."communityId"; | ||
-- UPDATE ubi_community_daily_state SET claims = claims + 1 WHERE "communityId"=community_id AND date=DATE(NEW."txAt"); | ||
-- update beneficiary table as well | ||
SELECT "lastClaimAt" INTO beneficiary_last_claim_at FROM beneficiary WHERE "communityId"=community_public_id AND address=NEW.address; | ||
UPDATE beneficiary SET claims = claims + 1, "penultimateClaimAt"=beneficiary_last_claim_at, "lastClaimAt"=NEW."txAt" WHERE "communityId"=community_public_id AND address=NEW.address; | ||
SELECT SUM(claimed + NEW.amount) INTO beneficiary_claimed FROM beneficiary WHERE "communityId"=community_public_id AND address=NEW.address; | ||
UPDATE beneficiary SET claimed = beneficiary_claimed WHERE "communityId"=community_public_id AND address=NEW.address; | ||
-- update total claimed | ||
-- SELECT SUM(claimed + NEW.amount) INTO state_claimed FROM ubi_community_state WHERE "communityId"=NEW."communityId"; | ||
-- UPDATE ubi_community_state SET claimed = state_claimed WHERE "communityId"=NEW."communityId"; | ||
-- SELECT SUM(claimed + NEW.amount) INTO state_daily_claimed FROM ubi_community_daily_state WHERE "communityId"=community_id AND date=DATE(NEW."txAt"); | ||
-- UPDATE ubi_community_daily_state SET claimed = state_daily_claimed WHERE "communityId"=community_id AND date=DATE(NEW."txAt"); | ||
return NEW; | ||
END; | ||
$$ LANGUAGE plpgsql;`); | ||
|
||
await queryInterface.sequelize.query(`DROP TRIGGER IF EXISTS update_managers_community_state ON manager`); | ||
await queryInterface.sequelize.query(`DROP TRIGGER IF EXISTS update_beneficiaries_community_states ON beneficiary`); | ||
}, | ||
|
||
down(queryInterface, Sequelize) {}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,18 +40,18 @@ | |
* description: Number of unique backers since contract inception | ||
*/ | ||
export interface UbiCommunityState { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove all instances of the community state table There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but do not add a drop table yet (needs to be at a later stage) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! |
||
communityId: number; | ||
communityId?: number; | ||
claimed: string; | ||
claims: number; | ||
claims?: number; | ||
beneficiaries: number; // only in community | ||
removedBeneficiaries: number; | ||
managers: number; | ||
removedBeneficiaries?: number; | ||
managers?: number; | ||
raised: string; | ||
backers: number; | ||
|
||
// timestamps | ||
createdAt: Date; | ||
updatedAt: Date; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
} | ||
|
||
export interface UbiCommunityStateCreation { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed unused lines on this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done