Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import db from '../../../firebase';
  2. import txProcessor, {TxProcessor, Status} from '../../../txProcessor';
  3. import deployerResolver from './1_deploy-resolver';
  4. import initResolver from './2_init-resolver';
  5. import zilliqa from '../../../helpers/zilliqa';
  6. import {DomainModel} from '../../../model';
  7.  
  8. const getNode = (node: string): Promise<any> => {
  9. return db
  10. .ref(node)
  11. .once('value')
  12. .then(snapshot => snapshot.val());
  13. };
  14.  
  15. const isTxConfirmed = (txId: string) => {
  16. return (txProcessor as TxProcessor)
  17. .getRequest(`/tx/${txId}`)
  18. .then(tx => {
  19. return tx && tx.status === Status.Confirmed;
  20. })
  21. .catch(() => false);
  22. };
  23.  
  24. const handler = async (req, res, next) => {
  25. const profiles = await getNode('user-profiles');
  26. for (const userId in profiles) {
  27. const profile = profiles[userId];
  28. if (
  29. !profile ||
  30. !profile.domains ||
  31. !profile.domains.zil ||
  32. !profile['primary-wallet-address'] ||
  33. !profile['primary-wallet-address'].zil
  34. ) {
  35. continue;
  36. }
  37. for (const label in profile.domains.zil) {
  38. try {
  39. const domainInfo = profile.domains.zil[label];
  40. if (
  41. !domainInfo['resolverStatus'] ||
  42. domainInfo['resolverStatus'] !== 'pending' ||
  43. !(await DomainModel.getOwner({label, extension: 'zil'}, ''))
  44. ) {
  45. continue;
  46. }
  47. if (
  48. !domainInfo['pending-txs'] ||
  49. !domainInfo['pending-txs']['deploy-resolver']
  50. ) {
  51. const tx = await deployerResolver(
  52. label,
  53. profile['primary-wallet-address'].zil,
  54. );
  55. db.ref(
  56. `user-profiles/${userId}/domains/zil/${label}/pending-txs/deploy-resolver`,
  57. ).set({id: (tx as any).id, hash: (tx as any).hash});
  58. } else if (
  59. domainInfo['pending-txs'] &&
  60. domainInfo['pending-tx']['init-resolver']
  61. ) {
  62. if (
  63. await isTxConfirmed(domainInfo['pending-tx']['init-resolver'].id)
  64. ) {
  65. db.ref(
  66. `user-profiles/${userId}/domains/zil/${label}/resolverStatus`,
  67. ).set('confirmed');
  68. }
  69. } else if (domainInfo['pending-txs']['deploy-resolver']) {
  70. if (
  71. await isTxConfirmed(domainInfo['pending-txs']['deploy-resolver'].id)
  72. ) {
  73. const resolverAddress = (await zilliqa.provider.send(
  74. 'GetContractAddressFromTransactionID',
  75. (domainInfo['pending-txs']['deploy-resolver'] as any).hash,
  76. )).result;
  77. const tx = await initResolver(resolverAddress);
  78. db.ref(
  79. `user-profiles/${userId}/domains/zil/${label}/pending-txs/init-resolver`,
  80. ).set({id: (tx as any).id});
  81. }
  82. }
  83. } catch (e) {
  84. console.log(e);
  85. }
  86. }
  87. }
  88. res.sendStatus(200);
  89. };
  90.  
  91. export default handler;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement