add fixedrate balances (#339)

* add fixedrate balances
* fix dispensers
This commit is contained in:
Alex Coseru 2022-02-20 10:00:16 +02:00 committed by GitHub
parent cb16470c9a
commit cbdbc64a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 170 additions and 61 deletions

View File

@ -293,13 +293,17 @@ type User @entity {
type FixedRateExchange @entity { type FixedRateExchange @entity {
"fixed rate exchange id" "fixed rate exchange id"
id: ID! id: ID!
contract: String!
exchangeId: String!
owner: User! owner: User!
datatoken: Token! datatoken: Token!
baseToken: Token! baseToken: Token!
"amount of datatokens available to be sold, this is relevant if the exchange is not able to mint" "amount of datatokens available to be sold, this is relevant if the exchange is not able to mint"
datatokenBalance: BigDecimal! datatokenSupply: BigDecimal!
"amount of basetokens available to be collected by the owner" "amount of basetokens available to be collected by the owner"
baseTokenSupply: BigDecimal!
datatokenBalance: BigDecimal!
baseTokenBalance: BigDecimal! baseTokenBalance: BigDecimal!
price: BigDecimal! price: BigDecimal!
active: Boolean! active: Boolean!

View File

@ -36,7 +36,7 @@ async function replaceContractAddresses() {
) )
subgraph = subgraph.replace( subgraph = subgraph.replace(
/__DISPENSERADDRESS__/g, /__DISPENSERADDRESS__/g,
"'" + addresses[network].FixedPrice + "'" "'" + addresses[network].Dispenser + "'"
) )
subgraph = subgraph.replace( subgraph = subgraph.replace(
/__FACTORYROUTERADDRESS__/g, /__FACTORYROUTERADDRESS__/g,

View File

@ -8,14 +8,22 @@ import {
import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory' import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory'
import { Dispenser, DispenserTransaction } from '../@types/schema' import { Dispenser, DispenserTransaction } from '../@types/schema'
import { decimal } from './utils/constants' import { decimal } from './utils/constants'
import { getDispenser } from './utils/dispenserUtils' import {
getDispenser,
getDispenserGraphID,
updateDispenserBalance
} from './utils/dispenserUtils'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { addDispenser } from './utils/globalUtils' import { addDispenser } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils' import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
export function handleNewDispenser(event: DispenserCreated): void { export function handleNewDispenser(event: DispenserCreated): void {
const dispenser = new Dispenser(event.params.datatokenAddress.toHex()) const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const dispenser = new Dispenser(dispenserID)
const token = getToken(event.params.datatokenAddress, false) const token = getToken(event.params.datatokenAddress, false)
dispenser.token = token.id dispenser.token = token.id
@ -40,13 +48,21 @@ export function handleNewDispenser(event: DispenserCreated): void {
} }
export function handleActivate(event: DispenserActivated): void { export function handleActivate(event: DispenserActivated): void {
const dispenser = getDispenser(event.params.datatokenAddress.toHex()) const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const dispenser = getDispenser(dispenserID)
dispenser.active = true dispenser.active = true
dispenser.save() dispenser.save()
} }
export function handleDeactivate(event: DispenserDeactivated): void { export function handleDeactivate(event: DispenserDeactivated): void {
const dispenser = getDispenser(event.params.datatokenAddress.toHex()) const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const dispenser = getDispenser(dispenserID)
dispenser.active = true dispenser.active = true
dispenser.save() dispenser.save()
} }
@ -54,22 +70,29 @@ export function handleDeactivate(event: DispenserDeactivated): void {
export function handleAllowedSwapperChanged( export function handleAllowedSwapperChanged(
event: DispenserAllowedSwapperChanged event: DispenserAllowedSwapperChanged
): void { ): void {
const dispenser = getDispenser(event.params.datatoken.toHex()) const dispenserID = getDispenserGraphID(event.address, event.params.datatoken)
const dispenser = getDispenser(dispenserID)
dispenser.allowedSwapper = event.params.newAllowedSwapper.toHex() dispenser.allowedSwapper = event.params.newAllowedSwapper.toHex()
dispenser.save() dispenser.save()
} }
export function handleTokensDispensed(event: TokensDispensed): void { export function handleTokensDispensed(event: TokensDispensed): void {
const dispenserID = getDispenserGraphID(
event.address,
event.params.datatokenAddress
)
const id = event.transaction.hash const id = event.transaction.hash
.toHexString() .toHexString()
.concat('-') .concat('-')
.concat(event.params.datatokenAddress.toHexString()) .concat(dispenserID)
const dispenserTransaction = new DispenserTransaction(id) const dispenserTransaction = new DispenserTransaction(id)
const dispenser = getDispenser(event.params.datatokenAddress.toHex()) const dispenser = getDispenser(dispenserID)
dispenser.balance = dispenser.balance.minus( dispenser.balance = updateDispenserBalance(
event.params.amount.toBigDecimal() event.address,
event.params.datatokenAddress
) )
dispenser.save() dispenser.save()
dispenserTransaction.dispenser = dispenser.id dispenserTransaction.dispenser = dispenser.id
@ -79,11 +102,17 @@ export function handleTokensDispensed(event: TokensDispensed): void {
dispenserTransaction.createdTimestamp = event.block.timestamp.toI32() dispenserTransaction.createdTimestamp = event.block.timestamp.toI32()
dispenserTransaction.tx = event.transaction.hash.toHex() dispenserTransaction.tx = event.transaction.hash.toHex()
dispenserTransaction.block = event.block.number.toI32() dispenserTransaction.block = event.block.number.toI32()
const token = getToken(event.params.datatokenAddress, true)
dispenserTransaction.amount = weiToDecimal(
event.params.amount.toBigDecimal(),
token.decimals
)
dispenserTransaction.save() dispenserTransaction.save()
} }
export function handleOwnerWinthdraw(event: OwnerWithdrawed): void { export function handleOwnerWinthdraw(event: OwnerWithdrawed): void {
const dispenser = getDispenser(event.params.datatoken.toHex()) const dispenserID = getDispenserGraphID(event.address, event.params.datatoken)
const dispenser = getDispenser(dispenserID)
dispenser.balance = decimal.ZERO dispenser.balance = decimal.ZERO
dispenser.save() dispenser.save()
} }

View File

@ -14,18 +14,27 @@ import {
FixedRateExchangeSwap, FixedRateExchangeSwap,
FixedRateExchangeUpdate FixedRateExchangeUpdate
} from '../@types/schema' } from '../@types/schema'
import { getFixedRateExchange, getUpdateOrSwapId } from './utils/fixedRateUtils' import {
getFixedRateExchange,
getUpdateOrSwapId,
getFixedRateGraphID,
updateFixedRateExchangeSupply
} from './utils/fixedRateUtils'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { addFixedRateExchange } from './utils/globalUtils' import { addFixedRateExchange } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils' import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
export function handleExchangeCreated(event: ExchangeCreated): void { export function handleExchangeCreated(event: ExchangeCreated): void {
const fixedRateExchange = new FixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = new FixedRateExchange(fixedRateId)
const user = getUser(event.params.exchangeOwner.toHexString()) const user = getUser(event.params.exchangeOwner.toHexString())
fixedRateExchange.owner = user.id fixedRateExchange.owner = user.id
fixedRateExchange.contract = event.address.toHexString()
fixedRateExchange.exchangeId = event.params.exchangeId.toHexString()
fixedRateExchange.datatoken = getToken(event.params.datatoken, true).id fixedRateExchange.datatoken = getToken(event.params.datatoken, true).id
fixedRateExchange.baseToken = getToken(event.params.baseToken, false).id fixedRateExchange.baseToken = getToken(event.params.baseToken, false).id
@ -40,17 +49,18 @@ export function handleExchangeCreated(event: ExchangeCreated): void {
fixedRateExchange.save() fixedRateExchange.save()
addFixedRateExchange() addFixedRateExchange()
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
} }
export function handleRateChange(event: ExchangeRateChanged): void { export function handleRateChange(event: ExchangeRateChanged): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate( const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId( getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
) )
newExchangeUpdate.oldPrice = fixedRateExchange.price newExchangeUpdate.oldPrice = fixedRateExchange.price
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32() newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
@ -68,9 +78,11 @@ export function handleRateChange(event: ExchangeRateChanged): void {
} }
export function handleMintStateChanged(event: ExchangeMintStateChanged): void { export function handleMintStateChanged(event: ExchangeMintStateChanged): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
fixedRateExchange.withMint = event.params.withMint fixedRateExchange.withMint = event.params.withMint
fixedRateExchange.save() fixedRateExchange.save()
} }
@ -78,14 +90,13 @@ export function handleMintStateChanged(event: ExchangeMintStateChanged): void {
// TODO: implement fre updates/history for changes // TODO: implement fre updates/history for changes
export function handleActivated(event: ExchangeActivated): void { export function handleActivated(event: ExchangeActivated): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate( const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId( getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
) )
newExchangeUpdate.oldActive = fixedRateExchange.active newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = true newExchangeUpdate.newActive = true
@ -100,14 +111,13 @@ export function handleActivated(event: ExchangeActivated): void {
} }
export function handleDeactivated(event: ExchangeDeactivated): void { export function handleDeactivated(event: ExchangeDeactivated): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate( const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId( getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
) )
newExchangeUpdate.oldActive = fixedRateExchange.active newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = false newExchangeUpdate.newActive = false
@ -124,14 +134,13 @@ export function handleDeactivated(event: ExchangeDeactivated): void {
export function handleAllowedSwapperChanged( export function handleAllowedSwapperChanged(
event: ExchangeAllowedSwapperChanged event: ExchangeAllowedSwapperChanged
): void { ): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const newExchangeUpdate = new FixedRateExchangeUpdate( const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId( getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
) )
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32() newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
@ -147,21 +156,20 @@ export function handleAllowedSwapperChanged(
// TODO: implement market fee, opf fee // TODO: implement market fee, opf fee
export function handleSwap(event: Swapped): void { export function handleSwap(event: Swapped): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const swap = new FixedRateExchangeSwap( const swap = new FixedRateExchangeSwap(
getUpdateOrSwapId( getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
event.transaction.hash.toHex(),
event.params.exchangeId.toHex()
)
) )
swap.createdTimestamp = event.block.timestamp.toI32() swap.createdTimestamp = event.block.timestamp.toI32()
swap.tx = event.transaction.hash.toHex() swap.tx = event.transaction.hash.toHex()
swap.block = event.block.number.toI32() swap.block = event.block.number.toI32()
swap.exchangeId = event.params.exchangeId.toHex() swap.exchangeId = fixedRateId
swap.by = getUser(event.params.by.toHex()).id swap.by = getUser(event.params.by.toHex()).id
// we need to fetch the decimals of the base token // we need to fetch the decimals of the base token
@ -179,14 +187,17 @@ export function handleSwap(event: Swapped): void {
) )
swap.save() swap.save()
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
} }
export function handlePublishMarketFeeChanged( export function handlePublishMarketFeeChanged(
event: PublishMarketFeeChanged event: PublishMarketFeeChanged
): void { ): void {
const fixedRateExchange = getFixedRateExchange( const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHex() event.params.exchangeId.toHexString(),
event.address
) )
const fixedRateExchange = getFixedRateExchange(fixedRateId)
if (fixedRateExchange) { if (fixedRateExchange) {
fixedRateExchange.publishMarketFeeAddress = fixedRateExchange.publishMarketFeeAddress =
event.params.newMarketCollector.toHexString() event.params.newMarketCollector.toHexString()

View File

@ -1,18 +1,40 @@
import { Dispenser } from '../../@types/schema' import { Dispenser } from '../../@types/schema'
import { getToken } from './tokenUtils' import { getToken } from './tokenUtils'
import { Address } from '@graphprotocol/graph-ts' import { Address, BigDecimal } from '@graphprotocol/graph-ts'
import { weiToDecimal } from './generic'
import { Dispenser as DispenserContract } from '../../@types/Dispenser/Dispenser'
export function createDispenser(address: string): Dispenser { export function getDispenserGraphID(
const dispenser = new Dispenser(address) contractAddress: Address,
dispenser.token = getToken(Address.fromString(address), true).id datatokenAddress: Address
): string {
return contractAddress.toHexString() + '-' + datatokenAddress.toHexString()
}
export function createDispenser(dispenserID: string): Dispenser {
const dispenser = new Dispenser(dispenserID)
dispenser.save() dispenser.save()
return dispenser return dispenser
} }
export function getDispenser(address: string): Dispenser { export function getDispenser(dispenserID: string): Dispenser {
let dispenser = Dispenser.load(address) let dispenser = Dispenser.load(dispenserID)
if (dispenser === null) { if (dispenser === null) {
dispenser = createDispenser(address) dispenser = createDispenser(dispenserID)
} }
return dispenser return dispenser
} }
export function updateDispenserBalance(
contractAddress: Address,
datatokenAddress: Address
): BigDecimal {
const contract = DispenserContract.bind(contractAddress)
const dispenserDetails = contract.try_status(datatokenAddress)
if (dispenserDetails == null) return BigDecimal.fromString('0')
const token = getToken(datatokenAddress, true)
return weiToDecimal(
dispenserDetails.value.value5.toBigDecimal(),
token.decimals
)
}

View File

@ -1,9 +1,21 @@
import { FixedRateExchange } from '../../@types/schema' import { FixedRateExchange } from '../../@types/schema'
export function getFixedRateExchange(exchangeId: string): FixedRateExchange { import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/FixedRateExchange/FixedRateExchange'
let fixedRateExhange = FixedRateExchange.load(exchangeId) import { Address, Bytes } from '@graphprotocol/graph-ts'
import { getToken } from './tokenUtils'
import { weiToDecimal } from './generic'
export function getFixedRateGraphID(
exchangeId: string,
contractAddress: Address
): string {
return contractAddress.toHexString() + '-' + exchangeId
}
export function getFixedRateExchange(fixedRateId: string): FixedRateExchange {
let fixedRateExhange = FixedRateExchange.load(fixedRateId)
if (fixedRateExhange === null) { if (fixedRateExhange === null) {
fixedRateExhange = new FixedRateExchange(exchangeId) fixedRateExhange = new FixedRateExchange(fixedRateId)
// TODO: get data from contract and fill in new fixed rate exchange, this is just a worst case scenario. We shouldn't reach this code // TODO: get data from contract and fill in new fixed rate exchange, this is just a worst case scenario. We shouldn't reach this code
fixedRateExhange.save() fixedRateExhange.save()
} }
@ -11,6 +23,37 @@ export function getFixedRateExchange(exchangeId: string): FixedRateExchange {
return fixedRateExhange return fixedRateExhange
} }
export function updateFixedRateExchangeSupply(
exchangeId: Bytes,
contractAddress: Address
): void {
const fixedRateID =
contractAddress.toHexString() + '-' + exchangeId.toHexString()
const fixedRateExchange = getFixedRateExchange(fixedRateID)
const contract = FixedRateExchangeContract.bind(contractAddress)
const fixedRateDetails = contract.try_getExchange(exchangeId)
if (fixedRateDetails == null) return
const baseToken = getToken(fixedRateDetails.value.value3, false)
const datatoken = getToken(fixedRateDetails.value.value1, true)
fixedRateExchange.datatokenBalance = weiToDecimal(
fixedRateDetails.value.value9.toBigDecimal(),
datatoken.decimals
)
fixedRateExchange.baseTokenBalance = weiToDecimal(
fixedRateDetails.value.value10.toBigDecimal(),
baseToken.decimals
)
fixedRateExchange.datatokenSupply = weiToDecimal(
fixedRateDetails.value.value7.toBigDecimal(),
datatoken.decimals
)
fixedRateExchange.baseTokenSupply = weiToDecimal(
fixedRateDetails.value.value8.toBigDecimal(),
baseToken.decimals
)
fixedRateExchange.save()
}
export function getUpdateOrSwapId( export function getUpdateOrSwapId(
txAddress: string, txAddress: string,
exchangeId: string exchangeId: string