ocean-subgraph/src/mappings/fixedRateExchange.ts
Maria Carmina e1df119197
Fix #628 & #629 & #621: Store event log index for all records (#630)
* Store eventIndex.

* Changed in veDelegation. Removed import of String.

* linter.

* Removed tx hash.

* Revert some tweaks.

* added logIndex.

* converted to big int.

* 0 -> BigInt.zero().

* Added eventIndex to template and OPC.

* updated tweak

* updated tweak2

* Added eventIndex in tests.

* revert.

* Added eventIndex for TokenCreated and NFTCreated events. Increase time sleep for graphql request.

* Changed position of eventIndex in entity.

* Added eventIndex for order events and for dt events. Added new test commands in package.json.

* Added eventIndex for NFT Update events.

* Added eventIndex for dispenser.

* Modified dispenser tests.

* Reverted sleep secs. Updated tests.

* Updated dispenser tests. Added eventIndex for FRE.

* Added eventIndex in df rewards.

* Updated Ve with eventIndex.

* Updating order id with event index.

* Updated @ocean/lib with multichain version.

* Updated to number type.

* Updated tests for order.

* some changes.

* Fix dt tests. Added logs for provider fee. Need fix for Simple publish and consume tests.

* Problem raised by retrieving order event index in provider fee event handler.

* Added function for searching the right order.

* Debug order with provider fee.

* Removed console logs.

* Added debug logs.

* Hardcoded eventIndex just for testing.

* Paste logs from graph node.

* added extra sleep in test.

* fixed command.

* Added comments. Modified util function for Order Reuse.

* Added more logs and other tweaks.

* fixed command for docker logs.

* Added more logs.

* Converted to lowercase.

* Removed condition just for test.

* Added more logs. Order is not null.

* Call getOrder.

* Added verification back.

* converted to hex and add toString.

* Pass toString as param.

* Added ethereumjs util.

* replaced with ==.

* Refactored logic.

* Print event index.

* added more logs of event index.

* Modified tweaks and asserts.

* Added log for reuse order.

* updates.

* Reverted changes.

* Added check for eventIndex == 0.

* Enhanced the code. Replaced while with for.

* Added more logs for reuse test.

* added another condition.

* Added logs in tests. Removed redundant condition.

* Added more logs to test.

* Fixed typo.

* still debugging

* Refactored code.

* Getting closer.

* Fix tests for orders part. Removed logs.

* Removed more logs.

* Fix DT tests related to order.

* Implemented Publishing Market Fee event handler. Added test.

* Added consume market fee handler. Test consume market fee handler.

* Fixed tests for fees. Added TODOs.

* Generated ID with eventIndex in ficed rate. Added assert errors messages.

* Added FRE tests to workflow.

* Generated dispenser transaction with event index.

* Fixed dispenser tests.

* Add event index for NftUpdate entity ID.

* Changed IDs for VeDelegation and VeAllocationUpdate.

* Added full test suite to workflow.

* Fixed nft transfer ID.

* print blocks.

* test just dt and ending test to see the last block.

* Added df test.

* Added dispenser test.

* Added FRE test.

* Added NFT test.

* Added simple publish consume test.

* Added simple subgraph test.

* Added users test.

* Added ve test.

* Increased sleep time.

* commented delegation test.

* respect lint

* Fixed ending tests. Removed commented code. Brought back test suite.

* Print values from failing tests. Updated mappings.

* Rollback changes in veDelegation and veUtils. Added more prints for debug in tests.

* Removed veDelegation creation record.

* Rollback to the approach for veDelegation like it is in main.

* Removed prints. Match changes with main.

* Removed -1 from last block.

* Added prints for eventIndex. Fixed some suggestions.

* Fixed veDelegation.

* Removed prints.
2023-05-13 15:00:52 +03:00

280 lines
9.7 KiB
TypeScript

import { BigInt, Address, BigDecimal } from '@graphprotocol/graph-ts'
import {
ExchangeActivated,
ExchangeAllowedSwapperChanged,
ExchangeCreated,
ExchangeDeactivated,
ExchangeMintStateChanged,
ExchangeRateChanged,
Swapped,
PublishMarketFeeChanged,
TokenCollected
} from '../@types/templates/FixedRateExchange/FixedRateExchange'
import {
FixedRateExchange,
FixedRateExchangeSwap,
FixedRateExchangeUpdate
} from '../@types/schema'
import {
getFixedRateExchange,
getUpdateOrSwapId,
getFixedRateGraphID,
updateFixedRateExchangeSupply
} from './utils/fixedRateUtils'
import { weiToDecimal } from './utils/generic'
import { addFixedRateExchange, addFixedSwap } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
export function handleExchangeCreated(event: ExchangeCreated): void {
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
fixedRateExchange.datatokenSupply = BigDecimal.zero()
fixedRateExchange.baseTokenSupply = BigDecimal.zero()
fixedRateExchange.datatokenBalance = BigDecimal.zero()
fixedRateExchange.baseTokenBalance = BigDecimal.zero()
fixedRateExchange.totalSwapValue = BigDecimal.zero()
fixedRateExchange.active = false
fixedRateExchange.price = weiToDecimal(
event.params.fixedRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
fixedRateExchange.createdTimestamp = event.block.timestamp.toI32()
fixedRateExchange.tx = event.transaction.hash.toHex()
fixedRateExchange.eventIndex = event.logIndex.toI32()
fixedRateExchange.block = event.block.number.toI32()
fixedRateExchange.save()
addFixedRateExchange()
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
}
export function handleRateChange(event: ExchangeRateChanged): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const eventIndex: number = event.logIndex.toI32()
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId, eventIndex)
)
newExchangeUpdate.oldPrice = fixedRateExchange.price
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.eventIndex = event.logIndex.toI32()
newExchangeUpdate.block = event.block.number.toI32()
newExchangeUpdate.exchangeId = fixedRateId
fixedRateExchange.price = weiToDecimal(
event.params.newRate.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
newExchangeUpdate.newPrice = fixedRateExchange.price
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleMintStateChanged(event: ExchangeMintStateChanged): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
fixedRateExchange.withMint = event.params.withMint
fixedRateExchange.eventIndex = event.logIndex.toI32()
fixedRateExchange.save()
}
export function handleActivated(event: ExchangeActivated): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const eventIndex: number = event.logIndex.toI32()
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId, eventIndex)
)
newExchangeUpdate.exchangeId = fixedRateId
newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = true
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.eventIndex = event.logIndex.toI32()
newExchangeUpdate.block = event.block.number.toI32()
fixedRateExchange.active = true
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleDeactivated(event: ExchangeDeactivated): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const eventIndex: number = event.logIndex.toI32()
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId, eventIndex)
)
newExchangeUpdate.oldActive = fixedRateExchange.active
newExchangeUpdate.newActive = false
newExchangeUpdate.exchangeId = fixedRateId
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.eventIndex = event.logIndex.toI32()
newExchangeUpdate.block = event.block.number.toI32()
fixedRateExchange.active = false
newExchangeUpdate.save()
fixedRateExchange.save()
}
export function handleAllowedSwapperChanged(
event: ExchangeAllowedSwapperChanged
): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const eventIndex: number = event.logIndex.toI32()
const newExchangeUpdate = new FixedRateExchangeUpdate(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId, eventIndex)
)
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
newExchangeUpdate.tx = event.transaction.hash.toHex()
newExchangeUpdate.eventIndex = event.logIndex.toI32()
newExchangeUpdate.block = event.block.number.toI32()
newExchangeUpdate.oldAllowedSwapper = fixedRateExchange.allowedSwapper
newExchangeUpdate.exchangeId = fixedRateId
fixedRateExchange.allowedSwapper = event.params.allowedSwapper.toHex()
newExchangeUpdate.newAllowedSwapper = fixedRateExchange.allowedSwapper
newExchangeUpdate.save()
fixedRateExchange.save()
}
// TODO: implement market fee, opf fee
export function handleSwap(event: Swapped): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
const eventIndex: number = event.logIndex.toI32()
const swap = new FixedRateExchangeSwap(
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId, eventIndex)
)
swap.createdTimestamp = event.block.timestamp.toI32()
swap.tx = event.transaction.hash.toHex()
swap.eventIndex = event.logIndex.toI32()
swap.block = event.block.number.toI32()
swap.exchangeId = fixedRateId
swap.by = getUser(event.params.by.toHex()).id
// we need to fetch the decimals of the base token
const baseToken = getToken(
Address.fromString(fixedRateExchange.baseToken),
false
)
swap.baseTokenAmount = weiToDecimal(
event.params.baseTokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
swap.dataTokenAmount = weiToDecimal(
event.params.datatokenSwappedAmount.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
// Track fees
swap.oceanFeeAmount = weiToDecimal(
event.params.oceanFeeAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
swap.marketFeeAmount = weiToDecimal(
event.params.marketFeeAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
swap.consumeMarketFeeAmount = weiToDecimal(
event.params.consumeMarketFeeAmount.toBigDecimal(),
BigInt.fromI32(baseToken.decimals).toI32()
)
swap.save()
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
if (event.params.tokenOutAddress.toHexString() == fixedRateExchange.baseToken)
addFixedSwap(
event.params.tokenOutAddress.toHexString(),
swap.dataTokenAmount
)
else addFixedSwap(fixedRateExchange.baseToken, swap.baseTokenAmount)
// update datatoken lastPriceToken and lastPriceValue
const datatoken = getToken(
Address.fromString(fixedRateExchange.datatoken),
true
)
const priceToken = getToken(
Address.fromString(fixedRateExchange.baseToken),
false
)
datatoken.lastPriceToken = priceToken.id
datatoken.lastPriceValue = fixedRateExchange.price
datatoken.save()
}
export function handlePublishMarketFeeChanged(
event: PublishMarketFeeChanged
): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
if (fixedRateExchange) {
fixedRateExchange.publishMarketFeeAddress =
event.params.newMarketCollector.toHexString()
fixedRateExchange.publishMarketSwapFee = weiToDecimal(
event.params.swapFee.toBigDecimal(),
BigInt.fromI32(18).toI32()
)
fixedRateExchange.eventIndex = event.logIndex.toI32()
fixedRateExchange.save()
}
}
export function handleTokenCollected(event: TokenCollected): void {
const fixedRateId = getFixedRateGraphID(
event.params.exchangeId.toHexString(),
event.address
)
const fixedRateExchange = getFixedRateExchange(fixedRateId)
if (event.params.token.toHexString() == fixedRateExchange.baseToken) {
const baseToken = getToken(event.params.token, false)
fixedRateExchange.baseTokenBalance =
fixedRateExchange.baseTokenBalance.minus(
weiToDecimal(event.params.amount.toBigDecimal(), baseToken.decimals)
)
fixedRateExchange.eventIndex = event.logIndex.toI32()
fixedRateExchange.save()
}
}