import { Action } from 'typescript-fsa' ; import { SagaIterator , delay } from 'redux-saga' ; import { takeEvery , put , call , select , fork , take , race , all } from 'redux-saga/effects' ; import { requestPrivateKeyWorker , refreshPrivateKeyExpireTime } from '../sagas/privateKey' ; import { ModalTypes } from '../../constants' ; import * as auth from 'modules/auth' ; import * as application from 'modules/application' ; import * as account from 'modules/account' ; import * as nodes from 'modules/nodes' ; import api , { apiSetUrl , apiSetToken } from 'utils/api' ; import Keyring from 'utils/keyring' ; import { TxDissect } from 'utils/common' ; import { REQUIRED_ALIVE_NODES_COUNT } from '../../constants' ; import { checkNodeValidity , loginCall , prepareContractWorker } from 'modules/sagas/sagaHelpers' ; import { runTransaction , checkTransactionStatus , confirmNestedContracts , setTransactions } from './actions' ; import { getTransactions } from './selectors' ; import { toHex } from 'utils/transactions/convert' ; class StatusError extends Error { } export interface IPrepareData { forsign : string ; signs ? : Array < { forsign : string ; field : string ; } > ; } export function * getTransactionStatus ( hashes ) { while ( true ) { const response = yield call ( api . transactionStatus , Object . values ( hashes ) ) ; yield put ( checkTransactionStatus ( response . data ) ) ; if ( response . data . results ) { const resultsValues = Object . values ( response . data . results ) ; for ( const item of resultsValues ) { if ( item . errmsg ) { throw ( { title : item . errmsg . type || 'didn\'t get error type' , message : item . errmsg . error || 'didn\'t get error message' , } ) ; } } if ( resultsValues . every ( item => item . blockid ) ) { return resultsValues ; } } yield call ( delay , 2000 ) ; } } export function * filterTransactions ( transactionToDelete ) { const transactions = yield select ( getTransactions ) ; for ( const key in transactions ) { if ( key === transactionToDelete ) { delete transactions [ key ] ; } } yield put ( setTransactions ( transactions ) ) ; } export function * contractWorker ( action ) { try { const getKey = yield call ( requestPrivateKeyWorker ) ; const locale = yield select ( application . selectors . getCurrentLocale ) ; if ( ! getKey ) { yield call ( filterTransactions , action . payload . uuid ) ; return ; } const { privateKey } = getKey ; const preparedContracts = yield call ( prepareContractWorker , action . payload , privateKey ) ; if ( ! preparedContracts ) throw new Error ( `Error while preparing contract` ) ; const transactionsStatus = yield call ( getTransactionStatus , preparedContracts ) ; yield put ( runTransaction . done ( { params : action . payload , result : { id : transactionsStatus [ 0 ] . result , block : transactionsStatus [ 0 ] . blockid , contract : action . payload . contracts [ 0 ] . contract , } } ) ) ; yield call ( refreshPrivateKeyExpireTime ) ; } catch ( error ) { yield put ( runTransaction . failed ( { params : action . payload , error } ) ) ; } } export function * pageSaga ( ) { yield takeEvery ( runTransaction . started , contractWorker ) ; } export default pageSaga ;