Skip to content

Commit

Permalink
feat(network): Throw error when can not get networkId (#863)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
nduchak and shekhar-shubhendu authored Jan 22, 2020
1 parent f31143f commit 41b7bd1
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 15 deletions.
17 changes: 17 additions & 0 deletions docs/api/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Chain from '@aeternity/aepp-sdk/es/chain'
* *[.mempool()](#module_@aeternity/aepp-sdk/es/chain+mempool)`Array.&lt;Object&gt;`*
* *[.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.&lt;Number&gt;`*
* *[.getMicroBlockTransactions()](#module_@aeternity/aepp-sdk/es/chain+getMicroBlockTransactions)`Array.&lt;Object&gt;`*
* *[.getKeyBlock()](#module_@aeternity/aepp-sdk/es/chain+getKeyBlock)`Object`*
* *[.getMicroBlockHeader()](#module_@aeternity/aepp-sdk/es/chain+getMicroBlockHeader)`Object`*
Expand Down Expand Up @@ -165,6 +166,22 @@ Get generation by hash or height
| --- | --- | --- |
| hashOrHeight | `String` \| `Number` | Generation hash or height |

<a id="module_@aeternity/aepp-sdk/es/chain+waitForTxConfirm"></a>

### *@aeternity/aepp-sdk/es/chain.waitForTxConfirm(txHash, [options]) ⇒ `Promise.&lt;Number&gt;`*
Wait for transaction confirmation

**Kind**: instance abstract method of [`@aeternity/aepp-sdk/es/chain`](#module_@aeternity/aepp-sdk/es/chain)
**Returns**: `Promise.&lt;Number&gt;` - Current Height
**Category**: async
**rtype**: `(txHash: String, { confirm: Number | Boolean } = { confirm: 3 }) => Promise<Number>`

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| txHash | `String` | | Generation hash or height |
| [options] | `String` | <code>{}</code> | options |
| [options.confirm] | `String` | <code>3</code> | Block confirmation count |

<a id="module_@aeternity/aepp-sdk/es/chain+getMicroBlockTransactions"></a>

### *@aeternity/aepp-sdk/es/chain.getMicroBlockTransactions() ⇒ `Array.&lt;Object&gt;`*
Expand Down
5 changes: 2 additions & 3 deletions es/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion es/ae/aepp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion es/ae/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions es/node-pool/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions es/utils/aepp-wallet-communication/rpc/aepp-rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions test/integration/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
})
})

Expand Down
8 changes: 6 additions & 2 deletions test/integration/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 41b7bd1

Please sign in to comment.