Skip to content

Commit

Permalink
Merge pull request #6 from neonlabsorg/NDEV-3235-add-auth-to-graph
Browse files Browse the repository at this point in the history
Ndev 3235 add auth to graph
  • Loading branch information
kristinaNikolaevaa authored Aug 28, 2024
2 parents 0e3941a + bdc5cfb commit f3f6f50
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 1 deletion.
19 changes: 18 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
workflow_dispatch:
inputs:
accounts:
type: string
default: "0x5743d2f08145f4fb7565323da8830f16c1990a26a601ead1013d72b867140f12"
required: true
description: "Accounts list"
repository_dispatch:
types: [integration-tests]

Expand Down Expand Up @@ -42,13 +46,26 @@ jobs:

- name: Install lld, jq and libpq-dev
run: sudo apt-get install -y lld jq libpq-dev protobuf-compiler

- name: Set accounts list for tests
run: |
echo "NEON_ACCOUNTS=$(
if [ ${{ github.event_name }} == 'workflow_dispatch' ]; then
echo ${{ inputs.accounts }}
else
echo ${{ github.event.client_payload.accounts }}
fi
)" >> $GITHUB_ENV
- name: Run integration tests
uses: actions-rs/cargo@v1
env:
N_CONCURRENT_TESTS: "1"
NEON_ACCOUNTS: ${{ github.event.client_payload.accounts }}
NEON_ACCOUNTS: ${{env.NEON_ACCOUNTS}}
SUBGRAPH_NAME: ${{ github.run_id }}
API_KEY: "${{ secrets.API_KEY }}"
SECURED_GRAPH_NODE_ADMIN_URI: "https://graph-secured.neontest.xyz/deploy"
SECURED_IPFS_URI: "https://ipfs-secured.neontest.xyz "
IPFS_URI: "https://ch-ipfs.neontest.xyz"
GRAPH_NODE_ADMIN_URI: "https://ch2-graph.neontest.xyz/deploy/"
with:
Expand Down
33 changes: 33 additions & 0 deletions tests/integration-tests/neon-auth/abis/Contract.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint16",
"name": "x",
"type": "uint16"
}
],
"name": "Trigger",
"type": "event"
},
{
"inputs": [
{
"internalType": "uint16",
"name": "x",
"type": "uint16"
}
],
"name": "emitTrigger",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
27 changes: 27 additions & 0 deletions tests/integration-tests/neon-auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "neon-auth",
"version": "0.1.0",
"scripts": {
"build-contracts": "../../common/build-contracts.sh",
"codegen": "graph codegen --skip-migrations",
"test": "yarn build-contracts && truffle test --compile-none --network test",
"neon": "yarn build-contracts && yarn truffle test --compile-none --network neonlabs",
"auth:test": "graph auth $SECURED_GRAPH_NODE_ADMIN_URI $API_KEY",
"create:test": "graph create $SUBGRAPH_NAME --node $SECURED_GRAPH_NODE_ADMIN_URI",
"deploy:test": "graph deploy $SUBGRAPH_NAME --version-label v0.0.1 --ipfs $SECURED_IPFS_URI --node $SECURED_GRAPH_NODE_ADMIN_URI --headers='{\"Authorization\": \"Bearer '$API_KEY'\"}'"
},
"devDependencies": {
"@graphprotocol/graph-cli": "0.52.0-alpha-20230628121316-48231a7",
"@graphprotocol/graph-ts": "0.31.0",
"solc": "^0.8.2"
},
"dependencies": {
"@truffle/contract": "^4.3",
"@truffle/hdwallet-provider": "^1.2",
"apollo-fetch": "^0.7.0",
"babel-polyfill": "^6.26.0",
"babel-register": "^6.26.0",
"gluegun": "^4.6.1",
"truffle": "^5.2"
}
}
27 changes: 27 additions & 0 deletions tests/integration-tests/neon-auth/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type Foo @entity {
id: ID!
value: Int!
}

type Initialize @entity {
id: ID!
block: BigInt!
}

type Block @entity {
id: ID!
number: BigInt!
hash: Bytes!
}

type BlockFromPollingHandler @entity {
id: ID!
number: BigInt!
hash: Bytes!
}

type BlockFromOtherPollingHandler @entity {
id: ID!
number: BigInt!
hash: Bytes!
}
80 changes: 80 additions & 0 deletions tests/integration-tests/neon-auth/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts';
import { Contract, Trigger } from '../generated/Contract/Contract';
import {
BlockFromOtherPollingHandler,
BlockFromPollingHandler,
Block,
Foo,
Initialize,
} from '../generated/schema';
import { ContractTemplate } from '../generated/templates';

export function handleBlock(block: ethereum.Block): void {
log.info('handleBlock {}', [block.number.toString()]);
let blockEntity = new Block(block.number.toString());
blockEntity.number = block.number;
blockEntity.hash = block.hash;
blockEntity.save();

if (block.number == BigInt.fromI32(2)) {
ContractTemplate.create(
Address.fromString('0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48')
);
}
}

export function handleBlockPolling(block: ethereum.Block): void {
log.info('handleBlockPolling {}', [block.number.toString()]);
let blockEntity = new BlockFromPollingHandler(block.number.toString());
blockEntity.number = block.number;
blockEntity.hash = block.hash;
blockEntity.save();
}

export function handleBlockPollingFromTemplate(block: ethereum.Block): void {
log.info('===> handleBlockPollingFromTemplate {}', [block.number.toString()]);
let blockEntity = new BlockFromOtherPollingHandler(block.number.toString());
blockEntity.number = block.number;
blockEntity.hash = block.hash;
blockEntity.save();
}

export function handleTrigger(event: Trigger): void {
// We set the value to 0 to test that the subgraph
// runs initialization handler before all other handlers
if (event.params.x == 1) {
let entity = Foo.load('initialize');

// If the intialization handler is called first
// this would set the value to -1 for Foo entity with id 0
// If it is not called first then the value would be 0
if (entity != null) {
entity.value = -1;
entity.id = 'initialize';
entity.save();
}
}

let obj = new Foo(event.params.x.toString());
obj.id = event.params.x.toString();
obj.value = event.params.x;

obj.save();
}

export function initialize(block: ethereum.Block): void {
log.info('initialize called at block', [block.number.toString()]);
let entity = new Initialize(block.number.toString());
entity.id = block.number.toString();
entity.block = block.number;
entity.save();

// If initialization handler is called then this would set
// the value to 0 for Foo entity with id 0
// This is to test that initialization handler is called
// before all other handlers
let foo = new Foo('initialize');
foo.id = 'initialize';
foo.value = 0;
foo.save();
}
63 changes: 63 additions & 0 deletions tests/integration-tests/neon-auth/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
specVersion: 0.0.6
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: Contract
network: neonlabs
source:
address: "0x0000000000000000000000000000000000000000"
abi: Contract
startBlock: 247101047
mapping:
kind: ethereum/events
apiVersion: 0.0.6
language: wasm/assemblyscript
abis:
- name: Contract
file: ./abis/Contract.abi
entities:
- Call
eventHandlers:
- event: Trigger(uint16)
handler: handleTrigger
blockHandlers:
- handler: handleBlockPolling
file: ./src/mapping.ts
- kind: ethereum/contract
name: BlockHandlerTest
network: neonlabs
source:
address: "0x0000000000000000000000000000000000000000"
abi: Contract
startBlock: 247101047
mapping:
kind: ethereum/events
apiVersion: 0.0.6
language: wasm/assemblyscript
abis:
- name: Contract
file: ./abis/Contract.abi
entities:
- Call
blockHandlers:
- handler: handleBlock
file: ./src/mapping.ts
templates:
- kind: ethereum/contract
name: ContractTemplate
network: neonlabs
source:
abi: Contract
mapping:
kind: ethereum/events
apiVersion: 0.0.6
language: wasm/assemblyscript
entities:
- Gravatar
abis:
- name: Contract
file: ./abis/Contract.abi
blockHandlers:
- handler: handleBlockPollingFromTemplate
file: ./src/mapping.ts
Loading

0 comments on commit f3f6f50

Please sign in to comment.