Skip to content

Commit 7a073e3

Browse files
chore: general improvements
1 parent 79b2f70 commit 7a073e3

File tree

6 files changed

+76
-45
lines changed

6 files changed

+76
-45
lines changed

apps/native/tests/shared/validators/WalletValidator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class WalletValidator {
2727
async expectSessionCard({ visible = true }: { visible?: boolean }) {
2828
if (visible) {
2929
await expect(
30-
this.page.getByTestId('session-card'),
30+
this.page.getByTestId('session-card').first(),
3131
'Session card should be visible'
3232
).toBeVisible({
3333
timeout: MAX_WAIT

apps/native/tests/wallet.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ sampleWalletTest('it should disconnect using hook', async () => {
185185
await modalValidator.expectDisconnected();
186186
});
187187

188-
sampleWalletTest('it should disconnect and close modal when connecting from wallet', async () => {
188+
sampleWalletTest('it should disconnect and close modal when disconnecting from wallet', async () => {
189189
await modalValidator.expectDisconnected();
190190
await modalPage.qrCodeFlow(modalPage, walletPage);
191191
await modalValidator.expectConnected();

packages/core/src/controllers/OnRampController.ts

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { subscribeKey as subKey } from 'valtio/vanilla/utils';
22
import { proxy, subscribe as sub } from 'valtio/vanilla';
3-
import type {
4-
OnRampPaymentMethod,
5-
OnRampCountry,
6-
OnRampFiatCurrency,
7-
OnRampQuote,
8-
OnRampFiatLimit,
9-
OnRampCryptoCurrency,
10-
OnRampServiceProvider,
11-
OnRampError,
12-
OnRampErrorTypeValues,
13-
OnRampCountryDefaults
3+
import {
4+
type OnRampPaymentMethod,
5+
type OnRampCountry,
6+
type OnRampFiatCurrency,
7+
type OnRampQuote,
8+
type OnRampFiatLimit,
9+
type OnRampCryptoCurrency,
10+
type OnRampServiceProvider,
11+
type OnRampError,
12+
type OnRampErrorTypeValues,
13+
type OnRampCountryDefaults,
14+
BlockchainOnRampError
1415
} from '../utils/TypeUtil';
1516

1617
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
@@ -115,27 +116,35 @@ export const OnRampController = {
115116
},
116117

117118
async setSelectedCountry(country: OnRampCountry, updateCurrency = true) {
118-
state.selectedCountry = country;
119-
state.loading = true;
119+
try {
120+
state.selectedCountry = country;
121+
state.loading = true;
120122

121-
if (updateCurrency) {
122-
const currencyCode =
123-
state.countriesDefaults?.find(d => d.countryCode === country.countryCode)
124-
?.defaultCurrencyCode || 'USD';
123+
if (updateCurrency) {
124+
const currencyCode =
125+
state.countriesDefaults?.find(d => d.countryCode === country.countryCode)
126+
?.defaultCurrencyCode || 'USD';
125127

126-
const currency = state.paymentCurrencies?.find(c => c.currencyCode === currencyCode);
128+
const currency = state.paymentCurrencies?.find(c => c.currencyCode === currencyCode);
127129

128-
if (currency) {
129-
this.setPaymentCurrency(currency);
130+
if (currency) {
131+
this.setPaymentCurrency(currency);
132+
}
130133
}
131-
}
132134

133-
await Promise.all([this.fetchPaymentMethods(), this.fetchCryptoCurrencies()]);
134-
this.clearQuotes();
135+
await Promise.all([this.fetchPaymentMethods(), this.fetchCryptoCurrencies()]);
136+
this.clearQuotes();
135137

136-
state.loading = false;
138+
state.loading = false;
137139

138-
StorageUtil.setOnRampPreferredCountry(country);
140+
StorageUtil.setOnRampPreferredCountry(country);
141+
} catch (error) {
142+
state.loading = false;
143+
state.error = {
144+
type: OnRampErrorType.FAILED_TO_LOAD_COUNTRIES,
145+
message: 'Failed to load countries'
146+
};
147+
}
139148
},
140149

141150
setSelectedPaymentMethod(paymentMethod: OnRampPaymentMethod) {
@@ -395,40 +404,45 @@ export const OnRampController = {
395404
}
396405
},
397406

398-
getQuotesDebounced: CoreHelperUtil.debounce(function () {
399-
OnRampController.getQuotes();
400-
}, 500),
401-
402407
async getQuotes() {
403-
if (!state.paymentAmount || state.paymentAmount <= 0) {
408+
if (!this.canGenerateQuote()) {
404409
this.clearQuotes();
405410

406411
return;
407412
}
408413

409-
state.quotesLoading = true;
410-
state.selectedQuote = undefined;
411-
state.selectedServiceProvider = undefined;
412-
state.error = undefined;
413-
414414
this.abortGetQuotes(false);
415415
quotesAbortController = new AbortController();
416416
const currentSignal = quotesAbortController.signal;
417417

418418
try {
419+
if (
420+
!state.selectedCountry?.countryCode ||
421+
!state.purchaseCurrency?.currencyCode ||
422+
!state.paymentCurrency?.currencyCode ||
423+
!AccountController.state.address
424+
) {
425+
throw new BlockchainOnRampError(OnRampErrorType.UNKNOWN, 'Invalid quote parameters');
426+
}
427+
428+
state.quotesLoading = true;
429+
state.selectedQuote = undefined;
430+
state.selectedServiceProvider = undefined;
431+
state.error = undefined;
432+
419433
const body = {
420-
countryCode: state.selectedCountry?.countryCode!,
421-
destinationCurrencyCode: state.purchaseCurrency?.currencyCode!,
422-
sourceAmount: state.paymentAmount,
423-
sourceCurrencyCode: state.paymentCurrency?.currencyCode!,
424-
walletAddress: AccountController.state.address!,
434+
countryCode: state.selectedCountry.countryCode,
435+
destinationCurrencyCode: state.purchaseCurrency.currencyCode,
436+
sourceAmount: state.paymentAmount!,
437+
sourceCurrencyCode: state.paymentCurrency.currencyCode,
438+
walletAddress: AccountController.state.address,
425439
excludeProviders: EXCLUDED_ONRAMP_PROVIDERS
426440
};
427441

428442
const response = await BlockchainApiController.getOnRampQuotes(body, currentSignal);
429443

430444
if (!response || !response.length) {
431-
throw { code: OnRampErrorType.NO_VALID_QUOTES };
445+
throw new BlockchainOnRampError(OnRampErrorType.NO_VALID_QUOTES, 'No valid quotes');
432446
}
433447

434448
const quotes = response.sort((a, b) => b.customerScore - a.customerScore);
@@ -647,3 +661,8 @@ export const OnRampController = {
647661
this.updateSelectedPurchaseCurrency();
648662
}
649663
};
664+
665+
// Add getQuotesDebounced to the controller after definition to avoid circular reference
666+
(OnRampController as any).getQuotesDebounced = CoreHelperUtil.debounce(function () {
667+
OnRampController.getQuotes();
668+
}, 500);

packages/core/src/utils/TypeUtil.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ export type BlockchainApiOnRampWidgetResponse = {
346346
widgetUrl: string;
347347
};
348348

349+
export class BlockchainOnRampError extends Error {
350+
code: string;
351+
constructor(code: string, message: string) {
352+
super(message);
353+
this.code = code;
354+
this.message = message;
355+
}
356+
}
357+
349358
// -- OptionsController Types ---------------------------------------------------
350359
export interface Token {
351360
address: string;

packages/scaffold/src/views/w3m-onramp-loading-view/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function OnRampLoadingView() {
4545
quote: OnRampController.state.selectedQuote
4646
});
4747
if (response?.widgetUrl) {
48-
Linking.openURL(response?.widgetUrl);
48+
Linking.openURL(response.widgetUrl);
4949
}
5050
}
5151
}, []);

packages/ui/src/composites/wui-numeric-keyboard/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { memo } from 'react';
12
import { TouchableOpacity, StyleSheet } from 'react-native';
23
import { Text } from '../../components/wui-text';
34
import { FlexView } from '../../layout/wui-flex';
@@ -7,7 +8,7 @@ export interface NumericKeyboardProps {
78
onKeyPress: (value: string) => void;
89
}
910

10-
export function NumericKeyboard({ onKeyPress }: NumericKeyboardProps) {
11+
function _NumericKeyboard({ onKeyPress }: NumericKeyboardProps) {
1112
const Theme = useTheme();
1213
const keys = [
1314
['1', '2', '3'],
@@ -48,6 +49,8 @@ export function NumericKeyboard({ onKeyPress }: NumericKeyboardProps) {
4849
);
4950
}
5051

52+
export const NumericKeyboard = memo(_NumericKeyboard);
53+
5154
const styles = StyleSheet.create({
5255
row: {
5356
marginBottom: 10

0 commit comments

Comments
 (0)