Skip to content

Commit

Permalink
feat(connect): update bnb to bsc
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Dec 16, 2024
1 parent f6dff70 commit cf2d1ab
Show file tree
Hide file tree
Showing 28 changed files with 252 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/address-validator/src/currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,7 @@ var CURRENCIES = [
},
{
name: 'BNB Smart Chain',
symbol: 'bnb',
symbol: 'bsc',
validator: ETHValidator,
},
{
Expand Down
20 changes: 10 additions & 10 deletions packages/address-validator/tests/wallet_address_validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,8 @@ describe('WAValidator.validate()', function () {
isValidAddressType('aaaaaaaaaaaaaaa000000000000000', 'siacoin', 'prod', undefined);
});

it('should return true for correct BNB addresses', function () {
valid('0x0590396689ee1d287147e9383fb8dd24532f2006', 'bnb');
it('should return true for correct BSC addresses', function () {
valid('0x0590396689ee1d287147e9383fb8dd24532f2006', 'bsc');
valid('0x07fc5c2bcaa0fa6bdaa4fff897490312c8f33c27', 'BNB smart chain');
});

Expand Down Expand Up @@ -1116,12 +1116,12 @@ describe('WAValidator.validate()', function () {
});

it('should return true for correct BNB smart chain address', function () {
valid('0x7ae2f5b9e386cd1b50a4550696d957cb4900f03a', 'bnb');
valid('0x7ae2f5b9e386cd1b50a4550696d957cb4900f03a', 'bsc');
valid('0x0000000000000000000000000000000000001000', 'BNB Smart Chain');
});

it('should return false for incorrect BNB smart chain address', function () {
invalid('bnb1xlvns0n2mxh77mzaspn2hgav4rr4m8eerfju38', 'bnb');
invalid('bnb1xlvns0n2mxh77mzaspn2hgav4rr4m8eerfju38', 'bsc');
});

it('should return true for correct xtz(tezos) address', function () {
Expand Down Expand Up @@ -1567,13 +1567,13 @@ describe('WAValidator.validate()', function () {
invalid('g4VPBPrHZkfE8CsjuG2S4yBQNd455UWmk', 'stellar');
});

it('should return false for incorrect bnb addresses', function () {
it('should return false for incorrect bsc addresses', function () {
commonTests('bnb smart chain');
commonTests('bnb');
invalid('xrb_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjdgu', 'bnb');
invalid('nano_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjdgu', 'bnb');
invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'bnb');
invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'bnb');
commonTests('bsc');
invalid('xrb_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjdgu', 'bsc');
invalid('nano_1f5e4w33ndqbkx4bw5jtp13kp5xghebfxcmw9hdt1f7goid1s4373w6tjdgu', 'bsc');
invalid('xrb_1111111112111111111111111111111111111111111111111111hifc8npp', 'bsc');
invalid('nano_111111111111111111111111111111111111111111111111111hifc8npp', 'bsc');
});

it('should return false for incorrect xtz(tezos) address', function () {
Expand Down
2 changes: 1 addition & 1 deletion packages/connect-explorer-theme/src/components/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function Menu({
const prevRoute = useRef(route);

const coinSymbols = {
binance: 'bnb',
binance: 'bsc',
bitcoin: 'btc',
cardano: 'ada',
eos: 'eos',
Expand Down
2 changes: 1 addition & 1 deletion packages/connect/src/data/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const config = {
],
},
{
coin: ['bnb'],
coin: ['bsc'],
min: { T1B1: '1.9.0', T2T1: '2.3.0' },
comment: [
'There were protobuf backwards incompatible changes with introduction of 1.9.0/2.3.0 firmwares',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const COINS: Record<NetworkSymbol | LegacyNetworkSymbol, string> = {
ada: require('../../images/coins/ada.svg'),
base: require('../../images/coins/base.svg'),
bch: require('../../images/coins/bch.svg'),
bnb: require('../../images/coins/bnb.svg'),
bsc: require('../../images/coins/bnb.svg'),
btc: require('../../images/coins/btc.svg'),
btg: require('../../images/coins/btg.svg'),
dash: require('../../images/coins/dash.svg'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const selectAssetModalOptions: (
coingeckoId: 'binancecoin',
contractAddress: null,
ticker: 'BNB',
symbol: 'bnb',
symbol: 'bsc',
},
{
type: 'group',
Expand Down Expand Up @@ -474,7 +474,7 @@ export const selectAssetModalNetworks: NetworkFilterCategory[] = [
},
{
name: 'BNB Smart Chain',
symbol: 'bnb',
symbol: 'bsc',
coingeckoId: 'binance-smart-chain',
coingeckoNativeId: 'binancecoin',
},
Expand Down
5 changes: 5 additions & 0 deletions packages/suite/src/storage/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { PartialRecord } from '@trezor/type-utils';

import type { CustomBackend, BlockbookUrl } from 'src/types/wallet/backend';
import type { State } from 'src/reducers/wallet/settingsReducer';
import { migrationOfBnbNetwork } from 'src/storage/migrations/networks/bnb';

import { updateAll } from './utils';
import type { DBWalletAccountTransaction, SuiteDBSchema } from '../definitions';
Expand Down Expand Up @@ -1149,4 +1150,8 @@ export const migrate: OnUpgradeFunc<SuiteDBSchema> = async (
return tx;
});
}

if (oldVersion < 50) {
await migrationOfBnbNetwork(db, oldVersion, newVersion, transaction);
}
};
201 changes: 201 additions & 0 deletions packages/suite/src/storage/migrations/networks/bnb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import type { OnUpgradeFunc } from '@trezor/suite-storage';

import { updateAll } from '../utils';
import type { SuiteDBSchema } from '../../definitions';

export const migrationOfBnbNetwork: OnUpgradeFunc<SuiteDBSchema> = async (
_db,
_oldVersion,
_newVersion,
transaction,
) => {
// migrate bnb to bsc

await updateAll(transaction, 'walletSettings', walletSettings => {
// @ts-expect-error
const indexOfBnb = walletSettings.enabledNetworks.indexOf('bnb');
if (indexOfBnb !== -1) {
walletSettings.enabledNetworks[indexOfBnb] = 'bsc';
}

return walletSettings;
});

await updateAll(transaction, 'suiteSettings', suiteSettings => {
if (
// @ts-expect-error
typeof suiteSettings.evmSettings?.confirmExplanationModalClosed?.bnb == 'boolean'
) {
suiteSettings.evmSettings.confirmExplanationModalClosed.bsc =
// @ts-expect-error
suiteSettings.evmSettings.confirmExplanationModalClosed.bnb;
// @ts-expect-error
delete suiteSettings.evmSettings.confirmExplanationModalClosed.bnb;
}

if (
// @ts-expect-error
typeof suiteSettings.evmSettings?.explanationBannerClosed?.bnb == 'boolean'
) {
suiteSettings.evmSettings.explanationBannerClosed.bsc =
// @ts-expect-error
suiteSettings.evmSettings.explanationBannerClosed.bnb;
// @ts-expect-error
delete suiteSettings.evmSettings.explanationBannerClosed.bnb;
}

return suiteSettings;
});

const backendSettings = transaction.objectStore('backendSettings');
// @ts-expect-error
const bnbBackendSettings = await backendSettings.get('bnb');
if (bnbBackendSettings) {
backendSettings.add(bnbBackendSettings, 'bsc');
// @ts-expect-error
backendSettings.delete('bnb');
}

const tokenManagement = transaction.objectStore('tokenManagement');
const bnbTokenManagementShow = await tokenManagement.get('bnb-coin-show');
if (bnbTokenManagementShow) {
tokenManagement.add(bnbTokenManagementShow, 'bsc-coin-show');
tokenManagement.delete('bnb-coin-show');
}

const bnbTokenManagementHide = await tokenManagement.get('bnb-coin-hide');
if (bnbTokenManagementHide) {
tokenManagement.add(bnbTokenManagementHide, 'bsc-coin-hide');
tokenManagement.delete('bnb-coin-hide');
}

await updateAll(transaction, 'discovery', discovery => {
discovery.networks = discovery.networks.map(network =>
// @ts-expect-error
network === 'bnb' ? 'bsc' : network,
);

discovery.failed = discovery.failed.map(network => {
// @ts-expect-error
if (network.symbol === 'bnb') {
network = { ...network, symbol: 'bsc' };
}

return network;
});

return discovery;
});

const accounts = transaction.objectStore('accounts');
let accountsCursor = await accounts.openCursor();
while (accountsCursor) {
const account = accountsCursor.value;
// @ts-expect-error
if (account.symbol === 'bnb') {
const newAccount = {
...account,
symbol: 'bsc' as const,
key: account.key.replace('bnb', 'bsc'),
};
await accountsCursor.delete();
await accounts.add(newAccount);
}

accountsCursor = await accountsCursor.continue();
}

await updateAll(transaction, 'walletSettings', walletSettings => {
if (walletSettings.lastUsedFeeLevel['bnb']) {
walletSettings.lastUsedFeeLevel = {
...walletSettings.lastUsedFeeLevel,
bsc: { ...walletSettings.lastUsedFeeLevel['bnb'] },
};

delete walletSettings.lastUsedFeeLevel['bnb'];
}

return walletSettings;
});

await updateAll(transaction, 'txs', tx => {
// @ts-expect-error
if (tx.tx.symbol === 'bnb') {
tx.tx = { ...tx.tx, symbol: 'bsc' };
}

return tx;
});

const graphs = transaction.objectStore('graph');
let graphCursor = await graphs.openCursor();
while (graphCursor) {
const graph = graphCursor.value;
//@ts-expect-error
if (graph.account.symbol === 'bnb') {
const newGraph = {
...graph,
account: { ...graph.account, symbol: 'bsc' as const },
};
await graphCursor.delete();
await graphs.add(newGraph);
}

graphCursor = await graphCursor.continue();
}

await updateAll(transaction, 'historicRates', rates => {
const rate = Object.keys(rates).reduce((newRates, key) => {
const newKey = key.replace('bnb', 'bsc');
// @ts-expect-error
newRates[newKey] = rates[key];

return newRates;
}, {});

return rate;
});

const historicRates = transaction.objectStore('historicRates');
const historicRatesKeys = await historicRates.getAllKeys();
const historicRatesKeysWithBnb = historicRatesKeys.filter(key => key.includes('bnb'));

historicRatesKeysWithBnb.forEach(async key => {
const rate = await historicRates.get(key);
if (rate) {
historicRates.add(rate, key.replace('bnb', 'bsc'));
}
historicRates.delete(key);
});

const sendFormDrafts = transaction.objectStore('sendFormDrafts');
const sendFormDraftsKeys = await sendFormDrafts.getAllKeys();
const sendFormDraftsKeysWithBnb = sendFormDraftsKeys.filter(key => key.includes('bnb'));

sendFormDraftsKeysWithBnb.forEach(async key => {
const draft = await sendFormDrafts.get(key);
if (draft) {
sendFormDrafts.add(draft, key.replace('bnb', 'bsc'));
}
sendFormDrafts.delete(key);
});

const formDrafts = transaction.objectStore('formDrafts');
const formDraftsKeys = await formDrafts.getAllKeys();
const formDraftsKeysWithBnb = formDraftsKeys.filter(key => key.includes('bnb'));

formDraftsKeysWithBnb.forEach(async key => {
const draft = await formDrafts.get(key);
if (draft) {
formDrafts.add(draft, key.replace('bnb', 'bsc'));
}
formDrafts.delete(key);
});

await updateAll(transaction, 'coinmarketTrades', trade => {
// @ts-expect-error
if (trade.account.symbol === 'bnb') {
trade.account.symbol = 'bsc';
}
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const CoinmarketFormInputCryptoSelect = <
};

const getNetworks = () => {
const networksToSelect: NetworkSymbol[] = ['eth', 'sol', 'pol', 'bnb'];
const networksToSelect: NetworkSymbol[] = ['eth', 'sol', 'pol', 'bsc'];
const networkKeys = networkSymbolCollection.filter(item => networksToSelect.includes(item));
const networksSelected: NetworkFilterCategory[] = networkKeys.map(networkKey => {
const network = getNetwork(networkKey);
Expand Down
2 changes: 1 addition & 1 deletion packages/theme/src/coinsColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const coinsColors: CoinsColors = {
ada: '#3468d1',
base: '#0052ff',
bch: '#0ac18e',
bnb: '#f0b90b',
bsc: '#f0b90b',
btc: '#f29937',
btg: '#3b5185',
dash: '#1796e4',
Expand Down
2 changes: 1 addition & 1 deletion packages/utxo-lib/src/coinselect/inputs/branchAndBound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const branchAndBound: CoinSelectAlgorithm = (
feeRate,
options,
): CoinSelectResult => {
if (options.baseFee) return { fee: 0 }; // TEMP: disable bnb algorithm for DOGE
if (options.baseFee) return { fee: 0 }; // TEMP: disable bsc algorithm for DOGE
if (utxos.find(u => u.required)) return { fee: 0 }; // TODO: enable bnb algorithm if required utxos are defined

// cost of change: cost of additional output in current tx (fee) + minimum possible value of that output (dust)
Expand Down
Loading

0 comments on commit cf2d1ab

Please sign in to comment.