MoralisSolanaApi.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  7. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  8. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  9. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  10. var axios = require('axios');
  11. var SolanaApi = function () {
  12. function SolanaApi() {
  13. (0, _classCallCheck2.default)(this, SolanaApi);
  14. }
  15. (0, _createClass2.default)(SolanaApi, null, [{
  16. key: "initialize",
  17. value: function (_ref) {
  18. var apiKey = _ref.apiKey,
  19. serverUrl = _ref.serverUrl,
  20. _ref$Moralis = _ref.Moralis,
  21. Moralis = _ref$Moralis === void 0 ? null : _ref$Moralis;
  22. if (!serverUrl && !apiKey) {
  23. throw new Error('SolanaApi.initialize failed: initialize with apiKey or serverUrl');
  24. }
  25. if (apiKey) this.apiKey = apiKey;
  26. if (serverUrl) this.serverUrl = serverUrl;
  27. this.Moralis = Moralis;
  28. }
  29. }, {
  30. key: "getBody",
  31. value: function (params, bodyParams) {
  32. var _this = this;
  33. if (!params || !bodyParams || !bodyParams.length) {
  34. return undefined;
  35. }
  36. var body = {};
  37. bodyParams.forEach(function (_ref2) {
  38. var key = _ref2.key,
  39. type = _ref2.type,
  40. required = _ref2.required;
  41. if (params[key] === undefined) {
  42. if (required) throw new Error("param " + key + " is required!");
  43. } else if (type === _this.BodyParamTypes.setBody) {
  44. body = params[key];
  45. } else {
  46. body[key] = params[key];
  47. }
  48. delete params[key];
  49. });
  50. return body;
  51. }
  52. }, {
  53. key: "getParameterizedUrl",
  54. value: function (url, params) {
  55. if (!Object.keys(params).length) return url;
  56. var requiredParams = url.split('/').filter(function (s) {
  57. return s && s.includes(':');
  58. });
  59. if (!requiredParams.length) return url;
  60. var parameterizedUrl = url;
  61. requiredParams.forEach(function (p) {
  62. var key = p.substr(1);
  63. var value = params[key];
  64. if (!value) {
  65. throw new Error("required param " + key + " not provided");
  66. }
  67. parameterizedUrl = parameterizedUrl.replace(p, value);
  68. delete params[key];
  69. });
  70. return parameterizedUrl;
  71. }
  72. }, {
  73. key: "getApiRateLimitInfo",
  74. value: function (headers) {
  75. return {
  76. 'x-rate-limit-limit': headers['x-rate-limit-limit'],
  77. 'x-rate-limit-remaining-ttl': headers['x-rate-limit-remaining-ttl'],
  78. 'x-rate-limit-used': headers['x-rate-limit-used'],
  79. 'x-rate-limit-remaining-ip-ttl': headers['x-rate-limit-remaining-ip-ttl'],
  80. 'x-rate-limit-ip-used': headers['x-rate-limit-ip-used']
  81. };
  82. }
  83. }, {
  84. key: "getErrorMessage",
  85. value: function (error, url) {
  86. var _error$response, _error$response$data;
  87. return (error == null ? void 0 : (_error$response = error.response) == null ? void 0 : (_error$response$data = _error$response.data) == null ? void 0 : _error$response$data.message) || (error == null ? void 0 : error.message) || (error == null ? void 0 : error.toString()) || "Solana API error while calling " + url;
  88. }
  89. }, {
  90. key: "fetch",
  91. value: function (_ref3) {
  92. var endpoint, providedParams, params, User, user;
  93. return _regenerator.default.async(function (_context) {
  94. while (1) {
  95. switch (_context.prev = _context.next) {
  96. case 0:
  97. endpoint = _ref3.endpoint, providedParams = _ref3.params;
  98. params = (0, _extends2.default)({}, providedParams);
  99. if (this.Moralis) {
  100. User = this.Moralis.User;
  101. user = User.current();
  102. if (!params.address) {
  103. if (user) {
  104. params.address = user.get('solAddress');
  105. }
  106. }
  107. }
  108. if (!params.network) params.network = 'mainnet';
  109. if (!params.responseType) params.responseType = 'native';
  110. if (this.apiKey) {
  111. _context.next = 7;
  112. break;
  113. }
  114. return _context.abrupt("return", this.fetchFromServer(endpoint.name, params));
  115. case 7:
  116. return _context.abrupt("return", this.fetchFromApi(endpoint, params));
  117. case 8:
  118. case "end":
  119. return _context.stop();
  120. }
  121. }
  122. }, null, this, null, Promise);
  123. }
  124. }, {
  125. key: "fetchFromApi",
  126. value: function (endpoint, params) {
  127. var _endpoint$method, method, url, bodyParams, parameterizedUrl, body, response, _error$response2, status, headers, data, msg;
  128. return _regenerator.default.async(function (_context2) {
  129. while (1) {
  130. switch (_context2.prev = _context2.next) {
  131. case 0:
  132. _endpoint$method = endpoint.method, method = _endpoint$method === void 0 ? 'GET' : _endpoint$method, url = endpoint.url, bodyParams = endpoint.bodyParams;
  133. _context2.prev = 1;
  134. parameterizedUrl = this.getParameterizedUrl(url, params);
  135. body = this.getBody(params, bodyParams);
  136. _context2.next = 6;
  137. return _regenerator.default.awrap(axios(this.baseURL + parameterizedUrl, {
  138. params: params,
  139. method: method,
  140. body: body,
  141. headers: {
  142. Accept: 'application/json',
  143. 'Content-Type': 'application/json',
  144. 'x-api-key': this.apiKey
  145. }
  146. }));
  147. case 6:
  148. response = _context2.sent;
  149. return _context2.abrupt("return", response.data);
  150. case 10:
  151. _context2.prev = 10;
  152. _context2.t0 = _context2["catch"](1);
  153. _error$response2 = _context2.t0.response, status = _error$response2.status, headers = _error$response2.headers, data = _error$response2.data;
  154. if (status === 429) {
  155. 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));
  156. } else {
  157. msg = this.getApiErrorMessage(_context2.t0, url);
  158. }
  159. throw new Error(msg);
  160. case 15:
  161. case "end":
  162. return _context2.stop();
  163. }
  164. }
  165. }, null, this, [[1, 10]], Promise);
  166. }
  167. }, {
  168. key: "fetchFromServer",
  169. value: function (name, options) {
  170. var http, user, response, _error$response3, _error$response3$data;
  171. return _regenerator.default.async(function (_context3) {
  172. while (1) {
  173. switch (_context3.prev = _context3.next) {
  174. case 0:
  175. if (this.serverUrl) {
  176. _context3.next = 2;
  177. break;
  178. }
  179. throw new Error('SolanaAPI not initialized, run Moralis.start() first');
  180. case 2:
  181. _context3.prev = 2;
  182. http = axios.create({
  183. baseURL: this.serverUrl
  184. });
  185. user = this.Moralis.User.current();
  186. if (user) {
  187. options._SessionToken = user.attributes.sessionToken;
  188. options._ApplicationId = this.Moralis.applicationId;
  189. }
  190. _context3.next = 8;
  191. return _regenerator.default.awrap(http.post("/functions/sol-" + name, options, {
  192. headers: {
  193. Accept: 'application/json',
  194. 'Content-Type': 'application/json'
  195. }
  196. }));
  197. case 8:
  198. response = _context3.sent;
  199. return _context3.abrupt("return", response.data.result);
  200. case 12:
  201. _context3.prev = 12;
  202. _context3.t0 = _context3["catch"](2);
  203. if (!((_error$response3 = _context3.t0.response) != null && (_error$response3$data = _error$response3.data) != null && _error$response3$data.error)) {
  204. _context3.next = 16;
  205. break;
  206. }
  207. throw new Error(_context3.t0.response.data.error);
  208. case 16:
  209. throw _context3.t0;
  210. case 17:
  211. case "end":
  212. return _context3.stop();
  213. }
  214. }
  215. }, null, this, [[2, 12]], Promise);
  216. }
  217. }]);
  218. return SolanaApi;
  219. }();
  220. SolanaApi.baseURL = 'https://solana-gateway.moralis.io';
  221. SolanaApi.BodyParamTypes = {
  222. setBody: 'set body',
  223. property: 'property'
  224. };
  225. SolanaApi.account = {
  226. balance: function () {
  227. var options,
  228. _args4 = arguments;
  229. return _regenerator.default.async(function (_context4) {
  230. while (1) {
  231. switch (_context4.prev = _context4.next) {
  232. case 0:
  233. options = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : {};
  234. return _context4.abrupt("return", SolanaApi.fetch({
  235. endpoint: {
  236. "method": "GET",
  237. "group": "account",
  238. "name": "balance",
  239. "url": "/account/:network/:address/balance"
  240. },
  241. params: options
  242. }));
  243. case 2:
  244. case "end":
  245. return _context4.stop();
  246. }
  247. }
  248. }, null, null, null, Promise);
  249. },
  250. getSPL: function () {
  251. var options,
  252. _args5 = arguments;
  253. return _regenerator.default.async(function (_context5) {
  254. while (1) {
  255. switch (_context5.prev = _context5.next) {
  256. case 0:
  257. options = _args5.length > 0 && _args5[0] !== undefined ? _args5[0] : {};
  258. return _context5.abrupt("return", SolanaApi.fetch({
  259. endpoint: {
  260. "method": "GET",
  261. "group": "account",
  262. "name": "getSPL",
  263. "url": "/account/:network/:address/tokens"
  264. },
  265. params: options
  266. }));
  267. case 2:
  268. case "end":
  269. return _context5.stop();
  270. }
  271. }
  272. }, null, null, null, Promise);
  273. },
  274. getNFTs: function () {
  275. var options,
  276. _args6 = arguments;
  277. return _regenerator.default.async(function (_context6) {
  278. while (1) {
  279. switch (_context6.prev = _context6.next) {
  280. case 0:
  281. options = _args6.length > 0 && _args6[0] !== undefined ? _args6[0] : {};
  282. return _context6.abrupt("return", SolanaApi.fetch({
  283. endpoint: {
  284. "method": "GET",
  285. "group": "account",
  286. "name": "getNFTs",
  287. "url": "/account/:network/:address/nft"
  288. },
  289. params: options
  290. }));
  291. case 2:
  292. case "end":
  293. return _context6.stop();
  294. }
  295. }
  296. }, null, null, null, Promise);
  297. },
  298. getPortfolio: function () {
  299. var options,
  300. _args7 = arguments;
  301. return _regenerator.default.async(function (_context7) {
  302. while (1) {
  303. switch (_context7.prev = _context7.next) {
  304. case 0:
  305. options = _args7.length > 0 && _args7[0] !== undefined ? _args7[0] : {};
  306. return _context7.abrupt("return", SolanaApi.fetch({
  307. endpoint: {
  308. "method": "GET",
  309. "group": "account",
  310. "name": "getPortfolio",
  311. "url": "/account/:network/:address/portfolio"
  312. },
  313. params: options
  314. }));
  315. case 2:
  316. case "end":
  317. return _context7.stop();
  318. }
  319. }
  320. }, null, null, null, Promise);
  321. }
  322. };
  323. SolanaApi.nft = {
  324. getNFTMetadata: function () {
  325. var options,
  326. _args8 = arguments;
  327. return _regenerator.default.async(function (_context8) {
  328. while (1) {
  329. switch (_context8.prev = _context8.next) {
  330. case 0:
  331. options = _args8.length > 0 && _args8[0] !== undefined ? _args8[0] : {};
  332. return _context8.abrupt("return", SolanaApi.fetch({
  333. endpoint: {
  334. "method": "GET",
  335. "group": "nft",
  336. "name": "getNFTMetadata",
  337. "url": "/nft/:network/:address/metadata"
  338. },
  339. params: options
  340. }));
  341. case 2:
  342. case "end":
  343. return _context8.stop();
  344. }
  345. }
  346. }, null, null, null, Promise);
  347. }
  348. };
  349. var _default = SolanaApi;
  350. exports.default = _default;