forked from cosmos/cosmos-multisig-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
bedb471
commit 3fa1c73
Showing
2 changed files
with
138 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React, { useState, useEffect } from "react"; | ||
|
||
import { useAppContext } from "../../context/AppContext"; | ||
import Input from "../../components/inputs/Input"; | ||
import { checkAddress, exampleAddress } from "../../lib/displayHelpers"; | ||
|
||
const MsgUnDelegate = (props) => { | ||
const onMsgChange = props.onMsgChange || (() => {}); | ||
const onCheck = props.onCheck || (() => {}); | ||
const { state } = useAppContext(); | ||
const [validatorAddressError, setValidatorAddressError] = useState(""); | ||
const [delegatorAddressError, setDelegatorAddressError] = useState(""); | ||
// const [amountError, setAmountError] = useState(""); | ||
|
||
function checkMsg(msg, updateInternalErrors) { | ||
if (!msg) return false; | ||
if (!msg.typeUrl) return false; | ||
if (!msg.value) return false; | ||
|
||
const v = msg.value; | ||
if (!v.amount) return false; | ||
if (!v.amount.denom) return false; | ||
if (!v.amount.amount) return false; | ||
|
||
const toValidatorAddressError = checkAddress( | ||
v.validatorAddress, | ||
state.chain.addressPrefix + "valoper", | ||
); | ||
if (updateInternalErrors) { | ||
if (toValidatorAddressError) { | ||
const errorMsg = `Invalid validator address for network ${state.chain.chainId}: ${toValidatorAddressError}`; | ||
setValidatorAddressError(errorMsg); | ||
} else { | ||
setValidatorAddressError(""); | ||
} | ||
} | ||
const toDelegatorAddressError = checkAddress(v.delegatorAddress, state.chain.addressPrefix); | ||
if (updateInternalErrors) { | ||
if (toDelegatorAddressError) { | ||
const errorMsg = `Invalid delegator address for network ${state.chain.chainId}: ${toDelegatorAddressError}`; | ||
setDelegatorAddressError(errorMsg); | ||
} else { | ||
setDelegatorAddressError(""); | ||
} | ||
} | ||
if (toValidatorAddressError || toDelegatorAddressError) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
function checkAndSetAmount(newAmount) { | ||
const newMsg = JSON.parse(JSON.stringify(props.msg)); | ||
newMsg.value.amount.amount = newAmount; | ||
onMsgChange(newMsg); | ||
onCheck(checkMsg(newMsg, true)); | ||
} | ||
|
||
function checkAndSetValidatorAddress(valaddr) { | ||
const newMsg = JSON.parse(JSON.stringify(props.msg)); | ||
newMsg.value.validatorAddress = valaddr; | ||
onMsgChange(newMsg); | ||
onCheck(checkMsg(newMsg, true)); | ||
} | ||
|
||
useEffect(() => { | ||
onCheck(checkMsg(props.msg, false)); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h2>Undelegate (warning: unbonding period)</h2> | ||
<div className="form-item"> | ||
<Input | ||
label="Delegator Address" | ||
name="delegatorAddress" | ||
value={props.msg.value.delegatorAddress} | ||
disabled={true} | ||
error={delegatorAddressError} | ||
/> | ||
</div> | ||
<div className="form-item"> | ||
<Input | ||
label="Validator Address" | ||
name="validatorAddress" | ||
value={props.msg.value.validatorAddress} | ||
onChange={(e) => checkAndSetValidatorAddress(e.target.value)} | ||
error={validatorAddressError} | ||
placeholder={`E.g. ${exampleAddress(0, state.chain.addressPrefix + "valoper")}`} | ||
/> | ||
</div> | ||
<div className="form-item"> | ||
<Input | ||
label={`Amount (${props.msg.value.amount.denom})`} | ||
name="amount" | ||
type="number" | ||
value={props.msg.value.amount.amount} | ||
onChange={(e) => checkAndSetAmount(e.target.value)} | ||
/> | ||
</div> | ||
<style jsx>{` | ||
p { | ||
margin-top: 15px; | ||
} | ||
.form-item { | ||
margin-top: 1.5em; | ||
} | ||
`}</style> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MsgUnDelegate; |