forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wallet nft transfer1 (MystenLabs#2848)
* nft transfer * wallet transfer
- Loading branch information
Showing
11 changed files
with
380 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
wallet/src/ui/app/pages/transfer-nft/TransferNFTForm.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
.container { | ||
display: flex; | ||
flex-flow: column nowrap; | ||
flex: 1; | ||
align-self: stretch; | ||
} | ||
|
||
.group { | ||
display: flex; | ||
flex-flow: column nowrap; | ||
|
||
& + & { | ||
margin-top: 8px; | ||
} | ||
} | ||
|
||
.label { | ||
margin-bottom: 4px; | ||
font-weight: 600; | ||
} | ||
|
||
.error { | ||
color: #8b1111; | ||
line-height: 1em; | ||
min-height: 1em; | ||
margin-top: 2px; | ||
margin-bottom: 5px; | ||
} | ||
|
||
.muted { | ||
font-size: 12px; | ||
font-weight: 400; | ||
color: #666; | ||
margin-top: 1px; | ||
} | ||
|
||
.input { | ||
padding: 12px; | ||
} |
104 changes: 104 additions & 0 deletions
104
wallet/src/ui/app/pages/transfer-nft/TransferNFTForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { ErrorMessage, Field, Form, useFormikContext } from 'formik'; | ||
import { useEffect, useRef, memo } from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import AddressInput from '_components/address-input'; | ||
import Alert from '_components/alert'; | ||
import LoadingIndicator from '_components/loading/LoadingIndicator'; | ||
import NumberInput from '_components/number-input'; | ||
import { balanceFormatOptions } from '_shared/formatting'; | ||
|
||
import type { FormValues } from '.'; | ||
|
||
import st from './TransferNFTForm.module.scss'; | ||
|
||
export type TransferNFTFormProps = { | ||
submitError: string | null; | ||
gasBalance: string; | ||
onClearSubmitError: () => void; | ||
}; | ||
|
||
function TransferNFTForm({ | ||
submitError, | ||
gasBalance, | ||
onClearSubmitError, | ||
}: TransferNFTFormProps) { | ||
const { | ||
isSubmitting, | ||
isValid, | ||
values: { to, amount }, | ||
} = useFormikContext<FormValues>(); | ||
const intl = useIntl(); | ||
const onClearRef = useRef(onClearSubmitError); | ||
onClearRef.current = onClearSubmitError; | ||
const transferCost = 10000; | ||
useEffect(() => { | ||
onClearRef.current(); | ||
}, [to, amount]); | ||
return ( | ||
<Form className={st.container} autoComplete="off" noValidate={true}> | ||
<div className={st.group}> | ||
<label className={st.label}>To:</label> | ||
<Field | ||
component={AddressInput} | ||
name="to" | ||
className={st.input} | ||
/> | ||
<div className={st.muted}>The recipient's address</div> | ||
<ErrorMessage className={st.error} name="to" component="div" /> | ||
</div> | ||
|
||
<div className={st.group}> | ||
<label className={st.label}>Amount:</label> | ||
<Field | ||
component={NumberInput} | ||
allowNegative={false} | ||
name="amount" | ||
value={transferCost} | ||
className={st.input} | ||
disabled={true} | ||
/> | ||
<div className={st.muted}> | ||
Available balance:{' '} | ||
{intl.formatNumber( | ||
BigInt(gasBalance), | ||
balanceFormatOptions | ||
)}{' '} | ||
</div> | ||
<ErrorMessage | ||
className={st.error} | ||
name="amount" | ||
component="div" | ||
/> | ||
</div> | ||
|
||
{BigInt(gasBalance) < transferCost && ( | ||
<div className={st.error}> | ||
* Insufficient balance to cover transfer cost | ||
</div> | ||
)} | ||
{submitError ? ( | ||
<div className={st.group}> | ||
<Alert> | ||
<strong>Transfer failed.</strong>{' '} | ||
<small>{submitError}</small> | ||
</Alert> | ||
</div> | ||
) : null} | ||
<div className={st.group}> | ||
<button | ||
type="submit" | ||
disabled={!isValid || isSubmitting} | ||
className="btn" | ||
> | ||
{isSubmitting ? <LoadingIndicator /> : 'Send'} | ||
</button> | ||
</div> | ||
</Form> | ||
); | ||
} | ||
|
||
export default memo(TransferNFTForm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Formik } from 'formik'; | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import { Navigate, useNavigate, useSearchParams } from 'react-router-dom'; | ||
|
||
import TransferNFTForm from './TransferNFTForm'; | ||
import { createValidationSchema } from './validation'; | ||
import Loading from '_components/loading'; | ||
import SuiObject from '_components/sui-object'; | ||
import { useAppSelector, useAppDispatch } from '_hooks'; | ||
import { | ||
accountAggregateBalancesSelector, | ||
accountNftsSelector, | ||
} from '_redux/slices/account'; | ||
import { transferSuiNFT } from '_redux/slices/sui-objects'; | ||
import { GAS_TYPE_ARG } from '_redux/slices/sui-objects/Coin'; | ||
|
||
import type { SerializedError } from '@reduxjs/toolkit'; | ||
import type { FormikHelpers } from 'formik'; | ||
|
||
const initialValues = { | ||
to: '', | ||
amount: 10000, | ||
}; | ||
|
||
export type FormValues = typeof initialValues; | ||
|
||
function TransferNFTPage() { | ||
const [searchParams] = useSearchParams(); | ||
const objectId = useMemo( | ||
() => searchParams.get('objectId'), | ||
[searchParams] | ||
); | ||
const address = useAppSelector( | ||
({ account: { address } }) => address && `0x${address}` | ||
); | ||
|
||
let selectedNFT; | ||
const nftCollections = useAppSelector(accountNftsSelector); | ||
if (nftCollections && nftCollections.length) { | ||
selectedNFT = nftCollections.filter( | ||
(nftItems) => nftItems.reference.objectId === objectId | ||
)[0]; | ||
} | ||
|
||
const aggregateBalances = useAppSelector(accountAggregateBalancesSelector); | ||
|
||
const gasAggregateBalance = useMemo( | ||
() => aggregateBalances[GAS_TYPE_ARG] || BigInt(0), | ||
[aggregateBalances] | ||
); | ||
|
||
const [sendError, setSendError] = useState<string | null>(null); | ||
|
||
const validationSchema = useMemo( | ||
() => | ||
createValidationSchema( | ||
gasAggregateBalance, | ||
address || '', | ||
objectId || '' | ||
), | ||
[gasAggregateBalance, address, objectId] | ||
); | ||
const dispatch = useAppDispatch(); | ||
const navigate = useNavigate(); | ||
const onHandleSubmit = useCallback( | ||
async ( | ||
{ to }: FormValues, | ||
{ resetForm }: FormikHelpers<FormValues> | ||
) => { | ||
if (objectId === null) { | ||
return; | ||
} | ||
setSendError(null); | ||
try { | ||
await dispatch( | ||
transferSuiNFT({ | ||
recipientAddress: to, | ||
nftId: objectId, | ||
}) | ||
).unwrap(); | ||
resetForm(); | ||
navigate('/nfts/'); | ||
} catch (e) { | ||
setSendError((e as SerializedError).message || null); | ||
} | ||
}, | ||
[dispatch, navigate, objectId] | ||
); | ||
const handleOnClearSubmitError = useCallback(() => { | ||
setSendError(null); | ||
}, []); | ||
const loadingBalance = useAppSelector( | ||
({ suiObjects }) => suiObjects.loading && !suiObjects.lastSync | ||
); | ||
|
||
if (!objectId || !selectedNFT) { | ||
return <Navigate to="/nfts" replace={true} />; | ||
} | ||
|
||
return ( | ||
<> | ||
<h3>Send This NFT</h3> | ||
<SuiObject obj={selectedNFT} /> | ||
<br /> | ||
<Loading loading={loadingBalance}> | ||
<Formik | ||
initialValues={initialValues} | ||
validateOnMount={true} | ||
validationSchema={validationSchema} | ||
onSubmit={onHandleSubmit} | ||
> | ||
<TransferNFTForm | ||
submitError={sendError} | ||
gasBalance={gasAggregateBalance.toString()} | ||
onClearSubmitError={handleOnClearSubmitError} | ||
/> | ||
</Formik> | ||
</Loading> | ||
</> | ||
); | ||
} | ||
|
||
export default TransferNFTPage; |
Oops, something went wrong.