diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 229fd14..39facb4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -76,6 +76,7 @@ jobs: npm run test-fixed npm run test-dispenser npm run test-nft + npm run test-ve env: ADDRESS_FILE: /home/runner/.ocean/ocean-contracts/artifacts/address.json BARGE_FOLDER: /home/runner/.ocean/ diff --git a/schema.graphql b/schema.graphql index fbbb787..688cf6d 100644 --- a/schema.graphql +++ b/schema.graphql @@ -461,7 +461,7 @@ enum veAllocationUpdateType { } type VeAllocationUpdate @entity { - "{tx}-{VeAllocation id}" + "{tx}-{VeAllocation id}-{eventIndex}" id: ID! veAllocation: VeAllocation! @@ -475,7 +475,7 @@ type VeAllocationUpdate @entity { } type VeDelegation @entity { - "id = tokenId" + "{tx}-{tokenId}-{eventIndex}" id: ID! delegator: VeOCEAN! receiver: VeOCEAN! @@ -486,6 +486,7 @@ type VeDelegation @entity { block: Int! timestamp: Int! tx: String! + eventIndex: Int! } type VeOCEAN @entity { diff --git a/src/mappings/utils/veUtils.ts b/src/mappings/utils/veUtils.ts index 0f06e86..58a1067 100644 --- a/src/mappings/utils/veUtils.ts +++ b/src/mappings/utils/veUtils.ts @@ -106,10 +106,11 @@ export function writeveAllocationUpdate( allocationType: string, amount: BigDecimal ): VeAllocationUpdate { - const tx = event.transaction.hash.toHex() - let allocationUpdate = VeAllocationUpdate.load(tx + '-' + veAllocationId) + const eventIndex: number = event.logIndex.toI32() + const id = `${event.transaction.hash.toHex()}-${veAllocationId}-${eventIndex}` + let allocationUpdate = VeAllocationUpdate.load(id) if (allocationUpdate === null) { - allocationUpdate = new VeAllocationUpdate(tx + '-' + veAllocationId) + allocationUpdate = new VeAllocationUpdate(id) allocationUpdate.veAllocation = veAllocationId allocationUpdate.type = allocationType allocationUpdate.allocatedTotal = amount @@ -125,22 +126,47 @@ export function writeveAllocationUpdate( return allocationUpdate } -export function getveDelegation(id: string): VeDelegation { - let veDelegation = VeDelegation.load(id) - - if (veDelegation === null) { - veDelegation = new VeDelegation(id) - veDelegation.cancelTime = BigInt.zero() - veDelegation.expireTime = BigInt.zero() - veDelegation.tokenId = BigInt.zero() - veDelegation.amount = BigInt.zero() - veDelegation.receiver = '' - veDelegation.delegator = '' - veDelegation.block = 0 - veDelegation.timestamp = 0 - veDelegation.tx = '' - veDelegation.save() +export function getveDelegation( + transactionHash: string, + tokenId: string, + eventIndex: number +): VeDelegation | null { + for (let i = eventIndex; i >= 0; i--) { + const id = `${transactionHash}-${tokenId}-${i}` + const veDelegation = VeDelegation.load(id) + if (veDelegation) { + return veDelegation + } } + + // if (veDelegation === null) { + // veDelegation = new VeDelegation(id) + // veDelegation.cancelTime = BigInt.zero() + // veDelegation.expireTime = BigInt.zero() + // veDelegation.tokenId = BigInt.zero() + // veDelegation.amount = BigInt.zero() + // veDelegation.receiver = '' + // veDelegation.delegator = '' + // veDelegation.block = 0 + // veDelegation.timestamp = 0 + // veDelegation.tx = '' + // veDelegation.save() + // } + return null +} + +export function createDefaultVeDelegation(id: string): VeDelegation { + const veDelegation = new VeDelegation(id) + veDelegation.cancelTime = BigInt.zero() + veDelegation.expireTime = BigInt.zero() + veDelegation.tokenId = BigInt.zero() + veDelegation.amount = BigInt.zero() + veDelegation.receiver = '' + veDelegation.delegator = '' + veDelegation.block = 0 + veDelegation.timestamp = 0 + veDelegation.tx = '' + veDelegation.save() return veDelegation } diff --git a/src/mappings/veDelegation.ts b/src/mappings/veDelegation.ts index fd082e6..d5a1805 100644 --- a/src/mappings/veDelegation.ts +++ b/src/mappings/veDelegation.ts @@ -5,7 +5,11 @@ import { ExtendBoost, TransferBoost } from '../@types/veDelegation/veDelegation' -import { getveDelegation, getveOCEAN } from './utils/veUtils' +import { + createDefaultVeDelegation, + getveDelegation, + getveOCEAN +} from './utils/veUtils' export function handleDelegation(event: DelegateBoost): void { const _delegator = event.params._delegator.toHex() @@ -14,8 +18,14 @@ export function handleDelegation(event: DelegateBoost): void { const _amount = event.params._amount const _cancelTime = event.params._cancel_time const _expireTime = event.params._expire_time - - const veDelegation = getveDelegation(_tokenId.toHex()) + const eventIndex: number = event.logIndex.toI32() + const tx = event.transaction.hash.toHex() + let veDelegation = getveDelegation(tx, _tokenId.toHex(), eventIndex) + if (!veDelegation) { + veDelegation = createDefaultVeDelegation( + `${tx}-${_tokenId.toHex()}-${eventIndex}` + ) + } veDelegation.delegator = _delegator getveOCEAN(_receiver) veDelegation.receiver = _receiver @@ -26,6 +36,7 @@ export function handleDelegation(event: DelegateBoost): void { veDelegation.block = event.block.number.toI32() veDelegation.timestamp = event.block.timestamp.toI32() veDelegation.tx = event.transaction.hash.toHex() + veDelegation.eventIndex = event.logIndex.toI32() veDelegation.save() } @@ -36,8 +47,15 @@ export function handleExtendBoost(event: ExtendBoost): void { const _amount = event.params._amount const _cancelTime = event.params._cancel_time const _expireTime = event.params._expire_time + const eventIndex: number = event.logIndex.toI32() + const tx = event.transaction.hash.toHex() + let veDelegation = getveDelegation(tx, _tokenId.toHex(), eventIndex) + if (!veDelegation) { + veDelegation = createDefaultVeDelegation( + `${tx}-${_tokenId.toHex()}-${eventIndex}` + ) + } - const veDelegation = getveDelegation(_tokenId.toHex()) veDelegation.delegator = _delegator veDelegation.receiver = _receiver veDelegation.tokenId = _tokenId @@ -46,6 +64,7 @@ export function handleExtendBoost(event: ExtendBoost): void { veDelegation.expireTime = _expireTime veDelegation.timestamp = event.block.timestamp.toI32() veDelegation.tx = event.transaction.hash.toHex() + veDelegation.eventIndex = event.logIndex.toI32() veDelegation.save() } @@ -62,7 +81,15 @@ export function handleBurnBoost(event: BurnBoost): void { const _tokenId = event.params._token_id // delete - const veDelegation = getveDelegation(_tokenId.toHex()) + const eventIndex: number = event.logIndex.toI32() + const tx = event.transaction.hash.toHex() + let veDelegation = getveDelegation(tx, _tokenId.toHex(), eventIndex) + if (!veDelegation) { + veDelegation = createDefaultVeDelegation( + `${tx}-${_tokenId.toHex()}-${eventIndex}` + ) + } veDelegation.amount = BigInt.zero() + veDelegation.eventIndex = event.logIndex.toI32() veDelegation.save() } diff --git a/test/integration/VeOcean.test.ts b/test/integration/VeOcean.test.ts index dd2b754..2ecdf22 100644 --- a/test/integration/VeOcean.test.ts +++ b/test/integration/VeOcean.test.ts @@ -706,7 +706,8 @@ describe('veOcean tests', async () => { tokenId, amount, cancelTime, - expireTime + expireTime, + eventIndex } }` } @@ -716,8 +717,15 @@ describe('veOcean tests', async () => { body: JSON.stringify(delegateQuery) }) const json = await delegateResponse.json() - console.log('json', json) - console.log('json?.data?.veDelegations', json?.data?.veDelegations) + console.log('json: ', json) assert(json?.data?.veDelegations, 'No veDelegations') + assert( + json?.data?.veDelegations.eventIndex !== null, + 'Invalid eventIndex for veDelegation' + ) + assert( + json?.data?.veDelegations.id !== null, + 'Invalid eventIndex for veDelegation' + ) }) })