You are on page 1of 12

with uu as (

select
s.*, _newDeposit,

ROW_NUMBER() over (
partition by s._depositor
order by s.evt_block_number desc, s.evt_index desc
) as updateNumber

from
liquity_ethereum.StabilityPool_evt_DepositSnapshotUpdated s
join
liquity_ethereum.StabilityPool_evt_UserDepositChanged d
on s.evt_block_number = d.evt_block_number and (
s.evt_index + 1 = d.evt_index or -- provideToSP(),
withdrawFromSP()
s.evt_index + 2 = d.evt_index -- withdrawETHGainToTrove()
)
),

u as (select * from uu where updateNumber = 1),

xx as (
select
u.*, _newFrontEndStake,

ROW_NUMBER() over (
partition by u._frontEnd
order by u.evt_block_number desc, u.evt_index desc
) as updateNumber

from
liquity_ethereum.StabilityPool_evt_FrontEndSnapshotUpdated u
join
liquity_ethereum.StabilityPool_evt_FrontEndStakeChanged c
on u.evt_block_number = c.evt_block_number and
u.evt_index + 1 = c.evt_index
),

x as (select * from xx where updateNumber = 1),

ff as (
select t._depositor, _frontEnd,
COALESCE(r._kickbackRate / 1e18, 1) as kickbackRate,
ROW_NUMBER() over (
partition by t._depositor
order by t.evt_block_number desc, t.evt_index desc
) as updateNumber
from liquity_ethereum.StabilityPool_evt_FrontEndTagSet t
left join liquity_ethereum.StabilityPool_evt_FrontEndRegistered r using (_frontEnd)
),

f as (select * from ff where updateNumber = 1),

p as (
select _P as latestP
from liquity_ethereum.StabilityPool_evt_P_Updated
order by evt_block_number desc, evt_index desc
limit 1
),

s as (
select _S as latestS
from liquity_ethereum.StabilityPool_evt_S_Updated
order by evt_block_number desc, evt_index desc
limit 1
),

g as (
select _G as latestG
from liquity_ethereum.StabilityPool_evt_G_Updated
order by evt_block_number desc, evt_index desc
limit 1
),

t as (
select _newBalance / 1e18 as lusdInStabilityPool
from liquity_ethereum.StabilityPool_evt_StabilityPoolLUSDBalanceUpdated
order by evt_block_number desc, evt_index desc
limit 1
),

bp as (
values (0x0d3AbAA7E088C2c82f54B2f47613DA438ea8C598), -- B.Protocol v1
(0x00FF66AB8699AAfa050EE5EF5041D1503aa0849a) -- B.Protocol v2
),

l as (
select
case
when _depositor in (select * from bp) then -- B.Protocol
_depositor
when i.account is not null then
0x2971AdFa57b20E5a416aE5a708A8655A9c74f723 -- InstaIndex
else
_frontEnd
end as frontend,

SUM(f._LQTY / 1e18) as frontendLQTY,


SUM(d._LQTY / 1e18) as depositorLQTY

from
liquity_ethereum.StabilityPool_evt_LQTYPaidToFrontEnd f
right join
liquity_ethereum.StabilityPool_evt_LQTYPaidToDepositor d
on f.evt_tx_hash = d.evt_tx_hash and
f.evt_index + 2 = d.evt_index
left join
instadapp_dsa_ethereum.InstaIndex_evt_LogAccountCreated i
on d._depositor = i.account

group by 1
),

bb as (
select
to as address,
value/1e18 as balanceChange
from erc20_ethereum.evt_Transfer
where
contract_address in (select * from bp) and
to != 0x0000000000000000000000000000000000000000

union all

select
"from" as address,
-(value/1e18) as balanceChange
from erc20_ethereum.evt_Transfer
where
contract_address in (select * from bp) and
"from" != 0x0000000000000000000000000000000000000000
),

b as (
select
address,
SUM(balanceChange) as balance
from bb
group by 1
),

bu as (select COUNT(*) as bProtocolUsers from b where balance > 0),

d as (
select
case
when _depositor in (select * from bp) then -- B.Protocol
_depositor
when i.account is not null then
0x2971AdFa57b20E5a416aE5a708A8655A9c74f723 -- InstaIndex
else
_frontEnd
end as frontend,

kickbackRate,

SUM(
case _newDeposit
when CAST(0 AS UINT256) then
0
else
1
end
) as users,

kickbackRate * SUM(
case
when _newDeposit != CAST(0 AS UINT256) then
_newDeposit * (latestG - u._G) / u._P / 1e36
else
0
end
) as pendingDepositorLQTY,

(1 - kickbackRate) * (
case
when _newFrontEndStake != CAST(0 AS UINT256) then
_newFrontEndStake * (latestG - x._G) / x._P / 1e36
else
0
end
) as pendingFrontendLQTY,

SUM(
case
when _newDeposit != CAST(0 AS UINT256) then
_newDeposit * latestP / u._P / 1e18
else
0
end
) as depositedLUSD

from
u
left join
f
using (_depositor)
left join
x
using (_frontEnd)
left join
instadapp_dsa_ethereum.InstaIndex_evt_LogAccountCreated i
on _depositor = i.account,
p, s, g

group by 1, kickbackRate, _newFrontEndStake, x._G, x._P, latestG


),

dl as (
select
case
when COALESCE(d.frontend, l.frontend) in (select * from bp) then
0x00FF66AB8699AAfa050EE5EF5041D1503aa0849a -- B.Protocol v2
else
COALESCE(d.frontend, l.frontend)
end as frontend,

kickbackRate,

SUM(frontendLQTY) as frontendLQTY,
SUM(depositorLQTY) as depositorLQTY,
SUM(pendingDepositorLQTY) as pendingDepositorLQTY,
SUM(pendingFrontendLQTY) as pendingFrontendLQTY,
SUM(depositedLUSD) as depositedLUSD,
SUM(users) as users

from
d
full outer join
l
ON d.frontend = l.frontend

where COALESCE(d.frontend, l.frontend) !=


0x0000000000000000000000000000000000000000
group by 1, 2
)

select
case frontend
when 0x30e5d10dc30a0ce2545a4dbe8de4fcba590062c5 then
'liquity.app'
when 0x5292f5dadefee3f34c271a41f2ca438c89593674 then
'freely.finance'
when 0x707f18262088e7a8b0d10b7f0c14b50d1cd2b019 then
'lusd.eth.limo [1kx]'
when 0x2ec9b51cf7dabc9f6c3f825c285b28db615eb232 then
'liquity.fun'
when 0xf1c30d30e07e6940d1c12ea25502e2c40d752171 then
'liquity.gg'
when 0x03cd116cabe0747f31a71b3565877717097fc06c then
'liquityapp.com'
when 0x47a7d15b7452820dd7a565ea9c39d8b6cef51ed7 then
'app.liquity.fi'
when 0x04e313306c008f704ad6e34a08d6523db3a271f9 then
'eth.liquity.fi'
when 0xcddb25d5b3776da869d83dfc45c692d556cc66ac then
'multiplier.fi'
when 0x843c837dd73d9a52403fd6d8d68ba4fc1df3c5b4 then
'zerion.io'
when 0x74630370197b4c4795bfeef6645ee14f8cf8997d then
'cp0x'
when 0x3417adc19481fef4fcf29147e681c7127a57cd42 then
'lusd.fi'
when 0x84ac08e2eee5c0d919c5ca82f6b216ae97c15ec0 then
'yliquity.finance'
when 0x5c7a871d65ced6e0063359c08f0589cebd672867 then
'lqty.io'
when 0x58dd12706093459958F5fCDd2ce065D3E6034903 then
'waterslide.app'
when 0xB7f4187D3D55275F81190154a824833700D232cC then
'waterslide.app'
when 0x76720aC2574631530eC8163e4085d6F98513fb27 then
'DeFi Saver'
when 0x00FF66AB8699AAfa050EE5EF5041D1503aa0849a then
'B.Protocol'
when 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723 then
'Instadapp'
when 0x77807bd61d6ebd00601471C1CAB368d91567C2F0 then
'IMTBL.top'
when 0x4Aad6d8D45ba9a906B3DbcD9ee6F4d42e8D4a6B1 then
'Bankless'
else
'unknown'
end as name,

'<a target="_blank" href=https://etherscan.io/address/'


|| CAST(frontend AS VARCHAR)
|| '>'
|| CAST(frontend AS VARCHAR)
|| '🔗</a>' as frontendTag,

COALESCE(depositedLUSD, 0) / lusdInStabilityPool as stabilityPoolShare,

(
COALESCE(frontendLQTY, 0) +
COALESCE(depositorLQTY, 0) +
COALESCE(pendingFrontendLQTY, 0) +
COALESCE(pendingDepositorLQTY, 0)
) as totalLQTY,

(
COALESCE(frontendLQTY, 0) +
COALESCE(pendingFrontendLQTY, 0)
) as frontendLQTY,

(
COALESCE(depositorLQTY, 0) +
COALESCE(pendingDepositorLQTY, 0)
) as depositorLQTY,

case frontend
when 0x00FF66AB8699AAfa050EE5EF5041D1503aa0849a then
bProtocolUsers
else
COALESCE(users, 0)
end as users,

kickbackRate

from dl, t, bu
order by COALESCE(depositedLUSD, 0) desc
/* v1 code
with uu as (
select
s.*, "_newDeposit",

ROW_NUMBER() over (
partition by s."_depositor"
order by s."evt_block_number" desc, s."evt_index" desc
) as "updateNumber"

from
liquity."StabilityPool_evt_DepositSnapshotUpdated" s
join
liquity."StabilityPool_evt_UserDepositChanged" d
on s."evt_block_number" = d."evt_block_number" and (
s."evt_index" + 1 = d."evt_index" or -- provideToSP(),
withdrawFromSP()
s."evt_index" + 2 = d."evt_index" --
withdrawETHGainToTrove()
)
),

u as (select * from uu where "updateNumber" = 1),

xx as (
select
u.*, "_newFrontEndStake",

ROW_NUMBER() over (
partition by u."_frontEnd"
order by u."evt_block_number" desc, u."evt_index" desc
) as "updateNumber"
from
liquity."StabilityPool_evt_FrontEndSnapshotUpdated" u
join
liquity."StabilityPool_evt_FrontEndStakeChanged" c
on u."evt_block_number" = c."evt_block_number" and
u."evt_index" + 1 = c."evt_index"
),

x as (select * from xx where "updateNumber" = 1),

ff as (
select
t."_depositor", t."_frontEnd",
COALESCE(r."_kickbackRate" / 1e18, 1) as "kickbackRate",

ROW_NUMBER() over (
partition by t."_depositor"
order by t."evt_block_number" desc, t."evt_index" desc
) as "updateNumber"

from
liquity."StabilityPool_evt_FrontEndTagSet" t
left join
liquity."StabilityPool_evt_FrontEndRegistered" r
using ("_frontEnd")
),

f as (select * from ff where "updateNumber" = 1),

p as (
select "_P" as "latestP"
from liquity."StabilityPool_evt_P_Updated"
order by "evt_block_number" desc, "evt_index" desc
limit 1
),

s as (
select "_S" as "latestS"
from liquity."StabilityPool_evt_S_Updated"
order by "evt_block_number" desc, "evt_index" desc
limit 1
),

g as (
select "_G" as "latestG"
from liquity."StabilityPool_evt_G_Updated"
order by "evt_block_number" desc, "evt_index" desc
limit 1
),

t as (
select "_newBalance" / 1e18 as "lusdInStabilityPool"
from liquity."StabilityPool_evt_StabilityPoolLUSDBalanceUpdated"
order by "evt_block_number" desc, "evt_index" desc
limit 1
),

bp as (
values ('\x0d3AbAA7E088C2c82f54B2f47613DA438ea8C598'::bytea), -- B.Protocol v1
('\x00FF66AB8699AAfa050EE5EF5041D1503aa0849a') -- B.Protocol v2
),

l as (
select
case
when "_depositor" in (select * from bp) then -- B.Protocol
"_depositor"
when i."account" is not null then
'\x2971AdFa57b20E5a416aE5a708A8655A9c74f723' -- InstaIndex
else
"_frontEnd"
end as "frontend",

SUM(f."_LQTY" / 1e18) as "frontendLQTY",


SUM(d."_LQTY" / 1e18) as "depositorLQTY"

from
liquity."StabilityPool_evt_LQTYPaidToFrontEnd" f
right join
liquity."StabilityPool_evt_LQTYPaidToDepositor" d
on f."evt_tx_hash" = d."evt_tx_hash" and
f."evt_index" + 2 = d."evt_index"
left join
instadapp_dsa."InstaIndex_evt_LogAccountCreated" i
on d."_depositor" = i."account"

group by 1
),

bb as (
select
"to" as "address",
"value" / 1e18 as "balanceChange"
from erc20."ERC20_evt_Transfer"
where
"contract_address" in (select * from bp) and
"to" != '\x0000000000000000000000000000000000000000'

union all

select
"from" as "address",
-"value" / 1e18 as "balanceChange"
from erc20."ERC20_evt_Transfer"
where
"contract_address" in (select * from bp) and
"from" != '\x0000000000000000000000000000000000000000'
),

b as (
select
"address",
SUM("balanceChange") as "balance"
from bb
group by 1
),
bu as (select COUNT(*) as "bProtocolUsers" from b where "balance" > 0),

d as (
select
case
when u."_depositor" in (select * from bp) then -- B.Protocol
u."_depositor"
when i."account" is not null then
'\x2971AdFa57b20E5a416aE5a708A8655A9c74f723' -- InstaIndex
else
"_frontEnd"
end as "frontend",

"kickbackRate",

SUM(
case "_newDeposit"
when 0 then
0
else
1
end
) as "users",

"kickbackRate" * SUM(
case
when "_newDeposit" != 0 then
"_newDeposit" * ("latestG" - u."_G") / u."_P" / 1e36
else
0
end
) as "pendingDepositorLQTY",

(1 - "kickbackRate") * (
case
when "_newFrontEndStake" != 0 then
"_newFrontEndStake" * ("latestG" - x."_G") / x."_P" / 1e36
else
0
end
) as "pendingFrontendLQTY",

SUM(
case
when "_newDeposit" != 0 then
"_newDeposit" * "latestP" / u."_P" / 1e18
else
0
end
) as "depositedLUSD"

from
u
left join
f
using ("_depositor")
left join
x
using ("_frontEnd")
left join
instadapp_dsa."InstaIndex_evt_LogAccountCreated" i
on u."_depositor" = i."account",
p, s, g

group by 1, "kickbackRate", "_newFrontEndStake", x."_G", x."_P", "latestG"


),

dl as (
select
case
when COALESCE(d."frontend", l."frontend") in (select * from bp) then
'\x00FF66AB8699AAfa050EE5EF5041D1503aa0849a' -- B.Protocol v2
else
COALESCE(d."frontend", l."frontend")
end as "frontend",

"kickbackRate",

SUM("frontendLQTY") as "frontendLQTY",
SUM("depositorLQTY") as "depositorLQTY",
SUM("pendingDepositorLQTY") as "pendingDepositorLQTY",
SUM("pendingFrontendLQTY") as "pendingFrontendLQTY",
SUM("depositedLUSD") as "depositedLUSD",
SUM("users") as "users"

from
d
full outer join
l
using ("frontend")

where COALESCE(d."frontend", l."frontend") != '\


x0000000000000000000000000000000000000000'
group by 1, 2
)

select
case "frontend"
when '\x30e5d10dc30a0ce2545a4dbe8de4fcba590062c5' then
'liquity.app'
when '\x5292f5dadefee3f34c271a41f2ca438c89593674' then
'freely.finance'
when '\x707f18262088e7a8b0d10b7f0c14b50d1cd2b019' then
'lusd.eth.link [1kx]'
when '\x2ec9b51cf7dabc9f6c3f825c285b28db615eb232' then
'liquity.fun'
when '\xf1c30d30e07e6940d1c12ea25502e2c40d752171' then
'liquity.gg'
when '\x03cd116cabe0747f31a71b3565877717097fc06c' then
'liquityapp.com'
when '\x47a7d15b7452820dd7a565ea9c39d8b6cef51ed7' then
'app.liquity.fi'
when '\x04e313306c008f704ad6e34a08d6523db3a271f9' then
'eth.liquity.fi'
when '\xcddb25d5b3776da869d83dfc45c692d556cc66ac' then
'multiplier.fi'
when '\x843c837dd73d9a52403fd6d8d68ba4fc1df3c5b4' then
'zerion.io'
when '\x74630370197b4c4795bfeef6645ee14f8cf8997d' then
'cp0x'
when '\x3417adc19481fef4fcf29147e681c7127a57cd42' then
'lusd.fi'
when '\x84ac08e2eee5c0d919c5ca82f6b216ae97c15ec0' then
'yliquity.finance'
when '\x5c7a871d65ced6e0063359c08f0589cebd672867' then
'lqty.io'
when '\x58dd12706093459958F5fCDd2ce065D3E6034903' then
'waterslide.app'
when '\xB7f4187D3D55275F81190154a824833700D232cC' then
'waterslide.app'
when '\x76720aC2574631530eC8163e4085d6F98513fb27' then
'DeFi Saver'
when '\x00FF66AB8699AAfa050EE5EF5041D1503aa0849a' then
'B.Protocol'
when '\x2971AdFa57b20E5a416aE5a708A8655A9c74f723' then
'Instadapp'
when '\x77807bd61d6ebd00601471C1CAB368d91567C2F0' then
'IMTBL.top'
else
'unknown'
end as "name",

'<a href="https://etherscan.io/address/0x'
|| ENCODE("frontend", 'hex')
|| '">0x'
|| ENCODE("frontend", 'hex')
|| '</a>' as "frontendTag",

COALESCE("depositedLUSD", 0) / "lusdInStabilityPool" as "stabilityPoolShare",

(
COALESCE("frontendLQTY", 0) +
COALESCE("depositorLQTY", 0) +
COALESCE("pendingFrontendLQTY", 0) +
COALESCE("pendingDepositorLQTY", 0)
) as "totalLQTY",

(
COALESCE("frontendLQTY", 0) +
COALESCE("pendingFrontendLQTY", 0)
) as "frontendLQTY",

(
COALESCE("depositorLQTY", 0) +
COALESCE("pendingDepositorLQTY", 0)
) as "depositorLQTY",

case "frontend"
when '\x00FF66AB8699AAfa050EE5EF5041D1503aa0849a' then
"bProtocolUsers"
else
COALESCE("users", 0)
end as "users",

"kickbackRate"

from dl, t, bu
order by COALESCE("depositedLUSD", 0) desc
;
*/

You might also like