Skip to content

Commit aab4d8d

Browse files
authored
Subgraph updates for SPOT v5 (#260)
* spot subgraph updates * updated bb subgraph * cleaned up spot subgraph * ran linter
1 parent dde1112 commit aab4d8d

File tree

14 files changed

+985
-191
lines changed

14 files changed

+985
-191
lines changed

spot-contracts/exported-artifacts/FeePolicy.json

Lines changed: 582 additions & 0 deletions
Large diffs are not rendered by default.

spot-staking-subgraph/schema.graphql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ type BillBrokerDailyStat @entity {
3434
perpPrice: BigDecimal!
3535
usdPrice: BigDecimal!
3636
totalSupply: BigDecimal!
37-
usdSwapAmt: BigDecimal!
38-
perpSwapAmt: BigDecimal!
39-
usdFeeAmt: BigDecimal!
40-
perpFeeAmt: BigDecimal!
37+
swapValue: BigDecimal!
38+
feeValue: BigDecimal!
39+
feeYield: BigDecimal!
4140
tvl:BigDecimal!
4241
price:BigDecimal!
4342
}
@@ -50,7 +49,8 @@ type BillBrokerSwap @entity {
5049
nonce: BigInt!
5150
type: String!
5251
swapAmt: BigDecimal!
53-
feeAmt: BigDecimal!
52+
swapValue: BigDecimal!
53+
feeValue: BigDecimal!
5454
tx: String!
5555
}
5656

spot-staking-subgraph/src/billBroker.ts

Lines changed: 120 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
import {
1919
BillBroker__computePerpToUSDSwapAmt1InputSStruct,
2020
BillBroker__computeUSDToPerpSwapAmtInputSStruct,
21+
BillBroker__computeMintAmtWithUSD1InputSStruct,
22+
BillBroker__computeMintAmtWithPerp1InputSStruct,
2123
} from '../generated/BillBroker/BillBroker'
2224
import { BillBroker as BillBrokerABI } from '../generated/BillBroker/BillBroker'
2325
import { ERC20 as ERC20ABI } from '../generated/BillBroker/ERC20'
@@ -74,7 +76,10 @@ export function fetchBillBroker(address: Address): BillBroker {
7476
return vault as BillBroker
7577
}
7678

77-
export function fetchBillBrokerDailyStat(vault: BillBroker, timestamp: BigInt): BillBrokerDailyStat {
79+
export function fetchBillBrokerDailyStat(
80+
vault: BillBroker,
81+
timestamp: BigInt,
82+
): BillBrokerDailyStat {
7883
let id = vault.id.concat('-').concat(timestamp.toString())
7984
let dailyStat = BillBrokerDailyStat.load(id)
8085
if (dailyStat === null) {
@@ -86,10 +91,9 @@ export function fetchBillBrokerDailyStat(vault: BillBroker, timestamp: BigInt):
8691
dailyStat.perpPrice = BIGDECIMAL_ZERO
8792
dailyStat.usdPrice = BIGDECIMAL_ZERO
8893
dailyStat.totalSupply = BIGDECIMAL_ZERO
89-
dailyStat.usdSwapAmt = BIGDECIMAL_ZERO
90-
dailyStat.perpSwapAmt = BIGDECIMAL_ZERO
91-
dailyStat.usdFeeAmt = BIGDECIMAL_ZERO
92-
dailyStat.perpFeeAmt = BIGDECIMAL_ZERO
94+
dailyStat.swapValue = BIGDECIMAL_ZERO
95+
dailyStat.feeValue = BIGDECIMAL_ZERO
96+
dailyStat.feeYield = BIGDECIMAL_ZERO
9397
dailyStat.tvl = BIGDECIMAL_ZERO
9498
dailyStat.price = BIGDECIMAL_ZERO
9599
dailyStat.save()
@@ -106,7 +110,8 @@ function fetchBillBrokerSwap(vault: BillBroker, nonce: BigInt): BillBrokerSwap {
106110
swap.nonce = nonce
107111
swap.type = ''
108112
swap.swapAmt = BIGDECIMAL_ZERO
109-
swap.feeAmt = BIGDECIMAL_ZERO
113+
swap.swapValue = BIGDECIMAL_ZERO
114+
swap.feeValue = BIGDECIMAL_ZERO
110115
swap.tx = '0x'
111116
swap.timestamp = BIGINT_ZERO
112117
swap.save()
@@ -152,6 +157,7 @@ export function handleSwapPerpsForUSD(event: SwapPerpsForUSD): void {
152157
vault.usdPrice = formatBalance(event.params.preOpState.usdPrice, vault.decimals)
153158
vault.tvl = vault.usdBal.times(vault.usdPrice).plus(vault.perpBal.times(vault.perpPrice))
154159
vault.price = vault.tvl.div(vault.totalSupply)
160+
vault.swapNonce = vault.swapNonce.plus(BIGINT_ONE)
155161
vault.save()
156162

157163
dailyStat.perpPrice = vault.perpPrice
@@ -162,6 +168,7 @@ export function handleSwapPerpsForUSD(event: SwapPerpsForUSD): void {
162168

163169
swap.type = 'perps'
164170
swap.swapAmt = formatBalance(event.params.perpAmtIn, vault.perpDecimals)
171+
swap.swapValue = swap.swapAmt.times(vault.perpPrice)
165172
swap.tx = event.transaction.hash.toHex()
166173
swap.timestamp = event.block.timestamp
167174
swap.save()
@@ -180,15 +187,16 @@ export function handleSwapPerpsForUSD(event: SwapPerpsForUSD): void {
180187
let r = vaultContract.try_computePerpToUSDSwapAmt1(event.params.perpAmtIn, reserveStateStruct)
181188
if (!r.reverted) {
182189
let swapAmts = r.value
183-
dailyStat.perpSwapAmt = dailyStat.perpSwapAmt.plus(
184-
formatBalance(event.params.perpAmtIn, vault.perpDecimals),
185-
)
186-
dailyStat.usdFeeAmt = dailyStat.usdFeeAmt.plus(
187-
formatBalance(swapAmts.value1, vault.usdDecimals),
188-
)
190+
let perpAmtIn = swap.swapAmt
191+
let usdAmtOut = formatBalance(swapAmts.value0, vault.usdDecimals)
192+
let estUsdAmtOut = perpAmtIn.times(vault.perpPrice).div(vault.usdPrice)
193+
let usdFeeAmt = estUsdAmtOut.minus(usdAmtOut)
194+
let feeValue = usdFeeAmt.times(vault.usdPrice)
195+
dailyStat.swapValue = dailyStat.swapValue.plus(swap.swapValue)
196+
dailyStat.feeValue = dailyStat.feeValue.plus(feeValue)
197+
dailyStat.feeYield = dailyStat.feeValue.div(dailyStat.tvl)
189198
dailyStat.save()
190-
191-
swap.feeAmt = dailyStat.usdFeeAmt
199+
swap.feeValue = feeValue
192200
swap.save()
193201
}
194202
}
@@ -204,6 +212,7 @@ export function handleSwapUSDForPerps(event: SwapUSDForPerps): void {
204212
vault.usdPrice = formatBalance(event.params.preOpState.usdPrice, vault.decimals)
205213
vault.tvl = vault.usdBal.times(vault.usdPrice).plus(vault.perpBal.times(vault.perpPrice))
206214
vault.price = vault.tvl.div(vault.totalSupply)
215+
vault.swapNonce = vault.swapNonce.plus(BIGINT_ONE)
207216
vault.save()
208217

209218
dailyStat.perpPrice = vault.perpPrice
@@ -213,7 +222,8 @@ export function handleSwapUSDForPerps(event: SwapUSDForPerps): void {
213222
dailyStat.save()
214223

215224
swap.type = 'usd'
216-
swap.swapAmt = formatBalance(event.params.usdAmtIn, vault.perpDecimals)
225+
swap.swapAmt = formatBalance(event.params.usdAmtIn, vault.usdDecimals)
226+
swap.swapValue = swap.swapAmt.times(vault.usdPrice)
217227
swap.tx = event.transaction.hash.toHex()
218228
swap.timestamp = event.block.timestamp
219229
swap.save()
@@ -232,15 +242,16 @@ export function handleSwapUSDForPerps(event: SwapUSDForPerps): void {
232242
let r = vaultContract.try_computeUSDToPerpSwapAmt(event.params.usdAmtIn, reserveStateStruct)
233243
if (!r.reverted) {
234244
let swapAmts = r.value
235-
dailyStat.usdSwapAmt = dailyStat.usdSwapAmt.plus(
236-
formatBalance(event.params.usdAmtIn, vault.usdDecimals),
237-
)
238-
dailyStat.perpFeeAmt = dailyStat.perpFeeAmt.plus(
239-
formatBalance(swapAmts.value1, vault.perpDecimals),
240-
)
245+
let usdAmtIn = swap.swapAmt
246+
let perpAmtOut = formatBalance(swapAmts.value0, vault.perpDecimals)
247+
let estPerpAmtOut = usdAmtIn.times(vault.usdPrice).div(vault.perpPrice)
248+
let perpFeeAmt = estPerpAmtOut.minus(perpAmtOut)
249+
let feeValue = perpFeeAmt.times(vault.perpPrice)
250+
dailyStat.swapValue = dailyStat.swapValue.plus(swap.swapValue)
251+
dailyStat.feeValue = dailyStat.feeValue.plus(feeValue)
252+
dailyStat.feeYield = dailyStat.feeValue.div(dailyStat.tvl)
241253
dailyStat.save()
242-
243-
swap.feeAmt = dailyStat.perpFeeAmt
254+
swap.feeValue = feeValue
244255
swap.save()
245256
}
246257
}
@@ -249,6 +260,7 @@ export function handleDepositUSD(event: DepositUSD): void {
249260
log.warning('triggered single sided deposit', [])
250261
let vault = fetchBillBroker(event.address)
251262
let dailyStat = fetchBillBrokerDailyStat(vault, dayTimestamp(event.block.timestamp))
263+
let swap = fetchBillBrokerSwap(vault, vault.swapNonce.plus(BIGINT_ONE))
252264
refreshBillBrokerStats(vault, dailyStat)
253265

254266
vault.perpPrice = formatBalance(event.params.preOpState.perpPrice, vault.decimals)
@@ -262,12 +274,55 @@ export function handleDepositUSD(event: DepositUSD): void {
262274
dailyStat.tvl = vault.tvl
263275
dailyStat.price = vault.price
264276
dailyStat.save()
277+
278+
let vaultContract = BillBrokerABI.bind(stringToAddress(vault.id))
279+
let reserveStateValues: Array<ethereum.Value> = [
280+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.usdBalance),
281+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.perpBalance),
282+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.usdPrice),
283+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.perpPrice),
284+
]
285+
let reserveStateTuple = changetype<ethereum.Tuple>(reserveStateValues)
286+
let reserveStateStruct = changetype<BillBroker__computeMintAmtWithUSD1InputSStruct>(
287+
reserveStateTuple,
288+
)
289+
let r = vaultContract.try_computeMintAmtWithUSD1(event.params.usdAmtIn, reserveStateStruct)
290+
if (!r.reverted) {
291+
let usdAmtIn = formatBalance(event.params.usdAmtIn, vault.usdDecimals)
292+
let valueIn = usdAmtIn.times(vault.usdPrice)
293+
let estMintAmt = formatBalance(r.value, vault.decimals)
294+
let mintAmt = valueIn.div(vault.tvl).times(vault.totalSupply)
295+
let feePerc = estMintAmt.minus(mintAmt).div(estMintAmt)
296+
297+
let usdClaimPost = vault.usdBal.times(estMintAmt).div(vault.totalSupply)
298+
let swapAmt = usdAmtIn.minus(usdClaimPost)
299+
let feeValue = swapAmt.times(feePerc)
300+
301+
vault.swapNonce = vault.swapNonce.plus(BIGINT_ONE)
302+
vault.save()
303+
304+
swap.type = 'usd'
305+
swap.swapAmt = swapAmt
306+
swap.swapValue = swap.swapAmt.times(vault.usdPrice)
307+
swap.tx = event.transaction.hash.toHex()
308+
swap.timestamp = event.block.timestamp
309+
swap.save()
310+
311+
dailyStat.swapValue = dailyStat.swapValue.plus(swap.swapValue)
312+
dailyStat.feeValue = dailyStat.feeValue.plus(feeValue)
313+
dailyStat.feeYield = dailyStat.feeValue.div(dailyStat.tvl)
314+
dailyStat.save()
315+
316+
swap.feeValue = feeValue
317+
swap.save()
318+
}
265319
}
266320

267321
export function handleDepositPerp(event: DepositPerp): void {
268322
log.warning('triggered single sided deposit', [])
269323
let vault = fetchBillBroker(event.address)
270324
let dailyStat = fetchBillBrokerDailyStat(vault, dayTimestamp(event.block.timestamp))
325+
let swap = fetchBillBrokerSwap(vault, vault.swapNonce.plus(BIGINT_ONE))
271326
refreshBillBrokerStats(vault, dailyStat)
272327

273328
vault.perpPrice = formatBalance(event.params.preOpState.perpPrice, vault.decimals)
@@ -281,4 +336,46 @@ export function handleDepositPerp(event: DepositPerp): void {
281336
dailyStat.tvl = vault.tvl
282337
dailyStat.price = vault.price
283338
dailyStat.save()
339+
340+
let vaultContract = BillBrokerABI.bind(stringToAddress(vault.id))
341+
let reserveStateValues: Array<ethereum.Value> = [
342+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.usdBalance),
343+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.perpBalance),
344+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.usdPrice),
345+
ethereum.Value.fromUnsignedBigInt(event.params.preOpState.perpPrice),
346+
]
347+
let reserveStateTuple = changetype<ethereum.Tuple>(reserveStateValues)
348+
let reserveStateStruct = changetype<BillBroker__computeMintAmtWithPerp1InputSStruct>(
349+
reserveStateTuple,
350+
)
351+
let r = vaultContract.try_computeMintAmtWithPerp1(event.params.perpAmtIn, reserveStateStruct)
352+
if (!r.reverted) {
353+
let perpAmtIn = formatBalance(event.params.perpAmtIn, vault.perpDecimals)
354+
let valueIn = perpAmtIn.times(vault.perpPrice)
355+
let estMintAmt = formatBalance(r.value, vault.decimals)
356+
let mintAmt = valueIn.div(vault.tvl).times(vault.totalSupply)
357+
let feePerc = estMintAmt.minus(mintAmt).div(estMintAmt)
358+
359+
let perpClaimPost = vault.perpBal.times(estMintAmt).div(vault.totalSupply)
360+
let swapAmt = perpAmtIn.minus(perpClaimPost)
361+
let feeValue = swapAmt.times(feePerc)
362+
363+
vault.swapNonce = vault.swapNonce.plus(BIGINT_ONE)
364+
vault.save()
365+
366+
swap.type = 'perp'
367+
swap.swapAmt = swapAmt
368+
swap.swapValue = swap.swapAmt.times(vault.perpPrice)
369+
swap.tx = event.transaction.hash.toHex()
370+
swap.timestamp = event.block.timestamp
371+
swap.save()
372+
373+
dailyStat.swapValue = dailyStat.swapValue.plus(swap.swapValue)
374+
dailyStat.feeValue = dailyStat.feeValue.plus(feeValue)
375+
dailyStat.feeYield = dailyStat.feeValue.div(dailyStat.tvl)
376+
dailyStat.save()
377+
378+
swap.feeValue = feeValue
379+
swap.save()
380+
}
284381
}

spot-staking-subgraph/src/charmVault.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function fetchCharmVault(address: Address): CharmVault {
5959
vault.tvl = BIGDECIMAL_ZERO
6060
vault.price = BIGDECIMAL_ZERO
6161
vault.totalSupply = BIGDECIMAL_ZERO
62-
62+
6363
let context = new DataSourceContext()
6464
context.setString('charmVault', id)
6565
RebasingERC20.createWithContext(getUnderlyingAddress(token1Address), context)
@@ -68,7 +68,10 @@ export function fetchCharmVault(address: Address): CharmVault {
6868
return vault as CharmVault
6969
}
7070

71-
export function fetchCharmVaultDailyStat(vault: CharmVault, timestamp: BigInt): CharmVaultDailyStat {
71+
export function fetchCharmVaultDailyStat(
72+
vault: CharmVault,
73+
timestamp: BigInt,
74+
): CharmVaultDailyStat {
7275
let id = vault.id.concat('-').concat(timestamp.toString())
7376
let dailyStat = CharmVaultDailyStat.load(id)
7477
if (dailyStat === null) {
@@ -103,7 +106,9 @@ export function refreshCharmVaultStats(vault: CharmVault, dailyStat: CharmVaultD
103106
vault.token1Bal = formatBalance(tokenBals.value1, vault.token1Decimals)
104107
vault.token0Price = BIGDECIMAL_ONE
105108
vault.token1Price = prices[0]
106-
vault.tvl = vault.token0Bal.times(vault.token0Price).plus(vault.token1Bal.times(vault.token1Price))
109+
vault.tvl = vault.token0Bal
110+
.times(vault.token0Price)
111+
.plus(vault.token1Bal.times(vault.token1Price))
107112
vault.totalSupply = formatBalance(vaultContract.totalSupply(), vault.decimals)
108113
vault.price = vault.tvl.div(vault.totalSupply)
109114
vault.save()
@@ -145,9 +150,15 @@ export function handleFees(event: CollectFees): void {
145150
let dailyStat = fetchCharmVaultDailyStat(vault, dayTimestamp(event.block.timestamp))
146151
refreshCharmVaultStats(vault, dailyStat)
147152

148-
dailyStat.token0Fees = dailyStat.token0Fees.plus(formatBalance(event.params.feesToVault0, vault.token0Decimals))
149-
dailyStat.token1Fees = dailyStat.token1Fees.plus(formatBalance(event.params.feesToVault1, vault.token1Decimals))
150-
dailyStat.totalFeeVal = dailyStat.token1Fees.times(dailyStat.token1Price).plus(dailyStat.token0Fees.times(dailyStat.token0Price))
153+
dailyStat.token0Fees = dailyStat.token0Fees.plus(
154+
formatBalance(event.params.feesToVault0, vault.token0Decimals),
155+
)
156+
dailyStat.token1Fees = dailyStat.token1Fees.plus(
157+
formatBalance(event.params.feesToVault1, vault.token1Decimals),
158+
)
159+
dailyStat.totalFeeVal = dailyStat.token1Fees
160+
.times(dailyStat.token1Price)
161+
.plus(dailyStat.token0Fees.times(dailyStat.token0Price))
151162
dailyStat.feeYield = dailyStat.totalFeeVal.div(dailyStat.tvl.minus(dailyStat.totalFeeVal))
152163
dailyStat.save()
153164
}

spot-staking-subgraph/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ export function getUnderlyingAddress(tokenAddress: Address): Address {
6767
return collateralResult.value
6868
}
6969
return Address.fromString('0x0000000000000000000000000000000000000000')
70-
}
70+
}

0 commit comments

Comments
 (0)