Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import db from '../../../firebase';
- import txProcessor, {TxProcessor, Status} from '../../../txProcessor';
- import deployerResolver from './1_deploy-resolver';
- import initResolver from './2_init-resolver';
- import zilliqa from '../../../helpers/zilliqa';
- import {DomainModel} from '../../../model';
- const getNode = (node: string): Promise<any> => {
- return db
- .ref(node)
- .once('value')
- .then(snapshot => snapshot.val());
- };
- const isTxConfirmed = (txId: string) => {
- return (txProcessor as TxProcessor)
- .getRequest(`/tx/${txId}`)
- .then(tx => {
- return tx && tx.status === Status.Confirmed;
- })
- .catch(() => false);
- };
- const handler = async (req, res, next) => {
- const profiles = await getNode('user-profiles');
- for (const userId in profiles) {
- const profile = profiles[userId];
- if (
- !profile ||
- !profile.domains ||
- !profile.domains.zil ||
- !profile['primary-wallet-address'] ||
- !profile['primary-wallet-address'].zil
- ) {
- continue;
- }
- for (const label in profile.domains.zil) {
- try {
- const domainInfo = profile.domains.zil[label];
- if (
- !domainInfo['resolverStatus'] ||
- domainInfo['resolverStatus'] !== 'pending' ||
- !(await DomainModel.getOwner({label, extension: 'zil'}, ''))
- ) {
- continue;
- }
- if (
- !domainInfo['pending-txs'] ||
- !domainInfo['pending-txs']['deploy-resolver']
- ) {
- const tx = await deployerResolver(
- label,
- profile['primary-wallet-address'].zil,
- );
- db.ref(
- `user-profiles/${userId}/domains/zil/${label}/pending-txs/deploy-resolver`,
- ).set({id: (tx as any).id, hash: (tx as any).hash});
- } else if (
- domainInfo['pending-txs'] &&
- domainInfo['pending-tx']['init-resolver']
- ) {
- if (
- await isTxConfirmed(domainInfo['pending-tx']['init-resolver'].id)
- ) {
- db.ref(
- `user-profiles/${userId}/domains/zil/${label}/resolverStatus`,
- ).set('confirmed');
- }
- } else if (domainInfo['pending-txs']['deploy-resolver']) {
- if (
- await isTxConfirmed(domainInfo['pending-txs']['deploy-resolver'].id)
- ) {
- const resolverAddress = (await zilliqa.provider.send(
- 'GetContractAddressFromTransactionID',
- (domainInfo['pending-txs']['deploy-resolver'] as any).hash,
- )).result;
- const tx = await initResolver(resolverAddress);
- db.ref(
- `user-profiles/${userId}/domains/zil/${label}/pending-txs/init-resolver`,
- ).set({id: (tx as any).id});
- }
- }
- } catch (e) {
- console.log(e);
- }
- }
- }
- res.sendStatus(200);
- };
- export default handler;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement