Skip to content

Commit 477eea4

Browse files
authored
fix: Do not show error message if payWithExchange is not enabled (#4908)
1 parent 628145c commit 477eea4

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

.changeset/khaki-experts-yell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@reown/appkit-controllers': patch
3+
---
4+
5+
Fixes issue where an error snack would be shown on non-enabled pay with exchange projects

packages/controllers/src/controllers/ExchangeController.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { subscribeKey as subKey } from 'valtio/vanilla/utils'
44
import { type CaipNetworkId } from '@reown/appkit-common'
55

66
import { getActiveNetworkTokenAddress } from '../utils/ChainControllerUtil.js'
7+
import { ConstantsUtil } from '../utils/ConstantsUtil.js'
78
import { CoreHelperUtil } from '../utils/CoreHelperUtil.js'
89
import {
910
type GetBuyStatusResult,
@@ -16,7 +17,9 @@ import {
1617
import type { CurrentPayment, Exchange, PayUrlParams, PaymentAsset } from '../utils/ExchangeUtil.js'
1718
import { AccountController } from './AccountController.js'
1819
import { BlockchainApiController } from './BlockchainApiController.js'
20+
import { ChainController } from './ChainController.js'
1921
import { EventsController } from './EventsController.js'
22+
import { OptionsController } from './OptionsController.js'
2023
import { SnackController } from './SnackController.js'
2124

2225
// -- Constants ----------------------------------------- //
@@ -135,10 +138,30 @@ export const ExchangeController = {
135138
state.paymentAsset = asset
136139
},
137140

141+
isPayWithExchangeEnabled() {
142+
return (
143+
OptionsController.state.remoteFeatures?.payWithExchange ||
144+
OptionsController.state.remoteFeatures?.payments ||
145+
OptionsController.state.features?.pay
146+
)
147+
},
148+
149+
isPayWithExchangeSupported() {
150+
return (
151+
ExchangeController.isPayWithExchangeEnabled() &&
152+
ChainController.state.activeCaipNetwork &&
153+
ConstantsUtil.PAY_WITH_EXCHANGE_SUPPORTED_CHAIN_NAMESPACES.includes(
154+
ChainController.state.activeCaipNetwork.chainNamespace
155+
)
156+
)
157+
},
158+
138159
// -- Getters ----------------------------------------- //
139160
async fetchExchanges() {
140161
try {
141-
if (!state.paymentAsset) {
162+
const isPayWithExchangeSupported = ExchangeController.isPayWithExchangeSupported()
163+
164+
if (!state.paymentAsset || !isPayWithExchangeSupported) {
142165
state.exchanges = []
143166
state.isLoading = false
144167

packages/controllers/tests/controllers/ExchangeController.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22

3+
import { ChainController } from '../../exports'
34
import { AccountController } from '../../src/controllers/AccountController'
45
import { EventsController } from '../../src/controllers/EventsController'
56
import { ExchangeController } from '../../src/controllers/ExchangeController'
7+
import { OptionsController } from '../../src/controllers/OptionsController'
68
import { SnackController } from '../../src/controllers/SnackController'
79
import { CoreHelperUtil } from '../../src/utils/CoreHelperUtil'
810
import * as ExchangeUtil from '../../src/utils/ExchangeUtil'
@@ -43,6 +45,30 @@ describe('ExchangeController', () => {
4345
})
4446

4547
describe('fetchExchanges', () => {
48+
beforeEach(() => {
49+
vi.restoreAllMocks()
50+
vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({
51+
remoteFeatures: { payWithExchange: true },
52+
features: { pay: true }
53+
} as any)
54+
ChainController.state.activeCaipNetwork = {
55+
caipNetworkId: 'eip155:1',
56+
chainNamespace: 'eip155',
57+
id: 1,
58+
name: 'Ethereum',
59+
nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18 },
60+
rpcUrls: {
61+
default: {
62+
http: ['https://rpc.ankr.com/eth']
63+
}
64+
}
65+
}
66+
vi.spyOn(ExchangeUtil, 'getExchanges').mockResolvedValue({
67+
exchanges: [],
68+
total: 0
69+
})
70+
})
71+
4672
it('loads exchanges and truncates to two', async () => {
4773
const mockResponse = {
4874
exchanges: [
@@ -95,6 +121,62 @@ describe('ExchangeController', () => {
95121
expect(SnackController.showError).toHaveBeenCalledWith('Unable to get exchanges')
96122
expect(ExchangeController.state.isLoading).toBe(false)
97123
})
124+
125+
it('does not fetch exchanges when pay with exchange is not enabled', async () => {
126+
vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({
127+
remoteFeatures: { payWithExchange: false },
128+
features: { pay: false }
129+
} as any)
130+
vi.spyOn(ExchangeUtil, 'getExchanges')
131+
await ExchangeController.fetchExchanges()
132+
expect(ExchangeUtil.getExchanges).not.toHaveBeenCalled()
133+
})
134+
135+
it('does not fetch exchanges when pay with exchange is not supported', async () => {
136+
vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({
137+
remoteFeatures: { payWithExchange: true },
138+
features: { pay: true }
139+
} as any)
140+
vi.spyOn(ChainController, 'state', 'get').mockReturnValue({
141+
activeCaipNetwork: { chainNamespace: 'bip122' }
142+
} as any)
143+
vi.spyOn(ExchangeUtil, 'getExchanges')
144+
await ExchangeController.fetchExchanges()
145+
expect(ExchangeUtil.getExchanges).not.toHaveBeenCalled()
146+
})
147+
it('fetches exchanges when pay is enabled but pay with exchange is not', async () => {
148+
vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({
149+
remoteFeatures: { payWithExchange: false },
150+
features: { pay: true }
151+
} as any)
152+
vi.spyOn(ChainController, 'state', 'get').mockReturnValue({
153+
activeCaipNetwork: { chainNamespace: 'eip155' }
154+
} as any)
155+
vi.spyOn(ExchangeUtil, 'getExchanges')
156+
await ExchangeController.fetchExchanges()
157+
expect(ExchangeUtil.getExchanges).toHaveBeenCalled()
158+
})
159+
it('fetches exchanges when pay with exchange is enabled but pay is not', async () => {
160+
vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({
161+
remoteFeatures: { payWithExchange: true },
162+
features: { pay: false }
163+
} as any)
164+
vi.spyOn(ChainController, 'state', 'get').mockReturnValue({
165+
activeCaipNetwork: { chainNamespace: 'eip155' }
166+
} as any)
167+
vi.spyOn(ExchangeUtil, 'getExchanges')
168+
await ExchangeController.fetchExchanges()
169+
expect(ExchangeUtil.getExchanges).toHaveBeenCalled()
170+
})
171+
it('fetches exchanges when payments is enabled and pay with exchange is not', async () => {
172+
vi.spyOn(OptionsController, 'state', 'get').mockReturnValue({
173+
remoteFeatures: { payWithExchange: false, payments: true },
174+
features: { pay: false }
175+
} as any)
176+
vi.spyOn(ExchangeUtil, 'getExchanges')
177+
await ExchangeController.fetchExchanges()
178+
expect(ExchangeUtil.getExchanges).toHaveBeenCalled()
179+
})
98180
})
99181

100182
describe('getPayUrl', () => {

packages/scaffold-ui/src/views/w3m-fund-wallet-view/index.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ export class W3mFundWalletView extends LitElement {
4646
}
4747

4848
public override async firstUpdated() {
49-
await this.setDefaultPaymentAsset()
50-
await ExchangeController.fetchExchanges()
49+
const isPayWithExchangeSupported = ExchangeController.isPayWithExchangeSupported()
50+
if (isPayWithExchangeSupported) {
51+
await this.setDefaultPaymentAsset()
52+
await ExchangeController.fetchExchanges()
53+
}
5154
}
5255

5356
// -- Render -------------------------------------------- //
@@ -104,13 +107,8 @@ export class W3mFundWalletView extends LitElement {
104107
return null
105108
}
106109

107-
const isPayWithExchangeEnabled =
108-
this.remoteFeatures?.payWithExchange &&
109-
CoreConstantsUtil.PAY_WITH_EXCHANGE_SUPPORTED_CHAIN_NAMESPACES.includes(
110-
this.activeCaipNetwork.chainNamespace
111-
)
112-
113-
if (!isPayWithExchangeEnabled) {
110+
const isPayWithExchangeSupported = ExchangeController.isPayWithExchangeSupported()
111+
if (!isPayWithExchangeSupported) {
114112
return null
115113
}
116114

0 commit comments

Comments
 (0)