-
Notifications
You must be signed in to change notification settings - Fork 350
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
Conversation
… Node Interface comment
… comments. Use Yarn. Fix typos. Show equivalence
…nnect. Add missing on ready
…i to ApiRx. Add Node Interface comment
There was a problem hiding this 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(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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)
There was a problem hiding this 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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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)
packages/rpc-provider/test/mockWs.ts
Outdated
@@ -4,7 +4,7 @@ | |||
|
|||
import { Server } from 'mock-socket'; | |||
|
|||
const TEST_WS_URL = 'ws://localhost:9955'; | |||
const TEST_WS_URL = 'ws://localhost:9944'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert, no overlap.
packages/rpc-rx/src/index.ts
Outdated
@@ -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'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it was.
@jacogr i've pushed commits that address the review comments |
.subscribe((current) => { | ||
previous = previous || current; | ||
|
||
// FIXME - refactor like the ApiPromise where only the balance change console.log's |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
packages/api/src/promise/index.ts
Outdated
@@ -36,70 +36,98 @@ import SubmittableExtrinsic from './SubmittableExtrinsic'; | |||
* <BR> | |||
* | |||
* ```javascript | |||
* import ApiPromise from '@polkadot/api/promise'; | |||
* import Api from '@polkadot/api/promise'; |
There was a problem hiding this comment.
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?
packages/api/src/promise/index.ts
Outdated
* ``` | ||
* <BR> | ||
* | ||
* Submitting a transaction - | ||
* <BR> | ||
* | ||
* ```javascript | ||
* import ApiPromise from '@polkadot/api/promise'; | ||
* import Api from '@polkadot/api/promise'; |
There was a problem hiding this comment.
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` |
There was a problem hiding this comment.
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.
packages/api/src/rx/index.ts
Outdated
* .subscribe((hash) => { | ||
* console.log(`submitted with hash ${hash}`); | ||
* }); | ||
* const ALICE_SEED = 'Alice'.padEnd(32, ' '); |
There was a problem hiding this comment.
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 :(
… functionality as promises example
… back-to-back example
…mit tx example and check it works
…) instead. Remove unnecessary use of provider
@jacogr I've pushed commits that address review comments, merge latest from master, and bump versions in examples |
Closing, replaced by #600 (which tracks current interfaces closer) |
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. |
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 tonew ApiRx().isReady
Fix example since should be connected instead of duplicate
disconnect
. Add missing onready
to exampleUpdate Typedocs examples to match Promises examples
Rename Api to ApiPromise or ApiRx depending on what's imported
Rename
provider
towsProvider
for consistencyAdd missing semicolons
Change WS and HTTP ports in tests to the ones we usually use
Remove unnecessary blank lines