Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #32 from Xavier59/master
Browse files Browse the repository at this point in the history
Moving masterchef lockup logic into lockup
  • Loading branch information
matthewlilley authored Feb 9, 2021
2 parents 93b20e0 + 8be3166 commit 08aadd7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 50 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"codegen:exchange": "graph codegen subgraphs/exchange.yaml",
"codegen:maker": "graph codegen subgraphs/maker.yaml",
"codegen:masterchef": "graph codegen subgraphs/masterchef.yaml",
"codegen:lockup": "graph codegen subgraphs/lockup.yaml",
"codegen:timelock": "graph codegen subgraphs/timelock.yaml",
"codegen:dexcandles": "graph codegen subgraphs/dexcandles.yaml",
"build": "graph build",
"build:bar": "graph build subgraphs/bar.yaml",
"build:exchange": "graph build subgraphs/exchange.yaml",
"build:maker": "graph build subgraphs/maker.yaml",
"build:masterchef": "graph build subgraphs/masterchef.yaml",
"build:lockup": "graph build subgraphs/lockup.yaml",
"build:timelock": "graph build subgraphs/timelock.yaml",
"build:dexcandles": "graph build subgraphs/dexcandles.yaml",
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/",
Expand Down
73 changes: 54 additions & 19 deletions src/lockup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts'
import { BIG_INT_ONE, BIG_INT_ZERO, LOCKUP_BLOCK_NUMBER, LOCKUP_POOL_NUMBER, MASTER_CHEF_ADDRESS } from './constants'
import { BIG_DECIMAL_1E12, BIG_DECIMAL_1E18, BIG_DECIMAL_ZERO, BIG_INT_ONE, BIG_INT_ZERO, LOCKUP_BLOCK_NUMBER, LOCKUP_POOL_NUMBER, MASTER_CHEF_ADDRESS } from './constants'
import {
Deposit,
MasterChef as MasterChefContract,
Expand All @@ -8,7 +8,7 @@ import {
Withdraw,
} from '../generated/MasterChef/MasterChef'
import { Lockup, Pool, User } from '../generated/schema'

import { getSushiPrice } from './price'
import { Pair as PairContract } from '../generated/MasterChef/Pair'

export function getUser(pid: BigInt, address: Address, block: ethereum.Block): User {
Expand All @@ -24,12 +24,32 @@ export function getUser(pid: BigInt, address: Address, block: ethereum.Block): U
user.address = address
user.amount = BIG_INT_ZERO
user.rewardDebt = BIG_INT_ZERO
user.sushiHarvestedSinceLockup = BIG_DECIMAL_ZERO
user.sushiHarvestedSinceLockupUSD = BIG_DECIMAL_ZERO
user.save()
}

return user as User
}

export function getPool(id: BigInt): Pool {
let pool = Pool.load(id.toString())

if (pool === null) {
const masterChefContract = MasterChefContract.bind(MASTER_CHEF_ADDRESS)

// Create new pool.
pool = new Pool(id.toString())
const poolInfo = masterChefContract.poolInfo(id)
pool.allocPoint = poolInfo.value1
pool.accSushiPerShare = poolInfo.value3

pool.save()
}

return pool as Pool
}

// Calls
export function set(call: SetCall): void {
if (call.inputs._pid == LOCKUP_POOL_NUMBER) {
Expand Down Expand Up @@ -63,31 +83,46 @@ export function set(call: SetCall): void {
pool.lockup = lockup.id
pool.allocPoint = poolInfo.value1
pool.accSushiPerShare = poolInfo.value3
pool.balance = pairContract.balanceOf(MASTER_CHEF_ADDRESS)
// pool.balance = pairContract.balanceOf(MASTER_CHEF_ADDRESS)
pool.save()
}
}
}

function transfer(pid: BigInt, userAddr: Address, block: ethereum.Block): void {
const masterChefContract = MasterChefContract.bind(MASTER_CHEF_ADDRESS)
const user = getUser(pid, userAddr, block)

const poolInfo = masterChefContract.poolInfo(pid)
const pool = getPool(pid)
pool.accSushiPerShare = poolInfo.value3
pool.save()

if (block.number.ge(LOCKUP_BLOCK_NUMBER)) {
const pool = getPool(pid)
const pending = user.amount
.toBigDecimal()
.times(pool.accSushiPerShare.toBigDecimal())
.div(BIG_DECIMAL_1E12)
.minus(user.rewardDebt.toBigDecimal())
.div(BIG_DECIMAL_1E18)
if (pending.gt(BIG_DECIMAL_ZERO)) {
user.sushiHarvestedSinceLockup = user.sushiHarvestedSinceLockup.plus(pending)
const sushiHarvestedUSD = pending.times(getSushiPrice(block))
user.sushiHarvestedSinceLockupUSD = user.sushiHarvestedSinceLockupUSD.plus(sushiHarvestedUSD)
}
}
const userInfo = masterChefContract.userInfo(pid, userAddr)
user.amount = userInfo.value0
user.rewardDebt = userInfo.value1
user.save()
}

// Events
export function deposit(event: Deposit): void {
if (event.block.number.lt(LOCKUP_BLOCK_NUMBER)) {
const masterChefContract = MasterChefContract.bind(MASTER_CHEF_ADDRESS)
const userInfo = masterChefContract.userInfo(event.params.pid, event.params.user)
const user = getUser(event.params.pid, event.params.user, event.block)
user.amount = userInfo.value0
user.rewardDebt = userInfo.value1
user.save()
}
transfer(event.params.pid, event.params.user, event.block)
}

export function withdraw(event: Withdraw): void {
if (event.block.number.lt(LOCKUP_BLOCK_NUMBER)) {
const masterChefContract = MasterChefContract.bind(MASTER_CHEF_ADDRESS)
const user = getUser(event.params.pid, event.params.user, event.block)
const userInfo = masterChefContract.userInfo(event.params.pid, event.params.user)
user.amount = userInfo.value0
user.rewardDebt = userInfo.value1
user.save()
}
transfer(event.params.pid, event.params.user, event.block)
}
13 changes: 0 additions & 13 deletions src/masterchef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import {
BIG_INT_ONE,
BIG_INT_ONE_DAY_SECONDS,
BIG_INT_ZERO,
LOCKUP_BLOCK_NUMBER,
MASTER_CHEF_ADDRESS,
MASTER_CHEF_START_BLOCK,
SUSHI_TOKEN_ADDRESS,
} from './constants'
import { History, MasterChef, Pool, PoolHistory, User } from '../generated/schema'
import { getSushiPrice, getUSDRate } from './price'
Expand Down Expand Up @@ -169,11 +167,8 @@ export function getUser(pid: BigInt, address: Address, block: ethereum.Block): U
user.address = address
user.amount = BIG_INT_ZERO
user.rewardDebt = BIG_INT_ZERO
user.sushiAtLockup = BIG_DECIMAL_ZERO
user.sushiHarvested = BIG_DECIMAL_ZERO
user.sushiHarvestedUSD = BIG_DECIMAL_ZERO
user.sushiHarvestedSinceLockup = BIG_DECIMAL_ZERO
user.sushiHarvestedSinceLockupUSD = BIG_DECIMAL_ZERO
user.entryUSD = BIG_DECIMAL_ZERO
user.exitUSD = BIG_DECIMAL_ZERO
user.timestamp = block.timestamp
Expand Down Expand Up @@ -334,10 +329,6 @@ export function deposit(event: Deposit): void {
const sushiHarvestedUSD = pending.times(getSushiPrice(event.block))
user.sushiHarvested = user.sushiHarvested.plus(pending)
user.sushiHarvestedUSD = user.sushiHarvestedUSD.plus(sushiHarvestedUSD)
if (event.block.number.ge(LOCKUP_BLOCK_NUMBER)) {
user.sushiHarvestedSinceLockup = user.sushiHarvestedSinceLockup.plus(pending)
user.sushiHarvestedSinceLockupUSD = user.sushiHarvestedSinceLockupUSD.plus(sushiHarvestedUSD)
}
pool.sushiHarvested = pool.sushiHarvested.plus(pending)
pool.sushiHarvestedUSD = pool.sushiHarvestedUSD.plus(sushiHarvestedUSD)
poolHistory.sushiHarvested = pool.sushiHarvested
Expand Down Expand Up @@ -493,10 +484,6 @@ export function withdraw(event: Withdraw): void {
const sushiHarvestedUSD = pending.times(getSushiPrice(event.block))
user.sushiHarvested = user.sushiHarvested.plus(pending)
user.sushiHarvestedUSD = user.sushiHarvestedUSD.plus(sushiHarvestedUSD)
if (event.block.number.ge(LOCKUP_BLOCK_NUMBER)) {
user.sushiHarvestedSinceLockup = user.sushiHarvestedSinceLockup.plus(pending)
user.sushiHarvestedSinceLockupUSD = user.sushiHarvestedSinceLockupUSD.plus(sushiHarvestedUSD)
}
pool.sushiHarvested = pool.sushiHarvested.plus(pending)
pool.sushiHarvestedUSD = pool.sushiHarvestedUSD.plus(sushiHarvestedUSD)
poolHistory.sushiHarvested = pool.sushiHarvested
Expand Down
15 changes: 6 additions & 9 deletions subgraphs/lockup.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type Pool @entity {
id: ID!

# Lockup
lockup: Lockup!
lockup: Lockup

# Balance
balance: BigInt!
# balance: BigInt!

# Alloc point
allocPoint: BigInt!
Expand Down Expand Up @@ -51,12 +51,9 @@ type User @entity {
# Reward debt
rewardDebt: BigInt!

# Sushi At Lockup
sushiAtLockup: BigInt!

# Sushi Harevested Since Lockup
sushiHarvestedSinceLockup: BigInt!

# Sushi Locked
sushiLocked: BigInt!
sushiHarvestedSinceLockup: BigDecimal!
# Sushi harvested since lockup USD
sushiHarvestedSinceLockupUSD: BigDecimal!
}
9 changes: 0 additions & 9 deletions subgraphs/masterchef.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -228,21 +228,12 @@ type User @entity {
# Exit USD
exitUSD: BigDecimal!

# Sushi at lockup
sushiAtLockup: BigDecimal!

# Sushi harvested
sushiHarvested: BigDecimal!

# Sushi harvested
sushiHarvestedUSD: BigDecimal!

# Sushi harvested since lockup
sushiHarvestedSinceLockup: BigDecimal!

# Sushi harvested since lockup USD
sushiHarvestedSinceLockupUSD: BigDecimal!

# Timestamp
timestamp: BigInt!

Expand Down

0 comments on commit 08aadd7

Please sign in to comment.