Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
5 changes: 2 additions & 3 deletions Algebra/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

{
"name": "algebra-subgraph",
"version": "1.0.0",
Expand All @@ -14,8 +13,8 @@
"create": "graph create cryptoalgebra/info --node https://api.thegraph.com/create/"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.22.1",
"@graphprotocol/graph-ts": "0.22.1",
"@graphprotocol/graph-cli": "^0.64.0",
"@graphprotocol/graph-ts": "^0.32.0",
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"eslint": "^6.2.2",
Expand Down
14 changes: 13 additions & 1 deletion Algebra/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ type Factory @entity {
totalVolumeMatic: BigDecimal!
# total swap fees all time in USD
totalFeesUSD: BigDecimal!
# total swap fees all time in USD
# total swap fees all time in Native Token
totalFeesMatic: BigDecimal!
# total swap protocol fees all time in USD
totalProtocolFeesUSD: BigDecimal!
# total swap protocol fees all time in Native Token
totalProtocolFeesMatic: BigDecimal!
# total untracked swap protocol fees all time in USD
totalProtocolFeesUSDUntracked: BigDecimal!
# all volume even through less reliable USD values
untrackedVolumeUSD: BigDecimal!
# TVL derived in USD
Expand Down Expand Up @@ -126,6 +132,12 @@ type Pool @entity {
collectedFeesToken1: BigDecimal!
# all time fees collected derived USD
collectedFeesUSD: BigDecimal!
# all time protocol fees collected token0
collectedProtocolFeesToken0: BigDecimal!
# all time protocol fees collected token1
collectedProtocolFeesToken1: BigDecimal!
# all time protocol fees collected derived USD
collectedProtocolFeesUSD: BigDecimal!
# total token 0 across all ticks
totalValueLockedToken0: BigDecimal!
# total token 1 across all ticks
Expand Down
31 changes: 24 additions & 7 deletions Algebra/src/mappings/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,43 +334,57 @@ export function handleSwap(event: SwapEvent): void {

// need absolute amounts for volume
let amount0Abs = amount0
let communityFeeAmount0 = BigDecimal.fromString("0");
if (amount0.lt(ZERO_BD)) {
amount0Abs = amount0.times(BigDecimal.fromString('-1'))
}
else {
let communityFeeAmount = amount0.times(BigDecimal.fromString((pool.fee.times(pool.communityFee0).toString())).div(BigDecimal.fromString('1000000000')))
communityFeeAmount = communityFeeAmount.times(BigDecimal.fromString("1"))
amount0 = amount0.minus(communityFeeAmount)
communityFeeAmount0 = amount0.times(BigDecimal.fromString((pool.fee.times(pool.communityFee0).toString())).div(BigDecimal.fromString('1000000000')))
communityFeeAmount0 = communityFeeAmount0.times(BigDecimal.fromString("1"))
amount0 = amount0.minus(communityFeeAmount0)
amount0Abs = amount0
}

let amount1Abs = amount1
let communityFeeAmount1 = BigDecimal.fromString("0");
if (amount1.lt(ZERO_BD)) {
amount1Abs = amount1.times(BigDecimal.fromString('-1'))
}
else{
let communityFeeAmount = amount1.times(BigDecimal.fromString((pool.fee.times(pool.communityFee1).toString())).div(BigDecimal.fromString('1000000000')))
communityFeeAmount = communityFeeAmount.times(BigDecimal.fromString("1"))
amount1 = amount1.minus(communityFeeAmount)
communityFeeAmount1 = amount1.times(BigDecimal.fromString((pool.fee.times(pool.communityFee1).toString())).div(BigDecimal.fromString('1000000000')))
communityFeeAmount1 = communityFeeAmount1.times(BigDecimal.fromString("1"))
amount1 = amount1.minus(communityFeeAmount1)
amount1Abs = amount1
}

let amount0Matic = amount0Abs.times(token0.derivedMatic)
let amount1Matic = amount1Abs.times(token1.derivedMatic)
let communityFeeMatic0 = communityFeeAmount0.times(token0.derivedMatic);
let communityFeeMatic1 = communityFeeAmount1.times(token0.derivedMatic);

let amount0USD = amount0Matic.times(bundle.maticPriceUSD)
let amount1USD = amount1Matic.times(bundle.maticPriceUSD)
let communityFee0USD = communityFeeMatic0.times(bundle.maticPriceUSD);
let communityFee1USD = communityFeeMatic1.times(bundle.maticPriceUSD);


//let factory.totalProtocolFeesUSD

// get amount that should be tracked only - div 2 because cant count both input and output as volume
let amountTotalUSDTracked = getTrackedAmountUSD(amount0Abs, token0 as Token, amount1Abs, token1 as Token).div(
BigDecimal.fromString('2')
)

// get amount that should be tracked only - div 2 because cant count both input and output as volume
let communityFeeTotalUSDTracked = getTrackedAmountUSD(communityFeeAmount0, token0 as Token, communityFeeAmount1, token1 as Token).div(
BigDecimal.fromString('2')
)

let amountTotalMaticTracked = safeDiv(amountTotalUSDTracked, bundle.maticPriceUSD)
let amountTotalUSDUntracked = amount0USD.plus(amount1USD).div(BigDecimal.fromString('2'))

let communityFeeMaticTracked = safeDiv(communityFeeTotalUSDTracked, bundle.maticPriceUSD)
let communityFeeUSDUntracked = communityFee0USD.plus(communityFee1USD).div(BigDecimal.fromString('2'))

let feesMatic = amountTotalMaticTracked.times(pool.fee.toBigDecimal()).div(BigDecimal.fromString('1000000'))
let feesUSD = amountTotalUSDTracked.times(pool.fee.toBigDecimal()).div(BigDecimal.fromString('1000000'))
let untrackedFees = amountTotalUSDUntracked.times(pool.fee.toBigDecimal()).div(BigDecimal.fromString('1000000'))
Expand All @@ -383,6 +397,9 @@ export function handleSwap(event: SwapEvent): void {
factory.untrackedVolumeUSD = factory.untrackedVolumeUSD.plus(amountTotalUSDUntracked)
factory.totalFeesMatic = factory.totalFeesMatic.plus(feesMatic)
factory.totalFeesUSD = factory.totalFeesUSD.plus(feesUSD)
factory.totalProtocolFeesUSD = factory.totalProtocolFeesUSD.plus(communityFeeTotalUSDTracked);
factory.totalProtocolFeesMatic = factory.totalProtocolFeesMatic.plus(communityFeeMaticTracked);
factory.totalProtocolFeesUSDUntracked = factory.totalProtocolFeesUSDUntracked.plus(communityFeeUSDUntracked);

// reset aggregate tvl before individual pool tvl updates
let currentPoolTvlMatic = pool.totalValueLockedMatic
Expand Down
14 changes: 12 additions & 2 deletions Algebra/src/mappings/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { FACTORY_ADDRESS, ZERO_BI, ONE_BI, ZERO_BD, ADDRESS_ZERO, pools_list} fr
import { Factory } from '../types/schema'
import { Pool as PoolEvent } from '../types/Factory/Factory'
import { DefaultCommunityFee } from '../types/Factory/Factory'
import { Factory as FactoryContract } from '../types/Factory/Factory'
import { Pool, Token, Bundle } from '../types/schema'
import { Pool as PoolTemplate} from '../types/templates'
import { fetchTokenSymbol, fetchTokenName, fetchTokenTotalSupply, fetchTokenDecimals } from '../utils/token'
import { log,BigInt } from '@graphprotocol/graph-ts'
import { log, BigInt, Address } from '@graphprotocol/graph-ts'

export function handlePoolCreated(event: PoolEvent): void {
// temp fix
Expand Down Expand Up @@ -121,6 +122,14 @@ export function handlePoolCreated(event: PoolEvent): void {
pool.token0 = token0.id
pool.token1 = token1.id
pool.fee = BigInt.fromI32(100)

let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS))
let tryBaseFee = factoryContract.try_baseFeeConfiguration()

if (!tryBaseFee.reverted) {
pool.fee = BigInt.fromI32(tryBaseFee.value.value8);
}

pool.createdAtTimestamp = event.block.timestamp
pool.createdAtBlockNumber = event.block.number
pool.liquidityProviderCount = ZERO_BI
Expand Down Expand Up @@ -183,6 +192,7 @@ export function handleDefaultCommFeeChange(event: DefaultCommunityFee): void{
bundle.maticPriceUSD = ZERO_BD
bundle.save()
}

factory.defaultCommunityFee = BigInt.fromI32(event.params.newDefaultCommunityFee as i32)
factory.save()
}
}
10 changes: 6 additions & 4 deletions Algebra/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
specVersion: 0.0.2
specVersion: 0.0.4
description: Algebra is a decentralized protocol for automated token exchange on Polygon.
schema:
file: ./schema.graphql
Expand All @@ -12,7 +12,7 @@ dataSources:
startBlock: 30096645
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.6
language: wasm/assemblyscript
file: ./src/mappings/factory.ts
entities:
Expand All @@ -32,6 +32,8 @@ dataSources:
eventHandlers:
- event: Pool(indexed address,indexed address,address)
handler: handlePoolCreated
- event: DefaultCommunityFee(uint8)
handler: handleDefaultCommFeeChange
- kind: ethereum/contract
name: NonfungiblePositionManager
network: gnosis
Expand All @@ -41,7 +43,7 @@ dataSources:
startBlock: 30096660
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.6
language: wasm/assemblyscript
file: ./src/mappings/position-manager.ts
entities:
Expand Down Expand Up @@ -74,7 +76,7 @@ templates:
abi: Pool
mapping:
kind: ethereum/events
apiVersion: 0.0.5
apiVersion: 0.0.6
language: wasm/assemblyscript
file: ./src/mappings/core.ts
entities:
Expand Down
Loading