MoralisWeb3Api.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function ownKeys(object, enumerableOnly) {
  7. var keys = Object.keys(object);
  8. if (Object.getOwnPropertySymbols) {
  9. var symbols = Object.getOwnPropertySymbols(object);
  10. enumerableOnly && (symbols = symbols.filter(function (sym) {
  11. return Object.getOwnPropertyDescriptor(object, sym).enumerable;
  12. })), keys.push.apply(keys, symbols);
  13. }
  14. return keys;
  15. }
  16. function _objectSpread(target) {
  17. for (var i = 1; i < arguments.length; i++) {
  18. var source = null != arguments[i] ? arguments[i] : {};
  19. i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
  20. _defineProperty(target, key, source[key]);
  21. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
  22. Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
  23. });
  24. }
  25. return target;
  26. }
  27. function _defineProperty(obj, key, value) {
  28. if (key in obj) {
  29. Object.defineProperty(obj, key, {
  30. value: value,
  31. enumerable: true,
  32. configurable: true,
  33. writable: true
  34. });
  35. } else {
  36. obj[key] = value;
  37. }
  38. return obj;
  39. }
  40. /**
  41. * Automatically generated code, via genWeb3API.js
  42. * Do not modify manually
  43. */
  44. const axios = require('axios');
  45. class Web3Api {
  46. static initialize({
  47. apiKey,
  48. serverUrl,
  49. Moralis = null
  50. }) {
  51. if (!serverUrl && !apiKey) {
  52. throw new Error('Web3Api.initialize failed: initialize with apiKey or serverUrl');
  53. }
  54. if (apiKey) this.apiKey = apiKey;
  55. if (serverUrl) this.serverUrl = serverUrl;
  56. this.Moralis = Moralis;
  57. }
  58. static getBody(params, bodyParams) {
  59. if (!params || !bodyParams || !bodyParams.length) {
  60. return undefined;
  61. }
  62. let body = {};
  63. bodyParams.forEach(({
  64. key,
  65. type,
  66. required
  67. }) => {
  68. if (params[key] === undefined) {
  69. if (required) throw new Error(`param ${key} is required!`);
  70. } else if (type === this.BodyParamTypes.setBody) {
  71. body = params[key];
  72. } else {
  73. body[key] = params[key];
  74. } // remove the param so it doesn't also get added as a query param
  75. delete params[key];
  76. });
  77. return body;
  78. }
  79. static getParameterizedUrl(url, params) {
  80. if (!Object.keys(params).length) return url; // find url params, they start with :
  81. const requiredParams = url.split('/').filter(s => s && s.includes(':'));
  82. if (!requiredParams.length) return url;
  83. let parameterizedUrl = url;
  84. requiredParams.forEach(p => {
  85. // strip the : and replace with param value
  86. const key = p.substr(1);
  87. const value = params[key];
  88. if (!value) {
  89. throw new Error(`required param ${key} not provided`);
  90. }
  91. parameterizedUrl = parameterizedUrl.replace(p, value); // remove required param from param list
  92. // so it doesn't become part of the query params
  93. delete params[key];
  94. });
  95. return parameterizedUrl;
  96. }
  97. static getApiRateLimitInfo(headers) {
  98. return {
  99. 'x-rate-limit-limit': headers['x-rate-limit-limit'],
  100. 'x-rate-limit-remaining-ttl': headers['x-rate-limit-remaining-ttl'],
  101. 'x-rate-limit-used': headers['x-rate-limit-used'],
  102. 'x-rate-limit-remaining-ip-ttl': headers['x-rate-limit-remaining-ip-ttl'],
  103. 'x-rate-limit-ip-used': headers['x-rate-limit-ip-used']
  104. };
  105. }
  106. static getApiErrorMessage(error, url) {
  107. var _error$response, _error$response$data;
  108. return (error === null || error === void 0 ? void 0 : (_error$response = error.response) === null || _error$response === void 0 ? void 0 : (_error$response$data = _error$response.data) === null || _error$response$data === void 0 ? void 0 : _error$response$data.message) || (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : error.toString()) || `Web3 API error while calling ${url}`;
  109. }
  110. static async fetch({
  111. endpoint,
  112. params: providedParams
  113. }) {
  114. // Make a shallow copy to prevent modification of original params
  115. const params = _objectSpread({}, providedParams);
  116. if (this.Moralis) {
  117. const {
  118. User,
  119. account
  120. } = this.Moralis;
  121. const user = User.current();
  122. if (!params.address) {
  123. if (user) {
  124. params.address = user.get('ethAddress');
  125. } else if (account) {
  126. params.address = account;
  127. }
  128. }
  129. }
  130. if (!this.apiKey) {
  131. return this.fetchFromServer(endpoint.name, params);
  132. }
  133. return this.fetchFromApi(endpoint, params);
  134. }
  135. static async fetchFromApi(endpoint, params) {
  136. const {
  137. method = 'GET',
  138. url,
  139. bodyParams
  140. } = endpoint;
  141. try {
  142. const parameterizedUrl = this.getParameterizedUrl(url, params);
  143. const body = this.getBody(params, bodyParams);
  144. const response = await axios(this.baseURL + parameterizedUrl, {
  145. params,
  146. method,
  147. body,
  148. headers: {
  149. Accept: 'application/json',
  150. 'Content-Type': 'application/json',
  151. 'x-api-key': this.apiKey
  152. }
  153. });
  154. return response.data;
  155. } catch (error) {
  156. const {
  157. status,
  158. headers,
  159. data
  160. } = error.response;
  161. let msg;
  162. if (status === 429) {
  163. msg = `This Moralis Server is rate-limited because of the plan restrictions. See the details about the current rate and throttle limits: ${JSON.stringify(this.getApiRateLimitInfo(headers))}`;
  164. } else {
  165. msg = this.getApiErrorMessage(error, url);
  166. }
  167. throw new Error(msg);
  168. }
  169. }
  170. static async fetchFromServer(name, options) {
  171. if (!this.serverUrl) {
  172. throw new Error('Web3Api not initialized, run Moralis.start() first');
  173. }
  174. try {
  175. const http = axios.create({
  176. baseURL: this.serverUrl
  177. });
  178. if (!options.chain) options.chain = 'eth';
  179. const user = this.Moralis.User.current();
  180. if (user) {
  181. options._SessionToken = user.attributes.sessionToken;
  182. options._ApplicationId = this.Moralis.applicationId;
  183. }
  184. const response = await http.post(`/functions/${name}`, options, {
  185. headers: {
  186. Accept: 'application/json',
  187. 'Content-Type': 'application/json'
  188. }
  189. });
  190. return response.data.result;
  191. } catch (error) {
  192. var _error$response2, _error$response2$data;
  193. if ((_error$response2 = error.response) !== null && _error$response2 !== void 0 && (_error$response2$data = _error$response2.data) !== null && _error$response2$data !== void 0 && _error$response2$data.error) {
  194. throw new Error(error.response.data.error);
  195. }
  196. throw error;
  197. }
  198. }
  199. }
  200. _defineProperty(Web3Api, "baseURL", 'https://deep-index.moralis.io/api/v2');
  201. _defineProperty(Web3Api, "BodyParamTypes", {
  202. setBody: 'set body',
  203. property: 'property'
  204. });
  205. _defineProperty(Web3Api, "native", {
  206. getBlock: async (options = {}) => Web3Api.fetch({
  207. endpoint: {
  208. "method": "GET",
  209. "group": "native",
  210. "name": "getBlock",
  211. "url": "/block/:block_number_or_hash"
  212. },
  213. params: options
  214. }),
  215. getDateToBlock: async (options = {}) => Web3Api.fetch({
  216. endpoint: {
  217. "method": "GET",
  218. "group": "native",
  219. "name": "getDateToBlock",
  220. "url": "/dateToBlock"
  221. },
  222. params: options
  223. }),
  224. getLogsByAddress: async (options = {}) => Web3Api.fetch({
  225. endpoint: {
  226. "method": "GET",
  227. "group": "native",
  228. "name": "getLogsByAddress",
  229. "url": "/:address/logs"
  230. },
  231. params: options
  232. }),
  233. getNFTTransfersByBlock: async (options = {}) => Web3Api.fetch({
  234. endpoint: {
  235. "method": "GET",
  236. "group": "native",
  237. "name": "getNFTTransfersByBlock",
  238. "url": "/block/:block_number_or_hash/nft/transfers"
  239. },
  240. params: options
  241. }),
  242. getTransaction: async (options = {}) => Web3Api.fetch({
  243. endpoint: {
  244. "method": "GET",
  245. "group": "native",
  246. "name": "getTransaction",
  247. "url": "/transaction/:transaction_hash"
  248. },
  249. params: options
  250. }),
  251. getContractEvents: async (options = {}) => Web3Api.fetch({
  252. endpoint: {
  253. "method": "POST",
  254. "group": "native",
  255. "name": "getContractEvents",
  256. "url": "/:address/events",
  257. "bodyParams": [{
  258. "key": "data",
  259. "type": "set body",
  260. "required": false
  261. }]
  262. },
  263. params: options
  264. }),
  265. runContractFunction: async (options = {}) => Web3Api.fetch({
  266. endpoint: {
  267. "method": "POST",
  268. "group": "native",
  269. "name": "runContractFunction",
  270. "url": "/:address/function",
  271. "bodyParams": [{
  272. "key": "abi",
  273. "type": "property",
  274. "required": true
  275. }, {
  276. "key": "params",
  277. "type": "property",
  278. "required": false
  279. }]
  280. },
  281. params: options
  282. })
  283. });
  284. _defineProperty(Web3Api, "account", {
  285. getTransactions: async (options = {}) => Web3Api.fetch({
  286. endpoint: {
  287. "method": "GET",
  288. "group": "account",
  289. "name": "getTransactions",
  290. "url": "/:address"
  291. },
  292. params: options
  293. }),
  294. getNativeBalance: async (options = {}) => Web3Api.fetch({
  295. endpoint: {
  296. "method": "GET",
  297. "group": "account",
  298. "name": "getNativeBalance",
  299. "url": "/:address/balance"
  300. },
  301. params: options
  302. }),
  303. getTokenBalances: async (options = {}) => Web3Api.fetch({
  304. endpoint: {
  305. "method": "GET",
  306. "group": "account",
  307. "name": "getTokenBalances",
  308. "url": "/:address/erc20"
  309. },
  310. params: options
  311. }),
  312. getTokenTransfers: async (options = {}) => Web3Api.fetch({
  313. endpoint: {
  314. "method": "GET",
  315. "group": "account",
  316. "name": "getTokenTransfers",
  317. "url": "/:address/erc20/transfers"
  318. },
  319. params: options
  320. }),
  321. getNFTs: async (options = {}) => Web3Api.fetch({
  322. endpoint: {
  323. "method": "GET",
  324. "group": "account",
  325. "name": "getNFTs",
  326. "url": "/:address/nft"
  327. },
  328. params: options
  329. }),
  330. getNFTTransfers: async (options = {}) => Web3Api.fetch({
  331. endpoint: {
  332. "method": "GET",
  333. "group": "account",
  334. "name": "getNFTTransfers",
  335. "url": "/:address/nft/transfers"
  336. },
  337. params: options
  338. }),
  339. getNFTsForContract: async (options = {}) => Web3Api.fetch({
  340. endpoint: {
  341. "method": "GET",
  342. "group": "account",
  343. "name": "getNFTsForContract",
  344. "url": "/:address/nft/:token_address"
  345. },
  346. params: options
  347. })
  348. });
  349. _defineProperty(Web3Api, "token", {
  350. getTokenMetadata: async (options = {}) => Web3Api.fetch({
  351. endpoint: {
  352. "method": "GET",
  353. "group": "token",
  354. "name": "getTokenMetadata",
  355. "url": "/erc20/metadata"
  356. },
  357. params: options
  358. }),
  359. getNFTTrades: async (options = {}) => Web3Api.fetch({
  360. endpoint: {
  361. "method": "GET",
  362. "group": "token",
  363. "name": "getNFTTrades",
  364. "url": "/nft/:address/trades"
  365. },
  366. params: options
  367. }),
  368. getNFTLowestPrice: async (options = {}) => Web3Api.fetch({
  369. endpoint: {
  370. "method": "GET",
  371. "group": "token",
  372. "name": "getNFTLowestPrice",
  373. "url": "/nft/:address/lowestprice"
  374. },
  375. params: options
  376. }),
  377. getTokenMetadataBySymbol: async (options = {}) => Web3Api.fetch({
  378. endpoint: {
  379. "method": "GET",
  380. "group": "token",
  381. "name": "getTokenMetadataBySymbol",
  382. "url": "/erc20/metadata/symbols"
  383. },
  384. params: options
  385. }),
  386. getTokenPrice: async (options = {}) => Web3Api.fetch({
  387. endpoint: {
  388. "method": "GET",
  389. "group": "token",
  390. "name": "getTokenPrice",
  391. "url": "/erc20/:address/price"
  392. },
  393. params: options
  394. }),
  395. getTokenAddressTransfers: async (options = {}) => Web3Api.fetch({
  396. endpoint: {
  397. "method": "GET",
  398. "group": "token",
  399. "name": "getTokenAddressTransfers",
  400. "url": "/erc20/:address/transfers"
  401. },
  402. params: options
  403. }),
  404. getTokenAllowance: async (options = {}) => Web3Api.fetch({
  405. endpoint: {
  406. "method": "GET",
  407. "group": "token",
  408. "name": "getTokenAllowance",
  409. "url": "/erc20/:address/allowance"
  410. },
  411. params: options
  412. }),
  413. searchNFTs: async (options = {}) => Web3Api.fetch({
  414. endpoint: {
  415. "method": "GET",
  416. "group": "token",
  417. "name": "searchNFTs",
  418. "url": "/nft/search"
  419. },
  420. params: options
  421. }),
  422. getNftTransfersFromToBlock: async (options = {}) => Web3Api.fetch({
  423. endpoint: {
  424. "method": "GET",
  425. "group": "token",
  426. "name": "getNftTransfersFromToBlock",
  427. "url": "/nft/transfers"
  428. },
  429. params: options
  430. }),
  431. getAllTokenIds: async (options = {}) => Web3Api.fetch({
  432. endpoint: {
  433. "method": "GET",
  434. "group": "token",
  435. "name": "getAllTokenIds",
  436. "url": "/nft/:address"
  437. },
  438. params: options
  439. }),
  440. getContractNFTTransfers: async (options = {}) => Web3Api.fetch({
  441. endpoint: {
  442. "method": "GET",
  443. "group": "token",
  444. "name": "getContractNFTTransfers",
  445. "url": "/nft/:address/transfers"
  446. },
  447. params: options
  448. }),
  449. getNFTOwners: async (options = {}) => Web3Api.fetch({
  450. endpoint: {
  451. "method": "GET",
  452. "group": "token",
  453. "name": "getNFTOwners",
  454. "url": "/nft/:address/owners"
  455. },
  456. params: options
  457. }),
  458. getNFTMetadata: async (options = {}) => Web3Api.fetch({
  459. endpoint: {
  460. "method": "GET",
  461. "group": "token",
  462. "name": "getNFTMetadata",
  463. "url": "/nft/:address/metadata"
  464. },
  465. params: options
  466. }),
  467. reSyncMetadata: async (options = {}) => Web3Api.fetch({
  468. endpoint: {
  469. "method": "GET",
  470. "group": "token",
  471. "name": "reSyncMetadata",
  472. "url": "/nft/:address/:token_id/metadata/resync"
  473. },
  474. params: options
  475. }),
  476. syncNFTContract: async (options = {}) => Web3Api.fetch({
  477. endpoint: {
  478. "method": "PUT",
  479. "group": "token",
  480. "name": "syncNFTContract",
  481. "url": "/nft/:address/sync"
  482. },
  483. params: options
  484. }),
  485. getTokenIdMetadata: async (options = {}) => Web3Api.fetch({
  486. endpoint: {
  487. "method": "GET",
  488. "group": "token",
  489. "name": "getTokenIdMetadata",
  490. "url": "/nft/:address/:token_id"
  491. },
  492. params: options
  493. }),
  494. getTokenIdOwners: async (options = {}) => Web3Api.fetch({
  495. endpoint: {
  496. "method": "GET",
  497. "group": "token",
  498. "name": "getTokenIdOwners",
  499. "url": "/nft/:address/:token_id/owners"
  500. },
  501. params: options
  502. }),
  503. getWalletTokenIdTransfers: async (options = {}) => Web3Api.fetch({
  504. endpoint: {
  505. "method": "GET",
  506. "group": "token",
  507. "name": "getWalletTokenIdTransfers",
  508. "url": "/nft/:address/:token_id/transfers"
  509. },
  510. params: options
  511. })
  512. });
  513. _defineProperty(Web3Api, "resolve", {
  514. resolveDomain: async (options = {}) => Web3Api.fetch({
  515. endpoint: {
  516. "method": "GET",
  517. "group": "resolve",
  518. "name": "resolveDomain",
  519. "url": "/resolve/:domain"
  520. },
  521. params: options
  522. }),
  523. resolveAddress: async (options = {}) => Web3Api.fetch({
  524. endpoint: {
  525. "method": "GET",
  526. "group": "resolve",
  527. "name": "resolveAddress",
  528. "url": "/resolve/:address/reverse"
  529. },
  530. params: options
  531. })
  532. });
  533. _defineProperty(Web3Api, "defi", {
  534. getPairReserves: async (options = {}) => Web3Api.fetch({
  535. endpoint: {
  536. "method": "GET",
  537. "group": "defi",
  538. "name": "getPairReserves",
  539. "url": "/:pair_address/reserves"
  540. },
  541. params: options
  542. }),
  543. getPairAddress: async (options = {}) => Web3Api.fetch({
  544. endpoint: {
  545. "method": "GET",
  546. "group": "defi",
  547. "name": "getPairAddress",
  548. "url": "/:token0_address/:token1_address/pairAddress"
  549. },
  550. params: options
  551. })
  552. });
  553. _defineProperty(Web3Api, "storage", {
  554. uploadFolder: async (options = {}) => Web3Api.fetch({
  555. endpoint: {
  556. "method": "POST",
  557. "group": "storage",
  558. "name": "uploadFolder",
  559. "url": "/ipfs/uploadFolder",
  560. "bodyParams": [{
  561. "key": "data",
  562. "type": "set body",
  563. "required": false
  564. }]
  565. },
  566. params: options
  567. })
  568. });
  569. _defineProperty(Web3Api, "info", {
  570. web3ApiVersion: async (options = {}) => Web3Api.fetch({
  571. endpoint: {
  572. "method": "GET",
  573. "group": "info",
  574. "name": "web3ApiVersion",
  575. "url": "/web3/version"
  576. },
  577. params: options
  578. }),
  579. endpointWeights: async (options = {}) => Web3Api.fetch({
  580. endpoint: {
  581. "method": "GET",
  582. "group": "info",
  583. "name": "endpointWeights",
  584. "url": "/info/endpointWeights"
  585. },
  586. params: options
  587. })
  588. });
  589. var _default = Web3Api;
  590. exports.default = _default;