2021-11-10 13:47:44 +01:00
|
|
|
import { FixedRateExchange } from '../../@types/schema'
|
|
|
|
|
2022-02-21 11:45:24 +01:00
|
|
|
import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/templates/FixedRateExchange/FixedRateExchange'
|
2022-02-20 09:00:16 +01:00
|
|
|
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)
|
2021-11-10 13:47:44 +01:00
|
|
|
if (fixedRateExhange === null) {
|
2022-02-20 09:00:16 +01:00
|
|
|
fixedRateExhange = new FixedRateExchange(fixedRateId)
|
2022-02-15 17:13:55 +01:00
|
|
|
// 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
|
2021-11-10 13:47:44 +01:00
|
|
|
fixedRateExhange.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixedRateExhange
|
|
|
|
}
|
|
|
|
|
2022-02-20 09:00:16 +01:00
|
|
|
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)
|
2022-06-10 12:47:04 +02:00
|
|
|
|
|
|
|
if (fixedRateDetails == null || fixedRateDetails.reverted === true) return
|
2022-02-20 09:00:16 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-11-10 13:47:44 +01:00
|
|
|
export function getUpdateOrSwapId(
|
|
|
|
txAddress: string,
|
|
|
|
exchangeId: string
|
|
|
|
): string {
|
|
|
|
return `${txAddress}-${exchangeId}`
|
|
|
|
}
|