Skip to content

Commit

Permalink
Add integration tests for multiple subgraph datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
incrypto32 committed Dec 16, 2024
1 parent 065ca55 commit 6dfd085
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "testCommand",
"type": "string"
}
],
"name": "TestEvent",
"type": "event"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "subgraph-data-sources",
"version": "0.1.0",
"scripts": {
"codegen": "graph codegen --skip-migrations",
"create:test": "graph create test/subgraph-data-sources --node $GRAPH_NODE_ADMIN_URI",
"deploy:test": "graph deploy test/subgraph-data-sources --version-label v0.0.1 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.79.0-alpha-20240711124603-49edf22",
"@graphprotocol/graph-ts": "0.31.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type MirrorBlock @entity {
id: String!
number: BigInt!
hash: Bytes!
testMessage: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Entity, log, store } from '@graphprotocol/graph-ts';
import { MirrorBlock } from '../generated/schema';

export class EntityTrigger {
constructor(
public entityOp: u32,
public entityType: string,
public entity: Entity,
public vid: i64,
) {}
}

export function handleEntity(trigger: EntityTrigger): void {
let blockEntity = trigger.entity;
let blockNumber = blockEntity.getBigInt('number');
let blockHash = blockEntity.getBytes('hash');
let testMessage = blockEntity.get('testMessage');
let id = blockEntity.getString('id');

log.info('Block number: {}', [blockNumber.toString()]);

if (trigger.entityOp == 2) {
log.info('Removing block entity with id: {}', [id]);
store.remove('MirrorBlock', id);
return;
}

let block = loadOrCreateMirrorBlock(id);
block.number = blockNumber;
block.hash = blockHash;
if (testMessage) {
block.testMessage = testMessage.toString();
}

block.save();
}

export function loadOrCreateMirrorBlock(id: string): MirrorBlock {
let block = MirrorBlock.load(id);
if (!block) {
log.info('Creating new block entity with id: {}', [id]);
block = new MirrorBlock(id);
}

return block;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
specVersion: 1.3.0
schema:
file: ./schema.graphql
dataSources:
- kind: subgraph
name: Contract
network: test
source:
address: 'Qmaqf8cRxfxbduZppSHKG9DMuX5JZPMoGuwGb2DQuo48sq'
startBlock: 0
mapping:
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Gravatar
handlers:
- handler: handleEntity
entity: Block
- handler: handleEntity
entity: Block2
file: ./src/mapping.ts

0 comments on commit 6dfd085

Please sign in to comment.