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 that deploy offchain in the layer 2. #53

Closed
wants to merge 5 commits into from
Closed
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
60 changes: 60 additions & 0 deletions packages/client/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Running Tests

This project includes unit tests and end-to-end (E2E) tests. Below are the instructions to run the E2E tests.

## Steps to Run E2E Tests

1. In the terminal, navigate to the contracts directory:
````cmd
yarn contracts build
````

2. Start the local Hardhat node:
````cmd
yarn hardhat node
````

3. With the local Hardhat node running, execute the E2E tests:
````cmd
yarn client test
````


# Running Tests with Local Arbitrum Node
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# Running Tests with Local Arbitrum Node
## Running Tests with Local Arbitrum Node


To run tests with a local Arbitrum node, follow the steps below:

## Deploying a Local Node

1. Open a new terminal for deploying the local node:

### Step 1: Install prerequisites

You'll need Docker and Docker Compose to run your node. Follow the instructions on their respective websites to install them.

### Step 2: Clone the nitro-testnode repo

You'll need the release branch.

````bash
git clone -b release --recurse-submodules https://github.com/OffchainLabs/nitro-testnode.git && cd nitro-testnode
````


### Step 3: Run your node

````bash
./test-node.bash --init
````

2. After deploying the local node, open another terminal and clone the external resolver from Blockful:

````bash
git clone https://github.com/blockful-io/external-resolver.git
`````

LeonardoVieira1630 marked this conversation as resolved.
Show resolved Hide resolved
3. Finally, run the tests:

````bash
yarn client hardhat test ./test/l2_e2e.spec.ts --network localhost
Copy link
Contributor

Choose a reason for hiding this comment

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

you could turn it into a script as well

Copy link
Member Author

Choose a reason for hiding this comment

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

I tryed to use a script, but it was not possible to run the node and the tests in parallel. Maybe we can work on this improviment later.

`````
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@blockful/client",
"packageManager": "yarn@4.1.0",
"scripts": {
"test": "vitest run",
"test": "yarn hardhat test ./test/arbitrum_e2e.spec.ts --network localhost",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"test": "yarn hardhat test ./test/arbitrum_e2e.spec.ts --network localhost",
"test:arb": "yarn hardhat test ./test/arbitrum_e2e.spec.ts --network localhost",

"test:watch": "vitest",
Copy link
Contributor

Choose a reason for hiding this comment

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

remove this line

"start": "nodemon src/index.ts --resolver 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
"lint": "eslint . --ext .ts --fix"
Expand Down
84 changes: 84 additions & 0 deletions packages/client/test/arbitrum_e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
This test script (e2e.spec.ts) aims to perform integrated testing of the project. It executes a series of actions,
including deploying the contracts (registry, offchain resolver, and universal resolver), creating the Client using Viem,
and initializing the gateway locally. After deploying and configuring the contracts, the Client can access
off-chain information during the tests. It's important to note that this initial test script only sets up the
environment and stops at the gateway call. It still requires implementing the connection between the gateway and
layer two, or the gateway and the database.
*/

// Importing abi and bytecode from contracts folder
import {
deployOffchainResolver,
deployRegistry,
deployUniversalResolver,
} from './utils'
import { ethers as eth } from 'hardhat'
import { Wallet } from 'ethers'
import { normalize } from 'viem/ens'
import { localhost } from 'viem/chains'
import { createTestClient, http, publicActions } from 'viem'
const gatewayUrl = 'http://127.0.0.1:3000'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const gatewayUrl = 'http://127.0.0.1:3000'
const gatewayUrl = 'http://127.0.0.1:3000/{sender}/{data}.json'


// Providers
const l2Provider = new eth.JsonRpcProvider('http://127.0.0.1:8547')
Copy link
Contributor

Choose a reason for hiding this comment

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

const l1Provider = new eth.JsonRpcProvider('http://127.0.0.1:8545')
const devAccountPrivateKey =
'0xb6b15c8cb491557369f3c7d2c287b053eb229daa9c22138887752191c9520659'
const devAccountPublicKey = '0x3f1Eae7D46d88F08fc2F8ed27FCb2AB183EB2d0E'

describe('SeuContrato', () => {
before(async () => {
const signers = await eth.getSigners()

const walletForL2 = new Wallet(devAccountPrivateKey)
Copy link
Contributor

Choose a reason for hiding this comment

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

const l2Signer = walletForL2.connect(l2Provider)
const l1Signer = await l1Provider.getSigner(devAccountPublicKey)

// Deploying the contracts
const offchainResolverContract = await deployOffchainResolver(
l2Signer,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be deployed on L1 and the PublicResolver on the L2

gatewayUrl,
signers as unknown as string,
Copy link
Contributor

Choose a reason for hiding this comment

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

double-check this conversion

)

const RegistryContract = await deployRegistry(
l1Signer,
await offchainResolverContract.getAddress(),
)

await deployUniversalResolver(
l1Signer,
await RegistryContract.getAddress(),
gatewayUrl,
)
})

/*
This test will fail when it reach the gateway
because the offchain part is not implemented yet.
*/
it('Call ENS flow with viem.', async () => {
// ENS address
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// ENS address

const ensAddress = normalize('public.eth')
// Getting Avatar from the ens address
const client = createTestClient({
chain: localhost,
mode: 'hardhat',
transport: http('http://127.0.0.1:8545'),
}).extend(publicActions)

// console.log('ok!')
// try {
// await client.getEnsAvatar({
// name: ensAddress,
// universalResolverAddress:
// (await UniversalResolverContract.getAddress()) as `0x${string}`,
// })
// } catch (error) {
// const customError = error as BaseError
// console.log(customError.message)
// // expect(customError.message).contain('HTTP request failed')
// }
})
})
90 changes: 90 additions & 0 deletions packages/client/test/utils.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

rename this file to deploys.ts

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { ethers as eth } from 'hardhat'
import { Contract, JsonRpcSigner, Wallet } from 'ethers'
import { labelhash, namehash } from 'viem/ens'
import {
abi as abiOffchainResolver,
bytecode as bytecodeOffchainResolver,
} from '@blockful/contracts/out/OffchainResolver.sol/OffchainResolver.json'
import {
abi as abiRegistry,
bytecode as bytecodeRegistry,
} from '@blockful/contracts/out/ENSRegistry.sol/ENSRegistry.json'
import {
abi as abiUniversalResolver,
bytecode as bytecodeUniversalResolver,
} from '@blockful/contracts/out/UniversalResolver.sol/UniversalResolver.json'

const root = eth.ZeroHash

// Function to deploy offchain resolver contract
export async function deployOffchainResolver(
Signer: Wallet,
gatewayUrl: string,
signers: string,
): Promise<Contract> {
const ResolverContract = await new eth.ContractFactory(
abiOffchainResolver,
bytecodeOffchainResolver,
Signer,
).deploy(gatewayUrl, signers)

const offchainResolver = await eth.getContractAt(
abiOffchainResolver,
await ResolverContract.getAddress(),
)
console.log('Offchain resolver: ', await ResolverContract.getAddress())
return offchainResolver
}

// Function to deploy registry contract
export async function deployRegistry(
Signer: Wallet | JsonRpcSigner,
offchainResolverAddress: string,
): Promise<Contract> {
const RegistryContract = await new eth.ContractFactory(
abiRegistry,
bytecodeRegistry,
Signer,
).deploy()

const registry = await eth.getContractAt(
abiRegistry,
await RegistryContract.getAddress(),
)
console.log('Registry: ', await RegistryContract.getAddress())

await (registry.connect(Signer) as Contract).setSubnodeRecord(
root,
labelhash('eth'),
Signer,
offchainResolverAddress,
10000000,
)
await (registry.connect(Signer) as Contract).setSubnodeRecord(
namehash('eth'),
labelhash('offchain'),
Signer,
offchainResolverAddress,
10000000,
)
return registry
}

// Function to deploy universal resolver contract
export async function deployUniversalResolver(
signer: Wallet | JsonRpcSigner,
registry: string,
gatewayUrl: string,
): Promise<void> {
const UniversalResolverContract = await new eth.ContractFactory(
abiUniversalResolver,
bytecodeUniversalResolver,
signer,
).deploy(registry, [gatewayUrl])

const universalResolver = await eth.getContractAt(
abiUniversalResolver,
await UniversalResolverContract.getAddress(),
)
console.log('universal resolver: ', await universalResolver.getAddress())
}
5 changes: 3 additions & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"test:watch": "forge test -vvv --watch",
"start": "anvil & copyfiles ../../.env contracts/.env && forge script script/OffchainResolver.s.sol --rpc-url http://localhost:8545 --broadcast -vv",
"postinstall": "forge install",
"lint": "solhint src/**/*.sol --fix --noPrompt"
"lint": "solhint src/**/*.sol --fix --noPrompt",
"build": "forge build"
},
"devDependencies": {
"copyfiles": "^2.4.1",
"solhint": "^4.1.1"
}
}
}