fetch certifiers from BadgeReg
This commit is contained in:
parent
b32b636697
commit
409c4adfbf
@ -14,6 +14,10 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
export const fetchCertifiers = () => ({
|
||||||
|
type: 'fetchCertifiers'
|
||||||
|
});
|
||||||
|
|
||||||
export const fetchCertifications = (address) => ({
|
export const fetchCertifications = (address) => ({
|
||||||
type: 'fetchCertifications', address
|
type: 'fetchCertifications', address
|
||||||
});
|
});
|
||||||
|
@ -14,57 +14,73 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import { uniq } from 'lodash';
|
||||||
|
|
||||||
|
import ABI from '~/contracts/abi/certifier.json';
|
||||||
|
import Contract from '~/api/contract';
|
||||||
import Contracts from '~/contracts';
|
import Contracts from '~/contracts';
|
||||||
import { addCertification } from './actions';
|
import { addCertification } from './actions';
|
||||||
|
|
||||||
const knownCertifiers = [
|
|
||||||
0 // sms verification
|
|
||||||
];
|
|
||||||
|
|
||||||
export default class CertificationsMiddleware {
|
export default class CertificationsMiddleware {
|
||||||
toMiddleware () {
|
toMiddleware () {
|
||||||
|
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');
|
||||||
|
|
||||||
|
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),
|
||||||
|
topics: [ Confirmed.signature, accounts ]
|
||||||
|
})
|
||||||
|
.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}.`);
|
||||||
|
const { name, title, icon } = certifier;
|
||||||
|
dispatch(addCertification(log.params.who.value, name, title, icon));
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error('Failed to fetch Confirmed events:', err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (store) => (next) => (action) => {
|
return (store) => (next) => (action) => {
|
||||||
if (action.type === 'fetchCertifiers') {
|
if (action.type === 'fetchCertifiers') {
|
||||||
badgeReg.nrOfCertifiers().then((count) => {
|
badgeReg.nrOfCertifiers().then((count) => {
|
||||||
new Array(+count).fill(null).forEach((_, id) => {
|
new Array(+count).fill(null).forEach((_, id) => {
|
||||||
badgeReg.fetchCertifier(id)
|
badgeReg.fetchCertifier(id)
|
||||||
.then((cert) => {
|
.then((cert) => {
|
||||||
const { address, name, title, icon } = cert;
|
if (!certifiers.some((c) => c.address === cert.address)) {
|
||||||
store.dispatch(addCertifier(address, name, title, icon));
|
certifiers = certifiers.concat(cert);
|
||||||
|
fetchConfirmedEvents(store.dispatch);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
if (err) {
|
console.warn(`Could not fetch certifier ${id}:`, err);
|
||||||
console.error(`Failed to fetch certifier ${id}:`, err);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
} else if (action.type === 'fetchCertifications') {
|
||||||
|
const { address } = action;
|
||||||
|
|
||||||
else if (action.type !== 'fetchCertifications') {
|
if (!accounts.includes(address)) {
|
||||||
return next(action);
|
accounts = accounts.concat(address);
|
||||||
}
|
fetchConfirmedEvents(store.dispatch);
|
||||||
|
}
|
||||||
const { address } = action;
|
} else if (action.type === 'setVisibleAccounts') {
|
||||||
const badgeReg = Contracts.get().badgeReg;
|
const { addresses } = action;
|
||||||
|
accounts = uniq(accounts.concat(addresses));
|
||||||
knownCertifiers.forEach((id) => {
|
fetchConfirmedEvents(store.dispatch);
|
||||||
badgeReg.fetchCertifier(id)
|
} else return next(action);
|
||||||
.then((cert) => {
|
|
||||||
return badgeReg.checkIfCertified(cert.address, address)
|
|
||||||
.then((isCertified) => {
|
|
||||||
if (isCertified) {
|
|
||||||
const { name, title, icon } = cert;
|
|
||||||
store.dispatch(addCertification(address, name, title, icon));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
if (err) {
|
|
||||||
console.error(`Failed to check if ${address} certified by ${id}:`, err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user