Skip to content

Commit 4815299

Browse files
committed
chore: cleaning + tx status
1 parent 2729b1e commit 4815299

File tree

4 files changed

+113
-112
lines changed

4 files changed

+113
-112
lines changed

app/src/api/bridge.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ try {
7676
console.log(e);
7777
}
7878

79+
export const BridgesStates = {
80+
'approving': {
81+
type: 'pending',
82+
text: '🕒 Approving'
83+
},
84+
'approved': {
85+
type: 'success',
86+
text: '✔️ Approved'
87+
},
88+
'not_approved': {
89+
type: 'error',
90+
text: '❌ Approve Error'
91+
},
92+
'transferring': {
93+
type: 'pending',
94+
text: '🕒 Transferring'
95+
},
96+
'mined': {
97+
type: 'success',
98+
text: '✔️ Mined'
99+
},
100+
'transfer_error': {
101+
type: 'error',
102+
text: '❌ Transfer Error'
103+
},
104+
'fulfilled': {
105+
type: 'success',
106+
text: '✔️ Fulfilled'
107+
}
108+
}
109+
79110
export async function bridgeToken(
80111
fromChainId,
81112
toChainId,
@@ -99,15 +130,15 @@ export async function bridgeToken(
99130
const approve = await Token.approve(BridgeAddress, tokenId, {
100131
from: signerAddress,
101132
});
102-
setTransactionStatus(`Approving`);
133+
setTransactionStatus(BridgesStates.approving);
103134
setTxLink(approve.hash);
104135
await approve.wait();
105-
setTransactionStatus(`Approved`);
136+
setTransactionStatus(BridgesStates.approved);
106137
setTxLink(approve.hash);
107138
} catch (error) {
108139
setError(error);
109140
setPending(false);
110-
setTransactionStatus(`Not approved`);
141+
setTransactionStatus(BridgesStates.not_approved);
111142
return;
112143
}
113144
}
@@ -118,10 +149,10 @@ export async function bridgeToken(
118149
// `${networks[fromChainId].brigingPrice[toChainId].value}`,
119150
// ),
120151
});
121-
setTransactionStatus(`Transfer`);
152+
setTransactionStatus(BridgesStates.transferring);
122153
setTxLink(transfer.hash);
123154
await transfer.wait();
124-
setTransactionStatus(`Mined`);
155+
setTransactionStatus(BridgesStates.mined);
125156
setTxLink(transfer.hash);
126157
setIsLoading(true);
127158
setPending(false);
@@ -132,7 +163,7 @@ export async function bridgeToken(
132163
if (token && token.chainId !== fromChainId) {
133164
setIsLoading(false);
134165
setConfirmed(true);
135-
setTransactionStatus(`Fulfilled`);
166+
setTransactionStatus(BridgesStates.fulfilled);
136167
return 'Fulfilled';
137168
} else {
138169
console.log('try again');
@@ -148,6 +179,6 @@ export async function bridgeToken(
148179
setPending(false);
149180
setIsLoading(false);
150181
setError(error);
151-
setTransactionStatus(`Transfer error`);
182+
setTransactionStatus(BridgesStates.transfer_error);
152183
}
153184
}

app/src/components/BridgeModal.js

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Box from '@mui/material/Box';
33
import Button from '@mui/material/Button';
44
import Dialog from '@mui/material/Dialog';
55
import DialogContent from '@mui/material/DialogContent';
6+
import DialogContentText from '@mui/material/DialogContentText';
67
import {
78
FormControl,
89
MenuItem,
@@ -11,6 +12,7 @@ import {
1112
DialogTitle,
1213
IconButton,
1314
Divider,
15+
Link,
1416
} from '@mui/material';
1517
import { Typography } from '@mui/material';
1618
import TokenCard from './TokenCard';
@@ -20,6 +22,7 @@ import { networksLogos } from '../constants';
2022
import networks from '../networks.json';
2123
import { styled } from '@mui/material/styles';
2224
import { bridgeToken } from '../api';
25+
import { shortenAddress } from '../utils';
2326

2427
/**
2528
* @param {string} fromId Chain Id of current item
@@ -42,7 +45,7 @@ const getAvaibleChains = (chainId) => {
4245
: [];
4346
};
4447

45-
function BridgeModal({ isOpen, toggle, currentItem, chainId, switchNetwork }) {
48+
function BridgeModal({ isOpen, toggle, currentItem, chainId, switchNetwork, userAddress }) {
4649
const descriptionElementRef = React.useRef(null);
4750
const [currentChain, setCurrentChain] = React.useState('');
4851
const [pending, setPending] = React.useState(false);
@@ -72,7 +75,7 @@ function BridgeModal({ isOpen, toggle, currentItem, chainId, switchNetwork }) {
7275

7376
const bridgePrice = getBridgePrice(currentItem.chainId, currentChain);
7477
const isSameNetwork = currentItem.chainId === Number(chainId);
75-
const bridge = async () => {
78+
const bridge = userAddress === currentItem.owner ? async () => {
7679
if (isSameNetwork) {
7780
// toggle()
7881
await bridgeToken(
@@ -90,15 +93,15 @@ function BridgeModal({ isOpen, toggle, currentItem, chainId, switchNetwork }) {
9093
}
9194

9295
switchNetwork(currentItem.chainId);
93-
};
96+
} : undefined;
9497

9598
return (
9699
<BridgeModal.Layout isOpen={isOpen} toggle={toggle}>
97100
<Box padding='40px'>
98101
<TokenCard {...currentItem} />
99102
</Box>
100103
<Divider variant='middle' orientation='vertical' flexItem />
101-
<DialogContent
104+
<DialogContentText
102105
id='scroll-dialog-description'
103106
ref={descriptionElementRef}
104107
tabIndex={-1}
@@ -119,16 +122,66 @@ function BridgeModal({ isOpen, toggle, currentItem, chainId, switchNetwork }) {
119122
currectChainId={currentChain}
120123
setChain={(e) => setCurrentChain(e.target.value)}
121124
/>
122-
<Typography
123-
align='left'
124-
fontWeight='400'
125-
fontSize='12px'
126-
lineHeight='140%'
127-
color='rgba(27, 28, 30, 1)'
125+
{ txStatus
126+
? <TxStatus txStatus={txStatus} txLink={txLink} txError={error} fromChainId={currentItem.chainId}/>
127+
: <Info bridgePrice={bridgePrice}/> }
128+
<MuiButton
129+
onClick={bridge}
130+
variant='contained'
131+
fullWidth
132+
size='large'
133+
disabled={!bridgePrice || pending || loading || userAddress !== currentItem.owner}
128134
>
129-
The stages of bridging will be shown here.
130-
</Typography>
131-
<BridgePrice bridgePrice={bridgePrice} />
135+
{isSameNetwork ? 'bridge' : 'Switch network'}
136+
</MuiButton>
137+
</BridgeModal.Body>
138+
</DialogContentText>
139+
</BridgeModal.Layout>
140+
);
141+
}
142+
143+
144+
const TxStatus = ({txStatus, txLink, txError, fromChainId }) => {
145+
return (
146+
<Box display='flex' flexDirection='column' mt='5px'>
147+
<Typography variant='subtitle1' fontWeight={500}>
148+
Status:{` `}
149+
<Typography
150+
component='span'
151+
color={txStatus.type === 'success' ? '#28a745' : txStatus.type === 'pending' ? '#ffc107' : '#dc3545'}
152+
fontWeight={700}
153+
>
154+
{txStatus.text}
155+
</Typography>
156+
157+
</Typography>
158+
<Typography variant='body1'>
159+
Transaction hash:{' '}
160+
<Link
161+
href={`${networks[fromChainId].blockExplorer}/tx/${txLink}`}
162+
target='_blank'
163+
fontSize={'14px'}
164+
>
165+
{shortenAddress(txLink)}
166+
</Link>
167+
</Typography>
168+
</Box>
169+
)
170+
}
171+
172+
const Info = ({bridgePrice}) => {
173+
return (
174+
<>
175+
<Typography
176+
align='left'
177+
fontWeight='400'
178+
fontSize='12px'
179+
lineHeight='140%'
180+
color='rgba(27, 28, 30, 1)'
181+
>
182+
The stages of bridging will be shown here.
183+
</Typography>
184+
<BridgePrice bridgePrice={bridgePrice} />
132185
<Typography
133186
align='left'
134187
fontWeight='400'
@@ -140,42 +193,8 @@ function BridgeModal({ isOpen, toggle, currentItem, chainId, switchNetwork }) {
140193
{bridgePrice + ' '}
141194
or the transaction will be rolled back
142195
</Typography>
143-
<MuiButton
144-
onClick={bridge}
145-
variant='contained'
146-
fullWidth
147-
size='large'
148-
disabled={!bridgePrice || pending || loading}
149-
>
150-
{isSameNetwork ? 'bridge' : 'Switch network'}
151-
</MuiButton>
152-
153-
{txStatus && (
154-
<Box display='flex' flexDirection='column'>
155-
<Typography>{pending}</Typography>
156-
<Typography>{txStatus}</Typography>
157-
<Typography>
158-
<a
159-
href={
160-
networks[currentItem.chainId].blockExplorer +
161-
'/tx/' +
162-
txLink
163-
}
164-
target='_blank'
165-
rel='noreferrer'
166-
>
167-
link
168-
</a>
169-
</Typography>
170-
<Typography>{confirmed}</Typography>
171-
<Typography>{loading}</Typography>
172-
{error && <Typography>{`Some error occured`}</Typography>}
173-
</Box>
174-
)}
175-
</BridgeModal.Body>
176-
</DialogContent>
177-
</BridgeModal.Layout>
178-
);
196+
</>
197+
)
179198
}
180199

181200
BridgeModal.Layout = ({ children, isOpen, toggle }) => {

app/src/components/TokenCard.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function TokenCard({
4343
setCurrentItem,
4444
toggleModal,
4545
hasButton,
46+
userAddress
4647
}) {
4748
return (
4849
<Card
@@ -96,7 +97,8 @@ function TokenCard({
9697
variant='contained'
9798
sx={{ margin: '25px 18px 32px 18px' }}
9899
fullWidth
99-
onClick={() => {
100+
disabled={userAddress !== owner}
101+
onClick={userAddress !== owner ? undefined : () => {
100102
setCurrentItem({
101103
tokenId,
102104
owner,

app/src/components/TokensList.js

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,6 @@ const onlyMyTokens = (accountId) => (tokens) => {
1010
const account = accountId.toLowerCase();
1111
return tokens.filter((token) => token.owner.toLowerCase().includes(account));
1212
};
13-
/*
14-
const tokens = [
15-
{
16-
tokenId: 0,
17-
owner: '0x0000000000000000000000000000000000000000',
18-
image:
19-
'https://cs12.pikabu.ru/post_img/big/2022/03/02/12/1646253355177860649.jpg',
20-
chainId: 534353,
21-
skill: 0,
22-
},
23-
{
24-
tokenId: 5,
25-
owner: '0x0000000000000000000000000000000000000000',
26-
image:
27-
'https://cs12.pikabu.ru/post_img/big/2022/03/02/12/1646253355177860649.jpg',
28-
chainId: 534353,
29-
skill: 0,
30-
},
31-
{
32-
tokenId: 6,
33-
owner: '0x0000000000000000000000000000000000000000',
34-
image:
35-
'https://cs12.pikabu.ru/post_img/big/2022/03/02/12/1646253355177860649.jpg',
36-
chainId: 534353,
37-
skill: 0,
38-
},
39-
{
40-
tokenId: 10,
41-
owner: '0x3A93BF9fCD41564E2213f021eF5A7C03907ECa77',
42-
image:
43-
'https://cs12.pikabu.ru/post_img/big/2022/03/02/12/1646253355177860649.jpg',
44-
chainId: 5,
45-
skill: 0,
46-
},
47-
{
48-
tokenId: 1,
49-
owner: '0x0000000000000000000000000000000000000000',
50-
image:
51-
'https://cs12.pikabu.ru/post_img/big/2022/03/02/12/1646253355177860649.jpg',
52-
chainId: 1442,
53-
skill: 10,
54-
},
55-
{
56-
tokenId: 2,
57-
owner: '0x0000000000000000000000000000000000000000',
58-
image:
59-
'https://cs12.pikabu.ru/post_img/big/2022/03/02/12/1646253355177860649.jpg',
60-
chainId: 5,
61-
skill: 999,
62-
},
63-
];
64-
*/
6513

6614
function TokensList({ chainId, switchNetwork, userAddress }) {
6715
const [currentItem, setCurrentItem] = useState({
@@ -73,8 +21,7 @@ function TokensList({ chainId, switchNetwork, userAddress }) {
7321
image: '',
7422
});
7523
const { isOpen, toggle } = useModal();
76-
// @todo: add error handling of loading
77-
const { asyncTokens, asyncTokensError, asyncTokensLoading } = useTokens();
24+
const { asyncTokens, asyncTokensLoading } = useTokens();
7825
const [filters, setFilters] = React.useState([]);
7926
const filteredTokens = React.useMemo(() => {
8027
if (!asyncTokens) return null;
@@ -114,7 +61,7 @@ function TokensList({ chainId, switchNetwork, userAddress }) {
11461
image,
11562
chain_id,
11663
skill,
117-
json_metadata /* @TODO: add metadata handling */,
64+
json_metadata,
11865
}) => (
11966
<Grid item key={token_id}>
12067
<TokenCard
@@ -127,6 +74,7 @@ function TokensList({ chainId, switchNetwork, userAddress }) {
12774
setCurrentItem={setCurrentItem}
12875
toggleModal={toggle}
12976
hasButton
77+
userAddress={userAddress}
13078
/>
13179
</Grid>
13280
)
@@ -137,6 +85,7 @@ function TokensList({ chainId, switchNetwork, userAddress }) {
13785
currentItem={currentItem}
13886
chainId={chainId}
13987
switchNetwork={switchNetwork}
88+
userAddress={userAddress}
14089
/>
14190
</Grid>
14291
</Box>

0 commit comments

Comments
 (0)