-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10467db
commit 786abb6
Showing
4 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
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,69 @@ | ||
with user_position as ( | ||
SELECT userAddress, | ||
poolAddress, | ||
borrowedAmountUsd | ||
FROM `BasePositionSnapshot` | ||
WHERE borrowedAmountUsd > 0 | ||
), | ||
user_collateral_positions as ( | ||
SELECT userAddress, | ||
poolAddress, | ||
SUM( | ||
collateralAmountUsd * CollateralPoolSnapshot.liquidationFactor | ||
) as totalCollateralValue | ||
FROM `CollateralPositionSnapshot` | ||
LEFT JOIN `CollateralPoolSnapshot` ON CollateralPositionSnapshot.underlyingTokenAddress = CollateralPoolSnapshot.underlyingTokenAddress | ||
AND CollateralPositionSnapshot.poolAddress = CollateralPoolSnapshot.poolAddress | ||
GROUP BY userAddress, | ||
poolAddress | ||
), | ||
user_selected_collateral_position as ( | ||
SELECT userAddress, | ||
poolAddress, | ||
SUM(collateralAmountNormalized) as selectedCollateralAmountNormalized, | ||
SUM(collateralAmountUsd) as selectedCollateralAmountUsd, | ||
SUM( | ||
collateralAmountNormalized * CollateralPoolSnapshot.liquidationFactor | ||
) as selectedCollateralAmount, | ||
SUM( | ||
collateralAmountUsd * CollateralPoolSnapshot.liquidationFactor | ||
) as selectedCollateralValue | ||
FROM `CollateralPositionSnapshot` | ||
LEFT JOIN `CollateralPoolSnapshot` ON CollateralPositionSnapshot.underlyingTokenAddress = CollateralPoolSnapshot.underlyingTokenAddress | ||
AND CollateralPositionSnapshot.poolAddress = CollateralPoolSnapshot.poolAddress | ||
WHERE underlyingTokenSymbol = 'ETH' | ||
GROUP BY userAddress, | ||
poolAddress | ||
), | ||
calculations as( | ||
SELECT | ||
borrowedAmountUsd, | ||
totalCollateralValue, | ||
selectedCollateralAmountNormalized, | ||
selectedCollateralAmountUsd, | ||
-- ALREADY MULTIPLIED BY LIQUIDATION FACTOR | ||
selectedCollateralAmount, | ||
-- ALREADY MULTIPLIED BY LIQUIDATION FACTOR | ||
COALESCE(selectedCollateralValue, 0) as selectedCollateralValue, | ||
(totalCollateralValue - selectedCollateralValue) as otherCollateralValue, | ||
(borrowedAmountUsd - otherCollateralValue) as diffDebt, | ||
ROUND( | ||
toFloat64(diffDebt) / toFloat64(selectedCollateralAmount) / 10 | ||
) * 10 as liqPrice | ||
FROM user_position | ||
LEFT JOIN user_collateral_positions ON user_position.userAddress = user_collateral_positions.userAddress | ||
AND user_position.poolAddress = user_collateral_positions.poolAddress | ||
LEFT JOIN user_selected_collateral_position ON user_position.userAddress = user_selected_collateral_position.userAddress | ||
AND user_position.poolAddress = user_selected_collateral_position.poolAddress | ||
WHERE selectedCollateralValue > 0 | ||
AND diffDebt > 0 | ||
) | ||
|
||
SELECT | ||
liqPrice, | ||
count(*) as count, | ||
ROUND(SUM(SUM(borrowedAmountUsd)) OVER (ORDER BY liqPrice DESC)) as cumulative_liquidated_amount, | ||
SUM(count(*)) OVER (ORDER BY liqPrice DESC) as cumulative_count | ||
FROM calculations | ||
GROUP BY liqPrice | ||
ORDER BY liqPrice DESC |
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,73 @@ | ||
with tokens as ( | ||
SELECT arrayJoin(['ETH']) as token | ||
), | ||
user_position as ( | ||
SELECT | ||
userAddress as user, | ||
poolAddress as pool, | ||
borrowedAmountUsd | ||
FROM `BasePositionSnapshot` | ||
WHERE borrowedAmountUsd > 0 | ||
), | ||
user_collateral_positions as ( | ||
SELECT | ||
CollateralPositionSnapshot.userAddress as user, | ||
CollateralPositionSnapshot.poolAddress as pool, | ||
SUM( | ||
collateralAmountUsd * CollateralPoolSnapshot.liquidationFactor | ||
) as totalCollateralValue | ||
FROM `CollateralPositionSnapshot` | ||
LEFT JOIN `CollateralPoolSnapshot` ON CollateralPositionSnapshot.underlyingTokenAddress = CollateralPoolSnapshot.underlyingTokenAddress | ||
AND CollateralPositionSnapshot.poolAddress = CollateralPoolSnapshot.poolAddress | ||
GROUP BY CollateralPositionSnapshot.userAddress, | ||
CollateralPositionSnapshot.poolAddress | ||
), | ||
user_selected_collateral_position as ( | ||
SELECT | ||
tokens.token as tokenSymbol, | ||
CollateralPositionSnapshot.userAddress as user, | ||
CollateralPositionSnapshot.poolAddress as pool, | ||
SUM(CollateralPositionSnapshot.collateralAmountNormalized) as selectedCollateralAmountNormalized, | ||
SUM(CollateralPositionSnapshot.collateralAmountUsd) as selectedCollateralAmountUsd, | ||
SUM( | ||
CollateralPositionSnapshot.collateralAmountNormalized * CollateralPoolSnapshot.liquidationFactor | ||
) as selectedCollateralAmount, | ||
SUM( | ||
CollateralPositionSnapshot.collateralAmountUsd * CollateralPoolSnapshot.liquidationFactor | ||
) as selectedCollateralValue | ||
FROM tokens | ||
CROSS JOIN `CollateralPositionSnapshot` | ||
LEFT JOIN `CollateralPoolSnapshot` ON CollateralPositionSnapshot.underlyingTokenAddress = CollateralPoolSnapshot.underlyingTokenAddress | ||
AND CollateralPositionSnapshot.poolAddress = CollateralPoolSnapshot.poolAddress | ||
WHERE CollateralPositionSnapshot.underlyingTokenSymbol = tokens.token | ||
GROUP BY tokens.token, CollateralPositionSnapshot.userAddress, CollateralPositionSnapshot.poolAddress | ||
), | ||
calculations as( | ||
SELECT | ||
usp.tokenSymbol, | ||
up.borrowedAmountUsd, | ||
ucp.totalCollateralValue, | ||
usp.selectedCollateralAmountNormalized, | ||
usp.selectedCollateralAmountUsd, | ||
usp.selectedCollateralAmount, | ||
COALESCE(usp.selectedCollateralValue, 0) as selectedCollateralValue, | ||
(ucp.totalCollateralValue - COALESCE(usp.selectedCollateralValue, 0)) as otherCollateralValue, | ||
(up.borrowedAmountUsd - (ucp.totalCollateralValue - COALESCE(usp.selectedCollateralValue, 0))) as diffDebt, | ||
ROUND( | ||
toFloat64(up.borrowedAmountUsd - (ucp.totalCollateralValue - COALESCE(usp.selectedCollateralValue, 0))) / | ||
toFloat64(usp.selectedCollateralAmount) | ||
) as liqPrice | ||
FROM user_position up | ||
LEFT JOIN user_collateral_positions ucp ON up.user = ucp.user AND up.pool = ucp.pool | ||
LEFT JOIN user_selected_collateral_position usp ON up.user = usp.user AND up.pool = usp.pool | ||
WHERE COALESCE(usp.selectedCollateralValue, 0) > 0 | ||
AND (up.borrowedAmountUsd - (ucp.totalCollateralValue - COALESCE(usp.selectedCollateralValue, 0))) > 0 | ||
ORDER BY liqPrice DESC) | ||
|
||
SELECT | ||
tokenSymbol, | ||
liqPrice, | ||
count(*) as count | ||
FROM calculations | ||
GROUP BY tokenSymbol, liqPrice | ||
ORDER BY tokenSymbol, liqPrice DESC; |
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,65 @@ | ||
with user_position as ( | ||
SELECT userAddress, | ||
poolAddress, | ||
borrowedAmountUsd | ||
FROM `BasePositionSnapshot` | ||
WHERE borrowedAmountUsd > 0 | ||
), | ||
user_collateral_positions as ( | ||
SELECT userAddress, | ||
poolAddress, | ||
SUM( | ||
collateralAmountUsd * CollateralPoolSnapshot.liquidationFactor | ||
) as totalCollateralValue | ||
FROM `CollateralPositionSnapshot` | ||
LEFT JOIN `CollateralPoolSnapshot` ON CollateralPositionSnapshot.underlyingTokenAddress = CollateralPoolSnapshot.underlyingTokenAddress | ||
AND CollateralPositionSnapshot.poolAddress = CollateralPoolSnapshot.poolAddress | ||
GROUP BY userAddress, | ||
poolAddress | ||
), | ||
user_selected_collateral_position as ( | ||
SELECT userAddress, | ||
poolAddress, | ||
SUM(collateralAmountNormalized) as selectedCollateralAmountNormalized, | ||
SUM(collateralAmountUsd) as selectedCollateralAmountUsd, | ||
SUM( | ||
collateralAmountNormalized * CollateralPoolSnapshot.liquidationFactor | ||
) as selectedCollateralAmount, | ||
SUM( | ||
collateralAmountUsd * CollateralPoolSnapshot.liquidationFactor | ||
) as selectedCollateralValue | ||
FROM `CollateralPositionSnapshot` | ||
LEFT JOIN `CollateralPoolSnapshot` ON CollateralPositionSnapshot.underlyingTokenAddress = CollateralPoolSnapshot.underlyingTokenAddress | ||
AND CollateralPositionSnapshot.poolAddress = CollateralPoolSnapshot.poolAddress | ||
WHERE underlyingTokenSymbol= 'ETH' | ||
GROUP BY userAddress, | ||
poolAddress | ||
), | ||
calculations as(SELECT borrowedAmountUsd, | ||
totalCollateralValue, | ||
selectedCollateralAmountNormalized, | ||
selectedCollateralAmountUsd, | ||
-- ALREADY MULTIPLIED BY LIQUIDATION FACTOR | ||
selectedCollateralAmount, | ||
-- ALREADY MULTIPLIED BY LIQUIDATION FACTOR | ||
COALESCE(selectedCollateralValue, 0) as selectedCollateralValue, | ||
(totalCollateralValue - selectedCollateralValue) as otherCollateralValue, | ||
(borrowedAmountUsd - otherCollateralValue) as diffDebt, | ||
ROUND( | ||
toFloat64(diffDebt) / toFloat64(selectedCollateralAmount) | ||
) as liqPrice | ||
FROM user_position | ||
LEFT JOIN user_collateral_positions ON user_position.userAddress = user_collateral_positions.userAddress | ||
AND user_position.poolAddress = user_collateral_positions.poolAddress | ||
LEFT JOIN user_selected_collateral_position ON user_position.userAddress = user_selected_collateral_position.userAddress | ||
AND user_position.poolAddress = user_selected_collateral_position.poolAddress | ||
WHERE selectedCollateralValue > 0 | ||
AND diffDebt > 0 | ||
ORDER BY liqPrice DESC) | ||
|
||
select | ||
liqPrice, count(*) as count | ||
FROM | ||
calculations | ||
GROUP BY liqPrice | ||
ORDER BY liqPrice DESC |