use wrapper functions for send and delegate

This commit is contained in:
fmtabbara
2021-09-13 13:30:05 +01:00
parent a601c28a20
commit 56a9527497
4 changed files with 48 additions and 16 deletions
+26 -1
View File
@@ -1,5 +1,13 @@
import { invoke } from '@tauri-apps/api'
import { Coin, Operation, TCreateAccount, TSignInWithMnemonic } from '../types'
import {
Coin,
DelegationResult,
EnumNodeType,
Operation,
TauriTxResult,
TCreateAccount,
TSignInWithMnemonic,
} from '../types'
export const createAccount = async (): Promise<TCreateAccount> =>
await invoke('create_new_account')
@@ -17,3 +25,20 @@ export const majorToMinor = async (amount: string): Promise<Coin> =>
export const getGasFee = async (operation: Operation): Promise<Coin> =>
await invoke('get_fee', { operation })
export const delegatedToMixnode = async ({
type,
identity,
amount,
}: {
type: EnumNodeType
identity: string
amount: Coin
}): Promise<DelegationResult> =>
await invoke(`delegate_to_${type}`, { identity, amount })
export const send = async (args: {
amount: Coin
address: string
memo: string
}): Promise<TauriTxResult> => await invoke('send', args)
@@ -11,13 +11,12 @@ import {
} from '@material-ui/core'
import { useForm } from 'react-hook-form'
import { NodeTypeSelector } from '../../components/NodeTypeSelector'
import { EnumNodeType, TFee } from '../../types'
import { DelegationResult, EnumNodeType, TFee } from '../../types'
import { yupResolver } from '@hookform/resolvers/yup'
import { validationSchema } from './validationSchema'
import { invoke } from '@tauri-apps/api'
import { Alert } from '@material-ui/lab'
import { ClientContext } from '../../context/main'
import { majorToMinor } from '../../requests'
import { delegatedToMixnode, majorToMinor } from '../../requests'
type TDelegateForm = {
nodeType: EnumNodeType
@@ -58,14 +57,22 @@ export const DelegateForm = ({
const onSubmit = async (data: TDelegateForm) => {
const amount = await majorToMinor(data.amount)
await invoke(`delegate_to_${data.nodeType}`, {
console.log({
type: data.nodeType,
identity: data.identity,
amount,
})
.then((res: any) => {
console.log(res)
onSuccess(res)
delegatedToMixnode({
type: data.nodeType,
identity: data.identity,
amount,
})
.then((res) => {
const successResponse = res as DelegationResult
console.log(successResponse)
onSuccess(
`Successfully delated ${data.amount} punk to ${successResponse.source_address}`
)
getBalance.fetchBalance()
})
.catch((e) => {
+4 -5
View File
@@ -3,14 +3,13 @@ import { useForm, FormProvider } from 'react-hook-form'
import { yupResolver } from '@hookform/resolvers/yup'
import { Button, Step, StepLabel, Stepper, Theme } from '@material-ui/core'
import { useTheme } from '@material-ui/styles'
import { invoke } from '@tauri-apps/api'
import { SendForm } from './SendForm'
import { SendReview } from './SendReview'
import { SendConfirmation } from './SendConfirmation'
import { ClientContext } from '../../context/main'
import { validationSchema } from './validationSchema'
import { TauriTxResult } from '../../types/rust/tauritxresult'
import { majorToMinor } from '../../requests'
import { TauriTxResult } from '../../types'
import { majorToMinor, send } from '../../requests'
const defaultValues = {
amount: '',
@@ -64,7 +63,7 @@ export const SendWizard = () => {
const formState = methods.getValues()
const amount = await majorToMinor(formState.amount)
invoke('send', {
send({
amount,
address: formState.to,
memo: formState.memo,
@@ -74,7 +73,7 @@ export const SendWizard = () => {
setActiveStep((s) => s + 1)
setConfirmedData({
...details,
amount: { denom: 'punk', amount: formState.amount },
amount: { denom: 'Major', amount: formState.amount },
})
setIsLoading(false)
getBalance.fetchBalance()
+3 -2
View File
@@ -1,8 +1,9 @@
export * from './balance'
export * from './coin'
export * from './mixnode'
export * from './delegationresult'
export * from './denom'
export * from './gateway'
export * from './mixnode'
export * from './operation'
export * from './tauritxresult'
export * from './transactiondetails'
export * from './operation'