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

refactor: Miscellaneous changes for consistency #292

Closed
wants to merge 128 commits into from

Conversation

ltfschoen
Copy link
Contributor

  • Split Promise Examples 02 and 03 into both ApiPromise and ApiRx versions

  • Update Promise Examples 05 and 07 to use Keyring Testpairs

  • Add Capitalised comments.

  • Add Node Interface comments

  • Fix typos

  • Fix incorrect variable name

  • Use Yarn instead of Npm in guides

  • Explain how ApiRx.create() is equivalent to new ApiRx().isReady

  • Fix example since should be connected instead of duplicate disconnect. Add missing on ready to example

  • Update Typedocs examples to match Promises examples

  • Rename Api to ApiPromise or ApiRx depending on what's imported

  • Rename provider to wsProvider for consistency

  • Add missing semicolons

  • Change WS and HTTP ports in tests to the ones we usually use

  • Remove unnecessary blank lines

… comments. Use Yarn. Fix typos. Show equivalence
@ltfschoen ltfschoen changed the title refactor: Miscellaneous changes refactor: Miscellaneous changes for consistency Oct 14, 2018
@ltfschoen ltfschoen requested a review from jacogr October 14, 2018 23:30
Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

Text cleanups work, the actual code changes tries to make things DRY and takes away learning. Here duplication is fine - examples for rx & promise need to be duplicated, so we have learning per API.

// Our address for Alice on the dev chain
const Alice = '5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ';
// Create an instance of the keyring that includes test accounts
const keyring = testingPairs();
Copy link
Member

Choose a reason for hiding this comment

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

Here we are trying to keep to an example with "real use", rather than using test data. Hence not relying on pre-populated. So keep to base-empty keyring

// Import the Api
const { ApiPromise } = require('@polkadot/api');
const exampleApiPromise = require('./exampleApiPromise').default;
const exampleApiRx = require('./exampleApiRx').default;
Copy link
Member

Choose a reason for hiding this comment

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

Not here. The examples need to be done for rx, but that means it needs to be duplicated - here we are trying to keep it simple for users of the specific type. (And other examples, keep as it was)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, so the Rx examples should be in the examples/rx directory:

  • examples/promise/03_listen_to_balance_change/index.js
  • examples/rx/03_listen_to_balance_change/index.js

So at https://polkadot.js.org/api/, the menu would display:

EXAMPLES
ApiPromise
Simple connect
Listen to blocks
Listen to balance change

ApiRx
Simple connect
Listen to blocks
Listen to balance change

// Create an instance of the keyring
const keyring = new Keyring();
// Create an instance of the keyring that includes test accounts
const keyring = testingPairs();
Copy link
Member

Choose a reason for hiding this comment

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

Just the normal keypair, no shortcuts :) We want to show use (actually here the "Alice add" is also not needed since it is there - hence stick with the barer bones, it is about showing how to use, not showing helpers)

jacogr
jacogr previously requested changes Dec 4, 2018
Copy link
Member

@jacogr jacogr left a comment

Choose a reason for hiding this comment

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

Comments added.

Overall this one seems to have taken on a life on it's own, i.e. changes all over which doesn't seem to be in any way related to the actual PR descriptions. Let's rather keep them short and to-the-point where we can.


## tests

1. [Run a local Substrate chain node](https://github.com/paritytech/substrate#61-hacking-on-substrate)
Copy link
Member

Choose a reason for hiding this comment

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

+1

const transfer = api.tx.balances.transfer(BOB_ADDR, 12345);

// Sign the transaction using our account
// Show unsigned transaction (alternate viewing forms are toU8a, toString, toJSON)
// Use Polkadot-JS utility to shorten displayed transaction value
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what this line refers to? (We are not doing any magic here, just the base toHex())

// Sign the transaction using our account
// Show unsigned transaction (alternate viewing forms are toU8a, toString, toJSON)
// Use Polkadot-JS utility to shorten displayed transaction value
console.log(`Unsigned transaction: `, transfer.toHex());
Copy link
Member

Choose a reason for hiding this comment

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

Use ' quotes

transfer.sign(alice, aliceNonce);

// Show signed transaction
console.log(`Signed transaction: `, transfer.toHex());
Copy link
Member

Choose a reason for hiding this comment

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

Use ' quotes

@@ -0,0 +1,49 @@
const { combineLatest } = require('rxjs');
const { first, switchMap } = require('rxjs/operators');
Copy link
Member

Choose a reason for hiding this comment

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

Errant space before first

@@ -26,11 +26,11 @@ const l = logger('api-http');
* <BR>
*
* ```javascript
* import Api from '@polkadot/api/promise';
* import ApiPromise from '@polkadot/api/promise';
Copy link
Member

Choose a reason for hiding this comment

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

Revert.

@@ -48,11 +48,11 @@ const l = logger('api-ws');
* <BR>
*
* ```javascript
* import Api from '@polkadot/api/promise';
* import ApiPromise from '@polkadot/api/promise';
Copy link
Member

Choose a reason for hiding this comment

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

Revert.

@@ -4,7 +4,7 @@

import nock from 'nock';

const TEST_HTTP_URL = 'http://localhost:9944';
const TEST_HTTP_URL = 'http://localhost:9933';
Copy link
Member

Choose a reason for hiding this comment

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

Should not overlap with the actual node endpoints. (So changing it is correct, changing it ot an overlapping port is not)

@@ -4,7 +4,7 @@

import { Server } from 'mock-socket';

const TEST_WS_URL = 'ws://localhost:9955';
const TEST_WS_URL = 'ws://localhost:9944';
Copy link
Member

Choose a reason for hiding this comment

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

Revert, no overlap.

@@ -30,8 +30,8 @@ type CachedMap = {
* import RpcRx from '@polkadot/rpc-rx';
* import WsProvider from '@polkadot/rpc-provider/ws';
*
* const provider = new WsProvider('http://127.0.0.1:9944');
* const api = new RpcRx(provider);
* const wsProvider = new WsProvider('http://127.0.0.1:9944');
Copy link
Member

Choose a reason for hiding this comment

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

As it was.

@ltfschoen
Copy link
Contributor Author

@jacogr i've pushed commits that address the review comments

@jacogr jacogr dismissed their stale review December 8, 2018 08:01

Changes made since review.

.subscribe((current) => {
previous = previous || current;

// FIXME - refactor like the ApiPromise where only the balance change console.log's
Copy link
Member

Choose a reason for hiding this comment

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

Not convinced we actually want FIXMEs in the examples at all. It should just be simple and working.

```

Subscribing to blocks via Promise-based API -

```javascript
import { ApiPromise } from '@polkadot/api';
import Api from '@polkadot/api/promise';
Copy link
Member

Choose a reason for hiding this comment

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

Any reason this is not the original import? (This has been done in a couple of places now). This example use is actually great since it shows the 2 side-by-side, so being a bit more explicit as it was is a bit cleaner and clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just changed the naming for consistency based on this review comment #292 (comment).

I agree that it would be better to show more explicitly here though since users can compare them side-by side.

I've pushed a commit reverting back

@@ -178,13 +178,17 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
* <BR>
*
* ```javascript
* api.on('disconnected', () => {
* api.on('connected', () => {
Copy link
Member

Choose a reason for hiding this comment

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

+1

@@ -36,70 +36,98 @@ import SubmittableExtrinsic from './SubmittableExtrinsic';
* <BR>
*
* ```javascript
* import ApiPromise from '@polkadot/api/promise';
* import Api from '@polkadot/api/promise';
Copy link
Member

Choose a reason for hiding this comment

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

Not 100% sure why it has been changed?

* ```
* <BR>
*
* Submitting a transaction -
* <BR>
*
* ```javascript
* import ApiPromise from '@polkadot/api/promise';
* import Api from '@polkadot/api/promise';
Copy link
Member

Choose a reason for hiding this comment

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

I would just remove this and revert to the original here. IT is a bit overwhelming as an in-code comment now. (Examples are great for making these valid). So rather -

  • revert to original
  • import keyring (can just do the test one here, with an appropriate comment)
  • leave rest as-is

* const elapsed = last
* ? `, ${timestamp.toNumber() - last}s since last`
* ? `, ${timestampSeconds - last}s since last`
Copy link
Member

Choose a reason for hiding this comment

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

+1 Ahhh, yes, this was pre-change to extending date. (Maybe use .getTime() above, it is more along the line what 90% out there use.

* .subscribe((hash) => {
* console.log(`submitted with hash ${hash}`);
* });
* const ALICE_SEED = 'Alice'.padEnd(32, ' ');
Copy link
Member

Choose a reason for hiding this comment

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

As per the previous in-code example, too unwieldly now :(

@ltfschoen
Copy link
Contributor Author

@jacogr I've pushed commits that address review comments, merge latest from master, and bump versions in examples

@jacogr
Copy link
Member

jacogr commented Jan 18, 2019

Closing, replaced by #600 (which tracks current interfaces closer)

@jacogr jacogr closed this Jan 18, 2019
@jacogr jacogr deleted the luke-refactor-misc branch January 18, 2019 02:11
@polkadot-js-bot
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@polkadot-js polkadot-js locked as resolved and limited conversation to collaborators Jun 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
WIP Work in Progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants