Skip to content

Commit

Permalink
Merge pull request #2 from ajna-finance/dynamic-data-sources
Browse files Browse the repository at this point in the history
use data source templates for pools
  • Loading branch information
EdNoepel authored Mar 10, 2023
2 parents 4b94fee + 85621f9 commit 96a8635
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 88 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"codegen": "graph codegen",
"build": "graph build",
"build-and-test-clean": "rm -rf generated && graph codegen && graph build && graph test",
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ ajna",
"create-local": "graph create --node http://localhost:8020/ ajna",
"remove-local": "graph remove --node http://localhost:8020/ ajna",
Expand Down
11 changes: 7 additions & 4 deletions src/erc-20-pool-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PoolCreated as PoolCreatedEvent } from "../generated/ERC20PoolFactory/ERC20PoolFactory"
import { ERC20Pool } from "../generated/ERC20Pool/ERC20Pool"
import { PoolCreated, Token } from "../generated/schema"
import { ERC20PoolFactory, Pool } from "../generated/schema"
import { ERC20Pool } from "../generated/templates"
import { ERC20Pool as ERC20PoolContract } from "../generated/templates/ERC20Pool/ERC20Pool"

import {
ERC20_FACTORY_ADDRESS,
Expand Down Expand Up @@ -42,7 +43,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void {
factory.txCount = factory.txCount.plus(ONE_BI)

// instantiate pool contract
const poolContract = ERC20Pool.bind(event.params.pool_)
const poolContract = ERC20PoolContract.bind(event.params.pool_)

// get pool initial interest rate
const interestRateResults = poolContract.interestRateInfo()
Expand Down Expand Up @@ -79,9 +80,11 @@ export function handlePoolCreated(event: PoolCreatedEvent): void {
quoteToken.poolCount = ONE_BI
}

// TODO: look into: https://thegraph.com/docs/en/developing/creating-a-subgraph/#data-source-templates-for-dynamically-created-contracts
// create entities
const pool = new Pool(event.params.pool_) as Pool // create pool entity
ERC20Pool.create(event.params.pool_) // create data source template

// record pool information
const pool = new Pool(event.params.pool_) as Pool
pool.createdAtTimestamp = event.block.timestamp
pool.createdAtBlockNumber = event.block.number
pool.collateralToken = collateralToken.id
Expand Down
8 changes: 4 additions & 4 deletions src/erc-20-pool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigDecimal, BigInt, Bytes, log } from "@graphprotocol/graph-ts"
import { Bytes } from "@graphprotocol/graph-ts"

import {
AddCollateral as AddCollateralEvent,
Expand All @@ -19,7 +19,7 @@ import {
Take as TakeEvent,
TransferLPTokens as TransferLPTokensEvent,
UpdateInterestRate as UpdateInterestRateEvent
} from "../generated/ERC20Pool/ERC20Pool"
} from "../generated/templates/ERC20Pool/ERC20Pool"
import {
AddCollateral,
AddQuoteToken,
Expand All @@ -44,13 +44,13 @@ import {
} from "../generated/schema"

import { ZERO_BD, ONE_BI } from "./utils/constants"
import { addressToBytes, wadToDecimal, rayToDecimal, bigDecimalExp18 } from "./utils/convert"
import { addressToBytes, wadToDecimal, rayToDecimal } from "./utils/convert"
import { loadOrCreateAccount, updateAccountLends, updateAccountLoans, updateAccountPools, updateAccountKicks, updateAccountTakes, updateAccountSettles, updateAccountReserveAuctions } from "./utils/account"
import { getBucketId, getBucketInfo, loadOrCreateBucket } from "./utils/bucket"
import { getLendId, loadOrCreateLend } from "./utils/lend"
import { getLoanId, loadOrCreateLoan } from "./utils/loan"
import { getBucketTakeIdFromBucketTakeLPAwarded, getLiquidationAuctionId, getAuctionInfoERC20Pool, loadOrCreateLiquidationAuction, updateLiquidationAuction } from "./utils/liquidation"
import { getBurnInfo, getCurrentBurnEpoch, getMomp, getPoolReservesInfo, updatePool, updatePoolLiquidationAuctions } from "./utils/pool"
import { getBurnInfo, getCurrentBurnEpoch, updatePool, updatePoolLiquidationAuctions } from "./utils/pool"
import { collateralizationAtLup, lpbValueInQuote, thresholdPrice } from "./utils/common"
import { getReserveAuctionId, loadOrCreateReserveAuctionProcess, reserveAuctionKickerReward } from "./utils/reserve-auction"
import { incrementTokenTxCount } from "./utils/token"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/bucket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, BigDecimal, BigInt, Bytes, dataSource, log } from "@graphprotocol/graph-ts"

import { Bucket } from "../../generated/schema"
import { PoolInfoUtils } from '../../generated/ERC20Pool/PoolInfoUtils'
import { PoolInfoUtils } from '../../generated/templates/ERC20Pool/PoolInfoUtils'

import { poolInfoUtilsNetworkLookUpTable, ONE_BD, ONE_BI, ONE_RAY_BD, ZERO_BD, ZERO_BI } from "./constants"
import { rayToDecimal, wadToDecimal } from "./convert"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, BigDecimal, BigInt, Bytes, dataSource, log } from "@graphprotocol/graph-ts"

import { Bucket, Lend } from "../../generated/schema"
import { PoolInfoUtils } from '../../generated/ERC20Pool/PoolInfoUtils'
import { PoolInfoUtils } from '../../generated/templates/ERC20Pool/PoolInfoUtils'

import { poolInfoUtilsNetworkLookUpTable } from "./constants"
import { bigDecimalRayToBigInt, wadToDecimal } from "./convert"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/liquidation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts"

import { LiquidationAuction, Kick, Loan, Pool } from "../../generated/schema"
import { ERC20Pool } from '../../generated/ERC20Pool/ERC20Pool'
import { ERC20Pool } from '../../generated/templates/ERC20Pool/ERC20Pool'

import { wadToDecimal } from "./convert"
import { ONE_BI, ZERO_BD } from "./constants"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/loan.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, BigDecimal, BigInt, Bytes, dataSource } from "@graphprotocol/graph-ts"
import { PoolInfoUtils } from "../../generated/ERC20Pool/PoolInfoUtils"
import { PoolInfoUtils } from "../../generated/templates/ERC20Pool/PoolInfoUtils"

import { Loan } from "../../generated/schema"
import { poolInfoUtilsNetworkLookUpTable, ZERO_BD, ZERO_BI } from "./constants"
Expand Down
4 changes: 2 additions & 2 deletions src/utils/pool.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BigDecimal, BigInt, Bytes, Address, dataSource } from '@graphprotocol/graph-ts'

import { PoolInfoUtils } from '../../generated/ERC20Pool/PoolInfoUtils'
import { LiquidationAuction, Pool } from "../../generated/schema"
import { ERC20Pool } from '../../generated/ERC20Pool/ERC20Pool'
import { ERC20Pool } from '../../generated/templates/ERC20Pool/ERC20Pool'
import { PoolInfoUtils } from '../../generated/templates/ERC20Pool/PoolInfoUtils'

import { poolInfoUtilsNetworkLookUpTable, ONE_BI } from "./constants"
import { wadToDecimal } from './convert'
Expand Down
146 changes: 73 additions & 73 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,6 @@ specVersion: 0.0.5
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum
name: ERC20Pool
network: goerli
source:
address: "0xbf332da94b818ac7972484997100c8cbb400b991"
abi: ERC20Pool
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- AddCollateral
- AddQuoteToken
- AuctionNFTSettle
- AuctionSettle
- BucketBankruptcy
- BucketTake
- BucketTakeLPAwarded
- DrawDebt
- Kick
- MoveQuoteToken
- RemoveCollateral
- RemoveQuoteToken
- RepayDebt
- ReserveAuction
- Settle
- Take
- TransferLPTokens
- UpdateInterestRate
abis:
- name: ERC20Pool
file: ./abis/ERC20Pool.json
- name: PoolInfoUtils
file: ./abis/PoolInfoUtils.json
eventHandlers:
- event: AddCollateral(indexed address,indexed uint256,uint256,uint256)
handler: handleAddCollateral
- event: AddQuoteToken(indexed address,indexed uint256,uint256,uint256,uint256)
handler: handleAddQuoteToken
- event: AuctionNFTSettle(indexed address,uint256,uint256,uint256)
handler: handleAuctionNFTSettle
- event: AuctionSettle(indexed address,uint256)
handler: handleAuctionSettle
- event: BucketBankruptcy(indexed uint256,uint256)
handler: handleBucketBankruptcy
- event: BucketTake(indexed address,uint256,uint256,uint256,uint256,bool)
handler: handleBucketTake
- event: BucketTakeLPAwarded(indexed address,indexed address,uint256,uint256)
handler: handleBucketTakeLPAwarded
- event: DrawDebt(indexed address,uint256,uint256,uint256)
handler: handleDrawDebt
- event: Kick(indexed address,uint256,uint256,uint256)
handler: handleKick
- event: MoveQuoteToken(indexed address,indexed uint256,indexed
uint256,uint256,uint256,uint256,uint256)
handler: handleMoveQuoteToken
- event: RemoveCollateral(indexed address,indexed uint256,uint256,uint256)
handler: handleRemoveCollateral
- event: RemoveQuoteToken(indexed address,indexed uint256,uint256,uint256,uint256)
handler: handleRemoveQuoteToken
- event: RepayDebt(indexed address,uint256,uint256,uint256)
handler: handleRepayDebt
- event: ReserveAuction(uint256,uint256)
handler: handleReserveAuction
- event: Settle(indexed address,uint256)
handler: handleSettle
- event: Take(indexed address,uint256,uint256,uint256,bool)
handler: handleTake
- event: TransferLPTokens(address,address,uint256[],uint256)
handler: handleTransferLPTokens
- event: UpdateInterestRate(uint256,uint256)
handler: handleUpdateInterestRate
file: ./src/erc-20-pool.ts
- kind: ethereum
name: PositionManager
network: goerli
Expand Down Expand Up @@ -186,3 +113,76 @@ dataSources:
- event: ERC721PoolFactoryPoolCreated(address)
handler: handleERC721PoolFactoryPoolCreated
file: ./src/erc-721-pool-factory.ts
templates:
- kind: ethereum
name: ERC20Pool
network: goerli
source:
abi: ERC20Pool
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- AddCollateral
- AddQuoteToken
- AuctionNFTSettle
- AuctionSettle
- BucketBankruptcy
- BucketTake
- BucketTakeLPAwarded
- DrawDebt
- Kick
- MoveQuoteToken
- RemoveCollateral
- RemoveQuoteToken
- RepayDebt
- ReserveAuction
- Settle
- Take
- TransferLPTokens
- UpdateInterestRate
abis:
- name: ERC20Pool
file: ./abis/ERC20Pool.json
- name: PoolInfoUtils
file: ./abis/PoolInfoUtils.json
eventHandlers:
- event: AddCollateral(indexed address,indexed uint256,uint256,uint256)
handler: handleAddCollateral
- event: AddQuoteToken(indexed address,indexed uint256,uint256,uint256,uint256)
handler: handleAddQuoteToken
- event: AuctionNFTSettle(indexed address,uint256,uint256,uint256)
handler: handleAuctionNFTSettle
- event: AuctionSettle(indexed address,uint256)
handler: handleAuctionSettle
- event: BucketBankruptcy(indexed uint256,uint256)
handler: handleBucketBankruptcy
- event: BucketTake(indexed address,uint256,uint256,uint256,uint256,bool)
handler: handleBucketTake
- event: BucketTakeLPAwarded(indexed address,indexed address,uint256,uint256)
handler: handleBucketTakeLPAwarded
- event: DrawDebt(indexed address,uint256,uint256,uint256)
handler: handleDrawDebt
- event: Kick(indexed address,uint256,uint256,uint256)
handler: handleKick
- event: MoveQuoteToken(indexed address,indexed uint256,indexed
uint256,uint256,uint256,uint256,uint256)
handler: handleMoveQuoteToken
- event: RemoveCollateral(indexed address,indexed uint256,uint256,uint256)
handler: handleRemoveCollateral
- event: RemoveQuoteToken(indexed address,indexed uint256,uint256,uint256,uint256)
handler: handleRemoveQuoteToken
- event: RepayDebt(indexed address,uint256,uint256,uint256)
handler: handleRepayDebt
- event: ReserveAuction(uint256,uint256)
handler: handleReserveAuction
- event: Settle(indexed address,uint256)
handler: handleSettle
- event: Take(indexed address,uint256,uint256,uint256,bool)
handler: handleTake
- event: TransferLPTokens(address,address,uint256[],uint256)
handler: handleTransferLPTokens
- event: UpdateInterestRate(uint256,uint256)
handler: handleUpdateInterestRate
file: ./src/erc-20-pool.ts
2 changes: 1 addition & 1 deletion tests/utils/erc-20-pool-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Take,
TransferLPTokens,
UpdateInterestRate
} from "../../generated/ERC20Pool/ERC20Pool"
} from "../../generated/templates/ERC20Pool/ERC20Pool"

export function createAddCollateralEvent(
pool: Address,
Expand Down

0 comments on commit 96a8635

Please sign in to comment.