Created
November 9, 2022 03:21
-
-
Save adairrr/d894aa420977a997bf034e271ec5c5f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connectStation = () => { | |
setWalletStatus(WalletStatus.INITIALIZING) | |
try { | |
const provider = await tendermint() | |
const supportedChains = await provider.getSupportedChains() | |
log.debug('supportedChains', supportedChains) | |
// If network not already there, add | |
if ( | |
![...supportedChains.official, ...supportedChains.unofficial].includes( | |
network.name.toLowerCase() | |
) | |
) { | |
// Mainnet chanis seem to use the address prefix | |
if (network.isMainnet) { | |
stationNetwork.chainId = network.addressPrefix | |
} | |
await provider.addChain(stationNetwork) | |
} | |
provider.onAccountChanged(disconnect) | |
provider.offAccountChanged(disconnect) | |
await provider.getAccount(network.id) | |
const offlineSigner = await getStationOfflineSigner(network.id) | |
setSigner(offlineSigner) | |
const client = await connectAndGetClient(offlineSigner) | |
setClient(client) | |
setWalletType('station') | |
setWalletStatus(WalletStatus.WALLET_CONNECTED) | |
} catch (e: any) { | |
setWalletStatus(WalletStatus.ERROR) | |
log.error('caught an error trying to connect to station', e) | |
/* | |
4001 - User rejected request | |
4100 - The requested account and/or method has not been authorized by the user. | |
4200 - The requested method is not supported | |
-32000 - Invalid input. | |
-32600 - The JSON sent is not a valid Request object. | |
-32602 - Invalid method parameter(s). | |
-32603 - Internal JSON-RPC error. | |
*/ | |
let message = undefined | |
switch (e.code) { | |
case 4001: | |
message = 'User rejected request' | |
break | |
case 4100: | |
message = | |
'The requested account and/or method has not been authorized by the user. Please click "connect" in the Cosmostation wallet' | |
break | |
case 4200: | |
message = 'The requested method is not supported' | |
break | |
case -32000: | |
message = 'Invalid input.' | |
break | |
case -32600: | |
message = 'The JSON sent is not a valid Request object.' | |
break | |
case -32602: | |
message = 'Invalid method parameter(s).' | |
break | |
case -32603: | |
message = 'Internal JSON-RPC error.' | |
break | |
default: | |
message = 'Unknown Cosmostation error' | |
if (e instanceof InstallError) { | |
message = 'Please install Cosmostation Wallet' | |
} | |
} | |
toast.error(message, { toastId: `cosmostation-${e.code}` }) | |
setAutoConnectionType(undefined) | |
throw e | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment