Skip to content

Commit

Permalink
Reuse Asset entity for IBT
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanchambras committed May 24, 2024
1 parent 207bed4 commit 7a1ede6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 41 deletions.
24 changes: 6 additions & 18 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,6 @@ type Factory @entity {
deployedLPVaults: [LPVault!]! @derivedFrom(field: "factory")
}

####################
# IBT and Rates
####################

type IBT @entity {
id: ID!
chainId: Int!
address: Bytes!
createdAtTimestamp: BigInt!
ibtDetails: Asset!
"IBT to underlying asset as a regular number with no decimals"
lastIBTRate: BigDecimal!
"IBT to underlying asset as returned by convertToAssets(UNIT). The number is in underlying asset decimals"
convertToAssetsUnit: BigInt!
lastUpdateTimestamp: BigInt!
}

####################
# Assets and prices
####################
Expand Down Expand Up @@ -160,6 +143,12 @@ type Asset @entity {
ibt: Asset
fytTokenDetails: FYTTokenDetails
lpTokenDetails: LPTokenDetails
"in case the AssetType is IBT we give the IBT Rates"
lastIBTRate: BigDecimal
"IBT to underlying asset as returned by convertToAssets(UNIT). The number is in underlying asset decimals"
convertToAssetsUnit: BigInt
lastUpdateTimestamp: BigInt

}

"AssetPrice entity to assign a price of a token to an Asset entity and its price source"
Expand Down Expand Up @@ -246,7 +235,6 @@ type Future @entity {
state: FutureState!
underlyingAsset: Asset!
ibtAsset: Asset!
ibt: IBT!

pools: [Pool!]! @derivedFrom(field: "futureVault")

Expand Down
21 changes: 8 additions & 13 deletions src/entities/IBT.ts → src/entities/IBTAsset.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { Address, BigInt, Bytes } from "@graphprotocol/graph-ts"

import { IBT } from "../../generated/schema"
import { Asset } from "../../generated/schema"
import { AssetType } from "../utils"
import { generateIBTId } from "../utils/idGenerators"
import { getAsset } from "./Asset"
import { getERC20Decimals } from "./ERC20"
import { getERC4626Asset, getIBTRate, getUnderlyingUnit } from "./ERC4626"
import { getNetwork } from "./Network"

export function getIBT(ibtAddress: Bytes, timestamp: BigInt): IBT {
const ibtID = generateIBTId(ibtAddress.toHex())
let ibt = IBT.load(ibtID)
export function getIBTAsset(ibtAddress: Bytes, timestamp: BigInt): Asset {
let ibt = Asset.load(ibtAddress.toString())
if (ibt) {
return ibt
}
ibt = createIBT(ibtAddress, timestamp)
ibt = createIBTAsset(ibtAddress, timestamp)
return ibt
}

export function updateIBTRates(ibtAddress: Bytes, timestamp: BigInt): void {
let ibt = getIBT(ibtAddress, timestamp)
let ibt = getIBTAsset(ibtAddress, timestamp)
let convertToAssets = getIBTRate(Address.fromBytes(ibtAddress))
ibt.convertToAssetsUnit = convertToAssets
const UNDERLYING_UNIT = getUnderlyingUnit(Address.fromBytes(ibtAddress))
Expand All @@ -28,21 +26,18 @@ export function updateIBTRates(ibtAddress: Bytes, timestamp: BigInt): void {
ibt.save()
}

export function createIBT(ibtAddress: Bytes, timestamp: BigInt): IBT {
const ibtID = generateIBTId(ibtAddress.toHex())
let ibt = new IBT(ibtID)
export function createIBTAsset(ibtAddress: Bytes, timestamp: BigInt): Asset {
let ibt = getAsset(ibtAddress.toHex(), timestamp, AssetType.IBT)
ibt.chainId = getNetwork().chainId
ibt.address = ibtAddress
ibt.createdAtTimestamp = timestamp
let ibtDetails = getAsset(ibtAddress.toHex(), timestamp, AssetType.IBT)
let convertToAssets = getIBTRate(Address.fromBytes(ibtAddress))
ibt.convertToAssetsUnit = convertToAssets
const underlying = getERC4626Asset(Address.fromBytes(ibtAddress))
const underlying_decimals = getERC20Decimals(underlying)
const UNDERLYING_UNIT = BigInt.fromI32(10).pow(underlying_decimals as u8)
ibt.ibtDetails = ibtDetails.id
ibt.lastIBTRate = convertToAssets.divDecimal(UNDERLYING_UNIT.toBigDecimal())
ibt.lastUpdateTimestamp = timestamp
ibt.save()
return ibt as IBT
return ibt as Asset
}
12 changes: 5 additions & 7 deletions src/mappings/futures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, ethereum, log } from "@graphprotocol/graph-ts"
import { Address, Bytes, ethereum, log } from "@graphprotocol/graph-ts"

import {
CurveFactoryChange, // CurveFactoryChange,
Expand Down Expand Up @@ -48,7 +48,7 @@ import {
getTotalAssets,
getYT,
} from "../entities/FutureVault"
import { getIBT as getIBTEntity } from "../entities/IBT"
import { getIBTAsset } from "../entities/IBTAsset"
import { getNetwork } from "../entities/Network"
import { createPool } from "../entities/Pool"
import { createTransaction } from "../entities/Transaction"
Expand Down Expand Up @@ -106,17 +106,15 @@ export function handlePTDeployed(event: PTDeployed): void {
underlyingAsset.save()

let ibtAddress = getIBT(ptAddress)
let ibtAsset = getAsset(
ibtAddress.toHex(),
event.block.timestamp,
AssetType.IBT
let ibtAsset = getIBTAsset(
Bytes.fromHexString(ibtAddress.toHex()),
event.block.timestamp
)
ibtAsset.underlying = underlyingAsset.id
ibtAsset.save()

newFuture.underlyingAsset = underlyingAddress.toHex()
newFuture.ibtAsset = ibtAddress.toHex()
newFuture.ibt = getIBTEntity(ibtAddress, event.block.timestamp).id
newFuture.yieldGenerators = []

newFuture.save()
Expand Down
2 changes: 1 addition & 1 deletion src/mappings/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
updateAccountAssetYTBalance,
} from "../entities/AccountAsset"
import { getAssetAmount } from "../entities/AssetAmount"
import { updateIBTRates } from "../entities/IBT"
import { updateIBTRates } from "../entities/IBTAsset"
import { updateYieldForAll } from "../entities/Yield"
import { AssetType, logWarning } from "../utils"
import { generateTransferId } from "../utils/idGenerators"
Expand Down
2 changes: 0 additions & 2 deletions src/utils/idGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,3 @@ export const generateYieldAssetId = (principalToken: string): string =>

export const generateClaimedYieldAssetId = (principalToken: string): string =>
`${principalToken}-claimed-yield`

export const generateIBTId = (ibtAddress: string): string => `${ibtAddress}-IBT`

0 comments on commit 7a1ede6

Please sign in to comment.