All files / test/StakePool/fixtures queries.ts

100% Statements 19/19
100% Branches 0/0
100% Functions 0/0
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 3122x                                                                                                                                                                                                                                                                     2x                                                   2x                                               2x                 2x                 2x       2x                         2x                         2x                         2x                         2x         2x     2x                   2x           2x           2x       2x   2x                                     2x  
export const subQueries = `
    WITH current_epoch AS (
    SELECT
      "no"
    FROM
      epoch
    ORDER BY
      no DESC
    LIMIT
      1
  ), pools_delegated AS (
    SELECT
      ph.id,
      ph.view,
      pu.id AS update_id,
      pu.active_epoch_no,
      pu.pledge,
      sa.id AS stake_address_id
    FROM
      pool_hash ph
      JOIN pool_update pu ON pu.id = (
        SELECT
          id
        FROM
          pool_update pu2
        WHERE
          pu2.hash_id = ph.id
        ORDER BY
          id DESC
        LIMIT
          1
      )
      JOIN stake_address sa ON sa.id = pu.reward_addr_id
      JOIN delegation d1 on sa.id = d1.addr_id
    WHERE
      NOT EXISTS (
        SELECT
          TRUE
        FROM
          delegation d2
        WHERE
          d2.addr_id = d1.addr_id
          AND d2.tx_id > d1.tx_id
      )
      AND NOT EXISTS (
        SELECT
          TRUE
        FROM
          stake_deregistration
        WHERE
          stake_deregistration.addr_id = d1.addr_id
          AND stake_deregistration.tx_id > d1.tx_id
      )
  ),
  pool_owner_rewards AS (
    SELECT
      COALESCE(SUM(r.amount), 0) AS total_amount,
      sa.id AS stake_address_id,
      r.pool_id
    FROM
      reward r
      JOIN stake_address sa ON sa.id = r.addr_id
    WHERE
      sa.id in (
        SELECT
          stake_address_id
        FROM
          pools_delegated
      )
      and r.spendable_epoch <= (
        SELECT
          "no"
        FROM
          current_epoch
      )
    GROUP BY
      r.pool_id,
      sa.id
  ),
  pool_owner_withdraws AS (
    SELECT
      COALESCE(SUM(w.amount), 0) AS total_amount,
      sa.id AS stake_address_id
    FROM
      withdrawal w
      JOIN tx ON tx.id = w.tx_id
      JOIN stake_address sa ON sa.id = w.addr_id
      JOIN pools_delegated pool on pool.stake_address_id = sa.id
    GROUP BY
      sa.id
  ),
  reward_acc_balance AS (
    SELECT
      (r.total_amount - w.total_amount) AS total_amount,
      r.stake_address_id,
      r.pool_id
    FROM
      pool_owner_rewards r
      JOIN pool_owner_withdraws w on r.stake_address_id = w.stake_address_id
  ),
  owners_utxo AS (
    SELECT
      tx_out.value AS value,
      pu.hash_id
    FROM
      tx_out
      JOIN pool_owner o ON o.addr_id = tx_out.stake_address_id
      JOIN pool_update pu ON o.pool_update_id = pu.id
      AND pu.hash_id IN (
        SELECT
          id
        FROM
          pools_delegated
      )
      LEFT JOIN tx_in ON tx_out.tx_id = tx_in.tx_out_id
      AND tx_out.index :: smallint = tx_in.tx_out_index :: smallint
      LEFT JOIN tx AS tx_in_tx ON tx_in_tx.id = tx_in.tx_in_id
      JOIN tx AS tx_out_tx ON tx_out_tx.id = tx_out.tx_id
    WHERE
      tx_in_tx.id IS NULL
  ),
  owners_balance AS (
    SELECT
      SUM(value) AS total_amount,
      hash_id AS pool_hash_id
    FROM
      owners_utxo
    GROUP BY
      hash_id
  )`;
 
export const beginFindPoolsWithMetadata = `
  SELECT
      ph.view AS pool_id,
      pod.json AS metadata,
      pool_hash_id as hash_id,
      pool_update.id as update_id
  FROM
    pools_delegated AS ph
    LEFT JOIN owners_balance o_balance ON ph.id = o_balance.pool_hash_id
    LEFT JOIN reward_acc_balance r_balance ON r_balance.pool_id = ph.id
    JOIN pool_update ON pool_update.hash_id = o_balance.pool_hash_id
    JOIN pool_metadata_ref metadata ON metadata.id = pool_update.meta_id
    JOIN off_chain_pool_data pod ON metadata.id = pod.pmr_id
    LEFT JOIN pool_retire pr ON pr.id = (
      SELECT
        id
      FROM
        pool_retire pr2
      WHERE
        pr2.hash_id = ph.id
      ORDER BY
        id desc
      LIMIT
        1
    )`;
 
export const beginFindPoolsWithoutMetadata = `
  SELECT
      ph.view AS pool_id,
      null AS metadata,
      pool_hash_id as hash_id,
      pool_update.id as update_id
  FROM
    pools_delegated AS ph
    LEFT JOIN owners_balance o_balance ON ph.id = o_balance.pool_hash_id
    LEFT JOIN reward_acc_balance r_balance ON r_balance.pool_id = ph.id
    JOIN pool_update ON pool_update.hash_id = o_balance.pool_hash_id
    LEFT JOIN pool_retire pr ON pr.id = (
      SELECT
        id
      FROM
        pool_retire pr2
      WHERE
        pr2.hash_id = ph.id
      ORDER BY
        id desc
      LIMIT
        1
    )`;
 
export const withPledgeMet = `
  WHERE
  (
    (
      COALESCE(o_balance.total_amount, 0) + COALESCE (r_balance.total_amount, 0)
    ) >= ph.pledge
  )
  AND`;
 
export const withPledgeNotMet = `
  WHERE
  (
    (
      COALESCE(o_balance.total_amount, 0) + COALESCE (r_balance.total_amount, 0)
    ) < ph.pledge
  )
  AND`;
 
export const withNoPledgeFilter = `
  WHERE
`;
 
export const withPoolActive = `
   (
    (
      ph.active_epoch_no <= (
        SELECT
          "no"
        FROM
          current_epoch
      )
      AND COALESCE(pr.retiring_epoch, 0) < ph.active_epoch_no
    )
  )`;
 
export const withPoolActivating = `
   (
    (
      ph.active_epoch_no > (
        SELECT
          "no"
        FROM
          current_epoch
      )
      AND COALESCE(pr.retiring_epoch, 0) <= ph.active_epoch_no
    )
  )`;
 
export const withPoolRetired = `
   (
    (
      COALESCE(pr.retiring_epoch, 0) <= (
        SELECT
          "no"
        FROM
          current_epoch
      )
      AND COALESCE(pr.retiring_epoch, 0) > ph.active_epoch_no
    )
  )`;
 
export const withPoolRetiring = `
   (
    (
      COALESCE(pr.retiring_epoch, 0) > (
        SELECT
          "no"
        FROM
          current_epoch
      )
      AND COALESCE(pr.retiring_epoch, 0) > ph.active_epoch_no
    )
  )`;
 
export const withNoStateFilter = `
  (
    true
  )`;
 
export const endFindPools = `
  LIMIT $1`;
 
export const beginPoolIds = `
  SELECT 
    ph.view AS pool_id
  FROM pool_update pu
  JOIN pool_hash ph ON 
    ph.id = pu.hash_id
  JOIN stake_address sa ON
    sa.id = pu.reward_addr_id
`;
 
export const withMetadata = `
  WHERE pu.hash_id IN (SELECT pool_id FROM pool_metadata_ref)
  GROUP BY pool_id
  ORDER BY pool_id ASC
  `;
 
export const withoutMetadata = `
  WHERE pu.hash_id NOT IN (SELECT pool_id FROM pool_metadata_ref)
  GROUP BY pool_id
  ORDER BY pool_id ASC
  `;
 
export const endPoolIds = `
  LIMIT $1
  `;
 
export const lastKnownEpoch = 'select no from epoch order by id desc limit 1';
 
const Queries = {
  beginFindPoolsWithMetadata,
  beginFindPoolsWithoutMetadata,
  beginPoolIds,
  endFindPools,
  endPoolIds,
  lastKnownEpoch,
  subQueries,
  withMetadata,
  withNoPledgeFilter,
  withPledgeMet,
  withPledgeNotMet,
  withPoolActivating,
  withPoolActive,
  withPoolRetired,
  withPoolRetiring,
  withoutMetadata
};
 
export default Queries;