check locked funds when bonding

This commit is contained in:
fmtabbara
2022-03-07 12:20:54 +00:00
parent 5f9b384efa
commit 5b9b4743dc
7 changed files with 48 additions and 14 deletions
@@ -13,13 +13,15 @@ use super::{OriginalVestingResponse, PledgeData};
#[tauri::command]
pub async fn locked_coins(
address: &str,
block_time: Option<u64>,
state: tauri::State<'_, Arc<RwLock<State>>>,
) -> Result<Coin, BackendError> {
Ok(
nymd_client!(state)
.locked_coins(address, block_time.map(Timestamp::from_seconds))
.locked_coins(
&nymd_client!(state).address().to_string(),
block_time.map(Timestamp::from_seconds),
)
.await?
.into(),
)
+7 -2
View File
@@ -1,4 +1,4 @@
import React, { useContext } from 'react'
import React, { useContext, useEffect } from 'react'
import {
Box,
Button,
@@ -91,13 +91,18 @@ export const BondForm = ({
handleSubmit,
setValue,
watch,
reset,
formState: { errors, isSubmitting },
} = useForm<TBondFormFields>({
resolver: yupResolver(validationSchema),
defaultValues,
})
const { userBalance, currency } = useContext(ClientContext)
const { userBalance, currency, clientDetails } = useContext(ClientContext)
useEffect(() => {
reset()
}, [clientDetails])
const watchNodeType = watch('nodeType', defaultValues.nodeType)
const watchAdvancedOptions = watch('withAdvancedOptions', defaultValues.withAdvancedOptions)
+4
View File
@@ -112,6 +112,10 @@ export const Bond = () => {
onClick={() => {
setStatus(EnumRequestStatus.initial)
}}
variant="contained"
color="primary"
size="large"
disableElevation
>
{status === EnumRequestStatus.error ? 'Again?' : 'Finish'}
</Button>
+12 -5
View File
@@ -1,6 +1,7 @@
import * as Yup from 'yup'
import {
checkHasEnoughFunds,
checkHasEnoughLockedTokens,
isValidHostname,
validateAmount,
validateKey,
@@ -34,15 +35,21 @@ export const validationSchema = Yup.object().shape({
.required('An amount is required')
.test('valid-amount', `Pledge error`, async function (value) {
const isValid = await validateAmount(value || '', '100000000')
const hasEnoughBalance = await checkHasEnoughFunds(value || '')
const hasEnoughLocked = await checkHasEnoughLockedTokens(value || '')
if (!isValid) {
return this.createError({ message: `A valid amount is required (min 100)` })
} else {
const hasEnough = await checkHasEnoughFunds(value || '')
if (!hasEnough) {
return this.createError({ message: 'Not enough funds in wallet' })
}
}
if (this.parent.tokenPool === 'balance' && !hasEnoughBalance) {
return this.createError({ message: 'Not enough funds in wallet' })
}
if (this.parent.tokenPool === 'locked' && !hasEnoughLocked) {
return this.createError({ message: 'Not enough locked tokens' })
}
return true
}),
+1 -1
View File
@@ -126,7 +126,7 @@ export const SendWizard = () => {
}}
>
{activeStep === 0 ? (
<SendForm transferFee={transferFee} />
<SendForm />
) : activeStep === 1 ? (
<SendReview transferFee={transferFee} />
) : (
+9 -3
View File
@@ -12,8 +12,8 @@ import {
PledgeData,
} from '../types'
export const getLockedCoins = async (address: string): Promise<Coin> => {
const res: Coin = await invoke('locked_coins', { address })
export const getLockedCoins = async (): Promise<Coin> => {
const res: Coin = await invoke('locked_coins')
return await minorToMajor(res.amount)
}
export const getSpendableCoins = async (vestingAccountAddress?: string): Promise<Coin> => {
@@ -79,7 +79,13 @@ export const getVestingPledgeInfo = async ({
}: {
address?: string
type: EnumNodeType
}): Promise<PledgeData> => await invoke(`vesting_get_${type}_pledge`, { address })
}): Promise<PledgeData | undefined> => {
try {
return await invoke(`vesting_get_${type}_pledge`, { address })
} catch (e) {
return undefined
}
}
export const vestingUpdateMixnode = async (profitMarginPercent: number) =>
await invoke('vesting_update_mixnode', { profitMarginPercent })
+11 -1
View File
@@ -2,7 +2,7 @@ import { invoke } from '@tauri-apps/api'
import { appWindow } from '@tauri-apps/api/window'
import bs58 from 'bs58'
import { minor, valid } from 'semver'
import { userBalance, majorToMinor } from '../requests'
import { userBalance, majorToMinor, getLockedCoins } from '../requests'
import { Coin, Network, Period, TCurrency } from '../types'
export const validateKey = (key: string, bytesLength: number): boolean => {
@@ -100,6 +100,16 @@ export const checkHasEnoughFunds = async (allocationValue: string) => {
}
}
export const checkHasEnoughLockedTokens = async (allocationValue: string) => {
try {
const lockedTokens = await getLockedCoins()
const remainingBalance = +lockedTokens.amount - +allocationValue
return isGreaterThan(remainingBalance, 0)
} catch (e) {
console.log(e)
}
}
export const randomNumberBetween = (min: number, max: number) => {
min = Math.ceil(min)
max = Math.floor(max)