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

feat: test env graphman #155

Draft
wants to merge 2 commits into
base: test/viem
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion packages/ens-test-env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"files": [
"src"
],
"types": "src/index.d.ts",
"types": "src/execute.d.ts",
"module": "src/execute.js",
"scripts": {
"prepublish": "cp ../../LICENSE ./",
"ver": "pnpm version --no-workspaces-update"
Expand Down
19 changes: 19 additions & 0 deletions packages/ens-test-env/src/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[store]
[store.primary]
connection = "postgresql://graph-node:let-me-in@postgres:5432/graph-node"
pool_size = 10

[deployment]
[[deployment.rule]]
indexers = ["index_node_1"]

[chains]
ingestor = "index_0"
[chains.mainnet]
shard = "primary"
provider = [
{ label = "mainnet-rpc-0", url = "http://anvil:8545", features = [
"archive",
"traces",
] },
]
2 changes: 2 additions & 0 deletions packages/ens-test-env/src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ services:
GRAPH_ETHEREUM_CLEANUP_BLOCKS: 'true'
extra_hosts:
- 'host.docker.internal:host-gateway'
volumes:
- $GRAPH_CONFIG_FILE:/opt/graph-node/config.toml
ipfs:
image: ipfs/kubo:v0.16.0
ports:
Expand Down
23 changes: 23 additions & 0 deletions packages/ens-test-env/src/execute.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable */
const compose = require('docker-compose')
const path = require('path')

const rewindSubgraph = async ({ blockNumber, blockHash }) => {
const dockerComposeFile = path.resolve(__dirname, './docker-compose.yml')
const composeOpts = {
log: true,
composeOptions: ['-p', 'ens-test-env'],
config: dockerComposeFile,
}
const items = await compose.ps({ ...composeOpts, log: false })
if (items.data.services.length === 0) {
throw new Error('No services running')
}
await compose.exec(
'graph-node',
`graphman --config /opt/graph-node/config.toml rewind ${blockHash} ${blockNumber} graphprotocol/ens --force --sleep 1`,
composeOpts,
)
}

module.exports.rewindSubgraph = rewindSubgraph
8 changes: 8 additions & 0 deletions packages/ens-test-env/src/execute.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */
export const rewindSubgraph = async ({
blockNumber,
blockHash,
}: {
blockNumber: number
blockHash: `0x${string}`
}) => Promise<void>
24 changes: 24 additions & 0 deletions packages/ens-test-env/src/execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable */
import compose from 'docker-compose'
import path from 'path'
import { URL as URLClass } from 'url'

const __dirname = new URLClass('.', import.meta.url).pathname

export const rewindSubgraph = async ({ blockNumber, blockHash }) => {
const dockerComposeFile = path.resolve(__dirname, './docker-compose.yml')
const composeOpts = {
log: true,
composeOptions: ['-p', 'ens-test-env'],
config: dockerComposeFile,
}
const items = await compose.ps({ ...composeOpts, log: false })
if (items.data.services.length === 0) {
throw new Error('No services running')
}
await compose.exec(
'graph-node',
`graphman --config /opt/graph-node/config.toml rewind ${blockHash} ${blockNumber} graphprotocol/ens --force --sleep 1`,
composeOpts,
)
}
1 change: 1 addition & 0 deletions packages/ens-test-env/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ program
data: path.resolve(cwd, './data'),
archive: path.resolve(cwd, './archive'),
composeFile: path.resolve(__dirname, './docker-compose.yml'),
graphConfigFile: path.resolve(__dirname, './config.toml'),
}
const configPaths = config.paths || {}
for (const [key, value] of Object.entries(configPaths)) {
Expand Down
1 change: 1 addition & 0 deletions packages/ens-test-env/src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const main = async (_config, _options, justKill) => {
opts.env = {
...process.env,
DATA_FOLDER: config.paths.data,
GRAPH_CONFIG_FILE: config.paths.graphConfigFile,
GRAPH_LOG_LEVEL: 'info',
ANVIL_EXTRA_ARGS: '',
BLOCK_TIMESTAMP: Math.floor(new Date().getTime() / 1000),
Expand Down
7 changes: 6 additions & 1 deletion packages/ensjs/deploy/02_get_contract_addresses.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ const func = async function (hre) {
]),
)

const block = await hre.ethers.provider.getBlock()

await writeFile(
resolve(__dirname, '../.env.local'),
`DEPLOYMENT_ADDRESSES='${JSON.stringify(deploymentAddressMap)}'`,
`DEPLOYMENT_ADDRESSES='${JSON.stringify(deploymentAddressMap)}'
DEPLOYMENT_FINISHED_AT_BLOCK=${block.number}
DEPLOYMENT_FINISHED_AT_HASH=${block.hash}
`,
)
console.log('Wrote contract addresses to .env.local')

Expand Down
2 changes: 2 additions & 0 deletions packages/ensjs/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const config = {
'^(\\.{1,2}/.*)\\.js$': '$1',
'^multiformats$': '<rootDir>/node_modules/multiformats/src/index.js',
'^multiformats/(.*)$': '<rootDir>/node_modules/multiformats/src/$1',
'^@ensdomains/ens-test-env$':
'<rootDir>/node_modules/@ensdomains/ens-test-env/src/execute.cjs',
},
}

Expand Down
144 changes: 112 additions & 32 deletions packages/ensjs/src/test/usage/helper.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,121 @@
import { rewindSubgraph } from '@ensdomains/ens-test-env'
import getPrice from '../../functions/public/getPrice.js'
import commitName from '../../functions/wallet/commitName.js'
import registerName from '../../functions/wallet/registerName.js'
import { waitForTransaction } from '../addTestContracts.js'
import { walletClient } from '../addTestContracts.js'
import { publicClient } from '../addTestContracts.js'
import { testClient } from '../addTestContracts.js'
import { createSubgraphClient } from '../../subgraph.js'
import type { RegistrationParameters } from '../../utils/registerHelpers.js'
import {
publicClient,
testClient,
waitForTransaction,
walletClient,
} from '../addTestContracts.js'

// const accounts = await walletClient.getAddresses()
// type accountType = typeof accounts[0]
export const commitAndRegisterName = async (params: RegistrationParameters, account: any): Promise<string> => {
const commitTx = await commitName(walletClient, {
...params,
account: account,
})
expect(commitTx).toBeTruthy()
const commitReceipt = await waitForTransaction(commitTx)

expect(commitReceipt.status).toBe('success')

await testClient.increaseTime({ seconds: 61 })
await testClient.mine({ blocks: 1 })

const price = await getPrice(publicClient, {
nameOrNames: params.name,
duration: params.duration,
})
const total = price!.base + price!.premium

const tx = await registerName(walletClient, {
...params,
account: account,
value: total,
export const commitAndRegisterName = async (
params: RegistrationParameters,
account: any,
): Promise<string> => {
const commitTx = await commitName(walletClient, {
...params,
account,
})
expect(commitTx).toBeTruthy()
const commitReceipt = await waitForTransaction(commitTx)

expect(commitReceipt.status).toBe('success')

await testClient.increaseTime({ seconds: 61 })
await testClient.mine({ blocks: 1 })

const price = await getPrice(publicClient, {
nameOrNames: params.name,
duration: params.duration,
})
const total = price!.base + price!.premium

const tx = await registerName(walletClient, {
...params,
account,
value: total,
})
expect(tx).toBeTruthy()
const receipt = await waitForTransaction(tx)
expect(receipt.status).toBe('success')

return tx
}

export const wait = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms)
})

const subgraphClient = createSubgraphClient({ client: publicClient })

export const getSubgraphBlock = (
{ depth }: { depth: number } = { depth: 0 },
): Promise<number> =>
subgraphClient
.request<{
_meta: {
block: {
number: number
}
}
}>(
`
{
_meta {
block {
number
}
}
}
`,
)
.then((res) => res._meta.block.number)
.catch(async (e) => {
console.error(e)
await wait(1000)
if (depth > 5) throw new Error('Could not get subgraph block')
return getSubgraphBlock({ depth: depth + 1 })
})
expect(tx).toBeTruthy()
const receipt = await waitForTransaction(tx)
expect(receipt.status).toBe('success')

return tx
}
export const getCurrentBlock = () =>
publicClient.getBlockNumber().then((b) => Number(b))

export const syncSubgraphBlock = async (
{
depth,
changes,
prevSubgraphBlock,
}: { depth: number; changes: number; prevSubgraphBlock: number } = {
depth: 0,
changes: 0,
prevSubgraphBlock: 0,
},
): Promise<void> => {
const subgraphBlock = await getSubgraphBlock()
const currentBlock = await getCurrentBlock()
if (subgraphBlock < currentBlock) {
const params = {
depth: depth + 1,
changes: changes + (subgraphBlock !== prevSubgraphBlock ? 1 : 0),
prevSubgraphBlock: subgraphBlock,
}
await wait(1000)
return syncSubgraphBlock(params)
}
if (subgraphBlock >= currentBlock && depth === 0) {
const blockNumber = parseInt(
process.env.DEPLOYMENT_FINISHED_AT_BLOCK as string,
)
const blockHash = process.env.DEPLOYMENT_FINISHED_AT_HASH as `0x${string}`
await rewindSubgraph({ blockNumber, blockHash })
await wait(1000)
return syncSubgraphBlock()
}
return
}
Loading
Loading