Skip to content

Commit

Permalink
fix(subgraph): adding more tests to avoid regressions and assemblyscr…
Browse files Browse the repository at this point in the history
…ipt does not like required fields to be null
  • Loading branch information
julien51 committed May 17, 2023
1 parent ee7b3e5 commit c8942bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 2 additions & 2 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type Receipt @entity {
"Sender of the transaction"
sender: String!
"Payer in the case of an ERC20 lock renewal, the sender and payer might differ"
payer: String!
payer: String
"Address of the Lock smart contract"
lockAddress: String!
"Address of the 'currency' ERC20 contract if the keys are priced using an ERC20"
Expand All @@ -115,5 +115,5 @@ type Receipt @entity {
"Total gas paid"
gasTotal: BigInt!
"Increasing number of receipt"
receiptNumber: BigInt
receiptNumber: BigInt!
}
2 changes: 1 addition & 1 deletion subgraph/src/receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function createReceipt(event: ethereum.Event): void {

// save receipt, but only if we have a payer
// (i.e. this is a paid transaction)
if (receipt.payer && receipt.amountTransferred > BigInt.fromI32(0)) {
if (receipt.payer !== null && receipt.amountTransferred > BigInt.fromI32(0)) {
// Updating the lock object
const newReceiptNumber = lock.numberOfReceipts.plus(BigInt.fromI32(1))
lock.numberOfReceipts = newReceiptNumber
Expand Down
44 changes: 44 additions & 0 deletions subgraph/tests/receipts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,47 @@ describe('Receipts for base currency locks', () => {
assert.fieldEquals('Receipt', hash, 'amountTransferred', `${amount}`)
})
})

describe('Receipts for an ERC20 locks', () => {
beforeEach(() => {
dataSourceMock.resetValues()
clearStore()
})

test('Receipt has not been created for transfers without an ERC20 transfer', () => {
mockDataSourceV11()

// create fake ETH lock in subgraph
const lock = new Lock(lockAddress)
lock.address = Bytes.fromHexString(lockAddress)
lock.tokenAddress = Bytes.fromHexString(tokenAddress)
lock.price = BigInt.fromU32(keyPrice)
lock.lockManagers = [Bytes.fromHexString(lockManagers[0])]
lock.version = BigInt.fromU32(12)
lock.totalKeys = BigInt.fromU32(0)
lock.deployer = Bytes.fromHexString(lockManagers[0])
lock.numberOfReceipts = BigInt.fromU32(0)
lock.save()

// transfer event
const newTransferEvent = createTransferEvent(
Address.fromString(nullAddress),
Address.fromString(keyOwnerAddress),
BigInt.fromU32(tokenId)
)
newTransferEvent.transaction.value = BigInt.fromU32(0) // This is a grantKeys transaction
handleTransfer(newTransferEvent)

const hash = newTransferEvent.transaction.hash.toHexString()
const timestamp = newTransferEvent.block.timestamp.toString()
const msgSender = newTransferEvent.transaction.from.toHexString()
const amount = newTransferEvent.transaction.value

// key is there
assert.entityCount('Key', 1)
assert.fieldEquals('Key', keyID, 'tokenId', `${tokenId}`)

// there is no receipt
assert.entityCount('Receipt', 0)
})
})

0 comments on commit c8942bb

Please sign in to comment.