2016-12-11 19:31:31 +01:00
|
|
|
// Copyright 2015, 2016 Parity Technologies (UK) Ltd.
|
2016-11-30 21:39:06 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-12-08 22:05:16 +01:00
|
|
|
import { uniq } from 'lodash';
|
|
|
|
|
|
|
|
import ABI from '~/contracts/abi/certifier.json';
|
|
|
|
import Contract from '~/api/contract';
|
2016-12-05 11:47:13 +01:00
|
|
|
import Contracts from '~/contracts';
|
2016-12-09 14:06:02 +01:00
|
|
|
import { addCertification, removeCertification } from './actions';
|
2016-11-30 21:39:06 +01:00
|
|
|
|
|
|
|
export default class CertificationsMiddleware {
|
|
|
|
toMiddleware () {
|
2016-12-08 22:05:16 +01:00
|
|
|
const api = Contracts.get()._api;
|
|
|
|
const badgeReg = Contracts.get().badgeReg;
|
|
|
|
const contract = new Contract(api, ABI);
|
|
|
|
const Confirmed = contract.events.find((e) => e.name === 'Confirmed');
|
2016-12-09 14:06:02 +01:00
|
|
|
const Revoked = contract.events.find((e) => e.name === 'Revoked');
|
2016-12-08 22:05:16 +01:00
|
|
|
|
|
|
|
let certifiers = [];
|
|
|
|
let accounts = []; // these are addresses
|
|
|
|
|
|
|
|
const fetchConfirmedEvents = (dispatch) => {
|
|
|
|
if (certifiers.length === 0 || accounts.length === 0) return;
|
|
|
|
api.eth.getLogs({
|
|
|
|
fromBlock: 0,
|
|
|
|
toBlock: 'latest',
|
|
|
|
address: certifiers.map((c) => c.address),
|
2016-12-09 14:06:02 +01:00
|
|
|
topics: [ [ Confirmed.signature, Revoked.signature ], accounts ]
|
2016-12-08 22:05:16 +01:00
|
|
|
})
|
|
|
|
.then((logs) => contract.parseEventLogs(logs))
|
|
|
|
.then((logs) => {
|
|
|
|
logs.forEach((log) => {
|
|
|
|
const certifier = certifiers.find((c) => c.address === log.address);
|
|
|
|
if (!certifier) throw new Error(`Could not find certifier at ${log.address}.`);
|
2016-12-09 13:35:10 +01:00
|
|
|
const { id, name, title, icon } = certifier;
|
2016-12-09 14:06:02 +01:00
|
|
|
if (log.event === 'Revoked') {
|
|
|
|
dispatch(removeCertification(log.params.who.value, id));
|
|
|
|
} else {
|
|
|
|
dispatch(addCertification(log.params.who.value, id, name, title, icon));
|
|
|
|
}
|
2016-12-08 22:05:16 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error('Failed to fetch Confirmed events:', err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-11-30 21:39:06 +01:00
|
|
|
return (store) => (next) => (action) => {
|
2016-12-08 13:07:27 +01:00
|
|
|
if (action.type === 'fetchCertifiers') {
|
|
|
|
badgeReg.nrOfCertifiers().then((count) => {
|
|
|
|
new Array(+count).fill(null).forEach((_, id) => {
|
|
|
|
badgeReg.fetchCertifier(id)
|
|
|
|
.then((cert) => {
|
2016-12-09 13:35:10 +01:00
|
|
|
if (!certifiers.some((c) => c.id === cert.id)) {
|
2016-12-08 22:05:16 +01:00
|
|
|
certifiers = certifiers.concat(cert);
|
|
|
|
fetchConfirmedEvents(store.dispatch);
|
|
|
|
}
|
2016-12-08 13:07:27 +01:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2016-12-08 22:05:16 +01:00
|
|
|
console.warn(`Could not fetch certifier ${id}:`, err);
|
2016-12-08 13:07:27 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-08 22:05:16 +01:00
|
|
|
} else if (action.type === 'fetchCertifications') {
|
|
|
|
const { address } = action;
|
2016-11-30 21:39:06 +01:00
|
|
|
|
2016-12-08 22:05:16 +01:00
|
|
|
if (!accounts.includes(address)) {
|
|
|
|
accounts = accounts.concat(address);
|
|
|
|
fetchConfirmedEvents(store.dispatch);
|
|
|
|
}
|
|
|
|
} else if (action.type === 'setVisibleAccounts') {
|
|
|
|
const { addresses } = action;
|
|
|
|
accounts = uniq(accounts.concat(addresses));
|
|
|
|
fetchConfirmedEvents(store.dispatch);
|
|
|
|
} else return next(action);
|
2016-11-30 21:39:06 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|