Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

allow full local testing with anvil integration #27

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

davisshaver
Copy link
Contributor

@davisshaver davisshaver commented Jun 25, 2022

Addresses #21. These changes should all be non-breaking, meaning that unless a user sets the new environmental variables, everything should work the same out of the box before and after merging this branch.

This PR:

  • Refactors CHAIN_ID to NEXT_PUBLIC_CHAIN_SLUGS so that the environment variable is available client side and allows for multiple chains to be specified
  • Introduces a NEXT_PUBLIC_CHAIN_STATUS environment variable allowing the chainStatus prop on <ConnectButton /> to be easily configured (this helps with UX when mainnet is included as a chain for ENS compatibility, but not meant to be used directly)
  • Introduces a NEXT_PUBLIC_CONTRACT_ADDRESS environment variable allowing the contract address to be switched without changing underlying code (this helps with local testing workflow)
  • Introduces a NEXT_PUBLIC_SUBGRAPH_URL environment variable allowing the subgraph URL to be explicitly specified without changing underlying code (if using a local graph node, for example)
  • Adds tests for the new getContractAddress(), getSubgraphURL(), getChainStatus(), and getChains() functions

Here are the .env.local settings that can be used for Foundry deployment.

DEPLOYER={ADDRESS}
DEPLOYER_PRIVATE_KEY={PRIVATE_KEY}
CHAIN_NAME=foundry
RPC_URL=http://127.0.0.1:8545
ETHERSCAN_API_KEY=

@davisshaver
Copy link
Contributor Author

I got the graph part of this working.

Screen Shot 2022-06-26 at 12 22 06 PM

Just in case I get hit by a 🚌, here are some notes on setup. I'll incorporate these into a readme before proposing the branch for merge.

  1. Follow the instructions to get a Graph Node running locally, but you can skip the parts about setting up the Gravatar example.
  2. Run Anvil with a custom gas limit set to match what the Graph Node will use, e.g. anvil --gas-limit 50000000. (Note: I'm not super sure why Graph Node uses 50 million as the hard coded limit, but this doesn't match the Anvil default value of 30 million. See discussion here: possible compatibility issue with running graph node against anvil bluealloy/revm#135)
  3. Start the ipfs daemon, as described in the Graph Node docs (ipfs daemon).
  4. Run the Graph Node locally, with something like cargo run -p graph-node --release -- --postgres-url postgresql://{YOUR_SYSTEM_USERNAME}:@localhost:5432/graph-node --ethereum-rpc foundry:http://127.0.0.1:8545 --ipfs 127.0.0.1:5001
  5. Deploy the contract to Anvil by setting your .env.local as described above and running bash deploy.sh in the contracts folder.
  6. Create the subgraph node with graph create --node http://127.0.0.1:8020 holic/example-nft
  7. Build/populate the subgraph with pnpm deploy:local in the subgraph folder

You should now be able to run pnpm dev, visit http://localhost:3000/, login with one of the Anvil accounts, mint an NFT, refresh the page, and see the new NFT in your inventory.

Some other notes: If you are doing test cycles you might need to jump into Postgres and drop the database (psql postgres and DROP DATABASE "graph-node";). Also I think you need to run pnpm build in the subgraph folder before deploying it but need to confirm this.

@rhlsthrm
Copy link

Hey I came across this from another thread. I still haven't been able to get a local graph node working with Anvil. The errors I get are:

Jun 28 16:23:18.218 WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #10) with result Err(Decoder error: Error("invalid length 1, expected a (both 0x-prefixed or not) hex string with length of 16", line: 0, column: 0)), provider: mainnet-rpc-0
Jun 28 16:23:19.159 WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #11) with result Err(Decoder error: Error("invalid length 1, expected a (both 0x-prefixed or not) hex string with length of 16", line: 0, column: 0)), provider: mainnet-rpc-0
Jun 28 16:23:20.856 WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #12) with result Err(Decoder error: Error("invalid length 1, expected a (both 0x-prefixed or not) hex string with length of 16", line: 0, column: 0)), provider: mainnet-rpc-0
Jun 28 16:23:23.110 WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #13) with result Err(Decoder error: Error("invalid length 1, expected a (both 0x-prefixed or not) hex string with length of 16", line: 0, column: 0)), provider: mainnet-rpc-0
Jun 28 16:23:26.481 WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #14) with result Err(Decoder error: Error("invalid length 1, expected a (both 0x-prefixed or not) hex string with length of 16", line: 0, column: 0)), provider: mainnet-rpc-0
Jun 28 16:23:42.260 WARN Trying again after eth_getBlockByNumber(0, false) RPC call failed (attempt #15) with result Err(Decoder error: Error("invalid length 1, expected a (both 0x-prefixed or not) hex string with length of 16", line: 0, column: 0)), provider: mainnet-rpc-0

Can you tell me how you got past this? I tried to use this docker-compose:

version: "3.3"

services:
  chain-1337:
    container_name: chain-1337-test
    environment:
      - MNEMONIC=$MNEMONIC
    image: ghcr.io/foundry-rs/foundry:latest
    ports:
      - "8547:8545"
    entrypoint: 'anvil --chain-id 1337 --mnemonic "$MNEMONIC" --host 0.0.0.0 --block-time 3 --verbosity'

  graph-node-1337:
    container_name: graph-node-1337-test
    image: graphprotocol/graph-node:v0.26.0
    ports:
      - "8010:8000"
      - "8001:8001"
      - "8020:8020"
      - "8030:8030"
      - "8040:8040"
    depends_on:
      - ipfs
      - postgres-1337
      - chain-1337
    environment:
      postgres_host: postgres-1337
      postgres_user: graph-node
      postgres_pass: let-me-in
      postgres_db: graph-node
      ipfs: "ipfs:5001"
      ethereum: "mainnet:http://chain-1337:8545"
      GRAPH_LOG: info

  postgres-1337:
    container_name: postgres-1337-test
    image: postgres
    ports:
      - "5432:5432"
    command: ["postgres", "-cshared_preload_libraries=pg_stat_statements"]
    environment:
      POSTGRES_USER: graph-node
      POSTGRES_PASSWORD: let-me-in
      POSTGRES_DB: graph-node

  ipfs:
    container_name: ipfs-test
    image: ipfs/go-ipfs:v0.4.23
    ports:
      - "5001:5001"

@davisshaver
Copy link
Contributor Author

@rhlsthrm Do you possibly have a typo in the port mapping for Foundry? Should it be 8545:8545 instead?

@rhlsthrm
Copy link

rhlsthrm commented Jun 29, 2022

@rhlsthrm Do you possibly have a typo in the port mapping for Foundry? Should it be 8545:8545 instead?

@davisshaver No, internally it uses the docker network port which is the internal 8545 anyways. This is just the external port mapping. This exact config works with a hardhat node.

@davisshaver
Copy link
Contributor Author

@rhlsthrm Where does the 8547 port value come from in the port mapping? That seems like a typo to me. Hardhat's default value is 8545 just like Forge.

@rhlsthrm
Copy link

@davisshaver this line right here:

ports:
      - "8547:8545"

maps the internal 8545 (the default port like you mentioned) to 8547 (this is just the exposed port on the host machine, this can be anything). This does not affect the internal ports on the node container, which is how the docker network refers to it. You can see more info here: https://docs.docker.com/compose/compose-file/#ports

@davisshaver
Copy link
Contributor Author

@rhlsthrm I see - well unfortunately I'm not sure what's going on, sorry I can't be more assistance. We are thinking about using the Docker container instead at some point so if we run into this issue I'll post notes here. Good luck!

@@ -1,4 +1,5 @@
import ExampleNFTGoerli from "@web3-scaffold/contracts/deploys/goerli/ExampleNFT.json";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to figure out a good way of allowing the chain to be specified here. 🤔

packages/app/.env Outdated Show resolved Hide resolved
@TheRightChoyce
Copy link
Contributor

TheRightChoyce commented Jul 5, 2022 via email

@davisshaver davisshaver marked this pull request as ready for review July 18, 2022 15:23
@rhlsthrm
Copy link

rhlsthrm commented Oct 11, 2022 via email

@nxcco
Copy link

nxcco commented Mar 27, 2023

Does anyone have a full working solution for how to connect anvil to the local graph node?

@davisshaver
Copy link
Contributor Author

@nxcco I haven't tried this in a while, but I should have some time in the next few days to test the steps I shared previously. I can let you know how that goes. In the meantime, have you tried giving them a run through? I'd be curious to know if you ran into the same issue as @rhlsthrm.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants