forked from traderjoe-xyz/joe-subgraphs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* setup stablejoe subgraph, missing dayData atm * update stableJoeDayData on events, track deposit fees collected * make sure to create stable joe, also save users after making updates * add joeUnstaked and joeUnstakedUSD for day data, fix some logic * add USDC_ADDRESS * update contract address, redeploy * update abi * cleanup user logic * fix yaml file * update joe-core and abi * fix templates * config lowercase * set api version to 0.0.4 * Fix stablejoe day data errors * run prettier * use debug log instead of warning * Use stablejoe contract address in day data identifiers Co-authored-by: robotmurloc-3599 <[email protected]> Co-authored-by: 0xmaxime <[email protected]>
- Loading branch information
1 parent
0732ec6
commit 8b1f332
Showing
15 changed files
with
390 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"network": "rinkeby", | ||
"address": "0x12ba94d04a7a7f0882155b2c55b88e269428227b", | ||
"startBlock": "10219081" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "StableJoeStaking", | ||
"license": "UNLICENSED", | ||
"scripts": { | ||
"prepare:avax": "mustache config/avax.json stablejoe.template.yaml > stablejoe.avax.yaml", | ||
"prepare:rinkeby": "mustache config/rinkeby.json stablejoe.template.yaml > stablejoe.rinkeby.yaml", | ||
"codegen:avax": "graph codegen stablejoe.avax.yaml", | ||
"codegen:rinkeby": "graph codegen stablejoe.rinkeby.yaml", | ||
"build:avax": "graph build stablejoe.avax.yaml", | ||
"build:rinkeby": "graph build stablejoe.rinkeby.yaml", | ||
"deploy:avax": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ traderjoe-xyz/stablejoe stablejoe.avax.yaml", | ||
"deploy:rinkeby": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ traderjoe-xyz/sjoe-rinkeby stablejoe.rinkeby.yaml", | ||
"create-local": "graph create --node http://localhost:8020/ traderjoe-xyz/stablejoe --access-token ", | ||
"remove-local": "graph remove --node http://localhost:8020/ traderjoe-xyz/stablejoe --access-token ", | ||
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 --access-token traderjoe-xyz/stablejoe stablejoe.fuji.yaml" | ||
}, | ||
"dependencies": { | ||
"@graphprotocol/graph-cli": "0.26.0", | ||
"@graphprotocol/graph-ts": "0.24.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { StableJoeDayData } from '../../generated/schema' | ||
import { BIG_DECIMAL_ZERO } from 'const' | ||
import { ethereum, BigInt, Address } from '@graphprotocol/graph-ts' | ||
|
||
export function getStableJoeDayData(eventAddress: Address, block: ethereum.Block): StableJoeDayData { | ||
const SECONDS_PER_DAY = 86400 | ||
const day = block.timestamp.toI32() / SECONDS_PER_DAY | ||
const id = eventAddress.toHex().concat('-').concat(BigInt.fromI32(day).toString()) | ||
|
||
let dayData = StableJoeDayData.load(id) | ||
|
||
if (dayData === null) { | ||
dayData = new StableJoeDayData(id) | ||
dayData.date = day | ||
dayData.joeStaked = BIG_DECIMAL_ZERO | ||
dayData.joeStakedUSD = BIG_DECIMAL_ZERO | ||
dayData.joeUnstaked = BIG_DECIMAL_ZERO | ||
dayData.joeUnstakedUSD = BIG_DECIMAL_ZERO | ||
dayData.usdHarvested = BIG_DECIMAL_ZERO | ||
dayData.depositFeeJOE = BIG_DECIMAL_ZERO | ||
dayData.depositFeeUSD = BIG_DECIMAL_ZERO | ||
} | ||
|
||
dayData.save() | ||
return dayData as StableJoeDayData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './daydata' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { Address, BigDecimal, BigInt, dataSource, ethereum, log } from '@graphprotocol/graph-ts' | ||
import { | ||
BIG_DECIMAL_1E18, | ||
BIG_DECIMAL_1E6, | ||
BIG_DECIMAL_ZERO, | ||
JOE_USDT_PAIR_ADDRESS, | ||
USDC_ADDRESS, | ||
USDC_E_ADDRESS, | ||
} from 'const' | ||
import { | ||
ClaimReward as ClaimRewardEvent, | ||
Deposit as DepositEvent, | ||
DepositFeeChanged as DepositFeeChangedEvent, | ||
Withdraw as WithdrawEvent, | ||
} from '../../generated/StableJoeStaking/StableJoeStaking' | ||
import { StableJoe, User } from '../../generated/schema' | ||
import { Pair as PairContract } from '../../generated/StableJoeStaking/Pair' | ||
import { getStableJoeDayData } from '../entities' | ||
|
||
function getJoePrice(): BigDecimal { | ||
const pair = PairContract.bind(JOE_USDT_PAIR_ADDRESS) | ||
const reservesResult = pair.try_getReserves() | ||
if (reservesResult.reverted) { | ||
log.info('[getJoePrice] getReserves reverted', []) | ||
return BIG_DECIMAL_ZERO | ||
} | ||
const reserves = reservesResult.value | ||
if (reserves.value0.toBigDecimal().equals(BigDecimal.fromString('0'))) { | ||
log.error('[getJoePrice] USDT reserve 0', []) | ||
return BIG_DECIMAL_ZERO | ||
} | ||
return reserves.value1.toBigDecimal().times(BIG_DECIMAL_1E18).div(reserves.value0.toBigDecimal()).div(BIG_DECIMAL_1E6) | ||
} | ||
|
||
export function convertAmountToDecimal(tokenAmount: BigInt): BigDecimal { | ||
return tokenAmount.toBigDecimal().div(exponentToBigDecimal(BigInt.fromString('18'))) | ||
} | ||
|
||
export function exponentToBigDecimal(decimals: BigInt): BigDecimal { | ||
let bd = BigDecimal.fromString('1') | ||
for (let i = BigInt.fromI32(0); i.lt(decimals as BigInt); i = i.plus(BigInt.fromI32(1))) { | ||
bd = bd.times(BigDecimal.fromString('10')) | ||
} | ||
return bd | ||
} | ||
|
||
function createStableJoe(block: ethereum.Block): StableJoe { | ||
log.debug('[createStableJoe] from {}', [dataSource.address().toHex()]) | ||
|
||
const stableJoe = new StableJoe(dataSource.address().toHex()) | ||
stableJoe.joeStaked = BIG_DECIMAL_ZERO | ||
stableJoe.joeStakedUSD = BIG_DECIMAL_ZERO | ||
stableJoe.usdHarvested = BIG_DECIMAL_ZERO | ||
stableJoe.depositFee = BIG_DECIMAL_ZERO | ||
stableJoe.depositFeeJOE = BIG_DECIMAL_ZERO | ||
stableJoe.depositFeeUSD = BIG_DECIMAL_ZERO | ||
stableJoe.updatedAt = block.timestamp | ||
stableJoe.save() | ||
|
||
return stableJoe as StableJoe | ||
} | ||
|
||
function getStableJoe(block: ethereum.Block): StableJoe { | ||
log.debug('[getStableJoe] from {}', [dataSource.address().toHex()]) | ||
|
||
let stableJoe = StableJoe.load(dataSource.address().toHex()) | ||
|
||
if (stableJoe === null) { | ||
stableJoe = createStableJoe(block) | ||
} | ||
|
||
return stableJoe as StableJoe | ||
} | ||
|
||
function createUser(address: Address, block: ethereum.Block): User { | ||
const user = new User(address.toHex()) | ||
|
||
user.stableJoe = dataSource.address().toHex() | ||
user.joeStaked = BIG_DECIMAL_ZERO | ||
user.joeStakedUSD = BIG_DECIMAL_ZERO | ||
user.usdHarvested = BIG_DECIMAL_ZERO | ||
user.updatedAt = block.timestamp | ||
|
||
return user as User | ||
} | ||
|
||
function getUser(address: Address, block: ethereum.Block): User { | ||
let user = User.load(address.toHex()) | ||
|
||
if (user === null) { | ||
user = createUser(address, block) | ||
} | ||
|
||
return user as User | ||
} | ||
|
||
export function handleClaimReward(event: ClaimRewardEvent): void { | ||
const stableJoe = getStableJoe(event.block) | ||
const user = getUser(event.params.user, event.block) | ||
|
||
user.usdHarvested = user.usdHarvested.plus(convertAmountToDecimal(event.params.amount)) | ||
user.updatedAt = event.block.timestamp | ||
user.save() | ||
|
||
stableJoe.usdHarvested = stableJoe.usdHarvested.plus(convertAmountToDecimal(event.params.amount)) | ||
stableJoe.updatedAt = event.block.timestamp | ||
stableJoe.save() | ||
|
||
// update day data | ||
if (USDC_ADDRESS === event.params.rewardToken || USDC_E_ADDRESS === event.params.rewardToken) { | ||
let stableJoeDayData = getStableJoeDayData(event.address, event.block) | ||
stableJoeDayData.usdHarvested = stableJoeDayData.usdHarvested.plus(convertAmountToDecimal(event.params.amount)) | ||
stableJoeDayData.save() | ||
} | ||
} | ||
|
||
export function handleDeposit(event: DepositEvent): void { | ||
log.debug('[handleDeposit] from {}', [dataSource.address().toHex()]) | ||
|
||
const stableJoe = getStableJoe(event.block) | ||
let user = getUser(event.params.user, event.block) | ||
|
||
user.joeStaked = user.joeStaked.plus(convertAmountToDecimal(event.params.amount)) | ||
user.joeStakedUSD = user.joeStakedUSD.plus(convertAmountToDecimal(event.params.amount).times(getJoePrice())) | ||
user.updatedAt = event.block.timestamp | ||
user.save() | ||
|
||
log.debug('[handleDeposit] updating stableJoe and saving {}', [event.address.toHexString()]) | ||
stableJoe.joeStaked = stableJoe.joeStaked.plus(convertAmountToDecimal(event.params.amount)) | ||
stableJoe.joeStakedUSD = stableJoe.joeStakedUSD.plus(convertAmountToDecimal(event.params.amount).times(getJoePrice())) | ||
stableJoe.updatedAt = event.block.timestamp | ||
stableJoe.save() | ||
|
||
log.debug('[handleDeposit] update deposit fees {}', [event.address.toHexString()]) | ||
// update deposit fees | ||
if (stableJoe.depositFee) { | ||
stableJoe.depositFeeJOE = stableJoe.depositFeeJOE.plus(stableJoe.depositFee) | ||
stableJoe.depositFeeUSD = stableJoe.depositFeeUSD.plus(stableJoe.depositFee).times(getJoePrice()) | ||
} | ||
|
||
log.debug('[handleDeposit] update day data {}', [event.address.toHexString()]) | ||
// update day data | ||
let stableJoeDayData = getStableJoeDayData(event.address, event.block) | ||
stableJoeDayData.joeStaked = stableJoeDayData.joeStaked.plus(convertAmountToDecimal(event.params.amount)) | ||
stableJoeDayData.joeStakedUSD = stableJoeDayData.joeStakedUSD.plus( | ||
convertAmountToDecimal(event.params.amount).times(getJoePrice()) | ||
) | ||
if (stableJoe.depositFee) { | ||
stableJoeDayData.depositFeeJOE = stableJoeDayData.depositFeeJOE.plus(stableJoe.depositFee) | ||
stableJoeDayData.depositFeeUSD = stableJoeDayData.depositFeeUSD.plus(stableJoe.depositFee).times(getJoePrice()) | ||
} | ||
|
||
stableJoeDayData.save() | ||
} | ||
|
||
export function handleDepositFeeChanged(event: DepositFeeChangedEvent): void { | ||
const stableJoe = getStableJoe(event.block) | ||
|
||
stableJoe.depositFee = convertAmountToDecimal(event.params.newFee) | ||
stableJoe.updatedAt = event.block.timestamp | ||
stableJoe.save() | ||
} | ||
|
||
export function handleWithdraw(event: WithdrawEvent): void { | ||
const stableJoe = getStableJoe(event.block) | ||
const user = getUser(event.params.user, event.block) | ||
|
||
user.joeStaked = user.joeStaked.minus(convertAmountToDecimal(event.params.amount)) | ||
user.joeStakedUSD = user.joeStakedUSD.minus(convertAmountToDecimal(event.params.amount).times(getJoePrice())) | ||
user.updatedAt = event.block.timestamp | ||
user.save() | ||
|
||
const joePrice = getJoePrice() | ||
stableJoe.joeStaked = stableJoe.joeStaked.minus(convertAmountToDecimal(event.params.amount)) | ||
stableJoe.joeStakedUSD = stableJoe.joeStakedUSD.minus( | ||
convertAmountToDecimal(event.params.amount).times(getJoePrice()) | ||
) | ||
stableJoe.updatedAt = event.block.timestamp | ||
stableJoe.save() | ||
|
||
// update day data | ||
let stableJoeDayData = getStableJoeDayData(event.address, event.block) | ||
stableJoeDayData.joeUnstaked = stableJoeDayData.joeUnstaked.plus(convertAmountToDecimal(event.params.amount)) | ||
stableJoeDayData.joeUnstakedUSD = stableJoeDayData.joeUnstakedUSD.plus( | ||
convertAmountToDecimal(event.params.amount).times(joePrice) | ||
) | ||
|
||
stableJoeDayData.save() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# StableJoe | ||
type StableJoe @entity { | ||
# Contract address | ||
id: ID! | ||
|
||
# Joe staked | ||
joeStaked: BigDecimal! | ||
|
||
# Joe staked USD | ||
joeStakedUSD: BigDecimal! | ||
|
||
# USD harvested | ||
usdHarvested: BigDecimal! | ||
|
||
# Users in the bar | ||
users: [User!]! @derivedFrom(field: "stableJoe") | ||
|
||
# Deposit fee | ||
depositFee: BigDecimal! | ||
|
||
# Deposit fees collected in JOE | ||
depositFeeJOE: BigDecimal! | ||
|
||
# Deposit fees collected in USD | ||
depositFeeUSD: BigDecimal! | ||
|
||
# Last Updated At | ||
updatedAt: BigInt! | ||
} | ||
|
||
# User | ||
type User @entity { | ||
# ID | ||
id: ID! | ||
|
||
# Stable Joe | ||
stableJoe: StableJoe | ||
|
||
# Staked | ||
joeStaked: BigDecimal! | ||
|
||
# Sum of $ at time of staking | ||
joeStakedUSD: BigDecimal! | ||
|
||
# USD harvested | ||
usdHarvested: BigDecimal! | ||
|
||
# Updated at | ||
updatedAt: BigInt! | ||
} | ||
|
||
type StableJoeDayData @entity { | ||
id: ID! | ||
date: Int! | ||
joeStaked: BigDecimal! | ||
joeStakedUSD: BigDecimal! | ||
joeUnstaked: BigDecimal! | ||
joeUnstakedUSD: BigDecimal! | ||
usdHarvested: BigDecimal! | ||
depositFeeJOE: BigDecimal! | ||
depositFeeUSD: BigDecimal! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
specVersion: 0.0.2 | ||
description: StableJoeStaking | ||
schema: | ||
file: ./stablejoe.graphql | ||
dataSources: | ||
- kind: ethereum/contract | ||
name: StableJoeStaking | ||
network: rinkeby | ||
source: | ||
address: '0x12ba94d04a7a7f0882155b2c55b88e269428227b' | ||
abi: StableJoeStaking | ||
startBlock: 10219081 | ||
mapping: | ||
kind: ethereum/events | ||
apiVersion: 0.0.4 | ||
language: wasm/assemblyscript | ||
file: ./src/mappings/stablejoe.ts | ||
entities: | ||
- StableJoe | ||
- User | ||
- StableJoeDayData | ||
abis: | ||
- name: StableJoeStaking | ||
file: ../../node_modules/@traderjoe-xyz/core/abi/StableJoeStaking.json | ||
- name: Pair | ||
file: ../../node_modules/@traderjoe-xyz/core/abi/JoePair.json | ||
eventHandlers: | ||
- event: ClaimReward(indexed address,indexed address,uint256) | ||
handler: handleClaimReward | ||
- event: Deposit(indexed address,uint256,uint256) | ||
handler: handleDeposit | ||
- event: DepositFeeChanged(uint256,uint256) | ||
handler: handleDepositFeeChanged | ||
- event: Withdraw(indexed address,uint256) | ||
handler: handleWithdraw |
Oops, something went wrong.