2017-07-28 00:31:59 +00:00
|
|
|
import {
|
|
|
|
AccessContractAction,
|
|
|
|
setInteractiveContract
|
|
|
|
} from 'actions/contracts';
|
|
|
|
import { showNotification } from 'actions/notifications';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { isValidETHAddress } from 'libs/validators';
|
|
|
|
import { SagaIterator } from 'redux-saga';
|
|
|
|
import { put, takeEvery } from 'redux-saga/effects';
|
|
|
|
import translate from 'translations';
|
2017-07-28 00:31:59 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
function* handleAccessContract(action: AccessContractAction): SagaIterator {
|
|
|
|
const contractFunctions: any[] = [];
|
2017-07-28 00:31:59 +00:00
|
|
|
|
|
|
|
if (!action.address || !isValidETHAddress(action.address)) {
|
|
|
|
yield put(showNotification('danger', translate('ERROR_5'), 5000));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const abi = JSON.parse(action.abiJson);
|
|
|
|
if (abi.constructor !== Array) {
|
|
|
|
throw new Error('ABI JSON was not an array!');
|
|
|
|
}
|
|
|
|
|
|
|
|
abi.forEach(instruction => {
|
|
|
|
if (instruction.type === 'function') {
|
|
|
|
contractFunctions.push(instruction);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
yield put(setInteractiveContract(contractFunctions));
|
|
|
|
} catch (err) {
|
|
|
|
yield put(showNotification('danger', translate('ERROR_26'), 5000));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export default function* contractsSaga(): SagaIterator {
|
2017-07-28 00:31:59 +00:00
|
|
|
yield takeEvery('ACCESS_CONTRACT', handleAccessContract);
|
|
|
|
}
|