-
Notifications
You must be signed in to change notification settings - Fork 14
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 #268 from anoma/tiago/token-supplies
Index token supplies
- Loading branch information
Showing
23 changed files
with
484 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,11 @@ | ||
-- This file should undo anything in `up.sql` | ||
|
||
-- Drop the foreign key constraint on the address column | ||
ALTER TABLE token_supplies_per_epoch | ||
DROP CONSTRAINT fk_token_supplies_per_epoch_address; | ||
|
||
-- Drop the trigger for enforcing the effective constraint | ||
DROP TRIGGER enforce_effective_constraint ON token_supplies_per_epoch; | ||
|
||
-- Drop the table itself | ||
DROP TABLE token_supplies_per_epoch; |
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,39 @@ | ||
-- Your SQL goes here | ||
|
||
CREATE TABLE token_supplies_per_epoch ( | ||
id SERIAL PRIMARY KEY, | ||
address VARCHAR(45) NOT NULL, | ||
epoch INT NOT NULL, | ||
-- `2^256 - 1` will fit in `NUMERIC(78, 0)` | ||
total NUMERIC(78, 0) NOT NULL, | ||
effective NUMERIC(78, 0), | ||
-- reference the `address` column in the `token` table | ||
CONSTRAINT fk_token_supplies_per_epoch_address | ||
FOREIGN KEY(address) REFERENCES token(address) ON DELETE CASCADE | ||
); | ||
|
||
ALTER TABLE token_supplies_per_epoch ADD UNIQUE (address, epoch); | ||
|
||
CREATE OR REPLACE FUNCTION check_effective_for_token_type() | ||
RETURNS TRIGGER AS $$ | ||
BEGIN | ||
-- Check if the referenced token_type is 'native' | ||
IF EXISTS ( | ||
SELECT 1 | ||
FROM token | ||
WHERE token.address = NEW.address AND token.token_type = 'native' | ||
) THEN | ||
-- If token_type is 'native', ensure token_supplies_per_epoch.effective is not NULL | ||
IF NEW.effective IS NULL THEN | ||
RAISE EXCEPTION 'token_supplies_per_epoch.effective cannot be NULL when token.token_type is ''native'''; | ||
END IF; | ||
END IF; | ||
-- Allow the insert or update to proceed | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER enforce_effective_constraint | ||
BEFORE INSERT OR UPDATE ON token_supplies_per_epoch | ||
FOR EACH ROW | ||
EXECUTE FUNCTION check_effective_for_token_type(); |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use bigdecimal::BigDecimal; | ||
use diesel::{Insertable, Queryable, Selectable}; | ||
use shared::balance::TokenSupply as SharedTokenSupply; | ||
|
||
use crate::schema::token_supplies_per_epoch; | ||
|
||
#[derive(Debug, Clone, Queryable, Selectable)] | ||
#[diesel(table_name = token_supplies_per_epoch)] | ||
#[diesel(check_for_backend(diesel::pg::Pg))] | ||
pub struct TokenSuppliesDb { | ||
pub id: i32, | ||
pub address: String, | ||
pub epoch: i32, | ||
pub total: BigDecimal, | ||
pub effective: Option<BigDecimal>, | ||
} | ||
|
||
#[derive(Debug, Clone, Queryable, Selectable, Insertable)] | ||
#[diesel(table_name = token_supplies_per_epoch)] | ||
#[diesel(check_for_backend(diesel::pg::Pg))] | ||
pub struct TokenSuppliesInsertDb { | ||
pub address: String, | ||
pub epoch: i32, | ||
pub total: BigDecimal, | ||
pub effective: Option<BigDecimal>, | ||
} | ||
|
||
impl From<SharedTokenSupply> for TokenSuppliesInsertDb { | ||
fn from(supply: SharedTokenSupply) -> Self { | ||
let SharedTokenSupply { | ||
address, | ||
epoch, | ||
total, | ||
effective, | ||
} = supply; | ||
|
||
Self { | ||
address, | ||
epoch, | ||
total, | ||
effective, | ||
} | ||
} | ||
} |
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
Oops, something went wrong.