Skip to content

Commit

Permalink
fix: Get provider for guest and connected wallets (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyaiox authored Aug 16, 2024
1 parent 6919a93 commit d1f8ef2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/warning/NetworkWarning.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const NetworkWarning: React.FC<NetworkWarningProps> = ({ onClose }) => (
</div>
<div className="network-warning-description">
Blockchain transactions in this network have a cost and real consequences. We recommend you use the{' '}
<strong>Sepolia</strong> or <strong>Goerli</strong> test networks instead.
<strong>Sepolia</strong> test network instead.
</div>
<button className="network-warning-button" onClick={onClose}>
Expand Down
9 changes: 6 additions & 3 deletions src/eth/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export const SECONDS_IN_MILLIS = 1000
export const CONNECTION_TIMEOUT_IN_MILLIS = 10 * SECONDS_IN_MILLIS

export const chainIdRpc = new Map<number, string>([
[1, 'wss://rpc.decentraland.org/mainnet'],
[5, 'wss://rpc.decentraland.org/goerli'],
[11155111, 'wss://rpc.decentraland.org/sepolia']
[ChainId.ETHEREUM_MAINNET, 'wss://rpc.decentraland.org/mainnet'],
[ChainId.ETHEREUM_SEPOLIA, 'wss://rpc.decentraland.org/sepolia']
])

export async function getEthereumProvider(
Expand All @@ -33,6 +32,10 @@ export async function getEthereumProvider(
}
}

if (connection.isConnected()) {
return connection.tryPreviousConnection()
}

const result = await connection.connect(type, chainId)
return {
provider: result.provider,
Expand Down
48 changes: 27 additions & 21 deletions src/kernel-loader/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { trackConnectWallet } from 'decentraland-dapps/dist/modules/analytics/utils'
import { getProviderChainId } from 'decentraland-dapps/dist/modules/wallet/utils/getProviderChainId'
import { connection } from 'decentraland-connect'
import { getConnectedProvider, getSigner } from 'decentraland-dapps/dist/lib/eth'
import { disconnect, restoreConnection } from '../eth/provider'
import { disconnect, getEthereumProvider, restoreConnection } from '../eth/provider'
import { internalTrackEvent, identifyUser, disableAnalytics } from '../integration/analytics'
import { injectKernel } from './injector'
import {
Expand Down Expand Up @@ -51,21 +50,8 @@ export function getWantedChainId() {
export async function authenticate(providerType: ProviderType | null) {
try {
const wantedChainId = getWantedChainId()
const provider = await getConnectedProvider()

if (!provider) {
store.dispatch(
setKernelError({
error: new Error('Not connected provider, E01)'),
code: ErrorType.NOT_SUPPORTED
})
)
return
}

const providerChainId = await getProviderChainId(provider)
const account = await getSigner()
const address = await account.getAddress()
const { provider, chainId: providerChainId, account } = await getEthereumProvider(providerType, wantedChainId)

if (providerChainId !== wantedChainId) {
store.dispatch(
Expand All @@ -84,6 +70,26 @@ export async function authenticate(providerType: ProviderType | null) {
return
}

{
const providerChainId = await getProviderChainId(provider)
if (providerChainId !== wantedChainId) {
store.dispatch(
setKernelError({
error: new Error(
`Network mismatch NETWORK url param is not equal to the provided by Ethereum Provider (wanted: ${wantedChainId} actual: ${providerChainId}, E02)`
),
code: ErrorType.NET_MISMATCH,
extra: {
providerType,
providerChainId: providerChainId,
wantedChainId: wantedChainId
}
})
)
return
}
}

const kernel = store.getState().kernel.kernel

if (!kernel) throw new Error('Kernel did not load yet')
Expand All @@ -94,11 +100,11 @@ export async function authenticate(providerType: ProviderType | null) {

// Track that the users wallet has connected.
// Only when the user has not connected as guest.
if (providerType && address) {
if (providerType && account) {
trackConnectWallet({
providerType,
chainId: providerChainId,
address: address,
address: account,
walletName: connection.getWalletName()
})
}
Expand Down Expand Up @@ -342,10 +348,10 @@ async function initLogin(kernel: KernelResult) {
}

export function startKernel() {
if (NETWORK && NETWORK !== 'mainnet' && NETWORK !== 'goerli' && NETWORK !== 'sepolia') {
if (NETWORK && NETWORK !== 'mainnet' && NETWORK !== 'sepolia') {
store.dispatch(
setKernelError({
error: new Error(`Invalid NETWORK url param, valid options are 'mainnet', 'goerli' and 'sepolia'`),
error: new Error(`Invalid NETWORK url param, valid options are 'mainnet' and 'sepolia'`),
code: ErrorType.FATAL
})
)
Expand All @@ -356,7 +362,7 @@ export function startKernel() {
store.dispatch(
setKernelError({
error: new Error(
`The "ENV" URL parameter is no longer supported. Please use NETWORK=goerli or NETWORK=sepolia in the cases where ENV=zone was used`
`The "ENV" URL parameter is no longer supported. Please use NETWORK=sepolia in the cases where ENV=zone was used`
),
code: ErrorType.FATAL
})
Expand Down

0 comments on commit d1f8ef2

Please sign in to comment.