From 10e5b823e2a835244288e552cb19bab80c850bed Mon Sep 17 00:00:00 2001 From: Sikkra <159844544+Sikkra@users.noreply.github.com> Date: Tue, 19 May 2026 18:40:27 -0500 Subject: [PATCH] fix: show BGL deposit submit errors --- app/src/components/BglToWbgl.js | 94 +++++++++++++++------------- app/src/components/BglToWbgl.test.js | 37 +++++++++++ 2 files changed, 89 insertions(+), 42 deletions(-) create mode 100644 app/src/components/BglToWbgl.test.js diff --git a/app/src/components/BglToWbgl.js b/app/src/components/BglToWbgl.js index 29ddab1..a0e4ea2 100644 --- a/app/src/components/BglToWbgl.js +++ b/app/src/components/BglToWbgl.js @@ -1,63 +1,73 @@ -import {useMetaMask} from 'metamask-react' -import {useState, Fragment} from 'react' -import {useForm} from 'react-hook-form' -import { - Box, - Button, - List, ListItemText, - Typography, -} from '@material-ui/core' -import {post, url, chainLabel, isChainBsc} from '../utils' -import CheckWalletConnection from './CheckWalletConnection' - -function BglToWbgl() { - const {handleSubmit} = useForm() - const [submitting, setSubmitting] = useState(false) +import {useMetaMask} from 'metamask-react' +import {useState, Fragment} from 'react' +import {useForm} from 'react-hook-form' +import { + Box, + Button, + List, ListItemText, + Typography, +} from '@material-ui/core' +import {post, url, chainLabel, isChainBsc} from '../utils' +import CheckWalletConnection from './CheckWalletConnection' + +function BglToWbgl() { + const {handleSubmit} = useForm() + const [submitting, setSubmitting] = useState(false) const [sendAddress, setSendAddress] = useState(false) const [balance, setBalance] = useState(0) const [feePercentage, setFeePercentage] = useState(0) + const [error, setError] = useState(null) const {chainId, account} = useMetaMask() - - const onSubmit = () => { - const data = { - chain: isChainBsc(chainId) ? 'bsc' : 'eth', - address: account, - } + + const onSubmit = () => { + const data = { + chain: isChainBsc(chainId) ? 'bsc' : 'eth', + address: account, + } setSubmitting(true) + setError(null) post(url('/submit/bgl'), data).then(response => { + if (response.status !== 'ok' || !response.bglAddress) { + setError(response.message || 'Unable to create a BGL deposit address.') + return + } setSendAddress(response.bglAddress) setBalance(Math.floor(response.balance)) setFeePercentage(response.feePercentage) }).catch(result => { console.error('Error submitting form:', result) + setError('Unable to create a BGL deposit address.') }).finally(() => setSubmitting(false)) } - - return !sendAddress ? ( - -
- - - - + + return !sendAddress ? ( + + + + + + + {error && ( + {error} + )}
) : ( - - Send BGL to: {sendAddress} - - The currently available WBGL balance is {balance}. If you send more BGL than is available to complete the exchange, your BGL will be returned to your address. - - - Please note, that a fee of {feePercentage}% will be automatically deducted from the transfer amount. This exchange pair is active for 7 days. - - - ) -} - -export default BglToWbgl + + Send BGL to: {sendAddress} + + The currently available WBGL balance is {balance}. If you send more BGL than is available to complete the exchange, your BGL will be returned to your address. + + + Please note, that a fee of {feePercentage}% will be automatically deducted from the transfer amount. This exchange pair is active for 7 days. + + + ) +} + +export default BglToWbgl diff --git a/app/src/components/BglToWbgl.test.js b/app/src/components/BglToWbgl.test.js new file mode 100644 index 0000000..5c844da --- /dev/null +++ b/app/src/components/BglToWbgl.test.js @@ -0,0 +1,37 @@ +import {fireEvent, render, screen, waitFor} from '@testing-library/react' +import '@testing-library/jest-dom' +import BglToWbgl from './BglToWbgl' + +jest.mock('metamask-react', () => ({ + useMetaMask: () => ({ + status: 'connected', + chainId: 3, + account: '0x0000000000000000000000000000000000000001', + }), +})) + +beforeEach(() => { + global.fetch = jest.fn(() => Promise.resolve({ + json: () => Promise.resolve({ + status: 'error', + message: 'Network is likely to be down.', + }), + })) +}) + +afterEach(() => { + jest.restoreAllMocks() +}) + +test('shows submit errors when the bridge service rejects BGL deposit creation', async () => { + render() + + fireEvent.click(screen.getByRole('button', {name: /Continue/i})) + + await waitFor(() => { + expect(screen.getByRole('alert')).toHaveTextContent('Network is likely to be down.') + }) + expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('/submit/bgl'), expect.objectContaining({ + method: 'POST', + })) +})