@ethersproject/providers#ExternalProvider TypeScript Examples

The following examples show how to use @ethersproject/providers#ExternalProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: api.ts    From commonwealth with GNU General Public License v3.0 6 votes vote down vote up
constructor(
    _factory: any,
    contractAddress: string,
    web3Provider: ExternalProvider
  ) {
    this.contractAddress = contractAddress;
    this.Provider = new Web3Provider(web3Provider);
    // 12s minute polling interval (default is 4s)
    this.Provider.pollingInterval = 12000;
  }
Example #2
Source File: contractApi.ts    From commonwealth with GNU General Public License v3.0 6 votes vote down vote up
constructor(
    factory: ContractFactoryT<ContractT>,
    contractAddress: string,
    web3Provider: ExternalProvider
  ) {
    this.contractAddress = contractAddress;
    this.Provider = new ethers.providers.Web3Provider(web3Provider);
    // 12s minute polling interval (default is 4s)
    this.Provider.pollingInterval = 12000;
    this.Contract = factory(this.contractAddress, this.Provider);
  }
Example #3
Source File: App.tsx    From ether-swr with MIT License 5 votes vote down vote up
function getLibrary(
  provider: ExternalProvider | JsonRpcFetchFunc
): Web3Provider {
  const library = new Web3Provider(provider)
  library.pollingInterval = 12000
  return library
}
Example #4
Source File: minirpc.ts    From sdk with ISC License 4 votes vote down vote up
export class MiniRpcProvider implements ExternalProvider {
    readonly isMetaMask:      boolean = false;
    readonly chainId:         number;

    private _url:             string;
    private _host:            string;
    private _path:            string;

    protected _batchInterval: number;

    private _nextId:          number         = 1;
    private _batchAggregator: NodeJS.Timeout = null;
    private _pendingBatch:    BatchItem[]    = null;

    constructor(chainId: number, url: string, batchWaitTimeMs: number=50) {
        this.chainId = chainId;

        const parsed = new URL(url);
        this._url  = parsed.toString();
        this._host = parsed.host;
        this._path = parsed.pathname;

        // how long to wait to batch calls
        this._batchInterval = batchWaitTimeMs;
    }

    /**
     * Amount of time, in milliseconds, between batch RPC calls
     */
    get batchInterval(): number {
        return this._batchInterval
    }

    /**
     * Sets the provider's interval for sending batch RPC calls.
     * @param interval amount of time in milliseconds the provider will wait between sending batch RPC calls
     * @internal
     */
    set batchInterval(interval: number) {
        this._batchInterval = interval;
    }

    get url(): string {
        return this._url
    }

    /**
     * @internal
     */
    set url(newUrl: string | URL) {
        const parsed = newUrl instanceof URL ? newUrl : new URL(newUrl);
        this._host = parsed.host;
        this._path = parsed.pathname;
        this._url  = parsed.toString();
    }

    get host(): string {
        return this._host
    }

    get path(): string {
        return this._path
    }

    async request(request: RPCRequest): Promise<any> {
        if (request.method === 'eth_chainId') {
            return `0x${this.chainId.toString(16)}`
        }

        if (this._pendingBatch === null) {
            this._pendingBatch = [];
        }

        const batchItem: BatchItem = {
            request: {
                jsonrpc: "2.0",
                id:     (this._nextId++),
                ...request
            },
            resolve: null,
            reject:  null,
        };

        const prom: Promise<any> = new Promise((resolve, reject) => {
            batchItem.resolve = resolve;
            batchItem.reject  = reject;
        });

        this._pendingBatch.push(batchItem);

        if (!this._batchAggregator) {
            setTimeout(() => this._processBatch(), this._batchInterval);
        }

        return prom
    }

    private async _processBatch() {
        let currentBatch = this._pendingBatch;

        this._pendingBatch    = null;
        this._batchAggregator = null;

        if (currentBatch === null) {
            currentBatch = [];
        }

        const requests: JsonRPCRequest[] = currentBatch.map(req => req.request);

        if (requests.length === 0) {
            return
        }

        return fetchJson(this._url, JSON.stringify(requests))
            .then(result =>
                currentBatch.forEach((req, idx) => {
                    const payload = result[idx];
                    if (payload.error) {
                        const {message, code, data} = payload.error;
                        req.reject(new RequestError(message, code, data));
                    } else {
                        req.resolve(payload.result);
                    }
                })
            )
            .catch(error => currentBatch.forEach(batchItem => batchItem.reject(error)))
    }
}