Skip to content

Commit

Permalink
Merge pull request #3 from morpho-labs/feat/create-fallback-provider
Browse files Browse the repository at this point in the history
Feat/create fallback provider
  • Loading branch information
julien-devatom committed May 8, 2023
2 parents d9df7b1 + fcdf708 commit 0e748b9
Show file tree
Hide file tree
Showing 10 changed files with 3,144 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Set the repository to show as TypeScript rather than JS in GitHub
*.js linguist-detectable=false
*.js linguist-detectable=false
39 changes: 39 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
😀 Wonderful! Thank you for opening a pull request.
Please fill in the information below to expedite the review
and (hopefully) merge of your change.
-->

### Description of change

<!--
Please be clear and concise what the change is intended to do,
why this change is needed, and how you've verified that it
corrects what you intended.
In some cases it may be helpful to include the current behavior
and the new behavior.
If the change is related to an open issue, you can link it here.
If you include `Fixes #0000` (replacing `0000` with the issue number)
when this is merged it will automatically mark the issue as fixed and
close it.
-->

### Pull-Request Checklist

<!--
Please make sure to review and check all of the following.
If an item is not applicable, you can add "N/A" to the end.
-->

- [ ] This pull request links relevant issues as `Fixes #0000`
- [ ] There are new or updated unit tests validating the change
- [ ] Documentation has been updated to reflect this change
- [ ] The new commits follow conventions outlined in the [conventional commit spec](https://www.conventionalcommits.org/en/v1.0.0/)

<!--
🎉 Thank you for contributing!
-->
83 changes: 83 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
name: Continuous integration

on:
push:
branches:
- main
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: yarn

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build package
run: yarn build

test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
needs: build
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: yarn

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Test package
run: yarn test

release:
runs-on: ubuntu-latest
needs: [build, test]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node.js 18.x
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: yarn

- name: Install dependencies
run: yarn --frozen-lockfile

- name: Build package
run: yarn build

- name: Publish package
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
lib

*.tsbuildinfo
27 changes: 27 additions & 0 deletions ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: "🐛 Bug Report"
about: Report a reproducible bug or regression.
title: ''
labels: bug
assignees: ''

---

## Current Behavior

<!-- Describe how the issue manifests. -->

## Expected Behavior

<!-- Describe what the desired behavior would be. -->

## Steps to Reproduce the Problem

1.
1.
1.

## Environment

- Version: <!-- Version set on npm -->
- Node.js Version: <!-- Output of running `node -v` -->
34 changes: 34 additions & 0 deletions ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
name: 🌈 Feature request
about: Suggest an amazing new idea for this project
title: ''
labels: enhancement
assignees: ''

---

## Feature Request

**Is your feature request related to a problem? Please describe.**
<!-- A clear and concise description of what the problem is. Ex. I have an issue when [...] -->

**Describe the solution you'd like**
<!-- A clear and concise description of what you want to happen. Add any considered drawbacks. -->

**Describe alternatives you've considered**
<!-- A clear and concise description of any alternative solutions or features you've considered. -->

## Are you willing to resolve this issue by submitting a Pull Request?

<!--
Remember that first-time contributors are welcome! 🙌
-->

- [ ] Yes, I have the time, and I know how to start.
- [ ] Yes, I have the time, but I don't know how to start. I would need guidance.
- [ ] No, I don't have the time, although I believe I could do it if I had the time...
- [ ] No, I don't have the time and I wouldn't even know how to start.

<!--
👋 Have a great day and thank you for the feature request!
-->
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
# ethers-fallback-provider
[![npm package][npm-img]][npm-url]
[![Build Status][build-img]][build-url]
[![Downloads][downloads-img]][downloads-url]
[![Issues][issues-img]][issues-url]
[![Commitizen Friendly][commitizen-img]][commitizen-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]

> Package providing a fallback provider based on `ethers` package, adding more resilience.
The provider fallbacks on multiple providers in case of failure, and returns the first successful result.

It throws an error if all providers failed.

The providers are called in the order they are passed to the constructor.

Contrary to the `FallbackProvider` provided by `ethers`, this one does not use all providers at the same time, but only one at a time.
The purpose is more to have resilience if one provider fails, rather than having a resilience on the result.

## Installation
```bash
npm install @morpho-labs/ethers-fallback-provider
```
or
```bash
yarn add @morpho-labs/ethers-fallback-provider
```

## Usage

```typescript
import FallbackProvider from '@morpho-labs/ethers-fallback-provider';

import {
InfuraProvider,
AlchemyProvider,
getDefaultProvider
} from "@ethersproject/providers";


const timeout = 1000; // 1 second, optionnal, default is 3000ms

const provider = new FallbackProvider([
new InfuraProvider('mainnet', 'your-api-key'),
new AlchemyProvider('mainnet', 'your-api-key'),
getDefaultProvider('mainnet')
]);

// You can now use the fallback provider as a classic provider
const blockNumber = await provider.getBlockNumber();

```


[build-img]: https://github.com/morpho-labs/ethers-fallback-provider/actions/workflows/ci.yml/badge.svg?branch=main
[build-url]: https://github.com/morpho-labs/ethers-fallback-provider/actions/workflows/ci.yml
[downloads-img]: https://img.shields.io/npm/dt/ethers-multicall-provider
[downloads-url]: https://www.npmtrends.com/ethers-multicall-provider
[npm-img]: https://img.shields.io/npm/v/ethers-multicall-provider
[npm-url]: https://www.npmjs.com/package/ethers-multicall-provider
[issues-img]: https://img.shields.io/github/issues/morpho-labs/ethers-fallback-provider
[issues-url]: https://github.com/morpho-labs/ethers-fallback-provider/issues
[codecov-img]: https://codecov.io/gh/morpho-labs/ethers-fallback-provider/branch/main/graph/badge.svg
[codecov-url]: https://codecov.io/gh/morpho-labs/ethers-fallback-provider
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
[commitizen-url]: http://commitizen.github.io/cz-cli/
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "ethers-fallback-provider",
"name": "@morpho-labs/ethers-fallback-provider",
"version": "1.0.0",
"description": "Package providing a fallback provider based on ethers package, adding more resilience.",
"description": "Package providing a fallback provider based on ethers-providers, adding more resilience.",
"main": "lib/index.js",
"files": [
"lib/*"
],
"scripts": {
"build": "tsc --build ./tsconfig.build.json",
"lint": "eslint ./src --fix",
"lint": "eslint ./src ./test --fix",
"test:watch": "jest --watch",
"test": "jest",
"typecheck": "tsc --noEmit"
Expand All @@ -19,7 +19,7 @@
},
"license": "MIT",
"engines": {
"node": ">=12.0"
"node": ">=14.0"
},
"keywords": [
"ethers",
Expand Down
4 changes: 4 additions & 0 deletions src/FallbackProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
import { FallbackProvider } from "./FallbackProvider";

export * from "./FallbackProvider";

export default FallbackProvider;

0 comments on commit 0e748b9

Please sign in to comment.