Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8a59985
FrequencyObservable as a function returning a observable
amaury1093 Sep 7, 2018
f113991
Fix tests
amaury1093 Sep 7, 2018
5e613d4
Test a new api
amaury1093 Sep 12, 2018
9ecae21
Make frequency observables work
amaury1093 Sep 13, 2018
660c5ef
Fix tests
amaury1093 Sep 13, 2018
6e99b67
Remove overview
amaury1093 Sep 13, 2018
e4298fa
Don't cover index.ts
amaury1093 Sep 13, 2018
bb5509c
Fix a lot of stuff
amaury1093 Sep 13, 2018
1008d5a
Remove useless import
amaury1093 Sep 13, 2018
3aa360b
Change testRegex for jest
amaury1093 Sep 13, 2018
ea75324
Add ambient for packages with no typigns
amaury1093 Sep 13, 2018
7c03c5c
Generate docs
amaury1093 Sep 13, 2018
80b283f
Remove NullProvider
amaury1093 Sep 13, 2018
a46b19a
Remove NullProvider
amaury1093 Sep 13, 2018
3447e95
Regenerate docs
amaury1093 Sep 13, 2018
1f0c25a
Silent jest on CI
amaury1093 Sep 13, 2018
b4404e2
Fix makeContract and post
amaury1093 Sep 13, 2018
165df16
Regen docs
amaury1093 Sep 13, 2018
9b8edd8
Update summary
amaury1093 Sep 13, 2018
fb89a94
Update ambient
amaury1093 Sep 13, 2018
439d3bd
Export post$ too
amaury1093 Sep 13, 2018
18e640f
Fix makeContract
amaury1093 Sep 13, 2018
431fe51
Fix memoization for frequency observables
amaury1093 Sep 17, 2018
44dde4a
Don't lint lib
amaury1093 Sep 17, 2018
15caa62
Fix bugs with rpc$
amaury1093 Sep 17, 2018
0cbe22a
Remove useless packages
amaury1093 Sep 17, 2018
85f8fb9
Generate docs
amaury1093 Sep 17, 2018
c5e0c56
Update withoutLoading syntax
amaury1093 Sep 17, 2018
82fca45
Use json.stringify for normalizer
amaury1093 Sep 17, 2018
4893e97
Fix bug normalizer
amaury1093 Sep 17, 2018
e449a57
Remove withApi in docs
amaury1093 Sep 17, 2018
3a885fe
Fix bug memoization
amaury1093 Sep 17, 2018
c6ee32e
Remove onEvery2Blocks
amaury1093 Sep 17, 2018
041441b
Options then args
amaury1093 Sep 17, 2018
8a066db
CreateRpc in makeContract fix
amaury1093 Sep 17, 2018
d2bb3c7
Fix getContract memoization
amaury1093 Sep 17, 2018
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
Prev Previous commit
Next Next commit
Fix makeContract and post
  • Loading branch information
amaury1093 committed Sep 13, 2018
commit b4404e2e4353c1e84a0cfedbb765635a4770feaf
8 changes: 5 additions & 3 deletions packages/light.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
//
// SPDX-License-Identifier: MIT

import { setProvider } from './api';
import { setApi, setProvider } from './api';
import frequency from './frequency';
import { makeContract } from './rpc/other/makeContract';
import rpc from './rpc';

export * from './utils/isLoading';
export * from './types';
export { withoutLoading } from './utils/operators/withoutLoading';

export { frequency };
export { frequency, makeContract }; // makeContract is a bit special, because it's not a RpcObservable
export const {
accounts$,
accountsInfo$,
balanceOf$,
blockNumber$,
chainName$,
defaultAccount$,
myBalance$,
peerCount$,
syncStatus$
} = rpc;
export default { setProvider };
export default { setApi, setProvider };
17 changes: 10 additions & 7 deletions packages/light.js/src/rpc/other/makeContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { abiEncode } from '@parity/api/lib/util/encode';
import * as memoizee from 'memoizee';

import { Address } from '../../types';
import { createApiFromProvider, getApi } from '../../api';
import createRpc from '../utils/createRpc';
import { switchMapPromise } from '../../utils/operators';
import api from '../../api';
import frequency from '../../frequency';
import { post$ } from './post';

Expand All @@ -32,7 +32,10 @@ interface MakeContract {
* @return - The contract object as defined in @parity/api.
*/
const getContract = memoizee(
(address: Address, abiJson: any[]) => api().newContract(abiJson, address), // use types from @parity/abi
(address: Address, abiJson: any[], provider: any) => {
const api = provider ? createApiFromProvider(provider) : getApi();
return api.newContract(abiJson, address);
}, // use types from @parity/abi
{ length: 1 } // Only memoize by address
Copy link
Contributor

Choose a reason for hiding this comment

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

memoization needs to be done based on address & api (provider ? createApiFromProvider(provider) : getApi()), otherwise if we do setApi() getContract() setApi() getContract() the last getContract() will return the memoized return value from the first getContract() call with the old api

Copy link
Contributor

Choose a reason for hiding this comment

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

does the memoization here needs to be based on api also or not?

Copy link
Collaborator Author

@amaury1093 amaury1093 Sep 17, 2018

Choose a reason for hiding this comment

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

Yes, api.newContract(...).contractMethod.call() makes a rpc call with that api object, fixed.

);

Expand All @@ -46,15 +49,15 @@ const getContract = memoizee(
* function resolves.
*/
export const makeContract = memoizee(
(address: Address, abiJson: any[]) => {
// use types from @parity/abi
const abi = new Abi(abiJson);
(address: Address, abiJson: any[], options: { provider?: any } = {}) => {
const { provider } = options;
const abi = new Abi(abiJson); // use types from @parity/abi
Copy link
Contributor

Choose a reason for hiding this comment

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

memoization needs to be done based on address and api (same as above)

Copy link
Contributor

@axelchalon axelchalon Sep 14, 2018

Choose a reason for hiding this comment

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

so it's a bit more tricky here
we could have getContract require an api/provider to be passed as parameter; put const api = provider ? createApiFromProvider(provider) : getApi(); here (in makeContract) and at that point memoize based on address and api

// Variable result will hold the final object to return
const result: MakeContract = {
abi,
address,
get contractObject() {
return getContract(address, abiJson);
return getContract(address, abiJson, provider);
}
};

Expand All @@ -66,7 +69,7 @@ export const makeContract = memoizee(
// We only get the contract when the function is called for the 1st
// time. Note: getContract is memoized, won't create contract on each
// call.
const contract = getContract(address, abiJson);
const contract = getContract(address, abiJson, provider);
const method = contract.instance[name]; // Hold the method from the Abi

// The last arguments in args can be an options object
Expand Down
41 changes: 18 additions & 23 deletions packages/light.js/src/rpc/other/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

import { Observable, Observer } from 'rxjs';

import api from '../../api';
import { createApiFromProvider, getApi } from '../../api';
import { distinctReplayRefCount } from '../../utils/operators';
import { RpcObservable, Tx, TxStatus } from '../../types';
import { RpcObservableOptions, Tx, TxStatus } from '../../types';

interface PostOptions extends RpcObservableOptions {
estimate?: boolean;
}

/**
* Post a transaction to the network.
Expand All @@ -16,31 +20,31 @@ import { RpcObservable, Tx, TxStatus } from '../../types';
* `parity_checkRequest` and `eth_getTransactionReceipt` to get the status of
* the transaction.
*
* @param options? - Options to pass.
* @param options? - Options to pass to the {@link RpcObservable}.
* @return - The status of the transaction.
*/
export const post$: RpcObservable<any, TxStatus> = (
tx: Tx,
options: { estimate?: boolean } = {}
) => {
export function post$(tx: Tx, options: PostOptions = {}) {
const { estimate, provider } = options;
const api = provider ? createApiFromProvider(provider) : getApi();

const source$ = Observable.create(async (observer: Observer<TxStatus>) => {
try {
if (options.estimate) {
if (estimate) {
observer.next({ estimating: true });
const gas = await api().eth.estimateGas(tx);
const gas = await api.eth.estimateGas(tx);
observer.next({ estimated: gas });
}
const signerRequestId = await api().parity.postTransaction(tx);
const signerRequestId = await api.parity.postTransaction(tx);
observer.next({ requested: signerRequestId });
const transactionHash = await api().pollMethod(
const transactionHash = await api.pollMethod(
'parity_checkRequest',
signerRequestId
);
if (tx.condition) {
observer.next({ signed: transactionHash, schedule: tx.condition });
} else {
observer.next({ signed: transactionHash });
const receipt = await api().pollMethod(
const receipt = await api.pollMethod(
'eth_getTransactionReceipt',
transactionHash,
(
Expand All @@ -58,14 +62,5 @@ export const post$: RpcObservable<any, TxStatus> = (
}).pipe(distinctReplayRefCount());

source$.subscribe(); // Run this Observable immediately;
return source$;
};
post$.metadata = {
calls: [
'eth_estimateGas',
'parity_postTransaction',
'parity_checkRequest',
'eth_getTransactionReceipt'
],
name: 'post$'
};
return source$ as Observable<TxStatus>;
}
13 changes: 4 additions & 9 deletions packages/light.js/src/rpc/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import isObservable from '../utils/isObservable';
import { resolveApi } from '../utils/testHelpers/mockApi';
import rpc from './rpc';
import { RpcKey, RpcMap, RpcObservable } from '../types';
import { RPC_LOADING } from '../utils/isLoading';
import { setApi } from '../api';

/**
Expand All @@ -26,20 +25,16 @@ const testRpc = (name: string, rpc$: RpcObservable<any, any>) =>
});

it('function should return an Observable', () => {
expect(isObservable(rpc$())).toBe(true);
expect(isObservable(rpc$({}))).toBe(true);
});

it('function result Observable should be subscribable', () => {
expect(() => rpc$().subscribe()).not.toThrow();
expect(() => rpc$({}).subscribe()).not.toThrow();
});

it('function result Observable should return values', done => {
rpc$().subscribe(data => {
// The first value is either 'foo' (defined in mockApi), or the
// RPC_LOADING symbole.
// In the case of defaultAccount$ (which is accounts$[0]), the returned
// value is 'f'. TODO not clean.
expect(['foo', 'f', RPC_LOADING]).toContain(data);
rpc$({}).subscribe(data => {
expect(data).toBeTruthy();
done();
});
});
Expand Down
3 changes: 2 additions & 1 deletion packages/light.js/src/rpc/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import * as eth from './eth';
import { memoizeAll } from '../utils/memoizeAll';
import * as net from './net';
import { post$ } from './other';
import * as parity from './parity';

const rpc = { ...eth, ...net, ...parity };
const rpc = { ...eth, ...net, ...parity, post$ };

export default memoizeAll(rpc);
Copy link
Contributor

Choose a reason for hiding this comment

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

remove memoization here (see previous comments)

7 changes: 3 additions & 4 deletions packages/light.js/src/rpc/utils/createRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
//
// SPDX-License-Identifier: MIT

import * as Api from '@parity/api';
import { isFunction } from '@parity/api/lib/util/types';
import { merge, ReplaySubject, Observable, OperatorFunction } from 'rxjs';
import { multicast, refCount } from 'rxjs/operators';
import * as prune from 'json-prune';

import { getApi } from '../../api';
import { Metadata, RpcObservable, RpcObservableOptions } from '../../types';
import { createApiFromProvider, getApi } from '../../api';
import {
distinctValues,
withoutLoading as withoutLoadingOperator
} from '../../utils/operators';
import { Metadata, RpcObservable, RpcObservableOptions } from '../../types';

interface RpcObservableWithoutMetadata<_, Out> {
(...args: any[]): Observable<Out>;
Expand Down Expand Up @@ -60,7 +59,7 @@ const createRpc = <Source, Out>(metadata: Metadata<Source, Out>) => (
options: RpcObservableOptions = {}
) => {
const { provider, withoutLoading } = options;
const api = provider ? new Api(provider) : getApi();
const api = provider ? createApiFromProvider(provider) : getApi();
// rpc$ will hold the RpcObservable minus its metadata
const rpc$: RpcObservableWithoutMetadata<Source, Out> = (...args: any[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

add memoization based on api and the observable args

// The source Observable can either be another RpcObservable (in the
Expand Down
11 changes: 9 additions & 2 deletions packages/light.js/src/utils/testHelpers/mockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const listOfMockRps: { [index: string]: string[] } = {
eth: ['accounts', 'blockNumber', 'getBalance', 'syncing'],
fake: ['method'],
net: ['peerCount'],
parity: ['accountsInfo', 'netChain']
parity: ['accountsInfo', 'netChain', 'postTransaction']
};

/**
Expand Down Expand Up @@ -50,7 +50,14 @@ const createApi = (

return apiObject;
},
{ isPubSub } as { [index: string]: any }
{
isPubSub,
pollMethod() {
return isError
? Promise.reject(resolveWith)
: Promise.resolve(resolveWith);
}
} as { [index: string]: any }
);

return result;
Expand Down