code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0074_game_details_api.sql (about)

     1  -- +goose Up
     2  
     3  alter table transfers add column if not exists game_id bytea null;
     4  alter table rewards add column if not exists game_id bytea null;
     5  
     6  -- Make sure we refresh the view to account for the new column
     7  create or replace view transfers_current as
     8  (
     9      SELECT DISTINCT ON (id, from_account_id, to_account_id) * FROM transfers ORDER BY id, from_account_id, to_account_id, vega_time DESC
    10  );
    11  
    12  create table game_reward_totals (
    13      game_id bytea not null,
    14      party_id bytea not null,
    15      asset_id bytea not null,
    16      market_id bytea not null,
    17      epoch_id bigint not null,
    18      team_id bytea not null,         -- participant may take part in a game as a team member or as an individual depending on the entity scope of the game
    19      total_rewards hugeint not null,
    20      primary key (game_id, party_id, asset_id, market_id, epoch_id, team_id)
    21  );
    22  
    23  -- +goose StatementBegin
    24  create or replace function insert_game_reward_totals()
    25  returns trigger
    26      language plpgsql
    27      as $$
    28      declare party_team_id bytea;
    29  begin
    30      with current_team_members as (
    31          select distinct on (party_id) *
    32          from team_members
    33          order by party_id, joined_at_epoch desc
    34      )
    35      select team_id into party_team_id from current_team_members where party_id = new.party_id;
    36  
    37      update game_reward_totals
    38      set team_id = coalesce(party_team_id, '\x')
    39      where game_id = new.game_id and party_id = new.party_id
    40        and asset_id = new.asset_id and market_id = new.market_id
    41        and epoch_id = new.epoch_id;
    42  
    43      return null;
    44  end;
    45  $$;
    46  -- +goose StatementEnd
    47  
    48  -- we don't know the team_id of the party when the reward is emitted by core
    49  -- so we need to look it up from the team_members table and insert it into the game_reward_totals table
    50  -- when we insert a new total reward for a party
    51  create trigger insert_game_reward_totals after insert on game_reward_totals
    52      for each row execute procedure insert_game_reward_totals();
    53  
    54  create or replace view game_team_rankings as
    55  with team_games as (
    56    -- get the games where the entity scope is individuals
    57    select distinct game_id from transfers
    58    where dispatch_strategy ->> 'entity_scope' = '2'
    59    and game_id is not null
    60  ), team_totals as (
    61      select t.game_id, t.asset_id, t.epoch_id, t.team_id, sum(t.total_rewards) as total_rewards
    62      from game_reward_totals t
    63      join team_games g on t.game_id = g.game_id
    64      where t.team_id != '\x'
    65      group by t.game_id, t.asset_id, t.epoch_id, t.team_id
    66  )
    67  select game_id, epoch_id, team_id, total_rewards, rank() over (partition by game_id, epoch_id order by total_rewards desc) as rank
    68  from team_totals;
    69  
    70  create or replace view game_team_member_rankings as
    71  with team_games as (
    72    -- get the games where the entity scope is individuals
    73    select distinct game_id from transfers
    74    where dispatch_strategy ->> 'entity_scope' = '2'
    75    and game_id is not null
    76  ), team_totals as (
    77      select t.game_id, t.asset_id, t.team_id, t.party_id, t.epoch_id, sum(t.total_rewards) as total_rewards
    78      from game_reward_totals t
    79      join team_games g on t.game_id = g.game_id
    80      where t.team_id != '\x'
    81      group by t.game_id, t.asset_id, t.team_id, t.party_id, t.epoch_id
    82  )
    83  select game_id, epoch_id, team_id, party_id, total_rewards, rank() over (partition by game_id, epoch_id, team_id order by total_rewards desc) as rank
    84  from team_totals;
    85  
    86  create or replace view game_individual_rankings as
    87  with individual_games as (
    88    -- get the games where the entity scope is individuals
    89    select game_id from transfers
    90    where dispatch_strategy ->> 'entity_scope' = '1'
    91    and game_id is not null
    92  ), individual_totals as (
    93    -- calculate the total rewards for each individual in each individual entity scoped game
    94    select t.game_id, t.epoch_id, t.asset_id, t.party_id, sum(t.total_rewards) as total_rewards
    95    from game_reward_totals t
    96    join individual_games i on t.game_id = i.game_id
    97    group by t.game_id, t.epoch_id, t.asset_id, t.party_id
    98  )
    99  -- rank the individuals for each game at each epoch
   100  select game_id, epoch_id, party_id, total_rewards, rank() over (partition by game_id, epoch_id order by total_rewards desc) as rank
   101  from individual_totals;
   102  
   103  create or replace view game_stats as
   104  with game_epochs as (
   105    select distinct game_id, epoch_id
   106    from rewards
   107    where game_id is not null
   108  ), game_rewards as (
   109    select r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tr.total_rewards as team_total_rewards, 'ENTITY_SCOPE_TEAMS' as entity_scope
   110    from rewards r
   111    join game_epochs ge on r.game_id = ge.game_id and r.epoch_id = ge.epoch_id
   112    join transfers t on r.game_id = t.game_id and t.transfer_type = 'Recurring'
   113    join team_members tm on r.party_id = tm.party_id
   114    left join game_team_rankings tr on r.game_id = tr.game_id and r.epoch_id = tr.epoch_id and tm.team_id = tr.team_id
   115    left join game_team_member_rankings tmr on r.game_id = tmr.game_id and r.epoch_id = tmr.epoch_id and tm.team_id = tmr.team_id and r.party_id = tmr.party_id
   116    where dispatch_strategy->>'entity_scope' = '2'
   117    union all
   118    select r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, null as team_total_rewards,'ENTITY_SCOPE_INDIVIDUALS' as entity_scope
   119    from rewards r
   120    join game_epochs ge on r.game_id = ge.game_id and r.epoch_id = ge.epoch_id
   121    join transfers t on r.game_id = t.game_id and t.transfer_type = 'Recurring'
   122    left join game_individual_rankings tmr on r.game_id = tmr.game_id and r.epoch_id = tmr.epoch_id and r.party_id = tmr.party_id
   123    where dispatch_strategy->>'entity_scope' = '1'
   124  )
   125  select *
   126  from game_rewards
   127  ;
   128  
   129  create or replace view game_stats_current as
   130  with game_epochs as (
   131      select game_id, max(epoch_id) as epoch_id
   132      from rewards
   133      where game_id is not null
   134      group by game_id
   135  ), game_rewards as (
   136    select r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, 'ENTITY_SCOPE_TEAMS' as entity_scope
   137    from rewards r
   138    join game_epochs ge on r.game_id = ge.game_id and r.epoch_id = ge.epoch_id
   139    join transfers t on r.game_id = t.game_id and t.transfer_type = 'Recurring'
   140    join team_members tm on r.party_id = tm.party_id
   141    left join game_team_rankings tr on r.game_id = tr.game_id and r.epoch_id = tr.epoch_id and tm.team_id = tr.team_id
   142    left join game_team_member_rankings tmr on r.game_id = tmr.game_id and r.epoch_id = tmr.epoch_id and tm.team_id = tmr.team_id and r.party_id = tmr.party_id
   143    where dispatch_strategy->>'entity_scope' = '2'
   144    union all
   145    select r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope
   146    from rewards r
   147    join game_epochs ge on r.game_id = ge.game_id and r.epoch_id = ge.epoch_id
   148    join transfers t on r.game_id = t.game_id and t.transfer_type = 'Recurring'
   149    left join game_individual_rankings tmr on r.game_id = tmr.game_id and r.epoch_id = tmr.epoch_id and r.party_id = tmr.party_id
   150    where dispatch_strategy->>'entity_scope' = '1'
   151  )
   152  select *
   153  from game_rewards
   154  ;
   155  
   156  create or replace view current_game_reward_totals as (
   157      with current_game_epochs as (
   158          select game_id, max(epoch_id) as epoch_id
   159          from game_reward_totals
   160          group by game_id
   161      )
   162      select grt.*
   163      from game_reward_totals grt
   164      join current_game_epochs cge on grt.game_id = cge.game_id and grt.epoch_id = cge.epoch_id
   165  );
   166  
   167  -- +goose Down
   168  drop view if exists current_game_reward_totals;
   169  drop view if exists game_stats_current;
   170  drop view if exists game_stats;
   171  drop view if exists game_individual_rankings;
   172  drop view if exists game_team_member_rankings;
   173  drop view if exists game_team_rankings;
   174  drop view if exists transfers_current;
   175  
   176  drop trigger if exists insert_game_reward_totals on game_reward_totals;
   177  drop function if exists insert_game_reward_totals;
   178  drop table if exists game_reward_totals;
   179  
   180  alter table transfers drop column if exists game_id;
   181  alter table rewards drop column if exists game_id;
   182  
   183  -- Make sure we refresh the view to account for the new column
   184  create view transfers_current as
   185  (
   186      SELECT DISTINCT ON (id, from_account_id, to_account_id) * FROM transfers ORDER BY id, from_account_id, to_account_id, vega_time DESC
   187  );