Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DataSourceContext in manifest #4848

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions store/test-store/tests/chain/ethereum/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::Arc;
use std::time::Duration;

use graph::blockchain::DataSource;
use graph::data::store::Value;
use graph::data::subgraph::schema::SubgraphError;
use graph::data::subgraph::{SPEC_VERSION_0_0_4, SPEC_VERSION_0_0_7, SPEC_VERSION_0_0_8};
use graph::data_source::offchain::OffchainDataSourceKind;
Expand Down Expand Up @@ -372,6 +373,59 @@ specVersion: 0.0.2
})
}

#[tokio::test]
async fn parse_data_source_context() {
const YAML: &str = "
dataSources:
- kind: ethereum/contract
name: Factory
network: mainnet
context:
foo:
type: String
data: bar
qux:
type: Int
data: 42
source:
address: \"0x0000000000000000000000000000000000000000\"
abi: Factory
startBlock: 9562480
mapping:
kind: ethereum/events
apiVersion: 0.0.4
language: wasm/assemblyscript
entities:
- TestEntity
file:
/: /ipfs/Qmmapping
abis:
- name: Factory
file:
/: /ipfs/Qmabi
blockHandlers:
- handler: handleBlock
schema:
file:
/: /ipfs/Qmschema
specVersion: 0.0.8
";

let manifest = resolve_manifest(YAML, SPEC_VERSION_0_0_8).await;
let data_source = manifest
.data_sources
.iter()
.find_map(|ds| ds.as_onchain().cloned())
.unwrap();

let context = data_source.context.as_ref().clone().unwrap();
let sorted = context.sorted();

assert_eq!(sorted.len(), 2);
assert_eq!(sorted[0], ("foo".into(), Value::String("bar".into())));
assert_eq!(sorted[1], ("qux".into(), Value::Int(42)));
}

#[tokio::test]
async fn parse_block_handlers_with_polling_filter() {
const YAML: &str = "
Expand Down
15 changes: 15 additions & 0 deletions tests/runner-tests/data-sources/abis/Contract.abi
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"
}
]
13 changes: 13 additions & 0 deletions tests/runner-tests/data-sources/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "data-sources",
"version": "0.1.0",
"scripts": {
"codegen": "graph codegen --skip-migrations",
"create:test": "graph create test/data-sources --node $GRAPH_NODE_ADMIN_URI",
"deploy:test": "graph deploy test/data-sources --version-label v0.0.1 --ipfs $IPFS_URI --node $GRAPH_NODE_ADMIN_URI"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.57.0-alpha-20230831103613-4c8bdf8",
"@graphprotocol/graph-ts": "0.30.0"
}
}
6 changes: 6 additions & 0 deletions tests/runner-tests/data-sources/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type Data @entity {
id: ID!
foo: String
bar: Int
isTest: Boolean
}
15 changes: 15 additions & 0 deletions tests/runner-tests/data-sources/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BigInt, dataSource, ethereum, log } from "@graphprotocol/graph-ts";
import { Data } from "../generated/schema";

export function handleBlock(block: ethereum.Block): void {
let foo = dataSource.context().getString("foo");
let bar = dataSource.context().getI32("bar");
let isTest = dataSource.context().getBoolean("isTest");
if (block.number == BigInt.fromI32(0)) {
let data = new Data("0");
data.foo = foo;
data.bar = bar;
data.isTest = isTest;
data.save();
}
}
32 changes: 32 additions & 0 deletions tests/runner-tests/data-sources/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
specVersion: 0.0.8
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: Contract
network: test
context:
foo:
type: String
data: test
bar:
type: Int
data: 1
isTest:
type: Bool
data: true
source:
address: "0x0000000000000000000000000000000000000000"
abi: Contract
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Gravatar
abis:
- name: Contract
file: ./abis/Contract.abi
blockHandlers:
- handler: handleBlock
file: ./src/mapping.ts
1 change: 1 addition & 0 deletions tests/runner-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"workspaces": [
"data-source-revert",
"data-source-revert2",
"data-sources",
"dynamic-data-source",
"fatal-error",
"file-data-sources",
Expand Down
Loading