holic / web3-scaffold

Quickly get up and running on web3
148 stars 19 forks source link

allow full local testing with anvil integration #27

Open davisshaver opened 2 years ago

davisshaver commented 2 years ago

Addresses https://github.com/holic/web3-scaffold/issues/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:

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 commented 2 years ago

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: https://github.com/bluealloy/revm/issues/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 commented 2 years ago

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 commented 2 years ago

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

rhlsthrm commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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!

TheRightChoyce commented 2 years ago

I started working on some docs that explain each ENV var and outline the steps needed to start customizing to use with your own contract (based on my experience). I’ll send a PR through tomorrow with it!

On Mon, Jul 4, 2022 at 8:31 PM Kevin Ingersoll @.***> wrote:

@.**** commented on this pull request.

In packages/app/.env https://github.com/holic/web3-scaffold/pull/27#discussion_r913306127:

@@ -1,2 +1,4 @@ -CHAIN_ID=5 +NEXT_PUBLIC_CHAIN_SLUGS= +NEXT_PUBLIC_CHAIN_STATUS= +NEXT_PUBLIC_GRAPH_HOST=

It might simplify things and make things a little more explicit to just put the whole URL here, e.g. NEXT_PUBLIC_SUBGRAPH_URL= https://api.thegraph.com/subgraphs/name/holic/example-nft

And we can make that the default value here in .env so folks start off with a clean + working subgraph, with instructions for pointing at their own URL (local or otherwise)

— Reply to this email directly, view it on GitHub https://github.com/holic/web3-scaffold/pull/27#pullrequestreview-1027945531, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEVN3FJVHDBOXDX3K6WM7TVSN66LANCNFSM5Z23TR2A . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Founder & CTO: https://therightchoyce.com Co-Creator & DJ: https://adventuresbk.com Music & Events: http://djchoycehacks.com

rhlsthrm commented 2 years ago

It shouldn’t matter because the subgraph is accessing the node through the internal container endpoint. This exact config works with a Hardhat node. On Jun 28, 2022, at 15:13, Davis Shaver @.***> wrote: @rhlsthrm Do you possibly have a typo in the port mapping for Foundry? Should it be 8545:8545 instead?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

nxcco commented 1 year ago

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

davisshaver commented 1 year ago

@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.