event.js 5.3 KB

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