Skip to content

Commit

Permalink
Expand base pool (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeHathaway authored Aug 22, 2023
1 parent 72ac2f0 commit 106d38b
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 232 deletions.
152 changes: 147 additions & 5 deletions src/mappings/base/base-pool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, BigInt, Bytes, ethereum, log } from "@graphprotocol/graph-ts"
import { Account, AddQuoteToken, Bucket, MoveQuoteToken, Pool, RemoveQuoteToken, Token, TransferLP, UpdateInterestRate } from "../../../generated/schema"
import { Account, AddQuoteToken, Bucket, Flashloan, LoanStamped, MoveQuoteToken, Pool, RemoveQuoteToken, ReserveAuctionKick, ReserveAuctionTake, Token, TransferLP, UpdateInterestRate } from "../../../generated/schema"
import {
AddQuoteToken as AddQuoteTokenERC20Event,
MoveQuoteToken as MoveQuoteTokenERC20Event,
Expand All @@ -13,16 +13,58 @@ import {
TransferLP as TransferLPERC721Event
} from "../../../generated/templates/ERC721Pool/ERC721Pool"

import { loadOrCreateAccount, updateAccountLends, updateAccountPools } from "../../utils/account"
import { ONE_BI, ZERO_BD } from "../../utils/constants"
import { loadOrCreateAccount, updateAccountLends, updateAccountPools, updateAccountReserveAuctions } from "../../utils/account"
import { ONE_BI, TEN_BI, ZERO_BD } from "../../utils/constants"
import { addressToBytes, bigIntArrayToIntArray, wadToDecimal } from "../../utils/convert"
import { getBucketId, getBucketInfo, loadOrCreateBucket, updateBucketLends } from "../../utils/pool/bucket"
import { getDepositTime, getLendId, loadOrCreateLend, lpbValueInQuote } from "../../utils/pool/lend"
import { getLenderInfo, isERC20Pool, updatePool } from "../../utils/pool/pool"
import { addReserveAuctionToPool, getBurnInfo, getLenderInfo, isERC20Pool, updatePool } from "../../utils/pool/pool"
import { incrementTokenTxCount as incrementTokenTxCountERC20Pool } from "../../utils/token-erc20"
import { incrementTokenTxCount as incrementTokenTxCountERC721Pool } from "../../utils/token-erc721"
import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "../../utils/pool/reserve-auction"


/*******************************/
/*** Borrower Event Handlers ***/
/*******************************/

export function _handleFlashLoan(event: ethereum.Event, tokenAddress: Address, borrower: Address, amount: BigInt): void {
const flashloan = new Flashloan(event.transaction.hash.concatI32(event.logIndex.toI32()))
const pool = Pool.load(addressToBytes(event.address))!
const token = Token.load(addressToBytes(tokenAddress))!
const scaleFactor = TEN_BI.pow(18 - token.decimals as u8)

flashloan.pool = pool.id
flashloan.borrower = borrower

const normalizedAmount = wadToDecimal(amount.times(scaleFactor))
flashloan.amount = normalizedAmount
if (token.id == pool.quoteToken) {
pool.quoteTokenFlashloaned = pool.quoteTokenFlashloaned.plus(normalizedAmount)
} else if (token.id == pool.collateralToken) {
pool.collateralFlashloaned = pool.collateralFlashloaned.plus(normalizedAmount)
}
token.txCount = token.txCount.plus(ONE_BI)
pool.txCount = pool.txCount.plus(ONE_BI)

token.save()
pool.save()
flashloan.save()
}

export function _handleLoanStamped(event: ethereum.Event, borrower: Address): void {
const entity = new LoanStamped(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
entity.borrower = borrower
entity.pool = addressToBytes(event.address)

entity.blockNumber = event.block.number
entity.blockTimestamp = event.block.timestamp
entity.transactionHash = event.transaction.hash
entity.save()
}

/*****************************/
/*** Lender Event Handlers ***/
/*****************************/
Expand Down Expand Up @@ -451,6 +493,106 @@ export function _handleTransferLP(erc20Event: TransferLPERC20Event | null, erc72
transferLP.save()
}

/*******************************/
/*** Reserves Event Handlers ***/
/*******************************/

export function _handleReserveAuctionKick(event: ethereum.Event, currentBurnEpoch: BigInt, claimableReservesRemaining: BigInt, auctionPrice: BigInt): void {
// create the ReserveAuctionKick entity (immutable) and ReserveAuction entity (mutable)
const reserveKick = new ReserveAuctionKick(
event.transaction.hash.concat(event.transaction.from)
)
const pool = Pool.load(addressToBytes(event.address))!
const reserveAuction = loadOrCreateReserveAuction(pool.id, currentBurnEpoch)

reserveKick.kicker = event.transaction.from
reserveKick.reserveAuction = reserveAuction.id
reserveKick.pool = pool.id
reserveKick.claimableReserves = wadToDecimal(claimableReservesRemaining)
reserveKick.startingPrice = wadToDecimal(auctionPrice)

reserveKick.blockNumber = event.block.number
reserveKick.blockTimestamp = event.block.timestamp
reserveKick.transactionHash = event.transaction.hash

reserveAuction.claimableReservesRemaining = reserveKick.claimableReserves
reserveAuction.kick = reserveKick.id

// update pool state
pool.burnEpoch = currentBurnEpoch
updatePool(pool)
addReserveAuctionToPool(pool, reserveAuction)
pool.txCount = pool.txCount.plus(ONE_BI)
reserveKick.kickerAward = reserveAuctionKickerReward(pool)

// update account state
const account = loadOrCreateAccount(addressToBytes(event.transaction.from))
account.txCount = account.txCount.plus(ONE_BI)
updateAccountReserveAuctions(account, reserveAuction.id)

account.save()
pool.save()
reserveAuction.save()
reserveKick.save()
}

export function _handleReserveAuctionTake(event: ethereum.Event, currentBurnEpoch: BigInt, claimableReservesRemaining: BigInt, auctionPrice: BigInt): void {
const reserveTake = new ReserveAuctionTake(
event.transaction.hash.concat(event.transaction.from)
)
const pool = Pool.load(addressToBytes(event.address))!
const reserveAuction = loadOrCreateReserveAuction(pool.id, currentBurnEpoch)

reserveTake.taker = event.transaction.from
reserveTake.reserveAuction = reserveAuction.id
reserveTake.pool = pool.id
reserveTake.claimableReservesRemaining = wadToDecimal(claimableReservesRemaining)
reserveTake.auctionPrice = wadToDecimal(auctionPrice)

// retrieve ajna burn information from the pool
const burnInfo = getBurnInfo(pool, currentBurnEpoch)
// update burn information of the reserve auction take
// since only one reserve auction can occur at a time, look at the difference since the last reserve auction
reserveTake.ajnaBurned = wadToDecimal(burnInfo.totalBurned).minus(pool.totalAjnaBurned)
reserveAuction.claimableReservesRemaining = reserveTake.claimableReservesRemaining
reserveAuction.lastTakePrice = reserveTake.auctionPrice
reserveAuction.ajnaBurned = reserveAuction.ajnaBurned.plus(reserveTake.ajnaBurned)
reserveAuction.takes = reserveAuction.takes.concat([reserveTake.id])

// event does not provide amount purchased; use auctionPrice and ajnaBurned to calculate
reserveTake.quotePurchased = reserveTake.ajnaBurned.div(reserveTake.auctionPrice)

reserveTake.blockNumber = event.block.number
reserveTake.blockTimestamp = event.block.timestamp
reserveTake.transactionHash = event.transaction.hash

// update pool state
pool.totalAjnaBurned = wadToDecimal(burnInfo.totalBurned)
pool.totalInterestEarned = wadToDecimal(burnInfo.totalInterest)
updatePool(pool)
pool.txCount = pool.txCount.plus(ONE_BI)
if (isERC20Pool(pool)) {
incrementTokenTxCountERC20Pool(pool)
} else {
incrementTokenTxCountERC721Pool(pool)
}

// update account state
const account = loadOrCreateAccount(addressToBytes(event.transaction.from))
account.txCount = account.txCount.plus(ONE_BI)
updateAccountReserveAuctions(account, reserveAuction.id)

// save entities to store
account.save()
pool.save()
reserveAuction.save()
reserveTake.save()
}

/***************************/
/*** Pool Event Handlers ***/
/***************************/

export function _handleInterestRateEvent(poolAddress: Address, event: ethereum.Event, newRate: BigInt): void {
const updateInterestRate = new UpdateInterestRate(
event.transaction.hash.concatI32(event.logIndex.toI32())
Expand Down Expand Up @@ -480,4 +622,4 @@ export function _handleInterestRateEvent(poolAddress: Address, event: ethereum.E
// save entities to the store
pool.save()
updateInterestRate.save()
}
}
118 changes: 5 additions & 113 deletions src/mappings/erc-20-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "../utils
import { incrementTokenTxCount } from "../utils/token-erc20"
import { approveTransferors, loadOrCreateTransferors, revokeTransferors } from "../utils/pool/lp-transferors"
import { loadOrCreateAllowances, increaseAllowances, decreaseAllowances, revokeAllowances } from "../utils/pool/lp-allowances"
import { _handleAddQuoteToken, _handleInterestRateEvent, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleTransferLP } from "./base/base-pool"
import { _handleAddQuoteToken, _handleFlashLoan, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleTransferLP } from "./base/base-pool"

export function handleAddCollateral(event: AddCollateralEvent): void {
const addCollateral = new AddCollateral(
Expand Down Expand Up @@ -446,27 +446,7 @@ export function handleDrawDebt(event: DrawDebtEvent): void {
}

export function handleFlashloan(event: FlashloanEvent): void {
const flashloan = new Flashloan(event.transaction.hash.concatI32(event.logIndex.toI32()))
const pool = Pool.load(addressToBytes(event.address))!
const token = Token.load(addressToBytes(event.params.token))!
const scaleFactor = TEN_BI.pow(18 - token.decimals as u8)

flashloan.pool = pool.id
flashloan.borrower = event.params.receiver

const normalizedAmount = wadToDecimal(event.params.amount.times(scaleFactor))
flashloan.amount = normalizedAmount
if (token.id == pool.quoteToken) {
pool.quoteTokenFlashloaned = pool.quoteTokenFlashloaned.plus(normalizedAmount)
} else if (token.id == pool.collateralToken) {
pool.collateralFlashloaned = pool.collateralFlashloaned.plus(normalizedAmount)
}
token.txCount = token.txCount.plus(ONE_BI)
pool.txCount = pool.txCount.plus(ONE_BI)

token.save()
pool.save()
flashloan.save()
_handleFlashLoan(event, event.params.token, event.params.receiver, event.params.amount)
}

export function handleIncreaseLPAllowance(event: IncreaseLPAllowanceEvent): void {
Expand Down Expand Up @@ -551,15 +531,7 @@ export function handleKick(event: KickEvent): void {
}

export function handleLoanStamped(event: LoanStampedEvent): void {
const entity = new LoanStamped(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
entity.borrower = event.params.borrower
entity.pool = addressToBytes(event.address)

entity.blockNumber = event.block.number
entity.blockTimestamp = event.block.timestamp
entity.transactionHash = event.transaction.hash
_handleLoanStamped(event, event.params.borrower)
}

export function handleMoveQuoteToken(event: MoveQuoteTokenEvent): void {
Expand Down Expand Up @@ -691,91 +663,11 @@ export function handleRepayDebt(event: RepayDebtEvent): void {
}

export function handleReserveAuctionKick(event: KickReserveAuctionEvent): void {
// create the ReserveAuctionKick entity (immutable) and ReserveAuction entity (mutable)
const reserveKick = new ReserveAuctionKick(
event.transaction.hash.concat(event.transaction.from)
)
const pool = Pool.load(addressToBytes(event.address))!
const reserveAuction = loadOrCreateReserveAuction(pool.id, event.params.currentBurnEpoch)

reserveKick.kicker = event.transaction.from
reserveKick.reserveAuction = reserveAuction.id
reserveKick.pool = pool.id
reserveKick.claimableReserves = wadToDecimal(event.params.claimableReservesRemaining)
reserveKick.startingPrice = wadToDecimal(event.params.auctionPrice)

reserveKick.blockNumber = event.block.number
reserveKick.blockTimestamp = event.block.timestamp
reserveKick.transactionHash = event.transaction.hash

reserveAuction.claimableReservesRemaining = reserveKick.claimableReserves
reserveAuction.kick = reserveKick.id

// update pool state
pool.burnEpoch = event.params.currentBurnEpoch
updatePool(pool)
addReserveAuctionToPool(pool, reserveAuction)
pool.txCount = pool.txCount.plus(ONE_BI)
reserveKick.kickerAward = reserveAuctionKickerReward(pool)

// update account state
const account = loadOrCreateAccount(addressToBytes(event.transaction.from))
account.txCount = account.txCount.plus(ONE_BI)
updateAccountReserveAuctions(account, reserveAuction.id)

account.save()
pool.save()
reserveAuction.save()
reserveKick.save()
_handleReserveAuctionKick(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice)
}

export function handleReserveAuctionTake(event: ReserveAuctionEvent): void {
const reserveTake = new ReserveAuctionTake(
event.transaction.hash.concat(event.transaction.from)
)
const pool = Pool.load(addressToBytes(event.address))!
const reserveAuction = loadOrCreateReserveAuction(pool.id, event.params.currentBurnEpoch)

reserveTake.taker = event.transaction.from
reserveTake.reserveAuction = reserveAuction.id
reserveTake.pool = pool.id
reserveTake.claimableReservesRemaining = wadToDecimal(event.params.claimableReservesRemaining)
reserveTake.auctionPrice = wadToDecimal(event.params.auctionPrice)

// retrieve ajna burn information from the pool
const burnInfo = getBurnInfo(pool, event.params.currentBurnEpoch)
// update burn information of the reserve auction take
// since only one reserve auction can occur at a time, look at the difference since the last reserve auction
reserveTake.ajnaBurned = wadToDecimal(burnInfo.totalBurned).minus(pool.totalAjnaBurned)
reserveAuction.claimableReservesRemaining = reserveTake.claimableReservesRemaining
reserveAuction.lastTakePrice = reserveTake.auctionPrice
reserveAuction.ajnaBurned = reserveAuction.ajnaBurned.plus(reserveTake.ajnaBurned)
reserveAuction.takes = reserveAuction.takes.concat([reserveTake.id])

// event does not provide amount purchased; use auctionPrice and ajnaBurned to calculate
reserveTake.quotePurchased = reserveTake.ajnaBurned.div(reserveTake.auctionPrice)

reserveTake.blockNumber = event.block.number
reserveTake.blockTimestamp = event.block.timestamp
reserveTake.transactionHash = event.transaction.hash

// update pool state
pool.totalAjnaBurned = wadToDecimal(burnInfo.totalBurned)
pool.totalInterestEarned = wadToDecimal(burnInfo.totalInterest)
updatePool(pool)
pool.txCount = pool.txCount.plus(ONE_BI)
incrementTokenTxCount(pool)

// update account state
const account = loadOrCreateAccount(addressToBytes(event.transaction.from))
account.txCount = account.txCount.plus(ONE_BI)
updateAccountReserveAuctions(account, reserveAuction.id)

// save entities to store
account.save()
pool.save()
reserveAuction.save()
reserveTake.save()
_handleReserveAuctionTake(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice)
}

export function handleResetInterestRate(event: ResetInterestRateEvent): void {
Expand Down
Loading

0 comments on commit 106d38b

Please sign in to comment.