Skip to content

Commit

Permalink
chore: fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Nov 6, 2024
1 parent 10467db commit 786abb6
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/market/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ impl Market for Contract {
/// # Number of Storage Accesses
/// * Reads: `5`
#[storage(read)]
fn collateral_value_to_sell(asset_id: AssetId, collateral_amount: u64) -> u64 { // decimals: collateral_asset.decimals
fn collateral_value_to_sell(asset_id: AssetId, collateral_amount: u64) -> u64 { // decimals: base_token_decimals
let collateral_configuration = storage.collateral_configurations.get(asset_id).read();
let market_configuration = storage.market_configuration.read();

Expand Down
69 changes: 69 additions & 0 deletions liquidations copy 2.sql
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
73 changes: 73 additions & 0 deletions liquidations copy.sql
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;
65 changes: 65 additions & 0 deletions liquidations.sql
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

0 comments on commit 786abb6

Please sign in to comment.