Skip to content

Commit

Permalink
Merge branch 'dev' into 'master'
Browse files Browse the repository at this point in the history
Merge dev into master

See merge request ergo/rosen-bridge/ui!259
  • Loading branch information
zargarzadehm committed Jul 3, 2024
2 parents 067457c + e23d1ed commit 770835b
Show file tree
Hide file tree
Showing 68 changed files with 482 additions and 236 deletions.
File renamed without changes.
5 changes: 5 additions & 0 deletions .changeset/tasty-carrots-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-ui/asset-calculator': minor
---

Save bridged token ids inside bridged assets table
6 changes: 3 additions & 3 deletions apps/guard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@rosen-bridge/icons": "^0.4.0",
"@rosen-bridge/ui-kit": "^1.1.1",
"@rosen-ui/constants": "^0.0.2",
"@rosen-bridge/ui-kit": "^1.1.2",
"@rosen-ui/constants": "^0.0.3",
"@rosen-ui/swr-helpers": "^0.1.0",
"@rosen-bridge/shared-contexts": "^0.0.1",
"@rosen-ui/utils": "^0.2.0",
"@rosen-ui/utils": "^0.3.0",
"moment": "^2.29.4",
"next": "14.1.0",
"react": "^18.2.0",
Expand Down
20 changes: 20 additions & 0 deletions apps/rosen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# @rosen-bridge/rosen-app

## 1.1.1

### Patch Changes

- Refactor the Network constant usage and eliminate unnecessary utilities
- Implement validation for Bitcoin addresses
- Prevent the assets page from crashing if tokens file networks mismatch with the project networks
- Updated dependencies
- @rosen-ui/nami-wallet@0.1.3
- @rosen-ui/nautilus-wallet@0.2.0
- @rosen-ui/xdefi-wallet@0.4.0
- @rosen-ui/utils@0.3.0
- @rosen-network/bitcoin@0.2.0
- @rosen-network/ergo@0.1.2
- @rosen-ui/eternl-wallet@0.1.3
- @rosen-ui/flint-wallet@0.1.3
- @rosen-ui/lace-wallet@0.1.3
- @rosen-ui/vespr-wallet@0.0.7
- @rosen-ui/wallet-api@1.0.2

## 1.1.0

### Minor Changes
Expand Down
6 changes: 6 additions & 0 deletions apps/rosen/app/(bridge)/BridgeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Button,
CircularProgress,
SvgIcon,
Alert,
} from '@rosen-bridge/ui-kit';

import useBridgeForm from '@/_hooks/useBridgeForm';
Expand Down Expand Up @@ -331,6 +332,11 @@ const BridgeForm = () => {
{...addressField}
value={addressField.value ?? ''}
/>
{targetField.value == 'bitcoin' && (
<Alert severity="warning">
Only Native SegWit (P2WPKH or P2WSH) addresses are supported.
</Alert>
)}
</FormContainer>
);
};
Expand Down
17 changes: 10 additions & 7 deletions apps/rosen/app/_actions/calculateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ const ergoExplorerClient = ergoExplorerClientFactory(
process.env.ERGO_EXPLORER_API!,
);

import { ERGO_EXPLORER_URL, Networks, feeConfigTokenId } from '@/_constants';
import { Networks } from '@rosen-ui/constants';
import { ERGO_EXPLORER_URL, feeConfigTokenId } from '@/_constants';
import { AvailableNetworks } from '@/_networks';

const GetHeight = {
cardano: async () => (await cardanoKoiosClient.getTip())[0].block_no,
ergo: async () =>
[Networks.CARDANO]: async () =>
(await cardanoKoiosClient.getTip())[0].block_no,
[Networks.ERGO]: async () =>
Number((await ergoExplorerClient.v1.getApiV1Networkstate()).height),
bitcoin: async (): Promise<number> => {
[Networks.BITCOIN]: async (): Promise<number> => {
const response = await fetch(
`${process.env.BITCOIN_ESPLORA_API}/api/blocks/tip/height`,
);
return response.json();
},
};
} as const;

/**
* fetches and return the minimum fee object for a specific token in network
Expand All @@ -37,8 +40,8 @@ const GetHeight = {
*/

export const calculateFee = async (
sourceNetwork: keyof typeof Networks,
targetNetwork: keyof typeof Networks,
sourceNetwork: AvailableNetworks,
targetNetwork: AvailableNetworks,
tokenId: string,
nextHeightInterval: number,
) => {
Expand Down
18 changes: 0 additions & 18 deletions apps/rosen/app/_actions/cardanoDecoder.ts

This file was deleted.

14 changes: 10 additions & 4 deletions apps/rosen/app/_actions/validateAddress.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
'use server';

import { Address } from 'ergo-lib-wasm-nodejs';
import { Networks } from '@/_constants';
import { Networks } from '@rosen-ui/constants';

import * as wasm from '@emurgo/cardano-serialization-lib-nodejs';
import { AvailableNetworks } from '@/_networks';
import { isValidAddress } from '@rosen-network/bitcoin';

/**
* server action to verify the wallet addresses
* @param walletAddress - wallet address to verify
* @returns the validation results for the passed address
*/
export const validateAddress = (
chain: keyof typeof Networks,
chain: AvailableNetworks,
walletAddress: string,
) => {
try {
if (chain === Networks.ergo) {
if (chain === Networks.ERGO) {
Address.from_base58(walletAddress);
} else if (chain === Networks.cardano) {
} else if (chain === Networks.CARDANO) {
wasm.Address.from_bech32(walletAddress);
} else if (chain == Networks.BITCOIN) {
if (!isValidAddress(walletAddress)) {
throw new Error();
}
}
return { isValid: true };
} catch {
Expand Down
29 changes: 19 additions & 10 deletions apps/rosen/app/_backend/events/event-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ const tokenMap = new TokenMap(getRosenTokens());
* @param chain
*/
const getFullTokenData = (tokenId: string, chain: string) => {
const token = tokenMap.search(chain, {
[tokenMap.getIdKey(chain)]: tokenId,
});

return {
tokenId: tokenId,
name: token[0]?.[chain].name ?? UNSUPPORTED_TOKEN_NAME,
decimals: token[0]?.[chain].decimals ?? 0,
isNativeToken: token[0]?.[chain].metaData.type === 'native',
};
try {
const token = tokenMap.search(chain, {
[tokenMap.getIdKey(chain)]: tokenId,
});

return {
tokenId: tokenId,
name: token[0]?.[chain].name ?? UNSUPPORTED_TOKEN_NAME,
decimals: token[0]?.[chain].decimals ?? 0,
isNativeToken: token[0]?.[chain].metaData.type === 'native',
};
} catch {
return {
tokenId: tokenId,
name: UNSUPPORTED_TOKEN_NAME,
decimals: 0,
isNativeToken: false,
};
}
};

/**
Expand Down
6 changes: 0 additions & 6 deletions apps/rosen/app/_constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export const Networks = {
ergo: 'ergo',
cardano: 'cardano',
bitcoin: 'bitcoin',
} as const;

export const feeConfigTokenId = process.env.NEXT_PUBLIC_FEE_CONFIG_TOKEN_ID!;

export const ERGO_EXPLORER_URL = 'https://api.ergoplatform.com/';
2 changes: 1 addition & 1 deletion apps/rosen/app/_hooks/useNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TokenMap } from '@rosen-bridge/tokens';
import { useMemo } from 'react';

import { Networks } from '@/_constants';
import { Networks } from '@rosen-ui/constants';
import { AvailableNetworks, availableNetworks } from '@/_networks';

import useBridgeForm from './useBridgeForm';
Expand Down
6 changes: 3 additions & 3 deletions apps/rosen/app/_hooks/useTransactionFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { useTokensMap } from './useTokensMap';

import { calculateFee } from '@/_actions/calculateFee';

import { Networks } from '@/_constants';
import { AvailableNetworks } from '@/_networks';

/**
* calculates the fees for a token swap between
* two networks
*/
const useTransactionFees = (
sourceChain: keyof typeof Networks | null,
targetChain: keyof typeof Networks | null,
sourceChain: AvailableNetworks | null,
targetChain: AvailableNetworks | null,
token: RosenChainToken | null,
amount: string | null,
) => {
Expand Down
4 changes: 2 additions & 2 deletions apps/rosen/app/_networks/bitcoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { compact } from 'lodash-es';

import getMaxTransfer from './getMaxTransfer';

import { Networks } from '@/_constants';
import { Networks } from '@rosen-ui/constants';

import { BitcoinNetwork as BitcoinNetworkType } from '@/_types/network';

Expand All @@ -22,7 +22,7 @@ import {
* functionality
*/
const BitcoinNetwork: BitcoinNetworkType = {
name: Networks.bitcoin,
name: Networks.BITCOIN,
label: 'Bitcoin',
logo: BitcoinIcon,
supportedWallets: [xdefiWalletInfo],
Expand Down
8 changes: 3 additions & 5 deletions apps/rosen/app/_networks/cardano/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { compact } from 'lodash-es';

import { validateDecimalPlaces } from '@rosen-ui/utils';
import { convertNumberToBigint, validateDecimalPlaces } from '@rosen-ui/utils';

import { Networks } from '@/_constants';
import { Networks } from '@rosen-ui/constants';

import { CardanoNetwork as CardanoNetworkType } from '@/_types/network';

import { RosenChainToken } from '@rosen-bridge/tokens';
import { CardanoIcon } from '@rosen-bridge/icons';

import { convertNumberToBigint } from '@/_utils';

import { eternlWalletCreator, eternlWalletInfo } from '@rosen-ui/eternl-wallet';
import { flintWalletCreator, flintWalletInfo } from '@rosen-ui/flint-wallet';
import { laceWalletCreator, laceWalletInfo } from '@rosen-ui/lace-wallet';
Expand All @@ -36,7 +34,7 @@ import getVesprWallet, {
* functionality
*/
const CardanoNetwork: CardanoNetworkType = {
name: Networks.cardano,
name: Networks.CARDANO,
label: 'Cardano',
supportedWallets: [
eternlWalletInfo,
Expand Down
4 changes: 2 additions & 2 deletions apps/rosen/app/_networks/ergo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@rosen-ui/nautilus-wallet';
import { compact } from 'lodash-es';

import { Networks } from '@/_constants';
import { Networks } from '@rosen-ui/constants';

import { ErgoNetwork as ErgoNetworkType } from '@/_types/network';
import { ErgoIcon } from '@rosen-bridge/icons';
Expand All @@ -22,7 +22,7 @@ import { generateUnsignedTx } from './server';
* functionality
*/
const ErgoNetwork: ErgoNetworkType = {
name: Networks.ergo,
name: Networks.ERGO,
label: 'Ergo',
supportedWallets: [nautilusWalletInfo],
availableWallets: compact([
Expand Down
15 changes: 9 additions & 6 deletions apps/rosen/app/_networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import BitcoinNetwork from './bitcoin';
import CardanoNetwork from './cardano';
import ErgoNetwork from './ergo';

import { Networks } from '@/_constants';
import { Networks } from '@rosen-ui/constants';

export const availableNetworks = {
[Networks.ergo]: ErgoNetwork,
[Networks.cardano]: CardanoNetwork,
[Networks.bitcoin]: BitcoinNetwork,
};
[Networks.ERGO]: ErgoNetwork,
[Networks.CARDANO]: CardanoNetwork,
[Networks.BITCOIN]: BitcoinNetwork,
} as const;

export type AvailableNetworks = keyof typeof Networks;
export type AvailableNetworks = Exclude<
(typeof Networks)[keyof typeof Networks],
'ethereum'
>;
6 changes: 2 additions & 4 deletions apps/rosen/app/_types/network.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Wallet, WalletInfo } from '@rosen-ui/wallet-api';

import { Networks } from '@/_constants';

type NetworksType = typeof Networks;
import { AvailableNetworks } from '@/_networks';

interface GetMaxTransferParams {
balance: number;
Expand All @@ -13,7 +11,7 @@ interface GetMaxTransferParams {
* the main network interface for all supported networks
*/
export interface BaseNetwork<
NetworkName extends keyof NetworksType,
NetworkName extends AvailableNetworks,
GetMaxTransferParamsExtra = {},
> {
name: NetworkName;
Expand Down
29 changes: 6 additions & 23 deletions apps/rosen/app/_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { getDecimalString } from '@rosen-ui/utils';

import { calculateFee } from '@/_actions/calculateFee';

import { Networks } from '@/_constants';

import { encode } from 'cbor-x';
import { Networks } from '@rosen-ui/constants';
import { AvailableNetworks } from '@/_networks';

/**
* a utility to make unique interface for accessing token name
*/
export const getTokenNameAndId = (
token: RosenChainToken,
network: keyof typeof Networks,
network: AvailableNetworks,
) => {
if ([Networks.ergo, Networks.cardano, Networks.bitcoin].includes(network)) {
if ([Networks.ERGO, Networks.CARDANO, Networks.BITCOIN].includes(network)) {
return {
tokenName: token.name,
tokenId: token.tokenId,
Expand All @@ -32,8 +31,8 @@ export const getTokenNameAndId = (
*/
export const getMinTransfer = async (
token: RosenChainToken,
sourceChain: keyof typeof Networks,
targetChain: keyof typeof Networks,
sourceChain: AvailableNetworks,
targetChain: AvailableNetworks,
tokensMap: any,
) => {
const tokenMap = new TokenMap(tokensMap);
Expand Down Expand Up @@ -62,19 +61,3 @@ export const getMinTransfer = async (
)
: '0';
};

/**
* convert a hex to cbor
* @param hex
*/
export const hexToCbor = (hex: string) =>
Buffer.from(encode(Buffer.from(hex, 'hex'))).toString('hex');

/**
* remove the decimal points from the input number and
* convert number to bigInt
* @param inputNumber
*/

export const convertNumberToBigint = (inputNumber: number) =>
BigInt(Math.trunc(inputNumber));
Loading

0 comments on commit 770835b

Please sign in to comment.