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: 4337 function #2

Merged
merged 10 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
8 changes: 8 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
lib
dist
jest.config.js
webpack.config.js
.eslintrc.js
cypress
cypress.config.js
test/black_box
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Run Tests

on:
pull_request:
types: [opened, synchronize, ready_for_review]

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 16

- name: Install Dependencies
run: yarn

- name: Lint
run: yarn lint

- name: Build
run: yarn build

- name: Unit Tests
run: yarn test

- name: End-to-End Tests (Firefox)
run: yarn test:e2e:firefox

- name: Black Box Tests
run: yarn test:black-box

- name: Generate Coverage Report
run: yarn test:coverage
13 changes: 12 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# General
.DS_Store
*.swp
*.orig
node_modules
lib
lib
.env
# Logs
*.log
.coverage
# IDE directories
.idea
tmp
33 changes: 29 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
Web3.js EIP4337 Plugin
===========
# Web3.js EIP4337 Plugin

This plugin is developed and maintained by [Blocto](https://blocto.io/) .

This is a [web3.js](https://github.com/web3/web3.js) `4.x` plugin for Account Abstraction functionality.

## Run the tests

1. Clone the repo
2. Run `yarn` to install dependencies
- If you receive the following warning, please remove the file `package-lock.json` and make sure to run `yarn` to install dependencies instead of `npm i`:

```console
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
```

3. Run the tests:
- `yarn test:unit`: Runs the mocked tests that do not make a network request using the [Jest](https://jestjs.io/) framework
- End-to-end tests: Runs Webpack bundled tests that make a network request to the RPC provider `https://rpc.ankr.com/eth` and returns an actual response from `MainnetPriceFeeds.LinkEth` smart contract using the [Cypress](https://www.cypress.io/) framework
- `yarn test:e2e:chrome`: Runs the tests using Chrome
- `yarn test:e2e:electron`: Runs the tests using Electron
- `yarn test:e2e:firefox`: Runs the tests using Firefox
- Black box tests: Uses a published version of the plugin from [Verdaccio](https://verdaccio.org/) to run tests that make a network request to the RPC provider `https://rpc.ankr.com/eth` and returns an actual response from `MainnetPriceFeeds.LinkEth` smart contract using the [Jest](https://jestjs.io/) framework
- NOTE The black box tests are setup to run within Github actions environment, but can be ran locally. The [black_box_test_helpers.sh](https://github.com/ChainSafe/web3.js-plugin-chainlink/blob/master/scripts/black_box_test_helpers.sh) script can be used to:
- `start`: Start Verdaccio using a Docker container
- `stop`: Kill the Docker container
- `startBackgroundAndPublish`: Starts a headless Docker container and publishes the plugin package
- `runTests`: `cd`s into the `test/black_box` directory, installs the black box package dependencies, and runs `yarn test` which will use Jest to run the tests
- In addition to the `black_box_test_helpers.sh` script, the black box tests can be ran using the following `package.json` scripts:
1. `yarn pre-black-box`: Calls `startBackgroundAndPublish` from the `black_box_test_helpers.sh` script
2. `yarn test:black-box`: Calls `yarn pre-black-box` and `runTests` from the from the `black_box_test_helpers.sh` script
3. `yarn post-black-box`: Calls `stop` from the `black_box_test_helpers.sh` script

License
-------
## License

[MIT](https://choosealicense.com/licenses/mit/)
15 changes: 15 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const config = {
screenshotOnRunFailure: false,
video: false,
e2e: {
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
return require('./cypress/plugins/index.js')(on, config);
},
specPattern: 'test/e2e/**/**/*.test.ts',
excludeSpecPattern: ['test/black_box/**'],
},
};

module.exports = config;
25 changes: 25 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference types="cypress" />
// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

const webpackPreprocessor = require('@cypress/webpack-preprocessor');
const webpackOptions = require('../webpack.config.js');

/**
* @type {Cypress.PluginConfig}
*/
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
on('file:preprocessor', webpackPreprocessor({ webpackOptions }));
return config;
};
43 changes: 43 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ***********************************************************
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Alternatively you can use CommonJS syntax:
// require('./commands')

import 'cypress-jest-adapter';

// Enable the hook to match Jest
global.beforeAll = global.before;
global.afterAll = global.after;

// In e2e tests we just need to use jest mocking API
global.jest = {
fn: global.cy.stub,
spyOn: global.cy.spy,
};

global.it = it;
global.test = it;
global.it.each = data => (describe, test) => {
const prs = [];
for (const d of data) {
if (Array.isArray(d)) {
prs.push(global.it(describe, test.bind(undefined, ...d)));
} else {
prs.push(global.it(describe, test.bind(undefined, d)));
}
}
return Promise.all(prs);
};
6 changes: 6 additions & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["cypress"]
}
}
26 changes: 26 additions & 0 deletions cypress/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const webpack = require('webpack');

module.exports = {
mode: 'development',
resolve: {
extensions: ['.ts', '.js'],
},
module: {
rules: [
{
test: /\.ts$/,
exclude: [/node_modules/],
use: [
{
loader: 'ts-loader',
},
],
},
],
},
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
}),
],
};
30 changes: 23 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,46 @@
"scripts": {
"lint": "eslint '{src,test}/**/*.ts'",
"build": "tsc --project tsconfig.build.json",
"test": "jest --config=./test/jest.config.js"
"test": "jest --config=./test/unit/jest.config.js",
"pre-black-box": "./scripts/black_box_test_helpers.sh startBackgroundAndPublish",
"test:black-box": "yarn pre-black-box && ./scripts/black_box_test_helpers.sh runTests",
"test:coverage": "jest --config=./test/unit/jest.config.js --coverage=true --coverage-reporters=text",
"test:e2e:chrome": "cypress run --headless --browser chrome",
"test:e2e:firefox": "cypress run --headless --browser firefox",
"test:e2e:electron": "cypress run --headless --browser electron",
"test:all": "yarn && yarn build && yarn test && yarn run test:e2e:firefox && yarn run test:black-box"
jdevcs marked this conversation as resolved.
Show resolved Hide resolved
},
"contributors": [
],
"contributors": [],
"license": "MIT",
"repository": {
"type": "git",
"url": "[email protected]:web3/web3.js-plugin-eip4337.git"
},
"dependencies": {
},
"devDependencies": {
"@blocto/sdk": "^0.7.2",
"@chainsafe/eslint-config": "^2.0.0",
"@cypress/webpack-preprocessor": "^5.15.5",
"@types/jest": "^29.5.2",
"@types/node": "^20.2.6",
"cypress": "^12.3.0",
"cypress-jest-adapter": "^0.1.1",
"eslint": "8",
"jest": "^29.5.0",
"jest-extended": "^4.0.0",
"npm-auth-to-token": "^1.0.0",
"process": "^0.11.10",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.3",
"web3": "^4.1.1"
"web3": "^4.2.0",
"web3-eth-abi": "^4.1.3",
"web3-utils": "^4.0.7",
"web3-validator": "^2.0.3",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1"
},
"peerDependencies": {
"web3": ">= 4.1.1"
"web3": ">= 4.2.0"
}
}
81 changes: 81 additions & 0 deletions scripts/black_box_test_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

ORIGARGS=("$@")

helpFunction() {
echo "Usage: $0 [start|stop|startBackgroundAndPublish|runTests] [background]"
exit 1 # Exit script after printing help
}

start() {
. scripts/env.sh
if [[ ${ORIGARGS[1]} == "background" ]]; then
startBackground
else
echo "Starting verdaccio..."
docker run -it --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio
fi
}

startBackground() {
echo "Starting verdaccio in background..."
docker run -d --rm --name verdaccio -p 4873:4873 verdaccio/verdaccio
}

stop() {
echo "Stopping verdaccio ..."
docker ps -q --filter ancestor="verdaccio/verdaccio" | xargs -r docker stop
}

createVerdaccioNPMUser() {
curl -XPUT \
-H "Content-type: application/json" \
-d '{ "name": "test", "password": "test" }' \
'http://localhost:4873/-/user/org.couchdb.user:test'
}

loginNPMUser() {
npx npm-auth-to-token \
-u test \
-p test \
-e [email protected] \
-r http://localhost:4873
}

yarnPublish() {
yarn publish \
--new-version 1.0.0 \
--no-git-tag-version \
--tag blackbox \
--registry http://localhost:4873
}

publish() {
echo "Publishing to verdaccio ..."

npx wait-port -t 60000 4873

createVerdaccioNPMUser
loginNPMUser
yarn build
yarnPublish
}

startBackgroundAndPublish() {
startBackground && publish
}

runTests() {
cd test/black_box
yarn --update-checksums
yarn install
yarn test
}

case $1 in
start) start ;;
stop) stop ;;
startBackgroundAndPublish) startBackgroundAndPublish ;;
runTests) runTests ;;
*) helpFunction ;; # Print helpFunction in case parameter is non-existent
esac
Loading
Loading