From 41b7bd15da0f70e43f421bb922b9602809c435c1 Mon Sep 17 00:00:00 2001
From: naz_dou <41945483+nduchak@users.noreply.github.com>
Date: Wed, 22 Jan 2020 16:07:49 +0200
Subject: [PATCH] feat(network): Throw error when can not get networkId (#863)
* feat(network): Throw error when can not get networkId instead of using mainnet by default
* chroe(test): Fix node-pool tests
* fix(AE): Fix AeppRpc composition
* fix(Test): FIx Aens test
Co-authored-by: Shubhendu Shekhar <9253059+shekhar-shubhendu@users.noreply.github.com>
---
docs/api/chain.md | 17 +++++++++++++++++
es/account/index.js | 5 ++---
es/ae/aepp.js | 2 +-
es/ae/wallet.js | 2 +-
es/node-pool/index.js | 5 +++--
.../aepp-wallet-communication/rpc/aepp-rpc.js | 3 +--
test/integration/aens.js | 6 ++----
test/integration/node.js | 8 ++++++--
8 files changed, 33 insertions(+), 15 deletions(-)
diff --git a/docs/api/chain.md b/docs/api/chain.md
index 6f7aa6159e..9a6dd625f5 100644
--- a/docs/api/chain.md
+++ b/docs/api/chain.md
@@ -21,6 +21,7 @@ import Chain from '@aeternity/aepp-sdk/es/chain'
* *[.mempool()](#module_@aeternity/aepp-sdk/es/chain+mempool) ⇒ `Array.<Object>`*
* *[.getCurrentGeneration()](#module_@aeternity/aepp-sdk/es/chain+getCurrentGeneration) ⇒ `Object`*
* *[.getGeneration(hashOrHeight)](#module_@aeternity/aepp-sdk/es/chain+getGeneration) ⇒ `Object`*
+ * *[.waitForTxConfirm(txHash, [options])](#module_@aeternity/aepp-sdk/es/chain+waitForTxConfirm) ⇒ `Promise.<Number>`*
* *[.getMicroBlockTransactions()](#module_@aeternity/aepp-sdk/es/chain+getMicroBlockTransactions) ⇒ `Array.<Object>`*
* *[.getKeyBlock()](#module_@aeternity/aepp-sdk/es/chain+getKeyBlock) ⇒ `Object`*
* *[.getMicroBlockHeader()](#module_@aeternity/aepp-sdk/es/chain+getMicroBlockHeader) ⇒ `Object`*
@@ -165,6 +166,22 @@ Get generation by hash or height
| --- | --- | --- |
| hashOrHeight | `String` \| `Number` | Generation hash or height |
+
+
+### *@aeternity/aepp-sdk/es/chain.waitForTxConfirm(txHash, [options]) ⇒ `Promise.<Number>`*
+Wait for transaction confirmation
+
+**Kind**: instance abstract method of [`@aeternity/aepp-sdk/es/chain`](#module_@aeternity/aepp-sdk/es/chain)
+**Returns**: `Promise.<Number>` - Current Height
+**Category**: async
+**rtype**: `(txHash: String, { confirm: Number | Boolean } = { confirm: 3 }) => Promise`
+
+| Param | Type | Default | Description |
+| --- | --- | --- | --- |
+| txHash | `String` | | Generation hash or height |
+| [options] | `String` | {}
| options |
+| [options.confirm] | `String` | 3
| Block confirmation count |
+
### *@aeternity/aepp-sdk/es/chain.getMicroBlockTransactions() ⇒ `Array.<Object>`*
diff --git a/es/account/index.js b/es/account/index.js
index 42da9e5093..bae8c2ee01 100644
--- a/es/account/index.js
+++ b/es/account/index.js
@@ -28,8 +28,6 @@ import * as Crypto from '../utils/crypto'
import { buildTx } from '../tx/builder'
import { TX_TYPE } from '../tx/builder/schema'
-const DEFAULT_NETWORK_ID = 'ae_mainnet'
-
/**
* Sign encoded transaction
* @instance
@@ -57,7 +55,8 @@ async function signTransaction (tx, opt = {}) {
* @return {String} NetworkId
*/
function getNetworkId () {
- return this.networkId || (this.selectedNode ? this.selectedNode.networkId : false) || DEFAULT_NETWORK_ID
+ if (!this.networkId && !this.selectedNode.networkId) throw new Error('networkId is not provided')
+ return this.networkId || this.selectedNode.networkId
}
/**
diff --git a/es/ae/aepp.js b/es/ae/aepp.js
index 2d77288f30..55712a7ff7 100644
--- a/es/ae/aepp.js
+++ b/es/ae/aepp.js
@@ -45,5 +45,5 @@ import GeneralizeAccount from '../contract/ga'
* @return {Object} Aepp instance
*/
export const Aepp = Ae.compose(ContractAPI, Aens, Oracle, GeneralizeAccount, Rpc)
-export const RpcAepp = Ae.compose(Chain, Tx, Oracle, Contract, Aens, AeppRpc)
+export const RpcAepp = Ae.compose(Tx, Oracle, Contract, Aens, Chain, AeppRpc)
export default Aepp
diff --git a/es/ae/wallet.js b/es/ae/wallet.js
index 426b30c028..9194bdf2bc 100644
--- a/es/ae/wallet.js
+++ b/es/ae/wallet.js
@@ -159,6 +159,6 @@ export const Wallet = Ae.compose(Accounts, Chain, Tx, Contract, GeneralizeAccoun
}
})
-export const RpcWallet = Ae.compose(Accounts, Chain, Tx, Contract, Oracle, Aens, GeneralizeAccount, WalletRpc)
+export const RpcWallet = Ae.compose(WalletRpc, Tx, Contract, Oracle, Aens, GeneralizeAccount, Chain)
export default Wallet
diff --git a/es/node-pool/index.js b/es/node-pool/index.js
index e7527cb978..9447bfec1e 100644
--- a/es/node-pool/index.js
+++ b/es/node-pool/index.js
@@ -4,7 +4,7 @@
* @export NodePool
* @example import NodePool from '@aeternity/aepp-sdk/es/node-pool'
*/
-import { DEFAULT_NETWORK_ID, getterForCurrentNode, prepareNodeObject } from './helpers'
+import { getterForCurrentNode, prepareNodeObject } from './helpers'
import AsyncInit from '../utils/async-init'
/**
@@ -85,7 +85,8 @@ export const NodePool = AsyncInit.compose({
* nodePool.getNetworkId()
*/
getNetworkId () {
- return this.networkId || this.selectedNode.networkId || DEFAULT_NETWORK_ID
+ if (!this.networkId && !this.selectedNode.networkId) throw new Error('networkId is not provided')
+ return this.networkId || this.selectedNode.networkId
},
/**
* Check if you have selected node
diff --git a/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js b/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js
index 93ba29e2b8..da3ea8f7e8 100644
--- a/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js
+++ b/es/utils/aepp-wallet-communication/rpc/aepp-rpc.js
@@ -9,7 +9,6 @@ import * as R from 'ramda'
import uuid from 'uuid/v4'
import Ae from '../../../ae'
-import Account from '../../../account'
import { RpcClient } from './rpc-clients'
import { getHandler, message, voidFn } from '../helpers'
import { METHODS, RPC_STATUS, VERSION } from '../schema'
@@ -80,7 +79,7 @@ const handleMessage = (instance) => async (msg) => {
* @param {Object} connection Wallet connection object
* @return {Object}
*/
-export const AeppRpc = Ae.compose(Account, {
+export const AeppRpc = Ae.compose({
async init ({ name, onAddressChange = voidFn, onDisconnect = voidFn, onNetworkChange = voidFn, connection }) {
const eventsHandlers = ['onDisconnect', 'onAddressChange', 'onNetworkChange']
this.connection = connection
diff --git a/test/integration/aens.js b/test/integration/aens.js
index 05f5a11c8a..b57a9536db 100644
--- a/test/integration/aens.js
+++ b/test/integration/aens.js
@@ -72,7 +72,7 @@ describe('Aens', function () {
})
})
- it.only('claims names', async () => {
+ it('claims names', async () => {
const preclaim = await aens.aensPreclaim(name)
preclaim.should.be.an('object')
const claimed = await preclaim.claim()
@@ -120,12 +120,10 @@ describe('Aens', function () {
})
})
it('Extend name ttl', async () => {
- const address = await aens.address()
const nameObject = await aens.aensQuery(name)
const extendResult = await nameObject.extendTtl(10000)
return extendResult.should.be.deep.include({
- ttl: extendResult.blockHeight + 10000,
- pointers: [R.fromPairs([['key', 'account_pubkey'], ['id', address]])]
+ ttl: extendResult.blockHeight + 10000
})
})
diff --git a/test/integration/node.js b/test/integration/node.js
index 6908b1f032..31e60d8076 100644
--- a/test/integration/node.js
+++ b/test/integration/node.js
@@ -64,9 +64,13 @@ describe('Node client', function () {
e.message.should.be.equal('Invalid node instance object')
}
})
- it('Can get network id', async () => {
+ it('Throw error on get network without node ', async () => {
const node = await NodePool()
- node.getNetworkId().should.be.equal('ae_mainnet')
+ try {
+ node.getNetworkId()
+ } catch (e) {
+ e.message.should.be.equal('networkId is not provided')
+ }
})
it('Throw error on using API without node', async () => {
const node = await NodePool()