Skip to content

Commit

Permalink
multi chain support
Browse files Browse the repository at this point in the history
  • Loading branch information
rastajpa committed Jul 26, 2023
1 parent 0137661 commit 1436747
Show file tree
Hide file tree
Showing 15 changed files with 388 additions and 155 deletions.
9 changes: 9 additions & 0 deletions assets/img/currencies/optimism.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/constants/SupportedCurrencyOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ShibIcon from '../../assets/img/currencies/shib.svg';
import ApeIcon from '../../assets/img/currencies/ape.svg';
import EurocIcon from '../../assets/img/currencies/euroc.svg';
import MaticIcon from '../../assets/img/currencies/matic.svg';
import OptimismIcon from '../../assets/img/currencies/optimism.svg';
import {ImageSourcePropType} from 'react-native';
import {orderBy} from 'lodash';

Expand Down Expand Up @@ -59,6 +60,7 @@ export const CurrencyListIcons: {
shib_m: props => <ShibIcon {...props} />,
ape_m: props => <ApeIcon {...props} />,
euroc_m: props => <EurocIcon {...props} />,
op: props => <OptimismIcon {...props} />,
};

export const SupportedUtxoCurrencyOptions: Array<SupportedCurrencyOption> = [
Expand Down
93 changes: 87 additions & 6 deletions src/constants/WalletConnectV2.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Network} from '.';
import {CurrencyOpts} from './currencies';
export const WALLETCONNECT_V2_METADATA = {
name: 'BitPay Wallet',
description: 'BitPay Wallet',
Expand All @@ -10,31 +11,51 @@ export const EIP155_MAINNET_CHAINS: {[key in string]: any} = {
'eip155:1': {
chainId: 1,
name: 'Ethereum',
chainName: 'eth',
chain: 'eth',
currencyAbbreviation: 'eth',
rpc: 'https://cloudflare-eth.com/',
network: Network.mainnet,
},
'eip155:137': {
chainId: 137,
name: 'Polygon',
chainName: 'matic',
chain: 'matic',
currencyAbbreviation: 'matic',
rpc: 'https://polygon-rpc.com/',
network: Network.mainnet,
},
'eip155:10': {
chainId: 10,
name: 'Optimism',
chain: 'op',
currencyAbbreviation: 'op',
rpc: 'https://mainnet.optimism.io',
network: Network.mainnet,
},
};

export const EIP155_TEST_CHAINS = {
'eip155:5': {
chainId: 5,
name: 'Ethereum Goerli',
chainName: 'eth',
chain: 'eth',
currencyAbbreviation: 'eth',
rpc: 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161',
},
'eip155:80001': {
chainId: 80001,
name: 'Polygon Mumbai',
chainName: 'matic',
rpc: 'https://matic-mumbai.chainstacklabs.com',
chain: 'matic',
currencyAbbreviation: 'matic',
rpc: 'https://matic-mumbai.chainstacklabs.com/',
network: Network.testnet,
},
'eip155:420': {
chainId: 420,
name: 'Optimism Goerli',
chain: 'op',
currencyAbbreviation: 'op',
rpc: 'https://goerli.optimism.io',
network: Network.testnet,
},
};
Expand All @@ -51,16 +72,76 @@ export const EIP155_SIGNING_METHODS = {
};

export const WALLET_CONNECT_SUPPORTED_CHAINS: {
[key in string]: {chain: string; network: string};
[key in string]: {chain: string; network: Network};
} = {
'eip155:1': {chain: 'eth', network: Network.mainnet},
'eip155:137': {chain: 'matic', network: Network.mainnet},
'eip155:10': {chain: 'op', network: Network.mainnet},
'eip155:5': {chain: 'eth', network: Network.testnet},
'eip155:80001': {chain: 'matic', network: Network.testnet},
'eip155:420': {chain: 'op', network: Network.testnet},
};
export type TEIP155Chain = keyof typeof EIP155_CHAINS;

export const EIP155_CHAINS: {[key in string]: any} = {
...EIP155_MAINNET_CHAINS,
...EIP155_TEST_CHAINS,
};

export const WC_EVM_BLOCKCHAIN_EXPLORERS: {[key in string]: any} = {
op: {
[Network.mainnet]: 'optimistic.etherscan.io/',
[Network.testnet]: 'goerli-optimism.etherscan.io/',
},
};

export const WC_PROTOCOL_NAME: {[key in string]: any} = {
op: {
[Network.mainnet]: 'OP Mainnet',
[Network.testnet]: 'OP Goerli',
},
};

export const WalletConnectSupportedEvmCoins: {[key in string]: CurrencyOpts} = {
op: {
name: 'Optimism',
chain: 'op',
coin: 'op',
unitInfo: {
unitName: 'OP',
unitToSatoshi: 1e18,
unitDecimals: 18,
unitCode: 'op',
},
properties: {
hasMultiSig: false,
hasMultiSend: false,
isUtxo: false,
isERCToken: false,
isStableCoin: false,
singleAddress: true,
},
paymentInfo: {
paymentCode: 'EIP155',
protocolPrefix: {livenet: 'op', testnet: 'op'},
ratesApi: '',
blockExplorerUrls: WC_EVM_BLOCKCHAIN_EXPLORERS.op.livenet,
blockExplorerUrlsTestnet: WC_EVM_BLOCKCHAIN_EXPLORERS.op.testnet,
},
feeInfo: {
feeUnit: 'Gwei',
feeUnitAmount: 1e9,
blockTime: 0.2,
maxMerchantFee: 'urgent',
},
theme: {
coinColor: '#ff0621',
backgroundColor: '#ff0621',
gradientBackgroundColor: '#ff0621',
},
},
};

export const WC_EVM_SUPPORTED_COINS = Object.keys(
WalletConnectSupportedEvmCoins,
);
27 changes: 23 additions & 4 deletions src/navigation/wallet-connect/components/Connections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
WCV2Wallet,
} from '../../../store/wallet-connect-v2/wallet-connect-v2.models';
import styled from 'styled-components/native';
import {findWalletByAddress} from '../../../store/wallet/utils/wallet';
import {
findWalletByAddress,
getKeyIdByAddress,
} from '../../../store/wallet/utils/wallet';
import {CurrencyListIcons} from '../../../constants/SupportedCurrencyOptions';

const NoGutter = styled.View`
margin: 0 -10px;
Expand All @@ -24,20 +28,35 @@ const Connections = ({
account?: string;
session?: WCV2SessionType;
keys?: {[key in string]: Key};
wallet?: Wallet;
wallet?: Partial<Wallet>;
}) => {
const navigation = useNavigation();
let address, chain: string;
let wallet: Wallet | undefined;
let wallet: Partial<Wallet> | undefined;

if (account && keys) {
// version 2
const index = account.indexOf(':', account.indexOf(':') + 1);
const protocolChainName = account.substring(0, index);
address = account.substring(index + 1);
chain = EIP155_CHAINS[protocolChainName]?.chainName;
chain = EIP155_CHAINS[protocolChainName]?.chain;
const network = EIP155_CHAINS[protocolChainName]?.network;
wallet = findWalletByAddress(address, chain, network, keys);
if (!wallet && chain && network) {
const keyId = getKeyIdByAddress(address, keys);
wallet = {
id: Math.random().toString(),
network,
receiveAddress: address,
keyId,
chain,
img: CurrencyListIcons[chain],
walletName: EIP155_CHAINS[protocolChainName]?.name,
currencyName: EIP155_CHAINS[protocolChainName]?.name,
currencyAbbreviation:
EIP155_CHAINS[protocolChainName]?.currencyAbbreviation,
};
}
}

const {keyId} = wallet || {};
Expand Down
28 changes: 21 additions & 7 deletions src/navigation/wallet-connect/components/WCV2KeyWalletsRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
WCV2Wallet,
} from '../../../store/wallet-connect-v2/wallet-connect-v2.models';
import {NoGutter} from '../styled/WalletConnectContainers';
import {Network} from '../../../constants';

interface KeyWalletsRowContainerProps {
isLast?: boolean;
Expand Down Expand Up @@ -52,24 +53,37 @@ interface WCV2KeyWalletProps {
keys: WCV2Key[];
onPress: (keyId: string, wallet: WCV2Wallet) => void;
topic?: string;
chainsSelected?: {chainId: string; chain: string; network: Network}[];
}

export const WCV2KeyWalletsRow = ({
keys,
onPress,
topic,
chainsSelected,
}: WCV2KeyWalletProps) => {
const renderItem = useCallback(
({item, keyId, isLast}) => {
return item ? (
<NoGutter key={item.id}>
({
item,
keyId,
isLast,
index,
}: {
item: WCV2Wallet;
keyId: string;
isLast: boolean;
index: number;
}) => {
return item && item.wallet.id ? (
<NoGutter key={`${item.wallet.id}_${index}`}>
<WCV2WalletRow
walletObj={item}
keyId={keyId}
isLast={isLast}
onPress={onPress}
showCheckbox={true}
topic={topic}
chainsSelected={chainsSelected}
/>
</NoGutter>
) : null;
Expand All @@ -78,7 +92,7 @@ export const WCV2KeyWalletsRow = ({
);

const renderKey = useCallback(
({item, isLast}) => {
({item, isLast}: {item: WCV2Key; isLast: boolean}) => {
const {wallets, keyName, keyId} = item;

return wallets.length ? (
Expand All @@ -96,7 +110,7 @@ export const WCV2KeyWalletsRow = ({
<FlatList
contentContainerStyle={{paddingBottom: 20}}
data={wallets}
keyExtractor={(_item, index) => index.toString()}
keyExtractor={(_item, index) => `${keyId}_${index}`}
renderItem={({
item,
index,
Expand All @@ -105,7 +119,7 @@ export const WCV2KeyWalletsRow = ({
index: number;
}) => {
const isLast = index === wallets.length - 1;
return renderItem({item, keyId, isLast});
return renderItem({item, keyId, isLast, index});
}}
/>
</View>
Expand All @@ -121,7 +135,7 @@ export const WCV2KeyWalletsRow = ({
{keys ? (
<FlatList
data={keys}
keyExtractor={(_item, index) => index.toString()}
keyExtractor={item => item.keyId.toString()}
renderItem={({item, index}: {item: WCV2Key; index: number}) => {
const isLast = index === keys.length - 1;
return renderKey({item, isLast});
Expand Down

0 comments on commit 1436747

Please sign in to comment.