Skip to content

Commit

Permalink
✨ Fallthrough RPC provider (#90)
Browse files Browse the repository at this point in the history
* setup static class exporting for provider prep

* ignore from docusaurus

* scaffold the fallthrough provider - needs tests

* broken

* partial progress

* step zero

* implement basic fallthrough behavior

* timeout post at 8 seconds

* reduce long string hex bloat

* Customize timeout duration

* mutex the current rpc url selection

* speed up husky update scripts

* document fallthroughprovider

* cleanup docs

* Move all jsonrpc to shared base provider

* polyfill performance
  • Loading branch information
dawsbot authored Apr 26, 2022
1 parent 2655153 commit f99f2fc
Show file tree
Hide file tree
Showing 22 changed files with 578 additions and 372 deletions.
28 changes: 20 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@ethersproject/keccak256": "^5.4.0",
"@types/body-parser": "^1.19.1",
"@types/eslint": "^8.4.1",
"@types/express": "^4.17.13",
Expand All @@ -52,8 +51,9 @@
"jest": "^27.5.1",
"jest-dev-server": "^6.0.3",
"just-omit": "^2.0.1",
"lint-staged": "^12.4.0",
"lint-staged": "^12.4.1",
"npm-run-all": "^4.1.5",
"perf_hooks": "^0.0.1",
"prettier": "^2.6.2",
"prettier-plugin-organize-imports": "^2.3.4",
"ts-jest": "^27.1.4",
Expand Down
23 changes: 22 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,31 @@ import { JsonRpcProvider } from 'essential-eth';
const essentialEth = new JsonRpcProvider(
'RPC URL HERE' /* Try POKT or Infura */,
);
// OR for very quick testing (limited to 500 requests)
// OR for very quick testing (limited to 10,000 requests)
const essentialEth = new JsonRpcProvider();
```

<details>
<summary>Want a redundant provider that handles outages?</summary>

```typescript
import { FallthroughProvider } from 'essential-eth';

// The FallthroughProvider handles falling through to the next valid URL.
// It's dynamic to never trust one URL again when it fails * until it has tried all other provided URLs
// The default timeout for a request is 8 seconds after which it moves to the next URL
const provider = new FallthroughProvider([
'https://bad.com',
'https://free-eth-node.com/api/eth',
]);
provider.getGasPrice().toNumber();
/*
39695942769
*/
```

</details>

<br/>

#### [`getTransactionCount(address, blockTag?): Promise<number>`](https://essential-eth.vercel.app/docs/api/classes/JsonRpcProvider#gettransactioncount)
Expand Down
2 changes: 1 addition & 1 deletion scripts/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
npx npm-check-updates -u "/.*lint.*$/"
npx npm-check-updates -u "/.*eslint.*$/"
npx npm-check-updates -u "/.*big.*$/"
npm i
npm i ethers@latest typescript@latest web3@latest prettier@latest
Expand Down
5 changes: 3 additions & 2 deletions src/classes/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class BaseContract {
: null;
const req = async (): Promise<string> => {
return await post(
this._provider._rpcUrl,
this._provider.selectRpcUrl(),
buildRPCPostBody('eth_call', [
{
to: this._address.toLowerCase(),
Expand Down Expand Up @@ -101,8 +101,9 @@ export function defineReadOnly<T>(object: T, name: string, value: any): void {
}

/**
* @alpha
* Only accepts ABIS in JSON format. This allows for stronger typing and assurances of data-types
* @alpha Only read-only function calls supported.
* * Only read-only function calls currently supported.
* @example
* ```typescript
* import { Contract, JsonRpcProvider } from 'essential-eth';
Expand Down
2 changes: 1 addition & 1 deletion src/classes/test/Contract/crv.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Contract as EthersContract } from '@ethersproject/contracts';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { TinyBig } from '../../..';
import { JsonRpcProvider } from '../../../providers/JsonRpcProvider';
import { JsonRpcProvider } from '../../../index';
import { rpcUrls } from '../../../providers/test/rpc-urls';
import { Contract as EssentialEthContract } from '../../Contract';
import { abi } from './crv-abi';
Expand Down
2 changes: 1 addition & 1 deletion src/classes/test/Contract/ens.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contract as EthersContract } from '@ethersproject/contracts';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { JsonRpcProvider } from '../../../providers/JsonRpcProvider';
import { JsonRpcProvider } from '../../../index';
import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { ensABI } from './ens-abi';
Expand Down
2 changes: 1 addition & 1 deletion src/classes/test/Contract/fei.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Contract as EthersContract } from '@ethersproject/contracts';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { TinyBig } from '../../..';
import { JsonRpcProvider } from '../../../providers/JsonRpcProvider';
import { JsonRpcProvider } from '../../../index';
import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { feiABI } from './fei-abi';
Expand Down
2 changes: 1 addition & 1 deletion src/classes/test/Contract/uni.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contract as EthersContract } from '@ethersproject/contracts';
import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { JsonRpcProvider } from '../../../providers/JsonRpcProvider';
import { JsonRpcProvider } from '../../../index';
import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { uniswapABI } from './uniswap-abi';
Expand Down
6 changes: 2 additions & 4 deletions src/classes/utils/encode-decode-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import { tinyBig, toChecksumAddress } from '../..';
import { ContractTypes, JSONABIArgument } from '../../types/Contract.types';
import { hexToDecimal } from './hex-to-decimal';

const hexTrue =
'0000000000000000000000000000000000000000000000000000000000000001';
const hexFalse =
'0000000000000000000000000000000000000000000000000000000000000000';
export const hexFalse = '0'.repeat(64);
const hexTrue = '0'.repeat(63) + '1';

function expandType(type: ContractTypes) {
// https://docs.soliditylang.org/en/v0.8.7/types.html#integers
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Contract } from './classes/Contract';
import { FallthroughProvider } from './providers/FallthroughProvider';
import { JsonRpcProvider, jsonRpcProvider } from './providers/JsonRpcProvider';
import { tinyBig, TinyBig } from './shared/tiny-big/tiny-big';
import { BlockResponse } from './types/Block.types';
Expand All @@ -24,6 +25,7 @@ export {
isAddress,
jsonRpcProvider,
JsonRpcProvider,
FallthroughProvider,
tinyBig,
toChecksumAddress,
weiToEther,
Expand Down
Loading

1 comment on commit f99f2fc

@vercel
Copy link

@vercel vercel bot commented on f99f2fc Apr 26, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

essential-eth – ./

essential-eth-earnifi.vercel.app
essential-eth.vercel.app
essential-eth-git-master-earnifi.vercel.app

Please sign in to comment.