event.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { TronWeb } from '../tronweb.js';
  2. import utils from '../utils/index.js';
  3. import { HttpProvider } from './providers/index.js';
  4. export class Event {
  5. tronWeb;
  6. constructor(tronWeb) {
  7. if (!tronWeb || !(tronWeb instanceof TronWeb))
  8. throw new Error('Expected instance of TronWeb');
  9. this.tronWeb = tronWeb;
  10. }
  11. setServer(eventServer, healthcheck = 'healthcheck') {
  12. if (!eventServer)
  13. return (this.tronWeb.eventServer = undefined);
  14. if (utils.isString(eventServer))
  15. eventServer = new HttpProvider(eventServer);
  16. if (!this.tronWeb.isValidProvider(eventServer))
  17. throw new Error('Invalid event server provided');
  18. this.tronWeb.eventServer = eventServer;
  19. this.tronWeb.eventServer.isConnected = () => this.tronWeb
  20. .eventServer.request(healthcheck)
  21. .then(() => true)
  22. .catch(() => false);
  23. }
  24. async getEventsByContractAddress(contractAddress, options = {}) {
  25. const newOptions = Object.assign({
  26. limit: 20,
  27. }, options);
  28. const { eventName, blockNumber, onlyUnconfirmed, onlyConfirmed, minBlockTimestamp, maxBlockTimestamp, orderBy, fingerprint, } = newOptions;
  29. let { limit } = newOptions;
  30. if (!this.tronWeb.eventServer) {
  31. throw new Error('No event server configured');
  32. }
  33. if (!this.tronWeb.isAddress(contractAddress)) {
  34. throw new Error('Invalid contract address provided');
  35. }
  36. if (typeof minBlockTimestamp !== 'undefined' && !utils.isInteger(minBlockTimestamp)) {
  37. throw new Error('Invalid minBlockTimestamp provided');
  38. }
  39. if (typeof maxBlockTimestamp !== 'undefined' && !utils.isInteger(maxBlockTimestamp)) {
  40. throw new Error('Invalid maxBlockTimestamp provided');
  41. }
  42. if (utils.isInteger(limit) && limit > 200) {
  43. console.warn('Defaulting to maximum accepted limit: 200');
  44. limit = 200;
  45. }
  46. const qs = {};
  47. if (eventName)
  48. qs.event_name = eventName;
  49. if (blockNumber)
  50. qs.block_number = blockNumber;
  51. if (typeof onlyUnconfirmed === 'boolean')
  52. qs.only_unconfirmed = onlyUnconfirmed;
  53. if (typeof onlyConfirmed === 'boolean')
  54. qs.only_confirmed = onlyConfirmed;
  55. if (minBlockTimestamp)
  56. qs.min_block_timestamp = minBlockTimestamp;
  57. if (maxBlockTimestamp)
  58. qs.max_block_timestamp = maxBlockTimestamp;
  59. if (orderBy)
  60. qs.order_by = orderBy;
  61. if (fingerprint)
  62. qs.fingerprint = fingerprint;
  63. if (utils.isInteger(limit))
  64. qs.limit = limit;
  65. const res = await this.tronWeb.eventServer.request(`v1/contracts/${this.tronWeb.address.fromHex(contractAddress)}/events?${new URLSearchParams(qs).toString()}`);
  66. if (res.success) {
  67. return res;
  68. }
  69. throw new Error(res.error);
  70. }
  71. async getEventsByTransactionID(transactionID, options = {}) {
  72. if (!this.tronWeb.eventServer) {
  73. throw new Error('No event server configured');
  74. }
  75. const qs = {};
  76. if (typeof options.only_unconfirmed === 'boolean') {
  77. qs.only_unconfirmed = options.only_unconfirmed;
  78. }
  79. if (typeof options.only_confirmed === 'boolean') {
  80. qs.only_confirmed = options.only_confirmed;
  81. }
  82. return this.tronWeb.eventServer
  83. .request(`v1/transactions/${transactionID}/events?${new URLSearchParams(qs).toString()}`)
  84. .then((res) => {
  85. if (res.success) {
  86. return res;
  87. }
  88. throw new Error(JSON.parse(res.error).message);
  89. });
  90. }
  91. async getEventsByBlockNumber(blockNumber, options = {}) {
  92. if (!this.tronWeb.eventServer) {
  93. throw new Error('No event server configured');
  94. }
  95. const qs = {};
  96. if (typeof options.only_confirmed === 'boolean') {
  97. qs.only_confirmed = options.only_confirmed;
  98. }
  99. if (options.limit) {
  100. qs.limit = options.limit;
  101. }
  102. if (options.fingerprint) {
  103. qs.fingerprint = options.fingerprint;
  104. }
  105. return this.tronWeb.eventServer
  106. .request(`v1/blocks/${blockNumber}/events?${new URLSearchParams(qs).toString()}`)
  107. .then((res) => {
  108. if (res.success) {
  109. return res;
  110. }
  111. throw new Error(res.error);
  112. });
  113. }
  114. async getEventsOfLatestBlock(options = {}) {
  115. if (!this.tronWeb.eventServer) {
  116. throw new Error('No event server configured');
  117. }
  118. const qs = {};
  119. if (typeof options.only_confirmed === 'boolean') {
  120. qs.only_confirmed = options.only_confirmed;
  121. }
  122. return this.tronWeb.eventServer
  123. .request(`v1/blocks/latest/events?${new URLSearchParams(qs).toString()}`)
  124. .then((res) => {
  125. if (res.success) {
  126. return res;
  127. }
  128. throw new Error(res.error);
  129. });
  130. }
  131. }
  132. //# sourceMappingURL=event.js.map