-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,17 @@ | ||
import Chainlink from './chainlink.js'; | ||
import ENS from './ens.js'; | ||
import FetchProvider from './provider.js'; | ||
import { ArchiveNodeProvider, calcTransfersDiff } from './archive.js'; | ||
import UniswapV2 from './uniswap-v2.js'; | ||
import UniswapV3 from './uniswap-v3.js'; | ||
import { Web3Provider, Web3CallArgs, hexToNumber } from '../utils.js'; | ||
|
||
// There are many low level APIs inside which are not exported yet. | ||
export { Chainlink, ENS, UniswapV2, UniswapV3 }; | ||
|
||
// There is a lot features required for network to make this useful | ||
// This is attempt to create them via small composable wrappers | ||
export type FetchFn = ( | ||
url: string, | ||
opt?: Record<string, any> | ||
) => Promise<{ json: () => Promise<any> }>; | ||
type Headers = Record<string, string>; | ||
type JsonFn = (url: string, headers: Headers, body: unknown) => Promise<any>; | ||
type PromiseCb<T> = { | ||
resolve: (value: T | PromiseLike<T>) => void; | ||
reject: (reason?: any) => void; | ||
}; | ||
|
||
function getJSONUsingFetch(fn: FetchFn): JsonFn { | ||
return async (url: string, headers: Headers = {}, body: unknown) => { | ||
const res = await fn(url, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json', ...headers }, | ||
body: JSON.stringify(body), | ||
}); | ||
return await res.json(); | ||
}; | ||
} | ||
|
||
// Unsafe. TODO: inspect for race conditions and bugs. | ||
function limitParallel(jsonFn: JsonFn, limit: number): JsonFn { | ||
let cur = 0; | ||
const queue: ({ url: string; headers: Headers; body: unknown } & PromiseCb<any>)[] = []; | ||
const process = () => { | ||
if (cur >= limit) return; | ||
const next = queue.shift(); | ||
if (!next) return; | ||
try { | ||
jsonFn(next.url, next.headers, next.body) | ||
.then(next.resolve) | ||
.catch(next.reject) | ||
.finally(() => { | ||
cur--; | ||
process(); | ||
}); | ||
} catch (e) { | ||
next.reject(e); | ||
cur--; | ||
} | ||
cur++; | ||
}; | ||
return (url, headers, body) => { | ||
return new Promise((resolve, reject) => { | ||
queue.push({ url, headers, body, resolve, reject }); | ||
process(); | ||
}); | ||
}; | ||
} | ||
|
||
type NetworkOpts = { | ||
limitParallel?: number; | ||
}; | ||
|
||
export const FetchProvider = ( | ||
fetch: FetchFn, | ||
url: string, | ||
headers: Headers = {}, | ||
opts: NetworkOpts = {} | ||
): Web3Provider => { | ||
let fn = getJSONUsingFetch(fetch); | ||
if (opts.limitParallel) fn = limitParallel(fn, opts.limitParallel); | ||
const jsonrpc = async (method: string, ...params: any[]) => { | ||
const json = await fn(url, headers, { jsonrpc: '2.0', id: 0, method, params }); | ||
if (json && json.error) | ||
throw new Error(`FetchProvider(${json.error.code}): ${json.error.message || json.error}`); | ||
return json.result; | ||
}; | ||
return { | ||
ethCall: (args: Web3CallArgs, tag = 'latest') => | ||
jsonrpc('eth_call', args, tag) as Promise<string>, | ||
estimateGas: async (args: Web3CallArgs, tag = 'latest') => | ||
hexToNumber(await jsonrpc('eth_estimateGas', args, tag)), | ||
call: (method: string, ...args: any[]) => jsonrpc(method, ...args), | ||
}; | ||
export { | ||
ArchiveNodeProvider, | ||
calcTransfersDiff, | ||
Chainlink, | ||
ENS, | ||
FetchProvider, | ||
UniswapV2, | ||
UniswapV3, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { Web3Provider, Web3CallArgs, hexToNumber } from '../utils.js'; | ||
|
||
export type FetchFn = ( | ||
url: string, | ||
opt?: Record<string, any> | ||
) => Promise<{ json: () => Promise<any> }>; | ||
type Headers = Record<string, string>; | ||
type NetworkOpts = { | ||
concurrencyLimit?: number; | ||
headers?: Headers; | ||
}; | ||
type PromiseCb<T> = { | ||
resolve: (value: T | PromiseLike<T>) => void; | ||
reject: (reason?: any) => void; | ||
}; | ||
|
||
export default class FetchProvider implements Web3Provider { | ||
private concurrencyLimit: number; | ||
private currentlyFetching: number; | ||
private headers: Headers; | ||
constructor( | ||
private fetchFunction: FetchFn, | ||
readonly rpcUrl: string, | ||
options: NetworkOpts = {} | ||
) { | ||
this.concurrencyLimit = options.concurrencyLimit == null ? 0 : options.concurrencyLimit; | ||
this.currentlyFetching = 0; | ||
this.headers = options.headers || {}; | ||
if (typeof this.headers !== 'object') throw new Error('invalid headers: expected object'); | ||
} | ||
private async fetchJson(body: unknown) { | ||
const url = this.rpcUrl; | ||
const args = { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json', ...this.headers }, | ||
body: JSON.stringify(body), | ||
}; | ||
const res = await this.fetchFunction(url, args); | ||
return res.json(); | ||
} | ||
private addToFetchQueue(body: unknown): Promise<any> { | ||
if (this.concurrencyLimit === 0) return this.fetchJson(body); | ||
const queue: ({ body: unknown } & PromiseCb<any>)[] = []; | ||
const process = () => { | ||
if (this.currentlyFetching >= this.concurrencyLimit) return; | ||
const next = queue.shift(); | ||
if (!next) return; | ||
try { | ||
this.fetchJson(next.body) | ||
.then(next.resolve) | ||
.catch(next.reject) | ||
.finally(() => { | ||
this.currentlyFetching--; | ||
process(); | ||
}); | ||
} catch (e) { | ||
next.reject(e); | ||
this.currentlyFetching--; | ||
} | ||
this.currentlyFetching++; | ||
}; | ||
return new Promise((resolve, reject) => { | ||
queue.push({ body, resolve, reject }); | ||
process(); | ||
}); | ||
} | ||
private async rpc(method: string, ...params: any[]): Promise<string> { | ||
const body = { | ||
jsonrpc: '2.0', | ||
id: 0, | ||
method, | ||
params, | ||
}; | ||
const json = await this.addToFetchQueue(body); | ||
if (json && json.error) | ||
throw new Error(`FetchProvider(${json.error.code}): ${json.error.message || json.error}`); | ||
return json.result; | ||
} | ||
|
||
ethCall(args: Web3CallArgs, tag = 'latest') { | ||
return this.rpc('eth_call', args, tag); | ||
} | ||
async estimateGas(args: Web3CallArgs, tag = 'latest') { | ||
return hexToNumber(await this.rpc('eth_estimateGas', args, tag)); | ||
} | ||
call(method: string, ...args: any[]) { | ||
return this.rpc(method, ...args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters