youchain-providers
TypeScript icon, indicating that this package has built-in type declarations

1.0.4-alpha.3 • Public • Published

youchain-providers

This is a sub package of youchain.js

Installation

npm install youchain-providers

Usage Examples

HttpProvider

You can pass with the options object the timeout and all known HTTP headers.

import {HttpProvider} from 'youchain-providers';

const options = {
    timeout: 20000,
    headers: [
        {
            name: 'Access-Control-Allow-Origin', value: '*'
        },
        ...
    ]
};

const httpProvider = new HttpProvider('http://localhost:8283', options);

WebsocketProvider

Instead of setting a authorization header you could also define the credentials over the URL with: ws://username:password@localhost:8546

import {WebsocketProvider} from 'youchain-providers';
const options = {
    timeout: 30000,
    headers: {
        authorization: 'Basic username:password'
    }
};

const websocketProvider = new WebsocketProvider('ws://localhost:8283', options);

IpcProvider

import {IpcProvider} from 'youchain-providers';
import net from 'net';

const ipcProvider = new IpcProvider('/Users/me/Library/youchain/youchain.ipc', net);

BatchRequest

The BatchRequest provides the possibility to send JSON-RPC requests as batch.

import {ProvidersModuleFactory, BatchRequest} 'youchain-providers';

const provider = new ProvidersModuleFactory()
                        .createProviderResolver
                        .resolve('http://localhost:8283');

const batchRequest = new BatchRequest(provider);

batchRequest.add(youchain.you.getBalance.request(
    '0x0000000000000000000000000000000000000000',
    'latest',
    callback
));

await batchRequest.execute();

ProviderDetector

Checks if an provider is given from the environment and returns the provider.

import {ProvidersModuleFactory} from 'youchain-providers';

const providerDetector = new ProvidersModuleFactory.createProviderDetector();
const givenProvider = providerDetector.detect();

ProviderResolver

The ProviderResolver resolves an url or an given provider object to the correct provider class. Because of the resolves does youchain has internally just one provider interface and we have no direct dependency to third party providers.

import {ProvidersModuleFactory} 'youchain-providers';

const socketProviderAdapter = new ProvidersModuleFactory()
                        .createProviderResolver
                        .resolve('ws://localhost:8283');

Types

All the typescript typings are placed in the types folder.

Readme

Keywords

none

Package Sidebar

Install

npm i youchain-providers

Weekly Downloads

7

Version

1.0.4-alpha.3

License

LGPL-3.0

Unpacked Size

101 kB

Total Files

7

Last publish

Collaborators

  • youchain