fetch certifiers by id
This commit is contained in:
parent
2b34d76b8c
commit
b32b636697
@ -18,7 +18,8 @@ import { bytesToHex, hex2Ascii } from '~/api/util/format';
|
|||||||
|
|
||||||
import ABI from './abi/certifier.json';
|
import ABI from './abi/certifier.json';
|
||||||
|
|
||||||
const ZERO = '0x0000000000000000000000000000000000000000000000000000000000000000';
|
const ZERO20 = '0x0000000000000000000000000000000000000000';
|
||||||
|
const ZERO32 = '0x0000000000000000000000000000000000000000000000000000000000000000';
|
||||||
|
|
||||||
export default class BadgeReg {
|
export default class BadgeReg {
|
||||||
constructor (api, registry) {
|
constructor (api, registry) {
|
||||||
@ -26,25 +27,36 @@ export default class BadgeReg {
|
|||||||
this._registry = registry;
|
this._registry = registry;
|
||||||
|
|
||||||
registry.getContract('badgereg');
|
registry.getContract('badgereg');
|
||||||
this.certifiers = {}; // by name
|
this.certifiers = []; // by id
|
||||||
this.contracts = {}; // by name
|
this.contracts = {}; // by name
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchCertifier (name) {
|
nrOfCertifiers () {
|
||||||
if (this.certifiers[name]) {
|
return this._registry.getContract('badgereg')
|
||||||
return Promise.resolve(this.certifiers[name]);
|
.then((badgeReg) => {
|
||||||
|
return badgeReg.instance.badgeCount.call({}, [])
|
||||||
|
.then((count) => count.valueOf());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchCertifier (id) {
|
||||||
|
if (this.certifiers[id]) {
|
||||||
|
return Promise.resolve(this.certifiers[id]);
|
||||||
}
|
}
|
||||||
return this._registry.getContract('badgereg')
|
return this._registry.getContract('badgereg')
|
||||||
.then((badgeReg) => {
|
.then((badgeReg) => {
|
||||||
return badgeReg.instance.fromName.call({}, [name])
|
return badgeReg.instance.badge.call({}, [ id ]);
|
||||||
.then(([ id, address ]) => {
|
})
|
||||||
return this.fetchMeta(id)
|
.then(([ address, name ]) => {
|
||||||
.then(({ title, icon }) => {
|
if (address === ZERO20) throw new Error(`Certifier ${id} does not exist.`);
|
||||||
const data = { address, name, title, icon };
|
name = bytesToHex(name);
|
||||||
this.certifiers[name] = data;
|
name = name === ZERO32 ? null : hex2Ascii(name);
|
||||||
return data;
|
return this.fetchMeta(id)
|
||||||
});
|
.then(({ title, icon }) => {
|
||||||
});
|
const data = { address, name, title, icon };
|
||||||
|
this.certifiers[id] = data;
|
||||||
|
return data;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,8 +70,8 @@ export default class BadgeReg {
|
|||||||
})
|
})
|
||||||
.then(([ title, icon ]) => {
|
.then(([ title, icon ]) => {
|
||||||
title = bytesToHex(title);
|
title = bytesToHex(title);
|
||||||
title = title === ZERO ? null : hex2Ascii(title);
|
title = title === ZERO32 ? null : hex2Ascii(title);
|
||||||
if (bytesToHex(icon) === ZERO) icon = null;
|
if (bytesToHex(icon) === ZERO32) icon = null;
|
||||||
return { title, icon };
|
return { title, icon };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -17,20 +17,39 @@
|
|||||||
import Contracts from '~/contracts';
|
import Contracts from '~/contracts';
|
||||||
import { addCertification } from './actions';
|
import { addCertification } from './actions';
|
||||||
|
|
||||||
const knownCertifiers = [ 'smsverification' ];
|
const knownCertifiers = [
|
||||||
|
0 // sms verification
|
||||||
|
];
|
||||||
|
|
||||||
export default class CertificationsMiddleware {
|
export default class CertificationsMiddleware {
|
||||||
toMiddleware () {
|
toMiddleware () {
|
||||||
return (store) => (next) => (action) => {
|
return (store) => (next) => (action) => {
|
||||||
if (action.type !== 'fetchCertifications') {
|
if (action.type === 'fetchCertifiers') {
|
||||||
|
badgeReg.nrOfCertifiers().then((count) => {
|
||||||
|
new Array(+count).fill(null).forEach((_, id) => {
|
||||||
|
badgeReg.fetchCertifier(id)
|
||||||
|
.then((cert) => {
|
||||||
|
const { address, name, title, icon } = cert;
|
||||||
|
store.dispatch(addCertifier(address, name, title, icon));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(`Failed to fetch certifier ${id}:`, err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (action.type !== 'fetchCertifications') {
|
||||||
return next(action);
|
return next(action);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { address } = action;
|
const { address } = action;
|
||||||
const badgeReg = Contracts.get().badgeReg;
|
const badgeReg = Contracts.get().badgeReg;
|
||||||
|
|
||||||
knownCertifiers.forEach((name) => {
|
knownCertifiers.forEach((id) => {
|
||||||
badgeReg.fetchCertifier(name)
|
badgeReg.fetchCertifier(id)
|
||||||
.then((cert) => {
|
.then((cert) => {
|
||||||
return badgeReg.checkIfCertified(cert.address, address)
|
return badgeReg.checkIfCertified(cert.address, address)
|
||||||
.then((isCertified) => {
|
.then((isCertified) => {
|
||||||
@ -42,7 +61,7 @@ export default class CertificationsMiddleware {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(`Failed to check if ${address} certified by ${name}:`, err);
|
console.error(`Failed to check if ${address} certified by ${id}:`, err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user