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

@ -294,12 +294,16 @@ type User @entity {
type FixedRateExchange @entity {
"fixed rate exchange id"
id: ID!
contract: String!
exchangeId: String!
owner: User!
datatoken: Token!
baseToken: Token!
"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"
baseTokenSupply: BigDecimal!
datatokenBalance: BigDecimal!
baseTokenBalance: BigDecimal!
price: BigDecimal!
active: Boolean!

View File

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

View File

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

View File

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

View File

@ -1,18 +1,40 @@
import { Dispenser } from '../../@types/schema'
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 {
const dispenser = new Dispenser(address)
dispenser.token = getToken(Address.fromString(address), true).id
export function getDispenserGraphID(
contractAddress: Address,
datatokenAddress: Address
): string {
return contractAddress.toHexString() + '-' + datatokenAddress.toHexString()
}
export function createDispenser(dispenserID: string): Dispenser {
const dispenser = new Dispenser(dispenserID)
dispenser.save()
return dispenser
}
export function getDispenser(address: string): Dispenser {
let dispenser = Dispenser.load(address)
export function getDispenser(dispenserID: string): Dispenser {
let dispenser = Dispenser.load(dispenserID)
if (dispenser === null) {
dispenser = createDispenser(address)
dispenser = createDispenser(dispenserID)
}
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'
export function getFixedRateExchange(exchangeId: string): FixedRateExchange {
let fixedRateExhange = FixedRateExchange.load(exchangeId)
import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/FixedRateExchange/FixedRateExchange'
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) {
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
fixedRateExhange.save()
}
@ -11,6 +23,37 @@ export function getFixedRateExchange(exchangeId: string): FixedRateExchange {
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(
txAddress: string,
exchangeId: string