Skip to content

Commit

Permalink
fix: fixed previously saved network loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Nov 21, 2023
1 parent be8b040 commit c8724b4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
34 changes: 30 additions & 4 deletions src/app/store/networks/networks.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Dictionary } from '@reduxjs/toolkit';
import { ChainID } from '@stacks/transactions';

import { NetworkConfiguration } from '@shared/constants';
import {
BITCOIN_API_BASE_URL_MAINNET,
BITCOIN_API_BASE_URL_TESTNET,
NetworkConfiguration,
} from '@shared/constants';

import { PersistedNetworkConfiguration } from './networks.slice';

Expand Down Expand Up @@ -37,14 +41,36 @@ export function findMatchingNetworkKey({
return null;
}

function checkBitcoinNetworkProperties(
network: PersistedNetworkConfiguration
): PersistedNetworkConfiguration {
if (!network.bitcoinNetwork || !network.bitcoinUrl) {
return {
id: network.id,
name: network.name,
chainId: network.chainId,
subnetChainId: network.subnetChainId,
url: network.url,
bitcoinNetwork: network.chainId === ChainID.Mainnet ? 'mainnet' : 'testnet',
bitcoinUrl:
network.chainId === ChainID.Mainnet
? BITCOIN_API_BASE_URL_MAINNET
: BITCOIN_API_BASE_URL_TESTNET,
};
} else {
return network;
}
}

export function transformNetworkStateToMultichainStucture(
state: Dictionary<PersistedNetworkConfiguration>
) {
return Object.fromEntries(
Object.entries(state)
.map(([key, network]) => {
if (!network) return ['', null];
const { id, name, chainId, subnetChainId, url, bitcoinNetwork, bitcoinUrl } = network;
const { id, name, chainId, subnetChainId, url, bitcoinNetwork, bitcoinUrl } =
checkBitcoinNetworkProperties(network);

return [
key,
Expand All @@ -60,8 +86,8 @@ export function transformNetworkStateToMultichainStucture(
},
bitcoin: {
blockchain: 'bitcoin',
bitcoinNetwork,
bitcoinUrl,
bitcoinNetwork: bitcoinNetwork ? bitcoinNetwork : 'testnet',
bitcoinUrl: bitcoinUrl ? bitcoinUrl : 'https://blockstream.info/testnet/api',
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export const HIRO_API_BASE_URL_MAINNET = 'https://api.hiro.so';
export const HIRO_API_BASE_URL_TESTNET = 'https://api.testnet.hiro.so';
export const HIRO_INSCRIPTIONS_API_URL = 'https://api.hiro.so/ordinals/v1/inscriptions';

const BITCOIN_API_BASE_URL_MAINNET = 'https://blockstream.info/api';
const BITCOIN_API_BASE_URL_TESTNET = 'https://blockstream.info/testnet/api';
export const BITCOIN_API_BASE_URL_MAINNET = 'https://blockstream.info/api';
export const BITCOIN_API_BASE_URL_TESTNET = 'https://blockstream.info/testnet/api';
const BITCOIN_API_BASE_URL_SIGNET = 'https://mempool.space/signet/api';

const networkMainnet: NetworkConfiguration = {
Expand Down

0 comments on commit c8724b4

Please sign in to comment.