Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/neuron-wallet/src/controllers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import Key from '../keys/key'
import { ResponseCode } from '.'
import { CatchControllerError } from '../utils/decorators'
import i18n from '../utils/i18n'

export enum HelpersMethod {
GenerateMnemonic = 'generateMnemonic',
}

/**
* @class HelpersController
* @description handle messages from helpers channel
*/
class HelpersController {
public static generateMnemonic = (): Controller.Response<string> => {
/**
* @method generateMnemonic
* @static
* @memberof HelpersController
* @description generate mnemonic
*/

@CatchControllerError
public static async generateMnemonic() {
const mnemonic = Key.generateMnemonic()
if (mnemonic) {
return {
status: ResponseCode.Success,
result: mnemonic,
}
}
return {
status: ResponseCode.Fail,
result: 'Failed to generate mnemonic',
}
throw new Error(i18n.t('messages.failed-to-create-mnemonic'))
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ResponseCode {
Fail,
Success,
}

export const methods = {
NetworksMethod,
WalletsMethod,
Expand Down
199 changes: 108 additions & 91 deletions packages/neuron-wallet/src/controllers/networks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ResponseCode } from '.'
import NetworksService, { NetworkType, NetworkID, Network } from '../services/networks'
import { CatchControllerError } from '../utils/decorators'
import i18n from '../utils/i18n'

export enum NetworksMethod {
GetAll = 'getAll',
Expand All @@ -13,136 +15,151 @@ export enum NetworksMethod {
Status = 'status',
}

/**
* @class NetworksController
* @description handle messages from networks channel
*/
class NetworksController {
static service = new NetworksService()

public static getAll = async () => {
/**
* @method getAll
* @static
* @memberof NetworksController
* @description return all networks if possible
*/

@CatchControllerError
public static async getAll() {
const networks = await NetworksController.service.getAll()
return {
status: ResponseCode.Success,
result: await NetworksController.service.getAll(),
result: networks,
}
}

public static get = async (id: NetworkID) => {
/**
* @method get
* @static
* @memberof NetworksController
* @description get netowrk by id
*/
@CatchControllerError
public static async get(id: NetworkID) {
if (typeof id === 'undefined') throw new Error(i18n.t('messages.id-is-required'))

const network = await NetworksController.service.get(id)
if (network) {
return {
status: ResponseCode.Success,
result: network,
}
}
if (!network) throw new Error(i18n.t('messages.network-of-id-is-not-found', { id }))

return {
status: ResponseCode.Fail,
msg: `Network of id ${id} is not found`,
status: ResponseCode.Success,
result: network,
}
}

public static create = async ({ name, remote, type = NetworkType.Normal }: Network) => {
if (!name || !remote) {
return {
status: ResponseCode.Fail,
msg: 'Name and remote are required',
}
}
// example for return error
if (name === 'error') {
return {
status: ResponseCode.Fail,
msg: `Name cannot be "error"`,
}
}
try {
const created = await NetworksController.service.create(name, remote, type)
return {
status: ResponseCode.Success,
result: created,
}
} catch (err) {
return {
status: ResponseCode.Fail,
msg: err.message,
}
/**
*
* @method create
* @static
* @memberof NetworksController
* @description create network with name, remote address, netowrk type
*/
@CatchControllerError
public static async create({ name, remote, type = NetworkType.Normal }: Network) {
if (!name || !remote) throw new Error(i18n.t('messages.name-and-remote-address-are-required'))
if (name === 'error') throw new Error(i18n.t('messages.invalid-name'))

const created = await NetworksController.service.create(name, remote, type)
return {
status: ResponseCode.Success,
result: created,
}
}

public static update = async (id: NetworkID, options: Partial<Network>) => {
try {
await NetworksController.service.update(id, options)
return {
status: ResponseCode.Success,
result: true,
}
} catch (err) {
return {
status: ResponseCode.Fail,
msg: err.message,
}
/**
* @method update
* @static
* @memberof NetworksController
* @description update network by id
*/
@CatchControllerError
public static async update(id: NetworkID, options: Partial<Network>) {
if (options.name && options.name === 'error') throw new Error(i18n.t('messages.invalid-name'))

await NetworksController.service.update(id, options)
return {
status: ResponseCode.Success,
result: true,
}
}

public static delete = async (id: NetworkID) => {
/**
* @method delete
* @static
* @memberof NetworksController
* @description delete network by id
*/
@CatchControllerError
public static async delete(id: NetworkID) {
const defaultNetwork = await NetworksController.service.defaultOne()
if (defaultNetwork && defaultNetwork.id === id) {
return {
status: ResponseCode.Fail,
msg: 'Default network is unremovable',
}
}
try {
const activeId = await NetworksController.service.activeId()
if (activeId === id) {
if (!defaultNetwork) {
return {
status: ResponseCode.Fail,
msg: 'Default network is not set, cannot delete active network',
}
}
await NetworksController.service.delete(id)
await NetworksController.activate(defaultNetwork.id)
}

if (defaultNetwork && defaultNetwork.id === id) throw new Error(i18n.t('messages.default-network-is-unremovable'))

const activeId = await NetworksController.service.activeId()
if (activeId === id) {
if (!defaultNetwork) throw new Error('messages.cannot-delete-active-network-due-to-lack-of-default-one')
await NetworksController.service.delete(id)
return {
status: ResponseCode.Success,
result: true,
}
} catch (err) {
return {
status: ResponseCode.Fail,
msg: err.message,
}
await NetworksController.activate(defaultNetwork.id)
}
await NetworksController.service.delete(id)

return {
status: ResponseCode.Success,
result: true,
}
}

public static activeOne = async () => {
/**
* @method activeOne
* @static
* @memberof NetworksController
* @description get the currecnt/active network id
*/
@CatchControllerError
public static async activeOne() {
const activeId = await NetworksController.service.activeId()
if (activeId) {
return {
status: ResponseCode.Success,
result: activeId,
}
}
return {
status: ResponseCode.Fail,
msg: 'Active network is not set',
}
throw new Error(i18n.t('messages.active-network-is-not-set'))
}

public static activate = async (id: NetworkID) => {
try {
await NetworksController.service.activate(id)
} catch (err) {
return {
status: ResponseCode.Fail,
msg: err.message,
}
}
/**
* @method activate
* @static
* @memberof NetworksController
* @description set the current/active network by id
*/
@CatchControllerError
public static async activate(id: NetworkID) {
await NetworksController.service.activate(id)
return {
status: ResponseCode.Success,
result: true,
}
}

public static clear = async () => {
/**
* @mehtod clear
* @static
* @memberof NetworksController
* @description clear the networks
*/
@CatchControllerError
public static async clear() {
await NetworksController.service.clear()
return {
status: ResponseCode.Success,
Expand Down
61 changes: 36 additions & 25 deletions packages/neuron-wallet/src/controllers/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,64 @@ import TransactionsService, {
PaginationResult,
TransactionsByLockHashesParam,
} from '../services/transactions'
import { CatchControllerError } from '../utils/decorators'
import i18n from '../utils/i18n'

export default class TransactionsController {
static service = new TransactionsService()

public static getAll = async (
/**
* @method getAll
* @static
* @memberof TransactionsController
* @description get all transactions
*/
@CatchControllerError
public static async getAll(
params: TransactionsByLockHashesParam,
): Promise<Controller.Response<PaginationResult<Transaction>>> => {
): Promise<Controller.Response<PaginationResult<Transaction>>> {
const transactions = await TransactionsService.getAll(params)

if (!transactions) {
return {
status: ResponseCode.Fail,
msg: 'Transactions not found',
}
}
if (!transactions) throw new Error(i18n.t('messages.no-response-from-transaction-service'))

return {
status: ResponseCode.Success,
result: { ...params, ...transactions },
}
}

public static getAllByAddresses = async (
/**
* @method getAllByAddresses
* @static
* @memberof TransactionsController
* @description get all transactions by page number, page size, address list
*/
@CatchControllerError
public static async getAllByAddresses(
params: TransactionsByAddressesParam,
): Promise<Controller.Response<PaginationResult<Transaction>>> => {
): Promise<Controller.Response<PaginationResult<Transaction>>> {
const transactions = await TransactionsService.getAllByAddresses(params)

if (transactions) {
return {
status: ResponseCode.Success,
result: { ...params, ...transactions },
}
}
if (!transactions) throw new Error(i18n.t('messages.no-response-from-transaction-service'))

return {
status: ResponseCode.Fail,
msg: 'Transactions not found',
status: ResponseCode.Success,
result: { ...params, ...transactions },
}
}

public static get = async (hash: string): Promise<Controller.Response<Transaction>> => {
/**
* @method get
* @static
* @memberof TransactionsController
* @description get transaction by hash
*/
@CatchControllerError
public static async get(hash: string): Promise<Controller.Response<Transaction>> {
const transaction = await TransactionsService.get(hash)
if (!transaction) {
return {
status: ResponseCode.Fail,
msg: 'Transaction not found',
}
}

if (!transaction) throw new Error(i18n.t('messages.transaction-is-not-found', { hash }))

return {
status: ResponseCode.Success,
result: transaction,
Expand Down
Loading