|  | 
|  | 1 | +/* eslint-disable camelcase */ | 
|  | 2 | + | 
|  | 3 | +exports.shorthands = undefined; | 
|  | 4 | + | 
|  | 5 | +exports.up = pgm => { | 
|  | 6 | +  // Remove old balances. | 
|  | 7 | +  pgm.sql(`TRUNCATE TABLE ft_balances`); | 
|  | 8 | + | 
|  | 9 | +  // Recalculate STX balances | 
|  | 10 | +  pgm.sql(` | 
|  | 11 | +    WITH all_balances AS ( | 
|  | 12 | +        SELECT sender AS address, -SUM(amount) AS balance_change | 
|  | 13 | +        FROM stx_events | 
|  | 14 | +        WHERE asset_event_type_id IN (1, 3) -- Transfers and Burns affect the sender's balance | 
|  | 15 | +        AND canonical = true AND microblock_canonical = true | 
|  | 16 | +        GROUP BY sender | 
|  | 17 | +      UNION ALL | 
|  | 18 | +        SELECT recipient AS address, SUM(amount) AS balance_change | 
|  | 19 | +        FROM stx_events | 
|  | 20 | +        WHERE asset_event_type_id IN (1, 2) -- Transfers and Mints affect the recipient's balance | 
|  | 21 | +        AND canonical = true AND microblock_canonical = true | 
|  | 22 | +        GROUP BY recipient | 
|  | 23 | +    ), | 
|  | 24 | +    net_balances AS ( | 
|  | 25 | +      SELECT address, SUM(balance_change) AS balance | 
|  | 26 | +      FROM all_balances | 
|  | 27 | +      GROUP BY address | 
|  | 28 | +    ), | 
|  | 29 | +    fees AS ( | 
|  | 30 | +      SELECT address, SUM(total_fees) AS total_fees | 
|  | 31 | +      FROM ( | 
|  | 32 | +          SELECT sender_address AS address, SUM(fee_rate) AS total_fees | 
|  | 33 | +          FROM txs | 
|  | 34 | +          WHERE canonical = true AND microblock_canonical = true AND sponsored = false | 
|  | 35 | +          GROUP BY sender_address | 
|  | 36 | +        UNION ALL | 
|  | 37 | +          SELECT sponsor_address AS address, SUM(fee_rate) AS total_fees | 
|  | 38 | +          FROM txs | 
|  | 39 | +          WHERE canonical = true AND microblock_canonical = true AND sponsored = true | 
|  | 40 | +          GROUP BY sponsor_address | 
|  | 41 | +      ) AS subquery | 
|  | 42 | +      GROUP BY address | 
|  | 43 | +    ), | 
|  | 44 | +    rewards AS ( | 
|  | 45 | +      SELECT | 
|  | 46 | +        recipient AS address, | 
|  | 47 | +        SUM( | 
|  | 48 | +          coinbase_amount + tx_fees_anchored + tx_fees_streamed_confirmed + tx_fees_streamed_produced | 
|  | 49 | +        ) AS total_rewards | 
|  | 50 | +      FROM miner_rewards | 
|  | 51 | +      WHERE canonical = true | 
|  | 52 | +      GROUP BY recipient | 
|  | 53 | +    ), | 
|  | 54 | +    all_addresses AS ( | 
|  | 55 | +      SELECT address FROM net_balances | 
|  | 56 | +      UNION | 
|  | 57 | +      SELECT address FROM fees | 
|  | 58 | +      UNION | 
|  | 59 | +      SELECT address FROM rewards | 
|  | 60 | +    ) | 
|  | 61 | +    INSERT INTO ft_balances (address, balance, token) | 
|  | 62 | +    SELECT | 
|  | 63 | +      aa.address, | 
|  | 64 | +      COALESCE(nb.balance, 0) - COALESCE(f.total_fees, 0) + COALESCE(r.total_rewards, 0) AS balance, | 
|  | 65 | +      'stx' AS token | 
|  | 66 | +    FROM all_addresses aa | 
|  | 67 | +    LEFT JOIN net_balances nb ON aa.address = nb.address | 
|  | 68 | +    LEFT JOIN fees f ON aa.address = f.address | 
|  | 69 | +    LEFT JOIN rewards r ON aa.address = r.address | 
|  | 70 | +  `); | 
|  | 71 | + | 
|  | 72 | +  // Recalculate FT balances | 
|  | 73 | +  pgm.sql(` | 
|  | 74 | +    WITH all_balances AS ( | 
|  | 75 | +        SELECT sender AS address, asset_identifier, -SUM(amount) AS balance_change | 
|  | 76 | +        FROM ft_events | 
|  | 77 | +        WHERE asset_event_type_id IN (1, 3) -- Transfers and Burns affect the sender's balance | 
|  | 78 | +          AND canonical = true  | 
|  | 79 | +          AND microblock_canonical = true | 
|  | 80 | +        GROUP BY sender, asset_identifier | 
|  | 81 | +      UNION ALL | 
|  | 82 | +        SELECT recipient AS address, asset_identifier, SUM(amount) AS balance_change | 
|  | 83 | +        FROM ft_events | 
|  | 84 | +        WHERE asset_event_type_id IN (1, 2) -- Transfers and Mints affect the recipient's balance | 
|  | 85 | +          AND canonical = true  | 
|  | 86 | +          AND microblock_canonical = true | 
|  | 87 | +        GROUP BY recipient, asset_identifier | 
|  | 88 | +    ), | 
|  | 89 | +    net_balances AS ( | 
|  | 90 | +      SELECT address, asset_identifier, SUM(balance_change) AS balance | 
|  | 91 | +      FROM all_balances | 
|  | 92 | +      GROUP BY address, asset_identifier | 
|  | 93 | +    ) | 
|  | 94 | +    INSERT INTO ft_balances (address, balance, token) | 
|  | 95 | +    SELECT address, balance, asset_identifier AS token | 
|  | 96 | +    FROM net_balances | 
|  | 97 | +  `); | 
|  | 98 | +}; | 
|  | 99 | + | 
|  | 100 | +exports.down = pgm => {}; | 
0 commit comments