Skip to content

Commit

Permalink
Merge branch 'feat/bitcoin-network' into 'dev'
Browse files Browse the repository at this point in the history
feat(rosen-app): add bitcoin network

Closes #252 and #251

See merge request ergo/rosen-bridge/ui!179
  • Loading branch information
vorujack committed Mar 19, 2024
2 parents 090895c + 0558b8c commit 006556e
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-onions-explode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/icons': minor
---

add Bitcoin icon
5 changes: 5 additions & 0 deletions .changeset/perfect-adults-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-ui/xdefi-wallet': minor
---

export some types from sats-connect
5 changes: 5 additions & 0 deletions .changeset/polite-chefs-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/rosen-app': minor
---

add Bitcoin network
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ build:
- npm run build --workspace wallets/flint-wallet
- npm run build --workspace wallets/vespr-wallet
- npm run build --workspace wallets/nautilus-wallet
- npm run build --workspace wallets/xdefi-wallet

type_check:
stage: type_check
Expand Down
2 changes: 2 additions & 0 deletions apps/rosen/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
CARDANO_KOIOS_API=''
ERGO_EXPLORER_API=''
BITCOIN_ESPLORA_API=''
NEXT_PUBLIC_ERGO_LOCK_ADDRESS=''
NEXT_PUBLIC_CARDANO_LOCK_ADDRESS=''
NEXT_PUBLIC_BITCOIN_LOCK_ADDRESS=''
NEXT_PUBLIC_FEE_CONFIG_TOKEN_ID=''

POSTGRES_URL= # postgresql://username:password@host:port/databasename
Expand Down
10 changes: 8 additions & 2 deletions apps/rosen/app/_actions/calculateFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ const ergoExplorerClient = ergoExplorerClientFactory(
process.env.ERGO_EXPLORER_API!,
);

import { Networks, feeConfigTokenId } from '@/_constants';
import { Networks, ERGO_EXPLORER_URL, feeConfigTokenId } from '@/_constants';

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

/**
Expand Down Expand Up @@ -46,7 +52,7 @@ export const calculateFee = async (
};
}

const minimumFee = new BridgeMinimumFee(explorerUrl, feeConfigTokenId);
const minimumFee = new BridgeMinimumFee(ERGO_EXPLORER_URL, feeConfigTokenId);

try {
const [fees, nextFees] = await Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions apps/rosen/app/_constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export const Networks = {
ergo: 'ergo',
cardano: 'cardano',
bitcoin: 'bitcoin',
} as const;

export const feeConfigTokenId = process.env.NEXT_PUBLIC_FEE_CONFIG_TOKEN_ID!;

export const CARDANO_BASE_TX_URL = 'https://cardanoscan.io/transaction/';
export const ERGO_BASE_TX_URL =
'https://explorer.ergoplatform.com/transactions/';
export const ERGO_EXPLORER_URL = 'https://api.ergoplatform.com/';
6 changes: 4 additions & 2 deletions apps/rosen/app/_hooks/useNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { useMemo } from 'react';
import { TokenMap } from '@rosen-bridge/tokens';
import { useMemo } from 'react';

import useBridgeForm from './useBridgeForm';
import { useTokensMap } from './useTokensMap';

import ErgoNetwork from '@/_networks/ergo';
import BitcoinNetwork from '@/_networks/bitcoin';
import CardanoNetwork from '@/_networks/cardano';
import ErgoNetwork from '@/_networks/ergo';

import { Networks } from '@/_constants';

const availableNetworks = {
[Networks.ergo]: ErgoNetwork,
[Networks.cardano]: CardanoNetwork,
[Networks.bitcoin]: BitcoinNetwork,
};

type Chain = string;
Expand Down
6 changes: 2 additions & 4 deletions apps/rosen/app/_hooks/useTransactionFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import { useTokensMap } from './useTokensMap';

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

import ErgoNetwork from '@/_networks/ergo';

import { Networks } from '@/_constants';
import { ERGO_EXPLORER_URL, Networks } from '@/_constants';

/**
* calculates the fees for a token swap between
Expand Down Expand Up @@ -74,7 +72,7 @@ const useTransactionFees = (
const data = await calculateFee(
sourceChain,
tokenId,
ErgoNetwork.api.explorerUrl,
ERGO_EXPLORER_URL,
selectedNetwork.nextHeightInterval,
);
if (data.status === 'success') {
Expand Down
90 changes: 90 additions & 0 deletions apps/rosen/app/_networks/bitcoin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { BitcoinIcon } from '@rosen-bridge/icons';
import { RosenChainToken } from '@rosen-bridge/tokens';
import getXdefiWallet, {
AddressPurpose,
BitcoinNetworkType,
walletInfo as XdefiWalletInfo,
isXdefiAvailable,
} from '@rosen-ui/xdefi-wallet';
import { validateDecimalPlaces } from '@rosen-ui/utils';
import { Wallet } from '@rosen-ui/wallet-api';

import { compact } from 'lodash-es';

import { convertNumberToBigint } from '@/_utils';

import { Networks } from '@/_constants';

import { Network } from '@/_types/network';

/**
* the main object for Bitcoin network
* providing access to network info and wallets and network specific
* functionality
*/
const BitcoinNetwork: Network<Wallet> = {
name: Networks.bitcoin,
label: 'Bitcoin',
supportedWallets: [XdefiWalletInfo],
availableWallets: compact([
isXdefiAvailable() && {
...getXdefiWallet(),
getBalance: (token) =>
new Promise((resolve, reject) => {
getXdefiWallet().api.getAddress({
payload: {
message: '',
network: {
type: BitcoinNetworkType.Mainnet,
},
purposes: [AddressPurpose.Payment],
},
onFinish: ({ addresses }) => {
/**
* TODO: Complete getBalance
* local:ergo/rosen-bridge/ui#236
*/
throw new Error('Not implemented');
},
onCancel: () => {
reject();
},
});
}),
transfer: async (
token: RosenChainToken,
decimalAmount: number,
toChain: string,
toAddress: string,
decimalBridgeFee: number,
decimalNetworkFee: number,
lockAddress: string,
) => {
validateDecimalPlaces(decimalAmount, token.decimals);
validateDecimalPlaces(decimalBridgeFee, token.decimals);
validateDecimalPlaces(decimalNetworkFee, token.decimals);

const amount = convertNumberToBigint(
decimalAmount * 10 ** token.decimals,
);
const bridgeFee = convertNumberToBigint(
decimalBridgeFee * 10 ** token.decimals,
);
const networkFee = convertNumberToBigint(
decimalNetworkFee * 10 ** token.decimals,
);

/**
* TODO: Complete transfer
* local:ergo/rosen-bridge/ui#236
*/
throw new Error('Not implemented');
},
},
]),
logo: BitcoinIcon,
nextHeightInterval: 1,
lockAddress: process.env.NEXT_PUBLIC_BITCOIN_LOCK_ADDRESS!,
};

export default BitcoinNetwork;
3 changes: 0 additions & 3 deletions apps/rosen/app/_networks/cardano/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,6 @@ const CardanoNetwork: Network<Wallet> = {
]),
nextHeightInterval: 25,
logo: CardanoIcon,
api: {
explorerUrl: 'https://api.koios.rest/api',
},
lockAddress: process.env.NEXT_PUBLIC_CARDANO_LOCK_ADDRESS!,
};

Expand Down
3 changes: 0 additions & 3 deletions apps/rosen/app/_networks/ergo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ const ErgoNetwork: Network<Wallet> = {
]),
logo: ErgoIcon,
nextHeightInterval: 5,
api: {
explorerUrl: 'https://api.ergoplatform.com/',
},
lockAddress: process.env.NEXT_PUBLIC_ERGO_LOCK_ADDRESS!,
};

Expand Down
3 changes: 0 additions & 3 deletions apps/rosen/app/_types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export interface Network<T> {
availableWallets: T[];
supportedWallets: WalletInfo[];
nextHeightInterval: number;
api: {
explorerUrl: string;
};
lockAddress: string;
}
export type SupportedWallets = Wallet;
5 changes: 2 additions & 3 deletions apps/rosen/app/_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import { RosenChainToken, TokenMap } from '@rosen-bridge/tokens';
import { getDecimalString } from '@rosen-ui/utils';

import { feeAndMinBoxValue as cardanoFeeAndMinBoxValue } from '@/_networks/cardano/transaction/consts';
import ErgoNetwork from '@/_networks/ergo';

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

import { Networks } from '@/_constants';
import { ERGO_EXPLORER_URL, Networks } from '@/_constants';
import {
fee as ergoFee,
minBoxValue as ergoMinBoxValue,
Expand Down Expand Up @@ -75,7 +74,7 @@ export const getMinTransferAmount = async (
const data = await calculateFee(
sourceChain,
ergoTokenId,
ErgoNetwork.api.explorerUrl,
ERGO_EXPLORER_URL,
0,
);
const parsedData = {
Expand Down
1 change: 1 addition & 0 deletions apps/rosen/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ npm run build --workspace wallets/eternl-wallet
npm run build --workspace wallets/flint-wallet
npm run build --workspace wallets/vespr-wallet
npm run build --workspace wallets/nautilus-wallet
npm run build --workspace wallets/xdefi-wallet

cd apps/rosen

Expand Down
9 changes: 5 additions & 4 deletions apps/rosen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@
"@rosen-bridge/watcher-data-extractor": "^4.0.0",
"@rosen-clients/cardano-koios": "^2.0.1",
"@rosen-clients/ergo-explorer": "^1.0.2",
"@rosen-ui/nami-wallet": "^0.0.3",
"@rosen-ui/lace-wallet": "^0.0.3",
"@rosen-ui/common-hooks": "^0.1.0",
"@rosen-ui/eternl-wallet": "^0.0.3",
"@rosen-ui/flint-wallet": "^0.0.3",
"@rosen-ui/vespr-wallet": "^0.0.3",
"@rosen-ui/lace-wallet": "^0.0.3",
"@rosen-ui/nami-wallet": "^0.0.3",
"@rosen-ui/nautilus-wallet": "^0.0.3",
"@rosen-ui/utils": "^0.1.0",
"@rosen-ui/vespr-wallet": "^0.0.3",
"@rosen-ui/wallet-api": "^0.0.2",
"@rosen-ui/common-hooks": "^0.1.0",
"@rosen-ui/xdefi-wallet": "^0.1.1",
"buffer": "^6.0.3",
"cbor-x": "^1.5.7",
"ergo-lib-wasm-nodejs": "^0.24.0",
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/icons/src/networks/bitcoin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/icons/src/networks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { ReactComponent as BitcoinIcon } from './bitcoin.svg';
export { ReactComponent as CardanoIcon } from './cardano.svg';
export { ReactComponent as ErgoIcon } from './ergo.svg';
2 changes: 2 additions & 0 deletions wallets/xdefi-wallet/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ const getXdefiWallet = () =>
export const isXdefiAvailable = () =>
typeof xfi !== 'undefined' && !!xfi?.bitcoin;

export { BitcoinNetworkType, AddressPurpose } from 'sats-connect';

export default getXdefiWallet;

0 comments on commit 006556e

Please sign in to comment.