From 86281c179de4db1103a19d0d5ed6c6eb6b199be6 Mon Sep 17 00:00:00 2001 From: Masood Abdolian Date: Sun, 17 Nov 2024 17:07:48 +0000 Subject: [PATCH] fix: Enhance the Rosen app's source code by reorganizing, relocating, and renaming certain files and methods --- .changeset/strange-lobsters-greet.md | 5 ++ apps/rosen/app/_backend/assets/repository.ts | 4 +- apps/rosen/app/_backend/assets/services.ts | 6 +-- apps/rosen/app/_backend/events/services.ts | 5 +- apps/rosen/app/_errors/NotFoundError.ts | 1 - apps/rosen/app/_hooks/useTokenMap.ts | 2 +- apps/rosen/app/_networks/bitcoin/client.ts | 39 +++++++++++++++ .../app/_networks/bitcoin/getMaxTransfer.ts | 5 +- apps/rosen/app/_networks/bitcoin/index.ts | 40 +--------------- apps/rosen/app/_networks/bitcoin/server.ts | 12 ++--- apps/rosen/app/_networks/cardano/client.ts | 47 ++++++++++++++++++ .../app/_networks/cardano/getMaxTransfer.ts | 5 +- apps/rosen/app/_networks/cardano/index.ts | 48 +------------------ apps/rosen/app/_networks/cardano/server.ts | 12 ++--- apps/rosen/app/_networks/ergo/client.ts | 31 ++++++++++++ .../app/_networks/ergo/getMaxTransfer.ts | 5 +- apps/rosen/app/_networks/ergo/index.ts | 32 +------------ apps/rosen/app/_networks/ergo/server.ts | 12 ++--- apps/rosen/app/_networks/ethereum/client.ts | 32 +++++++++++++ .../app/_networks/ethereum/getMaxTransfer.ts | 5 +- apps/rosen/app/_networks/ethereum/index.ts | 33 +------------ apps/rosen/app/_networks/ethereum/server.ts | 5 +- .../getClientTokenMap.ts} | 3 +- .../utils.ts => _tokenMap/getRosenTokens.ts} | 0 .../getServerTokenMap.ts} | 6 +-- apps/rosen/app/_utils/getMinTransfer.ts | 2 +- apps/rosen/app/api/v1/withValidation.ts | 4 +- 27 files changed, 193 insertions(+), 208 deletions(-) create mode 100644 .changeset/strange-lobsters-greet.md delete mode 100644 apps/rosen/app/_errors/NotFoundError.ts create mode 100644 apps/rosen/app/_networks/bitcoin/client.ts create mode 100644 apps/rosen/app/_networks/cardano/client.ts create mode 100644 apps/rosen/app/_networks/ergo/client.ts create mode 100644 apps/rosen/app/_networks/ethereum/client.ts rename apps/rosen/app/{_networks/getTokenMap.client.ts => _tokenMap/getClientTokenMap.ts} (86%) rename apps/rosen/app/{_backend/utils.ts => _tokenMap/getRosenTokens.ts} (100%) rename apps/rosen/app/{_networks/getTokenMap.server.ts => _tokenMap/getServerTokenMap.ts} (65%) diff --git a/.changeset/strange-lobsters-greet.md b/.changeset/strange-lobsters-greet.md new file mode 100644 index 00000000..07592714 --- /dev/null +++ b/.changeset/strange-lobsters-greet.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/rosen-app': patch +--- + +Enhance the Rosen app's source code by reorganizing, relocating, and renaming certain files and methods diff --git a/apps/rosen/app/_backend/assets/repository.ts b/apps/rosen/app/_backend/assets/repository.ts index ecce1068..894fbe6c 100644 --- a/apps/rosen/app/_backend/assets/repository.ts +++ b/apps/rosen/app/_backend/assets/repository.ts @@ -5,8 +5,6 @@ import { } from '@rosen-ui/asset-calculator'; import { Network } from '@rosen-ui/types'; -import { NotFoundError } from '@/_errors/NotFoundError'; - import { dataSource } from '../dataSource'; import '../initialize-datasource-if-needed'; @@ -41,7 +39,7 @@ export const getAsset = async (id: string) => { }); if (!token) { - throw new NotFoundError(`Token with id [${id}] not found`); + throw new ReferenceError(`Token with id [${id}] not found`); } const bridged: Pick< diff --git a/apps/rosen/app/_backend/assets/services.ts b/apps/rosen/app/_backend/assets/services.ts index 1a0a9935..ebd70bd9 100644 --- a/apps/rosen/app/_backend/assets/services.ts +++ b/apps/rosen/app/_backend/assets/services.ts @@ -1,6 +1,4 @@ -import { TokenMap } from '@rosen-bridge/tokens'; - -import { getRosenTokens } from '@/_backend/utils'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; import { AssetFilters, @@ -8,7 +6,7 @@ import { getAllAssets as repositoryGetAllAssets, } from './repository'; -const tokenMap = new TokenMap(getRosenTokens()); +const tokenMap = getTokenMap(); /** * return asset details diff --git a/apps/rosen/app/_backend/events/services.ts b/apps/rosen/app/_backend/events/services.ts index d05bbb2a..7d1df2fd 100644 --- a/apps/rosen/app/_backend/events/services.ts +++ b/apps/rosen/app/_backend/events/services.ts @@ -1,12 +1,11 @@ -import { TokenMap } from '@rosen-bridge/tokens'; import { Network } from '@rosen-ui/types'; -import { getRosenTokens } from '@/_backend/utils'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; import { UNSUPPORTED_TOKEN_NAME } from '../constants'; import { getEvents } from './repository'; -const tokenMap = new TokenMap(getRosenTokens()); +const tokenMap = getTokenMap(); /** * get full token data associated with a tokenId and a chain diff --git a/apps/rosen/app/_errors/NotFoundError.ts b/apps/rosen/app/_errors/NotFoundError.ts deleted file mode 100644 index 0e1f90f3..00000000 --- a/apps/rosen/app/_errors/NotFoundError.ts +++ /dev/null @@ -1 +0,0 @@ -export class NotFoundError extends Error {} diff --git a/apps/rosen/app/_hooks/useTokenMap.ts b/apps/rosen/app/_hooks/useTokenMap.ts index d2b47e41..8435523c 100644 --- a/apps/rosen/app/_hooks/useTokenMap.ts +++ b/apps/rosen/app/_hooks/useTokenMap.ts @@ -2,7 +2,7 @@ import { useEffect, useState, useTransition } from 'react'; import { TokenMap } from '@rosen-bridge/tokens'; -import { getTokenMap } from '@/_networks/getTokenMap.client'; +import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; /** * return TokenMap instance diff --git a/apps/rosen/app/_networks/bitcoin/client.ts b/apps/rosen/app/_networks/bitcoin/client.ts new file mode 100644 index 00000000..4dc49a33 --- /dev/null +++ b/apps/rosen/app/_networks/bitcoin/client.ts @@ -0,0 +1,39 @@ +import { BitcoinIcon } from '@rosen-bridge/icons'; +import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; +import { xdefiWalletCreator } from '@rosen-ui/xdefi-wallet'; + +import { unwrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; +import { BitcoinNetwork as BitcoinNetworkType } from '@/_types'; + +import { getMaxTransfer } from './getMaxTransfer'; +import { + generateOpReturnData, + generateUnsignedTx, + submitTransaction, + getAddressBalance, +} from './server'; + +const config = { + getTokenMap, + generateOpReturnData: unwrap(generateOpReturnData), + generateUnsignedTx: unwrap(generateUnsignedTx), + getAddressBalance: unwrap(getAddressBalance), + submitTransaction: unwrap(submitTransaction), +}; + +/** + * the main object for Bitcoin network + * providing access to network info and wallets and network specific + * functionality + */ +export const BitcoinNetwork: BitcoinNetworkType = { + name: NETWORKS.BITCOIN, + label: NETWORK_LABELS.BITCOIN, + logo: BitcoinIcon, + wallets: [xdefiWalletCreator(config)], + nextHeightInterval: 1, + lockAddress: process.env.NEXT_PUBLIC_BITCOIN_LOCK_ADDRESS!, + getMaxTransfer: unwrap(getMaxTransfer), + toSafeAddress: (address) => address, +}; diff --git a/apps/rosen/app/_networks/bitcoin/getMaxTransfer.ts b/apps/rosen/app/_networks/bitcoin/getMaxTransfer.ts index a8d497e3..b6e5bd0d 100644 --- a/apps/rosen/app/_networks/bitcoin/getMaxTransfer.ts +++ b/apps/rosen/app/_networks/bitcoin/getMaxTransfer.ts @@ -10,10 +10,9 @@ import { import { NATIVE_TOKENS, NETWORKS } from '@rosen-ui/constants'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; import { BitcoinNetwork } from '@/_types'; -import { getTokenMap } from '../getTokenMap.server'; - /** * get max transfer for bitcoin */ @@ -47,7 +46,7 @@ const getMaxTransferCore: BitcoinNetwork['getMaxTransfer'] = async ({ const estimatedFee = Math.ceil((estimatedTxWeight / 4) * feeRatio); const minSatoshi = await getMinimumMeaningfulSatoshi(feeRatio); - const tokenMap = await getTokenMap(); + const tokenMap = getTokenMap(); const offset = tokenMap.wrapAmount( NATIVE_TOKENS.BITCOIN, diff --git a/apps/rosen/app/_networks/bitcoin/index.ts b/apps/rosen/app/_networks/bitcoin/index.ts index 543b6c2f..4f1cce44 100644 --- a/apps/rosen/app/_networks/bitcoin/index.ts +++ b/apps/rosen/app/_networks/bitcoin/index.ts @@ -1,39 +1 @@ -import { BitcoinIcon } from '@rosen-bridge/icons'; -import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; -import { xdefiWalletCreator } from '@rosen-ui/xdefi-wallet'; - -import { unwrap } from '@/_safeServerAction'; -import { BitcoinNetwork as BitcoinNetworkType } from '@/_types'; - -import { getTokenMap } from '../getTokenMap.client'; -import { getMaxTransfer } from './getMaxTransfer'; -import { - generateOpReturnData, - generateUnsignedTx, - submitTransaction, - getAddressBalance, -} from './server'; - -const config = { - getTokenMap, - generateOpReturnData: unwrap(generateOpReturnData), - generateUnsignedTx: unwrap(generateUnsignedTx), - getAddressBalance: unwrap(getAddressBalance), - submitTransaction: unwrap(submitTransaction), -}; - -/** - * the main object for Bitcoin network - * providing access to network info and wallets and network specific - * functionality - */ -export const BitcoinNetwork: BitcoinNetworkType = { - name: NETWORKS.BITCOIN, - label: NETWORK_LABELS.BITCOIN, - logo: BitcoinIcon, - wallets: [xdefiWalletCreator(config)], - nextHeightInterval: 1, - lockAddress: process.env.NEXT_PUBLIC_BITCOIN_LOCK_ADDRESS!, - getMaxTransfer: unwrap(getMaxTransfer), - toSafeAddress: (address) => address, -}; +export * from './client'; diff --git a/apps/rosen/app/_networks/bitcoin/server.ts b/apps/rosen/app/_networks/bitcoin/server.ts index c3889df5..e60547ea 100644 --- a/apps/rosen/app/_networks/bitcoin/server.ts +++ b/apps/rosen/app/_networks/bitcoin/server.ts @@ -1,6 +1,5 @@ 'use server'; -import { TokenMap } from '@rosen-bridge/tokens'; import { generateOpReturnData as generateOpReturnDataCore, generateUnsignedTx as generateUnsignedTxCore, @@ -8,19 +7,16 @@ import { submitTransaction as submitTransactionCore, } from '@rosen-network/bitcoin'; -import { getRosenTokens } from '@/_backend/utils'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; export const generateOpReturnData = wrap(generateOpReturnDataCore, { traceKey: 'generateOpReturnData', }); -export const generateUnsignedTx = wrap( - generateUnsignedTxCore(new TokenMap(getRosenTokens())), - { - traceKey: 'generateUnsignedTx', - }, -); +export const generateUnsignedTx = wrap(generateUnsignedTxCore(getTokenMap()), { + traceKey: 'generateUnsignedTx', +}); export const getAddressBalance = wrap(getAddressBalanceCore, { cache: 3000, diff --git a/apps/rosen/app/_networks/cardano/client.ts b/apps/rosen/app/_networks/cardano/client.ts new file mode 100644 index 00000000..1dcd1385 --- /dev/null +++ b/apps/rosen/app/_networks/cardano/client.ts @@ -0,0 +1,47 @@ +import { CardanoIcon } from '@rosen-bridge/icons'; +import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; +import { eternlWalletCreator } from '@rosen-ui/eternl-wallet'; +import { flintWalletCreator } from '@rosen-ui/flint-wallet'; +import { laceWalletCreator } from '@rosen-ui/lace-wallet'; +import { namiWalletCreator } from '@rosen-ui/nami-wallet'; + +import { unwrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; +import { CardanoNetwork as CardanoNetworkType } from '@/_types'; + +import { getMaxTransfer } from './getMaxTransfer'; +import { + decodeWasmValue, + generateLockAuxiliaryData, + generateUnsignedTx, + setTxWitnessSet, +} from './server'; + +const config = { + getTokenMap, + decodeWasmValue: unwrap(decodeWasmValue), + generateLockAuxiliaryData: unwrap(generateLockAuxiliaryData), + generateUnsignedTx: unwrap(generateUnsignedTx), + setTxWitnessSet: unwrap(setTxWitnessSet), +}; + +/** + * the main object for Cardano network + * providing access to network info and wallets and network specific + * functionality + */ +export const CardanoNetwork: CardanoNetworkType = { + name: NETWORKS.CARDANO, + label: NETWORK_LABELS.CARDANO, + wallets: [ + eternlWalletCreator(config), + flintWalletCreator(config), + laceWalletCreator(config), + namiWalletCreator(config), + ], + nextHeightInterval: 25, + logo: CardanoIcon, + lockAddress: process.env.NEXT_PUBLIC_CARDANO_LOCK_ADDRESS!, + getMaxTransfer: unwrap(getMaxTransfer), + toSafeAddress: (address) => address, +}; diff --git a/apps/rosen/app/_networks/cardano/getMaxTransfer.ts b/apps/rosen/app/_networks/cardano/getMaxTransfer.ts index 0a9a2777..bc934ea6 100644 --- a/apps/rosen/app/_networks/cardano/getMaxTransfer.ts +++ b/apps/rosen/app/_networks/cardano/getMaxTransfer.ts @@ -4,10 +4,9 @@ import { feeAndMinBoxValue } from '@rosen-network/cardano/dist/src/constants'; import { NATIVE_TOKENS, NETWORKS } from '@rosen-ui/constants'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; import { CardanoNetwork } from '@/_types'; -import { getTokenMap } from '../getTokenMap.server'; - /** * get max transfer for cardano */ @@ -15,7 +14,7 @@ const getMaxTransferCore: CardanoNetwork['getMaxTransfer'] = async ({ balance, isNative, }) => { - const tokenMap = await getTokenMap(); + const tokenMap = getTokenMap(); const feeAndMinBoxValueWrapped = tokenMap.wrapAmount( NATIVE_TOKENS.CARDANO, feeAndMinBoxValue, diff --git a/apps/rosen/app/_networks/cardano/index.ts b/apps/rosen/app/_networks/cardano/index.ts index c42386c5..4f1cce44 100644 --- a/apps/rosen/app/_networks/cardano/index.ts +++ b/apps/rosen/app/_networks/cardano/index.ts @@ -1,47 +1 @@ -import { CardanoIcon } from '@rosen-bridge/icons'; -import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; -import { eternlWalletCreator } from '@rosen-ui/eternl-wallet'; -import { flintWalletCreator } from '@rosen-ui/flint-wallet'; -import { laceWalletCreator } from '@rosen-ui/lace-wallet'; -import { namiWalletCreator } from '@rosen-ui/nami-wallet'; - -import { unwrap } from '@/_safeServerAction'; -import { CardanoNetwork as CardanoNetworkType } from '@/_types'; - -import { getTokenMap } from '../getTokenMap.client'; -import { getMaxTransfer } from './getMaxTransfer'; -import { - decodeWasmValue, - generateLockAuxiliaryData, - generateUnsignedTx, - setTxWitnessSet, -} from './server'; - -const config = { - getTokenMap, - decodeWasmValue: unwrap(decodeWasmValue), - generateLockAuxiliaryData: unwrap(generateLockAuxiliaryData), - generateUnsignedTx: unwrap(generateUnsignedTx), - setTxWitnessSet: unwrap(setTxWitnessSet), -}; - -/** - * the main object for Cardano network - * providing access to network info and wallets and network specific - * functionality - */ -export const CardanoNetwork: CardanoNetworkType = { - name: NETWORKS.CARDANO, - label: NETWORK_LABELS.CARDANO, - wallets: [ - eternlWalletCreator(config), - flintWalletCreator(config), - laceWalletCreator(config), - namiWalletCreator(config), - ], - nextHeightInterval: 25, - logo: CardanoIcon, - lockAddress: process.env.NEXT_PUBLIC_CARDANO_LOCK_ADDRESS!, - getMaxTransfer: unwrap(getMaxTransfer), - toSafeAddress: (address) => address, -}; +export * from './client'; diff --git a/apps/rosen/app/_networks/cardano/server.ts b/apps/rosen/app/_networks/cardano/server.ts index 3584e0af..e0659d3a 100644 --- a/apps/rosen/app/_networks/cardano/server.ts +++ b/apps/rosen/app/_networks/cardano/server.ts @@ -1,6 +1,5 @@ 'use server'; -import { TokenMap } from '@rosen-bridge/tokens'; import { decodeWasmValue as decodeWasmValueCore, generateLockAuxiliaryData as generateLockAuxiliaryDataCore, @@ -8,8 +7,8 @@ import { setTxWitnessSet as setTxWitnessSetCore, } from '@rosen-network/cardano'; -import { getRosenTokens } from '@/_backend/utils'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; export const decodeWasmValue = wrap(decodeWasmValueCore, { cache: Infinity, @@ -20,12 +19,9 @@ export const generateLockAuxiliaryData = wrap(generateLockAuxiliaryDataCore, { traceKey: 'generateLockAuxiliaryData', }); -export const generateUnsignedTx = wrap( - generateUnsignedTxCore(new TokenMap(getRosenTokens())), - { - traceKey: 'generateUnsignedTx', - }, -); +export const generateUnsignedTx = wrap(generateUnsignedTxCore(getTokenMap()), { + traceKey: 'generateUnsignedTx', +}); export const setTxWitnessSet = wrap(setTxWitnessSetCore, { traceKey: 'setTxWitnessSet', diff --git a/apps/rosen/app/_networks/ergo/client.ts b/apps/rosen/app/_networks/ergo/client.ts new file mode 100644 index 00000000..3d5238e2 --- /dev/null +++ b/apps/rosen/app/_networks/ergo/client.ts @@ -0,0 +1,31 @@ +import { ErgoIcon } from '@rosen-bridge/icons'; +import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; +import { nautilusWalletCreator } from '@rosen-ui/nautilus-wallet'; + +import { unwrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; +import { ErgoNetwork as ErgoNetworkType } from '@/_types'; + +import { getMaxTransfer } from './getMaxTransfer'; +import { generateUnsignedTx } from './server'; + +const config = { + getTokenMap, + generateUnsignedTx: unwrap(generateUnsignedTx), +}; + +/** + * the main object for Ergo network + * providing access to network info and wallets and network specific + * functionality + */ +export const ErgoNetwork: ErgoNetworkType = { + name: NETWORKS.ERGO, + label: NETWORK_LABELS.ERGO, + wallets: [nautilusWalletCreator(config)], + logo: ErgoIcon, + nextHeightInterval: 5, + lockAddress: process.env.NEXT_PUBLIC_ERGO_LOCK_ADDRESS!, + getMaxTransfer: unwrap(getMaxTransfer), + toSafeAddress: (address) => address, +}; diff --git a/apps/rosen/app/_networks/ergo/getMaxTransfer.ts b/apps/rosen/app/_networks/ergo/getMaxTransfer.ts index 2fa4e2bb..3e4ec6ce 100644 --- a/apps/rosen/app/_networks/ergo/getMaxTransfer.ts +++ b/apps/rosen/app/_networks/ergo/getMaxTransfer.ts @@ -7,10 +7,9 @@ import { import { NATIVE_TOKENS, NETWORKS } from '@rosen-ui/constants'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; import { ErgoNetwork } from '@/_types'; -import { getTokenMap } from '../getTokenMap.server'; - /** * get max transfer for ergo */ @@ -18,7 +17,7 @@ const getMaxTransferCore: ErgoNetwork['getMaxTransfer'] = async ({ balance, isNative, }) => { - const tokenMap = await getTokenMap(); + const tokenMap = getTokenMap(); const feeAndMinBoxValueWrapped = tokenMap.wrapAmount( NATIVE_TOKENS.ERGO, ergoFee + ergoMinBoxValue, diff --git a/apps/rosen/app/_networks/ergo/index.ts b/apps/rosen/app/_networks/ergo/index.ts index cc21021a..4f1cce44 100644 --- a/apps/rosen/app/_networks/ergo/index.ts +++ b/apps/rosen/app/_networks/ergo/index.ts @@ -1,31 +1 @@ -import { ErgoIcon } from '@rosen-bridge/icons'; -import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; -import { nautilusWalletCreator } from '@rosen-ui/nautilus-wallet'; - -import { unwrap } from '@/_safeServerAction'; -import { ErgoNetwork as ErgoNetworkType } from '@/_types'; - -import { getTokenMap } from '../getTokenMap.client'; -import { getMaxTransfer } from './getMaxTransfer'; -import { generateUnsignedTx } from './server'; - -const config = { - getTokenMap, - generateUnsignedTx: unwrap(generateUnsignedTx), -}; - -/** - * the main object for Ergo network - * providing access to network info and wallets and network specific - * functionality - */ -export const ErgoNetwork: ErgoNetworkType = { - name: NETWORKS.ERGO, - label: NETWORK_LABELS.ERGO, - wallets: [nautilusWalletCreator(config)], - logo: ErgoIcon, - nextHeightInterval: 5, - lockAddress: process.env.NEXT_PUBLIC_ERGO_LOCK_ADDRESS!, - getMaxTransfer: unwrap(getMaxTransfer), - toSafeAddress: (address) => address, -}; +export * from './client'; diff --git a/apps/rosen/app/_networks/ergo/server.ts b/apps/rosen/app/_networks/ergo/server.ts index b4119ca3..7d071e42 100644 --- a/apps/rosen/app/_networks/ergo/server.ts +++ b/apps/rosen/app/_networks/ergo/server.ts @@ -1,14 +1,10 @@ 'use server'; -import { TokenMap } from '@rosen-bridge/tokens'; import { generateUnsignedTx as generateUnsignedTxCore } from '@rosen-network/ergo'; -import { getRosenTokens } from '@/_backend/utils'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; -export const generateUnsignedTx = wrap( - generateUnsignedTxCore(new TokenMap(getRosenTokens())), - { - traceKey: 'generateUnsignedTx', - }, -); +export const generateUnsignedTx = wrap(generateUnsignedTxCore(getTokenMap()), { + traceKey: 'generateUnsignedTx', +}); diff --git a/apps/rosen/app/_networks/ethereum/client.ts b/apps/rosen/app/_networks/ethereum/client.ts new file mode 100644 index 00000000..3c5ca9e3 --- /dev/null +++ b/apps/rosen/app/_networks/ethereum/client.ts @@ -0,0 +1,32 @@ +import { EthereumIcon } from '@rosen-bridge/icons'; +import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; +import { metaMaskWalletCreator } from '@rosen-ui/metamask-wallet'; + +import { unwrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; +import { EthereumNetwork as EthereumNetworkType } from '@/_types'; + +import { getMaxTransfer } from './getMaxTransfer'; +import { generateLockData, generateTxParameters } from './server'; + +/** + * the main object for Ethereum network + * providing access to network info and wallets and network specific + * functionality + */ +export const EthereumNetwork: EthereumNetworkType = { + name: NETWORKS.ETHEREUM, + label: NETWORK_LABELS.ETHEREUM, + wallets: [ + metaMaskWalletCreator({ + getTokenMap, + generateLockData: unwrap(generateLockData), + generateTxParameters: unwrap(generateTxParameters), + }), + ], + logo: EthereumIcon, + nextHeightInterval: 0, + lockAddress: process.env.NEXT_PUBLIC_ETHEREUM_LOCK_ADDRESS!, + getMaxTransfer: unwrap(getMaxTransfer), + toSafeAddress: (address) => address.toLowerCase(), +}; diff --git a/apps/rosen/app/_networks/ethereum/getMaxTransfer.ts b/apps/rosen/app/_networks/ethereum/getMaxTransfer.ts index 3a147659..c23d2f4e 100644 --- a/apps/rosen/app/_networks/ethereum/getMaxTransfer.ts +++ b/apps/rosen/app/_networks/ethereum/getMaxTransfer.ts @@ -4,10 +4,9 @@ import { ETH_TRANSFER_GAS, getFeeData } from '@rosen-network/ethereum'; import { NATIVE_TOKENS, NETWORKS } from '@rosen-ui/constants'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; import { EthereumNetwork } from '@/_types'; -import { getTokenMap } from '../getTokenMap.server'; - /** * get max transfer for ethereum */ @@ -18,7 +17,7 @@ const getMaxTransferCore: EthereumNetwork['getMaxTransfer'] = async ({ const feeData = await getFeeData(); if (!feeData.gasPrice) throw Error(`gas price is null`); const estimatedFee = feeData.gasPrice * ETH_TRANSFER_GAS; - const tokenMap = await getTokenMap(); + const tokenMap = getTokenMap(); const wrappedFee = tokenMap.wrapAmount( NATIVE_TOKENS.ETHEREUM, diff --git a/apps/rosen/app/_networks/ethereum/index.ts b/apps/rosen/app/_networks/ethereum/index.ts index 05eee767..4f1cce44 100644 --- a/apps/rosen/app/_networks/ethereum/index.ts +++ b/apps/rosen/app/_networks/ethereum/index.ts @@ -1,32 +1 @@ -import { EthereumIcon } from '@rosen-bridge/icons'; -import { NETWORK_LABELS, NETWORKS } from '@rosen-ui/constants'; -import { metaMaskWalletCreator } from '@rosen-ui/metamask-wallet'; - -import { unwrap } from '@/_safeServerAction'; -import { EthereumNetwork as EthereumNetworkType } from '@/_types'; - -import { getTokenMap } from '../getTokenMap.client'; -import { getMaxTransfer } from './getMaxTransfer'; -import { generateLockData, generateTxParameters } from './server'; - -/** - * the main object for Ethereum network - * providing access to network info and wallets and network specific - * functionality - */ -export const EthereumNetwork: EthereumNetworkType = { - name: NETWORKS.ETHEREUM, - label: NETWORK_LABELS.ETHEREUM, - wallets: [ - metaMaskWalletCreator({ - getTokenMap, - generateLockData: unwrap(generateLockData), - generateTxParameters: unwrap(generateTxParameters), - }), - ], - logo: EthereumIcon, - nextHeightInterval: 0, - lockAddress: process.env.NEXT_PUBLIC_ETHEREUM_LOCK_ADDRESS!, - getMaxTransfer: unwrap(getMaxTransfer), - toSafeAddress: (address) => address.toLowerCase(), -}; +export * from './client'; diff --git a/apps/rosen/app/_networks/ethereum/server.ts b/apps/rosen/app/_networks/ethereum/server.ts index 3999e814..d9c9a1b4 100644 --- a/apps/rosen/app/_networks/ethereum/server.ts +++ b/apps/rosen/app/_networks/ethereum/server.ts @@ -1,20 +1,19 @@ 'use server'; -import { TokenMap } from '@rosen-bridge/tokens'; import { generateLockData as generateLockDataCore, generateTxParameters as generateTxParametersCore, } from '@rosen-network/ethereum'; -import { getRosenTokens } from '@/_backend/utils'; import { wrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getServerTokenMap'; export const generateLockData = wrap(generateLockDataCore, { traceKey: 'generateLockData', }); export const generateTxParameters = wrap( - generateTxParametersCore(new TokenMap(getRosenTokens())), + generateTxParametersCore(getTokenMap()), { traceKey: 'generateTxParameters', }, diff --git a/apps/rosen/app/_networks/getTokenMap.client.ts b/apps/rosen/app/_tokenMap/getClientTokenMap.ts similarity index 86% rename from apps/rosen/app/_networks/getTokenMap.client.ts rename to apps/rosen/app/_tokenMap/getClientTokenMap.ts index 20bea5b2..e8383f50 100644 --- a/apps/rosen/app/_networks/getTokenMap.client.ts +++ b/apps/rosen/app/_tokenMap/getClientTokenMap.ts @@ -1,8 +1,9 @@ import { TokenMap } from '@rosen-bridge/tokens'; -import { getRosenTokensWithCache } from '@/_backend/utils'; import { unwrap } from '@/_safeServerAction'; +import { getRosenTokensWithCache } from './getRosenTokens'; + let tokenMap: TokenMap; /** diff --git a/apps/rosen/app/_backend/utils.ts b/apps/rosen/app/_tokenMap/getRosenTokens.ts similarity index 100% rename from apps/rosen/app/_backend/utils.ts rename to apps/rosen/app/_tokenMap/getRosenTokens.ts diff --git a/apps/rosen/app/_networks/getTokenMap.server.ts b/apps/rosen/app/_tokenMap/getServerTokenMap.ts similarity index 65% rename from apps/rosen/app/_networks/getTokenMap.server.ts rename to apps/rosen/app/_tokenMap/getServerTokenMap.ts index dbd6cf05..1962f5eb 100644 --- a/apps/rosen/app/_networks/getTokenMap.server.ts +++ b/apps/rosen/app/_tokenMap/getServerTokenMap.ts @@ -1,15 +1,15 @@ import { TokenMap } from '@rosen-bridge/tokens'; -import { getRosenTokens } from '@/_backend/utils'; +import { getRosenTokens } from './getRosenTokens'; let tokenMap: TokenMap; /** * get a TokenMap instance using the Rosen TokenMapObject in the server. */ -export const getTokenMap = async () => { +export const getTokenMap = () => { if (tokenMap) return tokenMap; - const tokens = await getRosenTokens(); + const tokens = getRosenTokens(); tokenMap = new TokenMap(tokens); return tokenMap; }; diff --git a/apps/rosen/app/_utils/getMinTransfer.ts b/apps/rosen/app/_utils/getMinTransfer.ts index 405c1b08..31eb5a3c 100644 --- a/apps/rosen/app/_utils/getMinTransfer.ts +++ b/apps/rosen/app/_utils/getMinTransfer.ts @@ -2,8 +2,8 @@ import { RosenChainToken } from '@rosen-bridge/tokens'; import { Network, RosenAmountValue } from '@rosen-ui/types'; import { calculateFee } from '@/_actions'; -import { getTokenMap } from '@/_networks/getTokenMap.client'; import { unwrap } from '@/_safeServerAction'; +import { getTokenMap } from '@/_tokenMap/getClientTokenMap'; /** * get min transfer amount of a token diff --git a/apps/rosen/app/api/v1/withValidation.ts b/apps/rosen/app/api/v1/withValidation.ts index 1a1a82bf..56c79e5b 100644 --- a/apps/rosen/app/api/v1/withValidation.ts +++ b/apps/rosen/app/api/v1/withValidation.ts @@ -2,8 +2,6 @@ import { NextRequest } from 'next/server'; import { ValidationResult } from 'joi'; -import { NotFoundError } from '@/_errors/NotFoundError'; - /** * a wrapper around handler returning a function which validates request, * converts request data during validation, and return general errors responses @@ -31,7 +29,7 @@ export const withValidation = const response = await handler(value); return Response.json(response); } catch (error) { - if (error instanceof NotFoundError) { + if (error instanceof ReferenceError) { return Response.json(error.message, { status: 404 }); } if (error instanceof Error) {