Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle setting feeTo #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion uniswap-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ and start a MintTransaction or BurnTransaction accordingly and then -

Because we can not rely on ordering of event triggers in graph node we need to implement reverse order of events as well in which -
* Mint event starts a MintTransaction and then Transfer event completes it
* Burn event starts a BurnTransaction and then Transfer event completes it
* Burn event starts a BurnTransaction and then Transfer event completes it

### Trisolaris fee enabled at

Fee enabled in block 67156413 with https://aurorascan.dev/tx/0x32d8f20b087f8d0854b774b5cb2ab0a793c78a7ecf76222b46311ea488154a57
6 changes: 5 additions & 1 deletion uniswap-v2/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ type PositionSnapshot @entity {

# UniswapV2 protocol entities

type PairFactory @entity {
id: ID! #{factory Address}
feeTo: String
}

# Created on PairCreated event on UniswapV2Factory.sol
# feeTo is not part of Pair contract. We need to fetch it from factory contract
type Pair @entity {
id: ID! # {pairAddress}
factory: Account!
Expand Down
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/apeswap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.APESWAP);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/auroraswap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.AURORASWAP);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/biswap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.BISWAP);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/cometh.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.COMETH);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/pancakeswap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
PairCreated
PairCreated, SetFeeToCall
} from "../../generated/UniswapV2Factory/UniswapV2Factory"

import { ProtocolName } from "../constants"
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory"
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.PANCAKESWAP)
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/pangolin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.PANGOLIN);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/quickswap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
PairCreated
PairCreated, SetFeeToCall
} from "../../generated/UniswapV2Factory/UniswapV2Factory"

import { ProtocolName } from "../constants"
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory"
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.QUICKSWAP)
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/sushiswap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
PairCreated
PairCreated, SetFeeToCall
} from "../../generated/UniswapV2Factory/UniswapV2Factory"

import { ProtocolName } from "../constants"
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory"
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.SUSHISWAP)
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/traderjoe.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.TRADER_JOE);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/trisolaris.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
PairCreated
PairCreated, SetFeeToCall
} from "../../generated/UniswapV2Factory/UniswapV2Factory"

import { ProtocolName } from "../constants"
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory"
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.TRISOLARIS)
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/uniswap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {
PairCreated
PairCreated, SetFeeToCall
} from "../../generated/UniswapV2Factory/UniswapV2Factory"

import { ProtocolName } from "../constants"
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory"
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.UNISWAP_V2)
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
7 changes: 6 additions & 1 deletion uniswap-v2/src/factories/wannaswap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { PairCreated } from "../../generated/UniswapV2Factory/UniswapV2Factory";
import { PairCreated, SetFeeToCall } from "../../generated/UniswapV2Factory/UniswapV2Factory";

import { ProtocolName } from "../constants";
import { handlePairCreated as PairCreatedHandler } from "../uniswapV2Factory";
import { handleSetFeeTo as SetFeeToHandler } from "../uniswapV2Factory"

export function handlePairCreated(event: PairCreated): void {
PairCreatedHandler(event, ProtocolName.WANNASWAP);
}

export function handleSetFeeTo(call: SetFeeToCall): void {
SetFeeToHandler(call)
}
24 changes: 22 additions & 2 deletions uniswap-v2/src/uniswapV2Factory.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { BigInt, DataSourceContext } from "@graphprotocol/graph-ts"
import { Pair as PairEntity } from "../generated/schema"
import {
Pair as PairEntity,
PairFactory as PairFactoryEntity
} from "../generated/schema"
import { UniswapV2Pair } from "../generated/templates"
import { PairCreated } from "../generated/UniswapV2Factory/UniswapV2Factory"
import { PairCreated, SetFeeToCall } from "../generated/UniswapV2Factory/UniswapV2Factory"
import { getOrCreateAccount, getOrCreateERC20Token, getOrCreateMarket } from "./common"
import { FEE_30_BASE_POINTS, protocolToFee, ProtocolType } from "./constants"

export function handlePairCreated(event: PairCreated, protocolName: string): void {
let pairFactory = PairFactoryEntity.load(event.address.toHexString())
if (pairFactory == null) {
pairFactory = new PairFactoryEntity(event.address.toHexString())
pairFactory.save()
}

// Create a tokens and market entity
let token0 = getOrCreateERC20Token(event, event.params.token0)
let token1 = getOrCreateERC20Token(event, event.params.token1)
Expand Down Expand Up @@ -57,3 +66,14 @@ function getProtocolFee(address: string): BigInt {

return fee as BigInt
}

export function handleSetFeeTo(call: SetFeeToCall): void {
let pairFactory = PairFactoryEntity.load(call.to.toHexString())
if (pairFactory == null) {
pairFactory = new PairFactoryEntity(call.to.toHexString())
pairFactory.save()
}

pairFactory.feeTo = call.inputs._feeTo.toHexString()
pairFactory.save()
}
59 changes: 58 additions & 1 deletion uniswap-v2/src/uniswapV2Pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
Market as MarketEntity,
MarketDayData,
Mint as MintEntity,
Pair as PairEntity
Pair as PairEntity,
PairFactory as PairFactoryEntity
} from "../generated/schema"
import {
Burn,
Expand Down Expand Up @@ -256,6 +257,52 @@ function checkIncompleteBurnFromLastTransaction(event: ethereum.Event, pair: Pai
pair.save()
}

function mintFee(event: ethereum.Event, pair: PairEntity, amount: BigInt, pairFactory: PairFactoryEntity): void {
let feeTo = pairFactory.feeTo as string
let accountAddress = Address.fromString(feeTo)
let account = new AccountEntity(feeTo)
let market = MarketEntity.load(pair.id) as MarketEntity
let accountLiquidity = getOrCreateLiquidity(pair, accountAddress)

let outputTokenAmount = amount as BigInt
let inputTokenAmounts: TokenBalance[] = []
inputTokenAmounts.push(new TokenBalance(pair.token0, feeTo, BigInt.fromI32(0)))
inputTokenAmounts.push(new TokenBalance(pair.token1, feeTo, BigInt.fromI32(0)))

let outputTokenBalance = accountLiquidity.balance
let token0Balance = outputTokenBalance.times(pair.reserve0).div(pair.totalSupply)
let token1Balance = outputTokenBalance.times(pair.reserve1).div(pair.totalSupply)
let inputTokenBalances: TokenBalance[] = []
inputTokenBalances.push(new TokenBalance(pair.token0, feeTo, token0Balance))
inputTokenBalances.push(new TokenBalance(pair.token1, feeTo, token1Balance))

investInMarket(
event,
account,
market,
outputTokenAmount,
inputTokenAmounts,
[],
outputTokenBalance,
inputTokenBalances,
[],
null
)

// update market
let marketInputTokenBalances: TokenBalance[] = []
marketInputTokenBalances.push(new TokenBalance(pair.token0, pair.id, pair.reserve0))
marketInputTokenBalances.push(new TokenBalance(pair.token1, pair.id, pair.reserve1))

// Update market
updateMarket(
event,
market,
marketInputTokenBalances,
pair.totalSupply
)
}

export function handleTransfer(event: Transfer): void {
if (event.params.value == BigInt.fromI32(0)) {
return
Expand Down Expand Up @@ -297,6 +344,16 @@ export function handleTransfer(event: Transfer): void {
marketDayData.save()
}

// Check if it's minting fee
let pairFactory = PairFactoryEntity.load(pair.factory) as PairFactoryEntity
if (pairFactory != null && pairFactory.feeTo != null && toHex == pairFactory.feeTo) {
// Mint amount to feeTo address
pair.totalSupply = pair.totalSupply.plus(event.params.value)
pair.save()

mintFee(event, pair, event.params.value, pairFactory)
}

let mint = getOrCreateMint(event, pair)
mint.transferEventApplied = true
mint.from = getOrCreateAccount(event.transaction.from).id
Expand Down
3 changes: 3 additions & 0 deletions uniswap-v2/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ dataSources:
eventHandlers:
- event: PairCreated(indexed address,indexed address,address,uint256)
handler: handlePairCreated
callHandlers:
- function: setFeeTo(address)
handler: handleSetFeeTo
file: ./src/factories/{{factoryMappingPath}}
templates:
- kind: ethereum/contract
Expand Down