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

feat(network): Throw error when can not get networkId #863

Merged
merged 9 commits into from
Jan 22, 2020
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(AeppRpc, Tx, Oracle, Contract, Aens, Chain)
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
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