Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coburncoburn/ccaabfd2935992d8b4e0f5bb05fe1ac7 to your computer and use it in GitHub Desktop.
Save coburncoburn/ccaabfd2935992d8b4e0f5bb05fe1ac7 to your computer and use it in GitHub Desktop.
{"SELECT s0.\"account\", s0.\"token_addresses\", s0.\"token_borrow_balance_underlyings\", s0.\"token_supply_balance_underlyings\", s0.\"total_liquidity_value\", s0.\"total_borrow_value\", s0.\"total_collateral_value\", ( s0.\"total_collateral_value\" )::numeric / NULLIF((s0.\"total_borrow_value\")::numeric, 0) FROM (SELECT s0.\"account\" AS \"account\", array_agg(s0.\"token_address\"::text) AS \"token_addresses\", json_object_agg(s0.\"token_address\", s0.\"borrow_balance_underlying\") AS \"token_borrow_balance_underlyings\", json_object_agg(s0.\"token_address\", s0.\"supply_balance_underlying\") AS \"token_supply_balance_underlyings\", sum(s0.\"liquidity_value\") AS \"total_liquidity_value\", sum(s0.\"borrow_value\") AS \"total_borrow_value\", sum(s0.\"collateral_value\") AS \"total_collateral_value\" FROM (SELECT s0.\"id\" AS \"id\", s0.\"account\" AS \"account\", s0.\"block_number\" AS \"block_number\", s0.\"log_index\" AS \"log_index\", s0.\"supply\" AS \"supply\", s0.\"borrow\" AS \"borrow\", s0.\"interest_index\" AS \"interest_index\", s0.\"net_supply_underlying\" AS \"net_supply_underlying\", s0.\"in_asset\" AS \"in_asset\", s0.\"inserted_at\" AS \"inserted_at\", s0.\"updated_at\" AS \"updated_at\", s0.\"supply\" * t2.\"exchange_rate\" * t2.\"collateral_factor\" * (p1.\"prices\" ->> s0.\"token_address\"::text)::int AS \"collateral_value\", s0.\"borrow\" * t2.\"interest_index\" / NULLIF(s0.\"interest_index\", 0) * (p1.\"prices\" ->> s0.\"token_address\"::text)::int AS \"borrow_value\", (s0.\"supply\" * t2.\"exchange_rate\" * t2.\"collateral_factor\" - s0.\"borrow\" * t2.\"interest_index\" / NULLIF(s0.\"interest_index\", 0)) * (p1.\"prices\" ->> s0.\"token_address\"::text)::int AS \"liquidity_value\", s0.\"supply\" * t2.\"exchange_rate\" AS \"supply_balance_underlying\", s0.\"borrow\" * t2.\"interest_index\" / NULLIF(s0.\"interest_index\", 0) AS \"borrow_balance_underlying\", s0.\"token_address\" AS \"token_address\" FROM (SELECT DISTINCT ON (ARRAY[(a0.\"account\")::text,(a0.\"token_address\")::text]) a0.\"id\" AS \"id\", a0.\"account\" AS \"account\", a0.\"token_address\" AS \"token_address\", a0.\"block_number\" AS \"block_number\", a0.\"log_index\" AS \"log_index\", a0.\"supply\" AS \"supply\", a0.\"borrow\" AS \"borrow\", a0.\"interest_index\" AS \"interest_index\", a0.\"net_supply_underlying\" AS \"net_supply_underlying\", a0.\"in_asset\" AS \"in_asset\", a0.\"inserted_at\" AS \"inserted_at\", a0.\"updated_at\" AS \"updated_at\" FROM \"account_token_moments\" AS a0 WHERE (a0.\"block_number\" <= $1) ORDER BY ARRAY[(a0.\"account\")::text,(a0.\"token_address\")::text], a0.\"block_number\" DESC, a0.\"log_index\" DESC) AS s0 INNER JOIN \"price_moments\" AS p1 ON TRUE INNER JOIN \"token_moments\" AS t2 ON s0.\"token_address\" = t2.\"token_address\" WHERE (s0.\"in_asset\" = TRUE) AND (p1.\"block_number\" <= $2) AND (t2.\"block_number\" <= $3) ORDER BY p1.\"block_number\" DESC, t2.\"block_number\" DESC, p1.\"log_index\" DESC, t2.\"log_index\" DESC) AS s0 GROUP BY s0.\"account\") AS s0 ORDER BY ( s0.\"total_collateral_value\" )::numeric / NULLIF((s0.\"total_borrow_value\")::numeric, 0)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment