Skip to content

Commit

Permalink
Merge branch 'master' into bean-ui-stalk-loss
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromguy authored Sep 4, 2023
2 parents 5727b3c + 67b1f2f commit 103e707
Show file tree
Hide file tree
Showing 66 changed files with 80,136 additions and 16,416 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
{
"name": "subgraph-wells",
"name": "subgraph-basin",
"license": "MIT",
"description": "A subgraph for indexing events emitted by Beanstalk.",
"description": "A subgraph for indexing events emitted by Basin.",
"repository": {
"type": "git",
"url": "https://github.com/BeanstalkFarms/Beanstalk.git",
"directory": "projects/subgraph-wells"
"directory": "projects/subgraph-basin"
},
"scripts": {
"codegen": "rm -rf ./generated && graph codegen",
"build": "yarn codegen && graph build",
"test": "graph test",
"deploy": "yarn codegen && graph deploy --node https://api.studio.thegraph.com/deploy/ beanstalk-wells",
"create-local": "graph create --node http://127.0.0.1:8020/ beanstalk-wells",
"deploy-local": "yarn codegen && graph deploy --node http://127.0.0.1:8020/ --ipfs http://127.0.0.1:5001 beanstalk-wells",
"create-hosted": "graph create --node http://graph.node.bean.money:8020/ beanstalk-wells",
"remove-hosted": "graph remove --node http://graph.node.bean.money:8020/ beanstalk-wells",
"deploy-hosted": "yarn codegen && graph deploy --node http://graph.node.bean.money:8020/ --ipfs http://graph.node.bean.money:5001 beanstalk-wells",
"create-hosted-staging": "graph create --node http://graph.node.bean.money:8020/ beanstalk-wells-staging",
"remove-hosted-staging": "graph remove --node http://graph.node.bean.money:8020/ beanstalk-wells-staging",
"deploy-hosted-staging": "yarn codegen && graph deploy --node http://graph.node.bean.money:8020/ --ipfs http://graph.node.bean.money:5001 beanstalk-wells-staging",
"create-hosted-dev": "graph create --node http://graph.node.bean.money:8020/ beanstalk-wells-dev",
"remove-hosted-dev": "graph remove --node http://graph.node.bean.money:8020/ beanstalk-wells-dev",
"deploy-hosted-dev": "yarn codegen && graph deploy --node http://graph.node.bean.money:8020/ --ipfs http://graph.node.bean.money:5001 beanstalk-wells-dev"
"deploy": "yarn codegen && graph deploy --node https://api.studio.thegraph.com/deploy/ basin",
"create-local": "graph create --node http://127.0.0.1:8020/ basin",
"deploy-local": "yarn codegen && graph deploy --node http://127.0.0.1:8020/ --ipfs http://127.0.0.1:5001 basin",
"create-hosted": "graph create --node http://graph.node.bean.money:8020/ basin",
"remove-hosted": "graph remove --node http://graph.node.bean.money:8020/ basin",
"deploy-hosted": "yarn codegen && graph deploy --node http://graph.node.bean.money:8020/ --ipfs http://graph.node.bean.money:5001 basin",
"create-hosted-staging": "graph create --node http://graph.node.bean.money:8020/ basin-staging",
"remove-hosted-staging": "graph remove --node http://graph.node.bean.money:8020/ basin-staging",
"deploy-hosted-staging": "yarn codegen && graph deploy --node http://graph.node.bean.money:8020/ --ipfs http://graph.node.bean.money:5001 basin-staging",
"create-hosted-dev": "graph create --node http://graph.node.bean.money:8020/ basin-dev",
"remove-hosted-dev": "graph remove --node http://graph.node.bean.money:8020/ basin-dev",
"deploy-hosted-dev": "yarn codegen && graph deploy --node http://graph.node.bean.money:8020/ --ipfs http://graph.node.bean.money:5001 basin-dev"
},
"dependencies": {
"@graphprotocol/graph-cli": "^0.30.4",
"@graphprotocol/graph-ts": "^0.27.0",
"@graphprotocol/graph-cli": "0.56.0",
"@graphprotocol/graph-ts": "0.31.0",
"matchstick-as": "^0.5.0"
},
"private": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
# Version: 3.0.3
# See https://github.com/messari/subgraphs/blob/master/docs/Schema.md for details

enum LiquidityEvent {
# Additions
ADD_LIQUIDITY
SYNC
# Removals
REMOVE_LIQUIDITY
REMOVE_LIQUIDITY_ONE_TOKEN
}

enum SwapEvent {
SWAP
SHIFT
}

type Token @entity {
" Smart contract address of the token "
id: Bytes!
Expand Down Expand Up @@ -137,7 +151,7 @@ type Well @entity {
" Liquidity well hourly snapshots "
hourlySnapshots: [WellHourlySnapshot!]! @derivedFrom(field: "well")

# ##### Events #####
##### Events #####

" All deposit (add liquidity) events occurred in this well "
deposits: [Deposit!]! @derivedFrom(field: "well")
Expand Down Expand Up @@ -296,7 +310,7 @@ type Account @entity {

##### EVENT LEVEL ENTITIES #####

" This maps to the AddLiquidity event "
" This maps to the AddLiquidity and Sync events "
type Deposit @entity(immutable: true) {
" { Transaction hash }-{ Log index } "
id: ID!
Expand All @@ -319,6 +333,9 @@ type Deposit @entity(immutable: true) {
" Gas price of the transaction that emitted this event "
gasPrice: BigInt

" Event type that was emitted "
eventType: LiquidityEvent!

" Account that emitted this event "
account: Account!

Expand Down Expand Up @@ -367,6 +384,9 @@ type Withdraw @entity(immutable: true) {
" Gas price of the transaction that emitted this event "
gasPrice: BigInt

" Event type that was emitted "
eventType: LiquidityEvent!

" Account that emitted this event "
account: Account!

Expand Down Expand Up @@ -414,6 +434,9 @@ type Swap @entity(immutable: true) {
" Gas price for the transaction "
gasPrice: BigInt

" Event type that was emitted "
eventType: SwapEvent!

" Address that sent the transaction "
account: Account!

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { AddLiquidity, Approval, RemoveLiquidity, RemoveLiquidityOneToken, Swap, Transfer } from "../generated/templates/Well/Well";
import {
AddLiquidity,
Approval,
RemoveLiquidity,
RemoveLiquidityOneToken,
Shift,
Swap,
Sync,
Transfer
} from "../generated/templates/Well/Well";
import { loadOrCreateAccount } from "./utils/Account";
import { emptyBigIntArray, ZERO_BI } from "../../subgraph-core/utils/Decimals";
import { recordAddLiquidityEvent, recordRemoveLiquidityEvent, recordRemoveLiquidityOneEvent } from "./utils/Liquidity";
import { recordSwapEvent } from "./utils/Swap";
import { deltaBigIntArray, emptyBigIntArray, ZERO_BI } from "../../subgraph-core/utils/Decimals";
import { recordAddLiquidityEvent, recordRemoveLiquidityEvent, recordRemoveLiquidityOneEvent, recordSyncEvent } from "./utils/Liquidity";
import { recordSwapEvent, recordShiftEvent } from "./utils/Swap";
import {
checkForSnapshot,
incrementWellDeposit,
Expand All @@ -14,6 +23,7 @@ import {
updateWellTokenUSDPrices,
updateWellVolumes
} from "./utils/Well";
import { Address } from "@graphprotocol/graph-ts";

export function handleAddLiquidity(event: AddLiquidity): void {
loadOrCreateAccount(event.transaction.from);
Expand Down Expand Up @@ -57,6 +67,8 @@ export function handleRemoveLiquidityOneToken(event: RemoveLiquidityOneToken): v
// Pre-process amount out into an indexed array for the well's input tokens.

let well = loadWell(event.address);
let fromTokenIndex = well.tokens.indexOf(event.params.tokenOut) == 0 ? 1 : 0;

let indexedBalances = emptyBigIntArray(well.tokens.length);

indexedBalances[well.tokens.indexOf(event.params.tokenOut)] = indexedBalances[well.tokens.indexOf(event.params.tokenOut)].plus(
Expand All @@ -72,6 +84,16 @@ export function handleRemoveLiquidityOneToken(event: RemoveLiquidityOneToken): v

checkForSnapshot(event.address, event.block.timestamp, event.block.number);

updateWellVolumes(
event.address,
Address.fromBytes(well.tokens[fromTokenIndex]),
indexedBalances[fromTokenIndex],
event.params.tokenOut,
event.params.tokenAmountOut,
event.block.timestamp,
event.block.number
);

updateWellTokenBalances(event.address, indexedBalances, event.block.timestamp, event.block.number);

updateWellLiquidityTokenBalance(event.address, ZERO_BI.minus(event.params.lpAmountIn), event.block.timestamp, event.block.number);
Expand Down Expand Up @@ -103,4 +125,59 @@ export function handleSwap(event: Swap): void {
recordSwapEvent(event);
}

export function handleTransfer(event: Transfer): void {}
export function handleShift(event: Shift): void {
loadOrCreateAccount(event.transaction.from);
checkForSnapshot(event.address, event.block.timestamp, event.block.number);

// Since the token in was already transferred before this event was emitted, we need to find the difference to record as the amountIn
let well = loadWell(event.address);

let fromTokenIndex = well.tokens.indexOf(event.params.toToken) == 0 ? 1 : 0;
let fromToken = Address.fromBytes(well.tokens[fromTokenIndex]);

// Subtract starting reserves from the updated amounts emitted by the event.
let deltaReserves = deltaBigIntArray(event.params.reserves, well.reserves);
let amountIn = deltaReserves[fromTokenIndex];

updateWellVolumes(
event.address,
fromToken,
amountIn,
event.params.toToken,
event.params.amountOut,
event.block.timestamp,
event.block.number
);

updateWellTokenUSDPrices(event.address, event.block.number);

incrementWellSwap(event.address);

recordShiftEvent(event, fromToken, amountIn);
}

export function handleSync(event: Sync): void {
loadOrCreateAccount(event.transaction.from);

checkForSnapshot(event.address, event.block.timestamp, event.block.number);

// Since the token(s) in were already transferred before this event was emitted, we need to find the difference to record as the amountIn
let well = loadWell(event.address);

// Subtract starting reserves from the updated amounts emitted by the event.
let deltaReserves = deltaBigIntArray(event.params.reserves, well.reserves);

updateWellTokenBalances(event.address, deltaReserves, event.block.timestamp, event.block.number);

updateWellLiquidityTokenBalance(event.address, event.params.lpAmountOut, event.block.timestamp, event.block.number);

updateWellTokenUSDPrices(event.address, event.block.number);

incrementWellDeposit(event.address);

recordSyncEvent(event, deltaReserves);
}

export function handleTransfer(event: Transfer): void {
// Placeholder for possible future liquidity holdings data
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Address } from "@graphprotocol/graph-ts";
import { BoreWell } from "../../generated/Aquifer/Aquifer";
import { ERC20 } from "../../generated/Aquifer/ERC20";
import { Well } from "../../generated/templates";
Expand All @@ -7,6 +8,10 @@ import { loadOrCreateToken } from "../utils/Token";
import { createWell, loadOrCreateWellFunction } from "../utils/Well";

export function handleBoreWell(event: BoreWell): void {
if (event.params.well == Address.fromString("0x875b1da8dcba757398db2bc35043a72b4b62195d")) {
// Ignore well with incorrect price function
return;
}
let aquifer = loadOrCreateAquifer(event.address);

Well.create(event.params.well);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigInt } from "@graphprotocol/graph-ts";
import { Deposit, Withdraw } from "../../generated/schema";
import { AddLiquidity, RemoveLiquidity, RemoveLiquidityOneToken } from "../../generated/templates/Well/Well";
import { AddLiquidity, RemoveLiquidity, RemoveLiquidityOneToken, Sync } from "../../generated/templates/Well/Well";
import { getBigDecimalArrayTotal } from "../../../subgraph-core/utils/Decimals";
import { getCalculatedReserveUSDValues, loadWell } from "./Well";

Expand All @@ -18,6 +18,7 @@ export function recordAddLiquidityEvent(event: AddLiquidity): void {
deposit.gasUsed = receipt.gasUsed;
}
deposit.gasPrice = event.transaction.gasPrice;
deposit.eventType = "ADD_LIQUIDITY";
deposit.account = event.transaction.from;
deposit.well = event.address;
deposit.blockNumber = event.block.number;
Expand All @@ -43,6 +44,7 @@ export function recordRemoveLiquidityEvent(event: RemoveLiquidity): void {
withdraw.gasUsed = receipt.gasUsed;
}
withdraw.gasPrice = event.transaction.gasPrice;
withdraw.eventType = "REMOVE_LIQUIDITY";
withdraw.account = event.transaction.from;
withdraw.well = event.address;
withdraw.blockNumber = event.block.number;
Expand All @@ -68,6 +70,7 @@ export function recordRemoveLiquidityOneEvent(event: RemoveLiquidityOneToken, to
withdraw.gasUsed = receipt.gasUsed;
}
withdraw.gasPrice = event.transaction.gasPrice;
withdraw.eventType = "REMOVE_LIQUIDITY_ONE_TOKEN";
withdraw.account = event.transaction.from;
withdraw.well = event.address;
withdraw.blockNumber = event.block.number;
Expand All @@ -78,3 +81,29 @@ export function recordRemoveLiquidityOneEvent(event: RemoveLiquidityOneToken, to
withdraw.amountUSD = getBigDecimalArrayTotal(getCalculatedReserveUSDValues(well.tokens, tokenAmounts));
withdraw.save();
}

export function recordSyncEvent(event: Sync, deltaReserves: BigInt[]): void {
let id = event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
let deposit = new Deposit(id);
let receipt = event.receipt;
let well = loadWell(event.address);

deposit.hash = event.transaction.hash;
deposit.nonce = event.transaction.nonce;
deposit.logIndex = event.logIndex.toI32();
deposit.gasLimit = event.transaction.gasLimit;
if (receipt !== null) {
deposit.gasUsed = receipt.gasUsed;
}
deposit.gasPrice = event.transaction.gasPrice;
deposit.eventType = "SYNC";
deposit.account = event.transaction.from;
deposit.well = event.address;
deposit.blockNumber = event.block.number;
deposit.timestamp = event.block.timestamp;
deposit.liquidity = event.params.lpAmountOut;
deposit.tokens = well.tokens;
deposit.reserves = deltaReserves;
deposit.amountUSD = getBigDecimalArrayTotal(getCalculatedReserveUSDValues(well.tokens, deltaReserves));
deposit.save();
}
File renamed without changes.
51 changes: 51 additions & 0 deletions projects/subgraph-basin/src/utils/Swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Shift, Swap } from "../../generated/templates/Well/Well";
import { Swap as SwapEvent } from "../../generated/schema";
import { Address, BigInt } from "@graphprotocol/graph-ts";

export function recordSwapEvent(event: Swap): void {
let swap = new SwapEvent(event.transaction.hash.toHexString() + "-" + event.logIndex.toString());
let receipt = event.receipt;

swap.hash = event.transaction.hash;
swap.nonce = event.transaction.nonce;
swap.logIndex = event.logIndex.toI32();
swap.gasLimit = event.transaction.gasLimit;
if (receipt !== null) {
swap.gasUsed = receipt.gasUsed;
}
swap.gasPrice = event.transaction.gasPrice;
swap.eventType = "SWAP";
swap.account = event.transaction.from;
swap.well = event.address;
swap.blockNumber = event.block.number;
swap.timestamp = event.block.timestamp;
swap.fromToken = event.params.fromToken;
swap.amountIn = event.params.amountIn;
swap.toToken = event.params.toToken;
swap.amountOut = event.params.amountOut;
swap.save();
}

export function recordShiftEvent(event: Shift, fromToken: Address, amountIn: BigInt): void {
let swap = new SwapEvent(event.transaction.hash.toHexString() + "-" + event.logIndex.toString());
let receipt = event.receipt;

swap.hash = event.transaction.hash;
swap.nonce = event.transaction.nonce;
swap.logIndex = event.logIndex.toI32();
swap.gasLimit = event.transaction.gasLimit;
if (receipt !== null) {
swap.gasUsed = receipt.gasUsed;
}
swap.gasPrice = event.transaction.gasPrice;
swap.eventType = "SHIFT";
swap.account = event.transaction.from;
swap.well = event.address;
swap.blockNumber = event.block.number;
swap.timestamp = event.block.timestamp;
swap.fromToken = fromToken;
swap.amountIn = amountIn;
swap.toToken = event.params.toToken;
swap.amountOut = event.params.amountOut;
swap.save();
}
Loading

0 comments on commit 103e707

Please sign in to comment.