Skip to content

Commit

Permalink
Move pool owner actions from actions.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTranDucVL committed Apr 7, 2021
1 parent a569469 commit 7da9572
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 163 deletions.
175 changes: 12 additions & 163 deletions app/frontend/actions.ts
@@ -1,25 +1,17 @@
import {saveAs} from './libs/file-saver'
import {withdrawalPlanValidator, mnemonicValidator} from './helpers/validators'
import debugLog from './helpers/debugLog'
import NamedError from './helpers/NamedError'
import {mnemonicValidator} from './helpers/validators'
import {exportWalletSecretDef} from './wallet/keypass-json'
import sanitizeMnemonic from './helpers/sanitizeMnemonic'
import {State, getSourceAccountInfo, Store} from './state'
import {
Lovelace,
CryptoProviderFeature,
TxType,
AuthMethodType,
DeregisterStakingKeyTransactionSummary,
} from './types'
import {parseCliUnsignedTx} from './wallet/shelley/helpers/stakepoolRegistrationUtils'
import {State, Store} from './state'
import {AuthMethodType} from './types'
import errorActions from './actions/error'
import loadingActions from './actions/loading'
import walletActions, {getWallet} from './actions/wallet'
import transactionActions from './actions/transaction'
import sendActions from './actions/send'
import delegateActions from './actions/delegate'
import accountsActions from './actions/accounts'
import poolOwnerActions from './actions/poolOwner'
import commonActions from './actions/common'
import generalActions from './actions/general'

Expand All @@ -44,7 +36,6 @@ export default (store: Store) => {
} = sendActions(store)
const {
calculateDelegationFee,
resetDelegation,
updateStakePoolIdentifier,
resetStakePoolIndentifier,
} = delegateActions(store)
Expand All @@ -60,12 +51,19 @@ export default (store: Store) => {
exploreNextAccount,
switchSourceAndTargetAccounts,
} = accountsActions(store)
const {
loadPoolCertificateTx,
openPoolRegTransactionModal,
closePoolRegTransactionModal,
signPoolCertificateTx,
resetPoolRegTransactionSummary,
deregisterStakingKey,
} = poolOwnerActions(store)
const {
resetTransactionSummary,
resetSendFormState,
resetSendFormFields,
setTransactionSummary,
prepareTxPlan,
} = commonActions(store)
const {
openWelcome,
Expand Down Expand Up @@ -197,155 +195,6 @@ export default (store: Store) => {
saveAs(blob, `${walletName}.json`)
}

/* POOL OWNER */

const loadPoolCertificateTx = async (state: State, fileContentStr: string) => {
try {
loadingAction(state, 'Loading pool registration certificate...')
setState({poolRegTxError: undefined})
const {txBodyType, unsignedTxParsed, ttl, validityIntervalStart} = parseCliUnsignedTx(
fileContentStr
)
const txPlan = await getWallet()
.getAccount(state.activeAccountIndex)
.getPoolRegistrationTxPlan({txType: TxType.POOL_REG_OWNER, unsignedTxParsed})
setState({
poolRegTransactionSummary: {
shouldShowPoolCertSignModal: false,
ttl,
validityIntervalStart,
witness: null,
plan: txPlan,
txBodyType,
},
})
} catch (err) {
debugLog(`Certificate file parsing failure: ${err}`)
setError(state, {
errorName: 'poolRegTxError',
error: {name: 'PoolRegTxParserError', message: err.message},
})
} finally {
stopLoadingAction(state)
}
}

const openPoolRegTransactionModal = (state: State) => {
setState({
poolRegTransactionSummary: {
...state.poolRegTransactionSummary,
shouldShowPoolCertSignModal: true,
},
})
}

const closePoolRegTransactionModal = (state: State) => {
setState({
poolRegTransactionSummary: {
...state.poolRegTransactionSummary,
shouldShowPoolCertSignModal: false,
},
})
}

const resetPoolRegTransactionSummary = (state: State) => {
setState({
poolRegTransactionSummary: {
shouldShowPoolCertSignModal: false,
ttl: null,
validityIntervalStart: null,
witness: null,
plan: null,
txBodyType: null,
},
poolRegTxError: null,
})
}

const signPoolCertificateTx = async (state: State) => {
try {
// TODO: refactor feature support logic
const supportError = getWallet().ensureFeatureIsSupported(CryptoProviderFeature.POOL_OWNER)
if (supportError) throw NamedError(supportError.code, {message: supportError.params.message})
if (state.usingHwWallet) {
setState({waitingForHwWallet: true})
loadingAction(state, `Waiting for ${state.hwWalletName}...`)
} else {
throw NamedError('PoolRegNoHwWallet')
}

const {plan, ttl, validityIntervalStart} = state.poolRegTransactionSummary

const txAux = await getWallet()
.getAccount(state.sourceAccountIndex)
.prepareTxAux(plan, ttl, validityIntervalStart)
const witness = await getWallet()
.getAccount(state.sourceAccountIndex)
.witnessPoolRegTxAux(txAux)

setState({
poolRegTransactionSummary: {
...state.poolRegTransactionSummary,
shouldShowPoolCertSignModal: false,
witness,
},
})
} catch (e) {
debugLog(`Certificate transaction file signing failure: ${e}`)
resetPoolRegTransactionSummary(state)
setError(state, {errorName: 'poolRegTxError', error: e})
} finally {
stopLoadingAction(state)
}
}

const deregisterStakingKey = async (state: State): Promise<void> => {
const supportError = getWallet().ensureFeatureIsSupported(CryptoProviderFeature.WITHDRAWAL)
if (supportError) {
setError(state, {
errorName: 'transactionSubmissionError',
error: supportError,
})
setState({shouldShowTransactionErrorModal: true})
return
}

state = getState()
const sourceAccount = getSourceAccountInfo(state)
const rewards = getSourceAccountInfo(state).shelleyBalances.rewardsAccountBalance as Lovelace
const balance = getSourceAccountInfo(state).balance as Lovelace

loadingAction(state, 'Preparing transaction...')

const txPlanResult = await prepareTxPlan({
txType: TxType.DEREGISTER_STAKE_KEY,
rewards,
stakingAddress: sourceAccount.stakingAddress,
})
if (txPlanResult.success === true) {
const summary = {
type: TxType.DEREGISTER_STAKE_KEY,
deposit: txPlanResult.txPlan.deposit,
rewards,
} as DeregisterStakingKeyTransactionSummary

setTransactionSummary(txPlanResult.txPlan, summary)
await confirmTransaction(getState(), 'deregisterStakeKey')
} else {
// Handled the same way as for withdrawal
const withdrawalValidationError =
withdrawalPlanValidator(rewards, balance, txPlanResult.estimatedFee) ||
getWallet().ensureFeatureIsSupported(CryptoProviderFeature.WITHDRAWAL) ||
txPlanResult.error
setError(state, {
errorName: 'transactionSubmissionError',
error: withdrawalValidationError,
})
setState({shouldShowTransactionErrorModal: true})
}
stopLoadingAction(state)
}

return {
setError,
loadingAction,
Expand Down
180 changes: 180 additions & 0 deletions app/frontend/actions/poolOwner.ts
@@ -0,0 +1,180 @@
import {Store, State, getSourceAccountInfo} from '../state'
import {getWallet} from './wallet'
import loadingActions from './loading'
import errorActions from './error'
import transactionActions from './transaction'
import commonActions from './common'
import NamedError from '../helpers/NamedError'
import {
CryptoProviderFeature,
DeregisterStakingKeyTransactionSummary,
Lovelace,
TxType,
} from '../types'
import debugLog from '../helpers/debugLog'
import {withdrawalPlanValidator} from '../helpers/validators'
import {parseCliUnsignedTx} from '../wallet/shelley/helpers/stakepoolRegistrationUtils'

export default (store: Store) => {
const {setState, getState} = store
const {setError} = errorActions(store)
const {loadingAction, stopLoadingAction} = loadingActions(store)
const {confirmTransaction} = transactionActions(store)
const {prepareTxPlan, setTransactionSummary} = commonActions(store)

const loadPoolCertificateTx = async (state: State, fileContentStr: string) => {
try {
loadingAction(state, 'Loading pool registration certificate...')
setState({poolRegTxError: undefined})
const {txBodyType, unsignedTxParsed, ttl, validityIntervalStart} = parseCliUnsignedTx(
fileContentStr
)
const txPlan = await getWallet()
.getAccount(state.activeAccountIndex)
.getPoolRegistrationTxPlan({txType: TxType.POOL_REG_OWNER, unsignedTxParsed})
setState({
poolRegTransactionSummary: {
shouldShowPoolCertSignModal: false,
ttl,
validityIntervalStart,
witness: null,
plan: txPlan,
txBodyType,
},
})
} catch (err) {
debugLog(`Certificate file parsing failure: ${err}`)
setError(state, {
errorName: 'poolRegTxError',
error: {name: 'PoolRegTxParserError', message: err.message},
})
} finally {
stopLoadingAction(state)
}
}

const openPoolRegTransactionModal = (state: State) => {
setState({
poolRegTransactionSummary: {
...state.poolRegTransactionSummary,
shouldShowPoolCertSignModal: true,
},
})
}

const closePoolRegTransactionModal = (state: State) => {
setState({
poolRegTransactionSummary: {
...state.poolRegTransactionSummary,
shouldShowPoolCertSignModal: false,
},
})
}

const resetPoolRegTransactionSummary = (state: State) => {
setState({
poolRegTransactionSummary: {
shouldShowPoolCertSignModal: false,
ttl: null,
validityIntervalStart: null,
witness: null,
plan: null,
txBodyType: null,
},
poolRegTxError: null,
})
}

const signPoolCertificateTx = async (state: State) => {
try {
// TODO: refactor feature support logic
const supportError = getWallet().ensureFeatureIsSupported(CryptoProviderFeature.POOL_OWNER)
if (supportError) throw NamedError(supportError.code, {message: supportError.params.message})
if (state.usingHwWallet) {
setState({waitingForHwWallet: true})
loadingAction(state, `Waiting for ${state.hwWalletName}...`)
} else {
throw NamedError('PoolRegNoHwWallet')
}

const {plan, ttl, validityIntervalStart} = state.poolRegTransactionSummary

const txAux = await getWallet()
.getAccount(state.sourceAccountIndex)
.prepareTxAux(plan, ttl, validityIntervalStart)
const witness = await getWallet()
.getAccount(state.sourceAccountIndex)
.witnessPoolRegTxAux(txAux)

setState({
poolRegTransactionSummary: {
...state.poolRegTransactionSummary,
shouldShowPoolCertSignModal: false,
witness,
},
})
} catch (e) {
debugLog(`Certificate transaction file signing failure: ${e}`)
resetPoolRegTransactionSummary(state)
setError(state, {errorName: 'poolRegTxError', error: e})
} finally {
stopLoadingAction(state)
}
}

const deregisterStakingKey = async (state: State): Promise<void> => {
const supportError = getWallet().ensureFeatureIsSupported(CryptoProviderFeature.WITHDRAWAL)
if (supportError) {
setError(state, {
errorName: 'transactionSubmissionError',
error: supportError,
})
setState({shouldShowTransactionErrorModal: true})
return
}

state = getState()
const sourceAccount = getSourceAccountInfo(state)
const rewards = getSourceAccountInfo(state).shelleyBalances.rewardsAccountBalance as Lovelace
const balance = getSourceAccountInfo(state).balance as Lovelace

loadingAction(state, 'Preparing transaction...')

const txPlanResult = await prepareTxPlan({
txType: TxType.DEREGISTER_STAKE_KEY,
rewards,
stakingAddress: sourceAccount.stakingAddress,
})
if (txPlanResult.success === true) {
const summary = {
type: TxType.DEREGISTER_STAKE_KEY,
deposit: txPlanResult.txPlan.deposit,
rewards,
} as DeregisterStakingKeyTransactionSummary

setTransactionSummary(txPlanResult.txPlan, summary)
await confirmTransaction(getState(), 'deregisterStakeKey')
} else {
// Handled the same way as for withdrawal
const withdrawalValidationError =
withdrawalPlanValidator(rewards, balance, txPlanResult.estimatedFee) ||
getWallet().ensureFeatureIsSupported(CryptoProviderFeature.WITHDRAWAL) ||
txPlanResult.error
setError(state, {
errorName: 'transactionSubmissionError',
error: withdrawalValidationError,
})
setState({shouldShowTransactionErrorModal: true})
}
stopLoadingAction(state)
}

return {
loadPoolCertificateTx,
openPoolRegTransactionModal,
closePoolRegTransactionModal,
signPoolCertificateTx,
resetPoolRegTransactionSummary,
deregisterStakingKey,
}
}

0 comments on commit 7da9572

Please sign in to comment.