Skip to content

Commit

Permalink
Update snapshots on clock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 26, 2024
1 parent 9969308 commit c585876
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 275 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ This Subgraph sources events from the Beefy CLM contracts in different networks.
### GraphiQL Explorer

- Arbitrum: [https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-arbitrum/graphql](https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-arbitrum/graphql)
- Base: [https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-base/graphql](https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-base/graphql)
- Optimism: [https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-optimism/graphql](https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-optimism/graphql)

### Api Endpoints

- Arbitrum: [https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-arbitrum](https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-arbitrum)
- Base: [https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-base](https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-base)
- Optimism: [https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-optimism](https://api.0xgraph.xyz/subgraphs/name/beefyfinance/clm-optimism)

# Contributing
Expand Down Expand Up @@ -76,7 +78,7 @@ yarn test:lint # run prettier linter
### How to add a new network

1. Add the network configuration [config/<network>.json](config/).
- `clockTickBlocks` is the number of blocks between each clock tick, aim for a clock tick every 15 minutes.
- `clockTickBlocks` is the number of blocks between each clock tick, aim for a clock tick every 5 minutes.
- Find the uniswap v3, QuoterV2 contract address [on uniswap's documentation](https://docs.uniswap.org/contracts/v3/reference/deployments)
- Find the <native>/USD price feed [on chainlink's documentation](https://docs.chain.link/data-feeds/price-feeds/addresses#networks). Verify that it's a ChainLink `AggregatorV3Interface` with the `latestRoundData()` method.
2. Add dev RPCs in graph-node config [docker/graph-node/config.toml](docker/graph-node/config.toml).
Expand Down
2 changes: 1 addition & 1 deletion config/arbitrum.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"strategyFactoryStartBlock": 193389019,
"boostFactoryAddress": "0x2951c806a75b19954ce0bed477676a54f3c1c200",
"boostFactoryStartBlock": 193388971,
"clockTickBlocks": 3600,
"clockTickBlocks": 1300,

"wrappedNativeAddress": "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
"wrappedNativeDecimals": 18,
Expand Down
2 changes: 1 addition & 1 deletion config/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"strategyFactoryStartBlock": 12294025,
"boostFactoryAddress": "0x1111111111111111111111111111111111111111",
"boostFactoryStartBlock": 12294025,
"clockTickBlocks": 450,
"clockTickBlocks": 150,

"wrappedNativeAddress": "0x4200000000000000000000000000000000000006",
"wrappedNativeDecimals": 18,
Expand Down
2 changes: 1 addition & 1 deletion config/optimism.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"strategyFactoryStartBlock": 117800962,
"boostFactoryAddress": "0xf0a7626eccbee00af144bb1f77cd187af85bbf41",
"boostFactoryStartBlock": 117800956,
"clockTickBlocks": 750,
"clockTickBlocks": 250,

"wrappedNativeAddress": "0x4200000000000000000000000000000000000006",
"wrappedNativeDecimals": 18,
Expand Down
351 changes: 102 additions & 249 deletions src/clock.ts

Large diffs are not rendered by default.

30 changes: 20 additions & 10 deletions src/entity/clock.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { BigInt, Bytes } from "@graphprotocol/graph-ts"
import { BigInt } from "@graphprotocol/graph-ts"
import { getIntervalFromTimestamp } from "../utils/time"
import { getSnapshotIdSuffix } from "../utils/snapshot"
import { ClockTick } from "../../generated/schema"


@inline
export function getClockTickId(timestamp: BigInt, period: BigInt): Bytes {
const interval = getIntervalFromTimestamp(timestamp, period)
return getSnapshotIdSuffix(period, interval)
export function getClockTick(timestamp: BigInt, period: BigInt): ClockRes {
let interval = getIntervalFromTimestamp(timestamp, period)
let clockTickId = getSnapshotIdSuffix(period, interval)
let clockTick = ClockTick.load(clockTickId)
let isNew = false
if (!clockTick) {
isNew = true
clockTick = new ClockTick(clockTickId)
clockTick.timestamp = timestamp
clockTick.roundedTimestamp = interval
clockTick.period = period
}
return new ClockRes(clockTick, isNew)
}


@inline
export function getPreviousClockTickId(timestamp: BigInt, period: BigInt): Bytes {
return getClockTickId(timestamp.minus(period), period)
class ClockRes {
constructor(
public tick: ClockTick,
public isNew: boolean,
) {}
}
20 changes: 10 additions & 10 deletions src/utils/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function fetchVaultPrices(
token0: Token,
token1: Token,
): VaultPrices {
log.debug("updateUserPosition: fetching data for vault {}", [vault.id.toHexString()])
log.debug("fetchVaultPrices: fetching data for vault {}", [vault.id.toHexString()])
const token0Path = strategy.lpToken0ToNativePath
const token1Path = strategy.lpToken1ToNativePath

Expand All @@ -30,39 +30,39 @@ export function fetchVaultPrices(
if (token0Path.length > 0 && token0.id.notEqual(WNATIVE_TOKEN_ADDRESS)) {
const token0PriceInNativeRes = quoter.try_quoteExactInput(token0Path, exponentToBigInt(token0.decimals))
if (token0PriceInNativeRes.reverted) {
log.error("updateUserPosition: quoteExactInput() of vault {} reverted for token {} (token0) with path {}", [
log.error("fetchVaultPrices: quoteExactInput() of vault {} reverted for token {} (token0) with path {}", [
vault.id.toHexString(),
token0.id.toHexString(),
token0Path.toHexString(),
])
throw Error("updateUserPosition: quoteExactInput() reverted")
throw Error("fetchVaultPrices: quoteExactInput() reverted")
}
token0PriceInNative = tokenAmountToDecimal(token0PriceInNativeRes.value.getAmountOut(), WNATIVE_DECIMALS)
log.debug("updateUserPosition: token0PriceInNativeRes: {}", [token0PriceInNative.toString()])
log.debug("fetchVaultPrices: token0PriceInNativeRes: {}", [token0PriceInNative.toString()])
}
let token1PriceInNative = ONE_BD
if (token1Path.length > 0 && token1.id.notEqual(WNATIVE_TOKEN_ADDRESS)) {
const token1PriceInNativeRes = quoter.try_quoteExactInput(token1Path, exponentToBigInt(token1.decimals))
if (token1PriceInNativeRes.reverted) {
log.error("updateUserPosition: quoteExactInput() of vault {} reverted for token {} (token1) with path {}", [
log.error("fetchVaultPrices: quoteExactInput() of vault {} reverted for token {} (token1) with path {}", [
vault.id.toHexString(),
token1.id.toHexString(),
token1Path.toHexString(),
])
throw Error("updateUserPosition: quoteExactInput() reverted")
throw Error("fetchVaultPrices: quoteExactInput() reverted")
}
token1PriceInNative = tokenAmountToDecimal(token1PriceInNativeRes.value.getAmountOut(), WNATIVE_DECIMALS)
log.debug("updateUserPosition: token1PriceInNativeRes: {}", [token1PriceInNative.toString()])
log.debug("fetchVaultPrices: token1PriceInNativeRes: {}", [token1PriceInNative.toString()])
}

// fetch the native price in USD
const nativePriceUSDRes = nativePriceFeed.try_latestRoundData()
if (nativePriceUSDRes.reverted) {
log.error("updateUserPosition: latestRoundData() reverted for native token", [])
throw Error("updateUserPosition: latestRoundData() reverted")
log.error("fetchVaultPrices: latestRoundData() reverted for native token", [])
throw Error("fetchVaultPrices: latestRoundData() reverted")
}
const nativePriceUSD = tokenAmountToDecimal(nativePriceUSDRes.value.getAnswer(), PRICE_FEED_DECIMALS)
log.debug("updateUserPosition: nativePriceUSD: {}", [nativePriceUSD.toString()])
log.debug("fetchVaultPrices: nativePriceUSD: {}", [nativePriceUSD.toString()])

return new VaultPrices(token0PriceInNative, token1PriceInNative, nativePriceUSD)
}
Expand Down
5 changes: 3 additions & 2 deletions src/vault-interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ function updateUserPosition(
if (isNewPosition) protocol.activeInvestorCount += 1
protocol.save()
for (let i = 0; i < periods.length; i++) {
const period = periods[i]
log.debug("updateUserPosition: updating protocol snapshot for vault {} and period {}", [
vault.id.toHexString(),
periods[i].toString(),
period.toString(),
])
const protocolSnapshot = getBeefyCLProtocolSnapshot(event.block.timestamp, periods[i])
const protocolSnapshot = getBeefyCLProtocolSnapshot(event.block.timestamp, period)
protocolSnapshot.totalValueLockedUSD = protocol.totalValueLockedUSD
if (newInvestor) protocolSnapshot.newInvestorCount += 1
if (investor.lastInteractionAt.lt(protocolSnapshot.roundedTimestamp))
Expand Down

0 comments on commit c585876

Please sign in to comment.