-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Description --- Add claim fees button to the dan wallet ui. Motivation and Context --- How Has This Been Tested? --- I've started dan testing with 1 VN. After I had account I started a second VN with the account public key as a claim fee public key for the 2nd VN. I mined 20 blocks (to get it into the committee). I did couple of transaction and I checked that the VN has some fees to be claimed. Mined another 10 blocks to switch epoch. Pressed the button for claiming fees, filled the VN public key, selected the correct account and claimed the fees. What process can a PR reviewer use to test or verify this change? --- Breaking Changes --- - [x] None - [ ] Requires data directory to be deleted - [ ] Other - Please specify
- Loading branch information
Showing
10 changed files
with
340 additions
and
7 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
252 changes: 252 additions & 0 deletions
252
applications/tari_dan_wallet_web_ui/src/routes/AssetVault/Components/ClaimFees.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,252 @@ | ||
// Copyright 2022. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import { useState } from "react"; | ||
import { Form } from "react-router-dom"; | ||
import Button from "@mui/material/Button"; | ||
import CheckBox from "@mui/material/Checkbox"; | ||
import TextField from "@mui/material/TextField"; | ||
import Dialog from "@mui/material/Dialog"; | ||
import DialogContent from "@mui/material/DialogContent"; | ||
import DialogTitle from "@mui/material/DialogTitle"; | ||
import FormControlLabel from "@mui/material/FormControlLabel"; | ||
import Box from "@mui/material/Box"; | ||
import { useAccountsList, useAccountsTransfer } from "../../../api/hooks/useAccounts"; | ||
import { useTheme } from "@mui/material/styles"; | ||
import useAccountStore from "../../../store/accountStore"; | ||
import { FormControl, InputLabel, MenuItem, Select, SelectChangeEvent } from "@mui/material"; | ||
import { claimFees } from "../../../utils/json_rpc"; | ||
import { useKeysList } from "../../../api/hooks/useKeys"; | ||
|
||
export default function ClaimFees() { | ||
const [open, setOpen] = useState(false); | ||
const [disabled, setDisabled] = useState(false); | ||
const [estimatedFee, setEstimatedFee] = useState(0); | ||
const [claimFeesFormState, setClaimFeesFormState] = useState({ | ||
account: "", | ||
fee: "", | ||
validatorNodePublicKey: "", | ||
epoch: "", | ||
key: "", | ||
}); | ||
|
||
const { data: dataAccountsList } = useAccountsList(0, 10); | ||
const { data: dataKeysList } = useKeysList(); | ||
const { setPopup } = useAccountStore(); | ||
|
||
const theme = useTheme(); | ||
|
||
const isFormFilled = () => { | ||
if (claimFeesFormState.validatorNodePublicKey.length !== 64) { | ||
return false; | ||
} | ||
return ( | ||
claimFeesFormState.account !== "" && | ||
claimFeesFormState.validatorNodePublicKey !== "" && | ||
claimFeesFormState.epoch !== "" | ||
); | ||
}; | ||
|
||
const is_filled = isFormFilled(); | ||
|
||
const onClaimFeesKeyChange = (e: SelectChangeEvent<string>) => { | ||
let key = e.target.value; | ||
let key_index = dataKeysList.keys.indexOf(key); | ||
let account = claimFeesFormState.account; | ||
let selected_account = dataAccountsList.accounts.find((account: any) => account.account.key_index === key_index); | ||
let new_account_name; | ||
account = selected_account.account.name; | ||
new_account_name = false; | ||
setClaimFeesFormState({ | ||
...claimFeesFormState, | ||
key: key, | ||
account: account, | ||
}); | ||
}; | ||
|
||
const onEpochChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (/^[0-9]*$/.test(e.target.value)) { | ||
setEstimatedFee(0); | ||
setClaimFeesFormState({ | ||
...claimFeesFormState, | ||
[e.target.name]: e.target.value, | ||
}); | ||
} | ||
}; | ||
|
||
const onClaimBurnAccountNameChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setEstimatedFee(0); | ||
setClaimFeesFormState({ | ||
...claimFeesFormState, | ||
[e.target.name]: e.target.value, | ||
}); | ||
}; | ||
|
||
const onPublicKeyChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (/^[0-9a-fA-F]*$/.test(e.target.value)) { | ||
setClaimFeesFormState({ | ||
...claimFeesFormState, | ||
[e.target.name]: e.target.value, | ||
}); | ||
} | ||
setEstimatedFee(0); | ||
}; | ||
|
||
const onClaim = async () => { | ||
if (claimFeesFormState.account) { | ||
setDisabled(true); | ||
claimFees( | ||
claimFeesFormState.account, | ||
3000, | ||
claimFeesFormState.validatorNodePublicKey, | ||
parseInt(claimFeesFormState.epoch), | ||
estimatedFee == 0, | ||
) | ||
.then((resp) => { | ||
if (estimatedFee == 0) { | ||
setEstimatedFee(resp.fee); | ||
} else { | ||
setEstimatedFee(0); | ||
setOpen(false); | ||
setPopup({ title: "Claim successful", error: false }); | ||
setClaimFeesFormState({ | ||
account: "", | ||
fee: "", | ||
validatorNodePublicKey: "", | ||
epoch: "", | ||
key: "", | ||
}); | ||
} | ||
}) | ||
.catch((e) => { | ||
setPopup({ title: "Claim failed", error: true, message: e.message }); | ||
}) | ||
.finally(() => { | ||
setDisabled(false); | ||
}); | ||
} | ||
}; | ||
|
||
const handleClickOpen = () => { | ||
setOpen(true); | ||
}; | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
const formattedKey = (key: any[]) => { | ||
let account = dataAccountsList.accounts.find((account: any) => account.account.key_index === +key[0]); | ||
if (account === undefined) { | ||
return null; | ||
return ( | ||
<div> | ||
<b>{key[0]}</b> {key[1]} | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div> | ||
<b>{key[0]}</b> {key[1]} | ||
<br></br>Account <i>{account.account.name}</i> | ||
</div> | ||
); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Button variant="outlined" onClick={handleClickOpen}> | ||
Claim Fees | ||
</Button> | ||
<Dialog open={open} onClose={handleClose}> | ||
<DialogTitle>Claim Fees</DialogTitle> | ||
<DialogContent className="dialog-content"> | ||
<Form onSubmit={onClaim} className="flex-container-vertical" style={{ paddingTop: theme.spacing(1) }}> | ||
<FormControl> | ||
<InputLabel id="key">Key</InputLabel> | ||
<Select | ||
labelId="key" | ||
name="key" | ||
label="Key" | ||
value={claimFeesFormState.key} | ||
onChange={onClaimFeesKeyChange} | ||
style={{ flexGrow: 1, minWidth: "200px" }} | ||
disabled={disabled} | ||
> | ||
{dataKeysList?.keys?.map((key: any) => ( | ||
<MenuItem key={key} value={key}> | ||
{formattedKey(key)} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
</FormControl> | ||
<TextField | ||
name="account" | ||
label="Account Name" | ||
value={claimFeesFormState.account} | ||
onChange={onClaimBurnAccountNameChange} | ||
style={{ flexGrow: 1 }} | ||
disabled={true} | ||
></TextField> | ||
<TextField | ||
name="fee" | ||
label="Fee" | ||
value={estimatedFee || "Press fee estimate to calculate"} | ||
style={{ flexGrow: 1 }} | ||
InputProps={{ readOnly: true }} | ||
disabled={disabled} | ||
/> | ||
<TextField | ||
name="validatorNodePublicKey" | ||
label="Validator Node Public Key" | ||
value={claimFeesFormState.validatorNodePublicKey} | ||
onChange={onPublicKeyChange} | ||
style={{ flexGrow: 1 }} | ||
disabled={disabled} | ||
/> | ||
<TextField | ||
name="epoch" | ||
label="Epoch" | ||
value={claimFeesFormState.epoch} | ||
style={{ flexGrow: 1 }} | ||
onChange={onEpochChange} | ||
disabled={disabled} | ||
/> | ||
<Box | ||
className="flex-container" | ||
style={{ | ||
justifyContent: "flex-end", | ||
}} | ||
> | ||
<Button variant="outlined" onClick={handleClose} disabled={disabled}> | ||
Cancel | ||
</Button> | ||
<Button variant="contained" type="submit" disabled={disabled || !is_filled}> | ||
{estimatedFee ? "Claim" : "Estimate fee"} | ||
</Button> | ||
</Box> | ||
</Form> | ||
</DialogContent> | ||
</Dialog> | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.