Skip to content

Commit

Permalink
feature(subgraphs): Add creationTransactionHash to lock entity in s…
Browse files Browse the repository at this point in the history
…ubgraph (#15261)

* update schema

* update lock handler

* update test lock util

* update tests

* improve naming

* update tests
  • Loading branch information
0xTxbi authored and julien51 committed Dec 11, 2024
1 parent 6d02e56 commit f15cd65
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Lock @entity {
The primary reason for this role is to support additional purchase mechanisms beyond direct key purchases like credit-card purchases.
"""
keyGranters: [Bytes!]!
"transaction hash of the lock's creation"
creationTransactionHash: String!
}

type Key @entity {
Expand Down
3 changes: 2 additions & 1 deletion subgraph/src/unlock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */
/* eslint-disable prefer-const */
import { log, BigInt, ethereum } from '@graphprotocol/graph-ts'
import { log, BigInt } from '@graphprotocol/graph-ts'
import { NewLock, LockUpgraded, GNPChanged } from '../generated/Unlock/Unlock'
import { PublicLock as PublicLockMerged } from '../generated/templates/PublicLock/PublicLock'
import { PublicLock } from '../generated/templates'
Expand Down Expand Up @@ -105,6 +105,7 @@ export function handleNewLock(event: NewLock): void {
lock.createdAtBlock = event.block.number
lock.lastKeyMintedAt = null
lock.lastKeyRenewedAt = null
lock.creationTransactionHash = event.transaction.hash.toHexString()

if (version.le(BigInt.fromI32(8))) {
// prior to v8, add default lock manager
Expand Down
5 changes: 5 additions & 0 deletions subgraph/tests/locks-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export function createNewLockEvent(
): NewLock {
const newLockEvent = changetype<NewLock>(newMockEvent())

// Set a deterministic transaction hash for testing
newLockEvent.transaction.hash = Bytes.fromHexString(
'0x0000000000000000000000000000000000000000000000000000000000000001'
)

newLockEvent.parameters = []

newLockEvent.parameters.push(
Expand Down
6 changes: 6 additions & 0 deletions subgraph/tests/locks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ describe('Describe Locks events', () => {
assert.entityCount('Lock', 1)
assert.fieldEquals('Lock', lockAddress, 'address', lockAddress)
assert.fieldEquals('Lock', lockAddress, 'createdAtBlock', '1')
assert.fieldEquals(
'Lock',
lockAddress,
'creationTransactionHash',
'0x0000000000000000000000000000000000000000000000000000000000000001'
)
assert.fieldEquals('Lock', lockAddress, 'version', '11')
assert.fieldEquals('Lock', lockAddress, 'price', '1000')
assert.fieldEquals('Lock', lockAddress, 'name', 'My lock graph')
Expand Down
30 changes: 30 additions & 0 deletions subgraph/tests/receipts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ describe('Receipts for base currency locks', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

// transfer event
Expand Down Expand Up @@ -99,6 +102,9 @@ describe('Receipts for base currency locks', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

// create a key
Expand Down Expand Up @@ -149,6 +155,9 @@ describe('Receipts for base currency locks', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

// transfer event
Expand Down Expand Up @@ -182,6 +191,9 @@ describe('Receipts for base currency locks', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

// renew event
Expand Down Expand Up @@ -226,6 +238,9 @@ describe('Receipts for base currency locks', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

const key = new Key(`${lockAddress}-${tokenId}`)
Expand Down Expand Up @@ -290,6 +305,9 @@ describe('Receipts for an ERC20 locks', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

// transfer event
Expand Down Expand Up @@ -331,6 +349,9 @@ describe('Receipts for Cancel and refund', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

const key = new Key(`${lockAddress}-${tokenId}`)
Expand Down Expand Up @@ -407,6 +428,9 @@ describe('Receipts for Cancel and refund', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

const key = new Key(`${lockAddress}-${tokenId}`)
Expand Down Expand Up @@ -445,6 +469,9 @@ describe('Receipts for Cancel and refund', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

const key = new Key(`${lockAddress}-${tokenId}`)
Expand Down Expand Up @@ -521,6 +548,9 @@ describe('Receipts for Cancel and refund', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()

const key = new Key(`${lockAddress}-${tokenId}`)
Expand Down
3 changes: 3 additions & 0 deletions subgraph/tests/referrer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ describe('Referrer', () => {
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.numberOfCancelReceipts = BigInt.fromU32(0)
lock.creationTransactionHash =
'0x0000000000000000000000000000000000000000000000000000000000000001'
lock.createdAtBlock = BigInt.fromI32(1)
lock.save()
})

Expand Down

0 comments on commit f15cd65

Please sign in to comment.