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

🧼 Configure fallthrough contracts example #162

Merged
merged 1 commit into from
Sep 4, 2022
Merged
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
32 changes: 13 additions & 19 deletions src/classes/Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
decodeRPCResponse,
encodeData,
} from './utils/encode-decode-transaction';
import { buildRPCPostBody, post } from './utils/fetchers';
/**
*
* @param txnData
* @example
*/
Expand Down Expand Up @@ -71,19 +69,16 @@ export class BaseContract {
? estimateGas(data)
: null;
const req = async (): Promise<string> => {
return await post(
this._provider.selectRpcUrl(),
buildRPCPostBody('eth_call', [
{
to: this._address.toLowerCase(),
data,
// sometimes gas is defined in the ABI
...(decimalGas
? { gas: `0x${decimalGas.toString(16)}` }
: {}),
},
'latest',
]),
return await this._provider.call(
{
to: this._address.toLowerCase(),
data,
// sometimes gas is defined in the ABI
...(decimalGas
? { gas: `0x${decimalGas.toString(16)}` }
: {}),
},
'latest',
);
};
const nodeResponse = await req();
Expand All @@ -98,10 +93,7 @@ export class BaseContract {
/**
* Applies the unique contract's methods to the instantiated Contract in the constructor based-upon the provided ABI
*
* @param object
* @param name
* @param value
* @example
* @internal
*/
export function defineReadOnly<T>(object: T, name: string, value: any): void {
Object.defineProperty(object, name, {
Expand All @@ -121,6 +113,8 @@ export function defineReadOnly<T>(object: T, name: string, value: any): void {
* // UNI airdrop contract
* const contractAddress = '0x090D4613473dEE047c3f2706764f49E0821D256e';
* const provider = new JsonRpcProvider();
* // for more robust contract calls, provide a fallback:
* // const provider = new FallthroughProvider(['bad', 'https://free-eth-node.com/api/eth']);
*
* const JSONABI = [
* {
Expand Down
8 changes: 6 additions & 2 deletions 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 '../../../index';
import { FallthroughProvider } from '../../../index';
import { Contract as EssentialEthContract } from '../../Contract';
import { rpcUrls } from './../../../providers/test/rpc-urls';
import { ensABI } from './ens-abi';
Expand All @@ -10,7 +10,11 @@ const JSONABI = ensABI;

const rpcURL = rpcUrls.mainnet;
const ethersProvider = new StaticJsonRpcProvider(rpcURL);
const essentialEthProvider = new JsonRpcProvider(rpcURL);
const essentialEthProvider = new FallthroughProvider([
'nope',
'https://flash-the-slow-api.herokuapp.com/delay/1',
rpcURL,
]);

const contractAddress = '0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85';

Expand Down
5 changes: 4 additions & 1 deletion src/classes/utils/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import unfetch from 'isomorphic-unfetch';
/**
* Makes a post request with the specified JSON data, normally to the a Ethereum JSON RPC API endpoint
*
* @internal
* @param url the URL to send the request to
* @param body the body data (JSON) to send with the request
* @param body JSON POST body
* @returns the JSON response from the server
* @example
* ```javascript
Expand Down Expand Up @@ -59,6 +60,7 @@ type RPCMethodName =
/**
* Prepares data to be sent using the {@link post} function. Data is prepared per the {@link https://en.wikipedia.org/wiki/JSON-RPC#Examples JSON RPC v2 spec}
*
* @internal
* @param method the RPC method to be invoked
* @param params the parameters to be passed to the defined method
* @returns a POST method body matching the {@link https://en.wikipedia.org/wiki/JSON-RPC#Examples JSON RPC v2 spec}
Expand All @@ -74,6 +76,7 @@ type RPCMethodName =
export function buildRPCPostBody(method: RPCMethodName, params: unknown[]) {
return {
jsonrpc: '2.0',
// TODO: Increment ID will be needed when websocket support is added
id: 1,
method,
params,
Expand Down