-
Notifications
You must be signed in to change notification settings - Fork 17
3DPass Node interaction
In this tutorial we'll get you through some basic operations with your node. However, you should always refer to the proper documentation for the tool you are using:
Polkadot-JS RPC is a JavaScript library for interacting with the Substrate RPC API endpoint, distributed as @polkadot/api
Node.js package.
Substrate API Sidecar is using the Polkadot-JS RPC to provide separately runnable REST services.
As most of the nodes based on Substrate, 3DPass Node exposes HTTP and WS endpoints for RPC connections. The default ports are 9933 for HTTP and 9944 for WS.
Public RPC API endpoints:
wss://rpc.3dpass.org
wss://rpc2.3dpass.org
-
wss://127.0.0.1:9944
for local connections
In order to get a list of all RPC methods available, the node has an RPC endpoint called rpc_methods
.
For example:
$ curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "rpc_methods"}' http://localhost:9933/
{"jsonrpc":"2.0","result":{"methods":["account_nextIndex","author_hasKey","author_hasSessionKeys","author_insertKey","author_pendingExtrinsics","author_removeExtrinsic","author_rotateKeys","author_submitAndWatchExtrinsic","author_submitExtrinsic","author_unwatchExtrinsic","chain_getBlock","chain_getBlockHash","chain_getFinalisedHead","chain_getFinalizedHead","chain_getHead","chain_getHeader","chain_getRuntimeVersion","chain_subscribeAllHeads","chain_subscribeFinalisedHeads","chain_subscribeFinalizedHeads","chain_subscribeNewHead","chain_subscribeNewHeads","chain_subscribeRuntimeVersion","chain_unsubscribeAllHeads","chain_unsubscribeFinalisedHeads","chain_unsubscribeFinalizedHeads","chain_unsubscribeNewHead","chain_unsubscribeNewHeads","chain_unsubscribeRuntimeVersion","offchain_localStorageGet","offchain_localStorageSet","payment_queryInfo","state_call","state_callAt","state_getChildKeys","state_getChildStorage","state_getChildStorageHash","state_getChildStorageSize","state_getKeys","state_getKeysPaged","state_getKeysPagedAt","state_getMetadata","state_getPairs","state_getRuntimeVersion","state_getStorage","state_getStorageAt","state_getStorageHash","state_getStorageHashAt","state_getStorageSize","state_getStorageSizeAt","state_queryStorage","state_subscribeRuntimeVersion","state_subscribeStorage","state_unsubscribeRuntimeVersion","state_unsubscribeStorage","subscribe_newHead","system_accountNextIndex","system_addReservedPeer","system_chain","system_health","system_name","system_networkState","system_nodeRoles","system_peers","system_properties","system_removeReservedPeer","system_version","unsubscribe_newHead"],"version":1},"id":1}
Be noticed that this call will show all RPC methods available, including ones disabled by a safety flag like --rpc-methods Safe
Add parameters in the call, for example get a block by its hash value:
$ curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "chain_getBlock", "params":["0x3fa6a530850324391fde50bdf0094bdc17ee17ec84aca389b4047ef54fea0037"]}' http://localhost:9933
{"jsonrpc":"2.0","result":{"block":{"extrinsics":["0x280402000b50055ee97001","0x1004140000"],"header":{"digest":{"logs":["0x06424142453402af000000937fbd0f00000000","0x054241424501011e38401b0aab22f4d72ebc95329c3798445786b92ca1ae69366aacb6e1584851f5fcdfcc0f518df121265c343059c62ab0a34e8e88fda8578810fbe508b6f583"]},"extrinsicsRoot":"0x0e354333c062892e774898e7ff5e23bf1cdd8314755fac15079e25c1a7765f06","number":"0x16c28c","parentHash":"0xe3bf2e8f0e901c292de24d07ebc412d67224ce52a3d1ffae76dc4bd78351e8ac","stateRoot":"0xd582f0dfeb6a7c73c47db735ae82d37fbeb5bada67ee8abcd43479df0f8fc8d8"}},"justification":null},"id":1}
Some return values may not appear meaningful at first glance. 3DPass uses SCALE encoding as a format that is suitable for resource-constrained execution environments. You will need to decode the information and use the chain metadata (state_getMetadata
) to obtain human-readable information.
Use the RPC endpoint chain_subscribeFinalizedHeads
to subscribe to a stream of hashes of finalized headers, or chain_FinalizedHeads
to fetch the latest hash of the finalized header. Use chain_getBlock
to get the block associated with a given hash. chain_getBlock
only accepts block hashes, so if you need to query intermediate blocks, use chain_getBlockHash
to get the block hash from a block number.
Using specific RPC_TYPES is required to decode any data from 3DPass blockchain. These are the types:
{
AccountInfo: "AccountInfoWithTripleRefCount",
Address: "AccountId",
LookupSource: "AccountId",
Weight: "u32",
Difficulty: "u256",
DifficultyAndTimestamp: {
difficulty: "Difficulty",
timestamp: "u64",
},
LockParameters: {
period: "u16",
divide: "u16",
},
StorageVersion: {
_enum: ["V0", "V1"],
V0: "u8",
V1: "u8",
},
};
- Simple connect
- Listen to new blocks
- Listen to balance changes
- Read storage
- Read storage at a specific blockhash
3DPass web wallet uses Polkadot-JS RPC API. Follow the code examples down below as a reference to its exact implementation:
- The network state - here we retrieve total issuance amount, best block number, best block finalized, etc.
- Account balance
- API config
3dpass.org - The Ledger of Things
Join our community: Discord | Telegram | Bitcointalk