-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
434 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
|
||
import { fromSqd, toSqd } from '@lib/network/utils'; | ||
import { LoadingButton } from '@mui/lab'; | ||
import { Chip } from '@mui/material'; | ||
import * as yup from '@schema'; | ||
import { useFormik } from 'formik'; | ||
import toast from 'react-hot-toast'; | ||
|
||
import { overTheCounterAbi } from '@api/contracts'; | ||
import { useWriteSQDTransaction } from '@api/contracts/useWriteTransaction'; | ||
import { errorMessage } from '@api/contracts/utils'; | ||
import { AccountType, SourceWalletWithBalance } from '@api/subsquid-network-squid'; | ||
import { ContractCallDialog } from '@components/ContractCallDialog'; | ||
import { Form, FormikSelect, FormikTextInput, FormRow } from '@components/Form'; | ||
import { SourceWalletOption } from '@components/SourceWallet'; | ||
import { useSquidHeight } from '@hooks/useSquidNetworkHeightHooks'; | ||
|
||
export const depositSchema = yup.object({ | ||
source: yup.string().label('Source').trim().required().typeError('${path} is invalid'), | ||
amount: yup | ||
.decimal() | ||
.label('Amount') | ||
.required() | ||
.positive() | ||
.max(yup.ref('max'), 'Insufficient balance') | ||
.typeError('${path} is invalid'), | ||
max: yup.string().label('Max').required().typeError('${path} is invalid'), | ||
}); | ||
|
||
export function DepositButton({ | ||
sources, | ||
otc, | ||
disabled, | ||
variant = 'outlined', | ||
}: { | ||
sources?: SourceWalletWithBalance[]; | ||
otc?: string; | ||
variant?: 'outlined' | 'contained'; | ||
disabled?: boolean; | ||
}) { | ||
const [open, setOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<LoadingButton | ||
loading={open} | ||
disabled={disabled || !otc} | ||
onClick={() => setOpen(true)} | ||
variant={variant} | ||
color={variant === 'contained' ? 'info' : 'secondary'} | ||
> | ||
DEPOSIT | ||
</LoadingButton> | ||
<DepositDialog open={open} onClose={() => setOpen(false)} sources={sources} otc={otc} /> | ||
</> | ||
); | ||
} | ||
|
||
export function DepositDialog({ | ||
open, | ||
sources, | ||
otc, | ||
onClose, | ||
}: { | ||
open: boolean; | ||
sources?: SourceWalletWithBalance[]; | ||
otc?: string; | ||
onClose: () => void; | ||
}) { | ||
const { writeTransactionAsync, isPending } = useWriteSQDTransaction(); | ||
|
||
const { setWaitHeight } = useSquidHeight(); | ||
|
||
const isSourceDisabled = (source: SourceWalletWithBalance) => source.balance === '0'; | ||
|
||
const initialValues = useMemo(() => { | ||
const source = sources?.find(c => !isSourceDisabled(c)) || sources?.[0]; | ||
|
||
return { | ||
source: source?.id || '', | ||
amount: '0', | ||
max: fromSqd(source?.balance).toString(), | ||
}; | ||
}, [sources]); | ||
|
||
const formik = useFormik({ | ||
initialValues, | ||
validationSchema: depositSchema, | ||
validateOnChange: true, | ||
validateOnBlur: true, | ||
validateOnMount: true, | ||
enableReinitialize: true, | ||
|
||
onSubmit: async values => { | ||
if (!otc) return; | ||
|
||
try { | ||
const { amount, source: sourceId } = depositSchema.cast(values); | ||
|
||
const source = sources?.find(w => w?.id === sourceId); | ||
if (!source) return; | ||
|
||
const sqdAmount = BigInt(toSqd(amount)); | ||
|
||
const receipt = await writeTransactionAsync({ | ||
abi: overTheCounterAbi, | ||
address: otc as `0x${string}`, | ||
functionName: 'deposit', | ||
args: [sqdAmount], | ||
vesting: source.type === AccountType.Vesting ? (source.id as `0x${string}`) : undefined, | ||
approve: sqdAmount, | ||
}); | ||
setWaitHeight(receipt.blockNumber, []); | ||
|
||
onClose(); | ||
} catch (e) { | ||
toast.error(errorMessage(e)); | ||
} | ||
}, | ||
}); | ||
|
||
return ( | ||
<ContractCallDialog | ||
title="Deposit OTC contract" | ||
open={open} | ||
onResult={confirmed => { | ||
if (!confirmed) return onClose(); | ||
|
||
formik.handleSubmit(); | ||
}} | ||
loading={isPending} | ||
disableConfirmButton={!formik.isValid} | ||
> | ||
<Form onSubmit={formik.handleSubmit}> | ||
<FormRow> | ||
<FormikSelect | ||
id="source" | ||
showErrorOnlyOfTouched | ||
options={ | ||
sources?.map(s => { | ||
return { | ||
label: <SourceWalletOption source={s} />, | ||
value: s.id, | ||
disabled: isSourceDisabled(s), | ||
}; | ||
}) || [] | ||
} | ||
formik={formik} | ||
onChange={e => { | ||
const source = sources?.find(w => w?.id === e.target.value); | ||
if (!source) return; | ||
|
||
formik.setFieldValue('source', source.id); | ||
formik.setFieldValue('max', fromSqd(source.balance).toString()); | ||
}} | ||
/> | ||
</FormRow> | ||
<FormRow> | ||
<FormikTextInput | ||
id="amount" | ||
label="Amount" | ||
formik={formik} | ||
showErrorOnlyOfTouched | ||
autoComplete="off" | ||
InputProps={{ | ||
endAdornment: ( | ||
<Chip | ||
clickable | ||
disabled={formik.values.max === formik.values.amount} | ||
onClick={() => { | ||
formik.setValues({ | ||
...formik.values, | ||
amount: formik.values.max, | ||
}); | ||
}} | ||
label="Max" | ||
/> | ||
), | ||
}} | ||
/> | ||
</FormRow> | ||
</Form> | ||
</ContractCallDialog> | ||
); | ||
} |
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,26 @@ | ||
import { addressFormatter } from '@lib/formatters/formatters'; | ||
import { Box, Stack, styled, Typography } from '@mui/material'; | ||
|
||
import { Avatar } from '@components/Avatar'; | ||
import { CopyToClipboard } from '@components/CopyToClipboard'; | ||
|
||
const Name = styled(Box, { | ||
name: 'Name', | ||
})(({ theme }) => ({ | ||
marginBottom: theme.spacing(0.25), | ||
whiteSpace: 'nowrap', | ||
})); | ||
|
||
export function SourceWalletName({ source }: { source: { id: string } }) { | ||
return ( | ||
<Stack direction="row" spacing={2} alignItems="center"> | ||
<Avatar name={source.id.slice(2)} colorDiscriminator={source.id} /> | ||
<Box> | ||
<Name>Contract</Name> | ||
<Typography variant="caption"> | ||
<CopyToClipboard text={source.id} content={addressFormatter(source.id, true)} /> | ||
</Typography> | ||
</Box> | ||
</Stack> | ||
); | ||
} |
Oops, something went wrong.