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
15 changes: 10 additions & 5 deletions packages/neuron-wallet/src/controllers/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
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> => {
@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
149 changes: 58 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,101 @@ export enum NetworksMethod {
Status = 'status',
}

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

public static getAll = async () => {
@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) => {
@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,
}
@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,
}
@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) => {
@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 () => {
@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,
}
}
@CatchControllerError
public static async activate(id: NetworkID) {
await NetworksController.service.activate(id)
return {
status: ResponseCode.Success,
result: true,
}
}

public static clear = async () => {
@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
10 changes: 10 additions & 0 deletions packages/neuron-wallet/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export default {
'failed-to-activate-wallet': 'Failed to activate wallet',
'failed-to-delete-wallet': 'Failed to delete wallet',
'wallet-name-existed': 'Wallet name existed',
'network-of-id-is-not-found': 'Network of id {{id}} is not found',
'id-is-required': 'ID is required',
'name-and-remote-address-are-required': 'Name and remote address are required',
'invalid-name': 'Name is invalid',
'default-network-is-unremovable': 'Default network is unremovable',
'cannot-delete-active-network-due-to-lack-of-default-one':
'Cannot delete active network due to lack of default one',
'active-network-is-not-set': 'Active network is not set',
'no-response-from-transaction-service': 'No response from transaction service',
'transaction-is-not-found': 'Transaction {{hash}} is not found',
},
},
}
9 changes: 9 additions & 0 deletions packages/neuron-wallet/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export default {
'failed-to-activate-wallet': '设置默认钱包失败',
'failed-to-delete-wallet': '删除钱包失败',
'wallet-name-existed': '钱包名称重复',
'network-of-id-is-not-found': '未找到 Id 为 {{id}} 的网络设置',
'id-is-required': '缺少参数 ID',
'name-and-remote-address-are-required': '缺少名称或地址',
'invalid-name': '非法名称',
'default-network-is-unremovable': '默认网络不可删除',
'cannot-delete-active-network-due-to-lack-of-default-one': '未设置默认网络, 因此无法删除当前网络',
'active-network-is-not-set': '未设置当前网络',
'no-response-from-transaction-service': '交易服务未响应',
'transaction-is-not-found': '未找到交易 {{hash}}',
},
},
}
22 changes: 22 additions & 0 deletions packages/neuron-wallet/src/utils/decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ResponseCode } from '../controllers'

export const CatchControllerError = (_target: any, _name: string, descripor: PropertyDescriptor) => {
const originalMethod = descripor.value
return {
...descripor,
async value(...args: any[]) {
try {
return await originalMethod.apply(this, args)
} catch (err) {
return {
status: ResponseCode.Fail,
msg: err.message,
}
}
},
}
}

export default {
CatchControllerError,
}