moralis_sdk.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* import moralis */
  2. const Moralis = require("moralis/node");
  3. var utils = require('./utils.js');
  4. var config = require('../config/config.js');
  5. /* Moralis init code */
  6. var serverUrl = config.moralis_config.test_server.SERVER_URL;
  7. var appId = config.moralis_config.test_server.APP_ID;
  8. var masterKey = config.moralis_config.test_server.MASTER_KEY;
  9. var moralisSecret = config.moralis_config.test_server.MORALIS_SECRET;
  10. if (config.build_config.open_test == 0) {
  11. serverUrl = config.moralis_config.main_server.SERVER_URL;
  12. appId = config.moralis_config.main_server.APP_ID;
  13. masterKey = config.moralis_config.main_server.MASTER_KEY;
  14. moralisSecret = config.moralis_config.main_server.MORALIS_SECRET;
  15. }
  16. // 内部异常
  17. const ERROR_CODE_001 = -1;
  18. const SUCCEED_CODE = 0;
  19. /**
  20. * 初始化 moralis
  21. * https://st94nif1cq.feishu.cn/docs/doccnNxG2UwHPCdZXbywgbdy13f#
  22. */
  23. async function initMasterSDK() {
  24. await Moralis.start({ serverUrl, appId, masterKey });
  25. }
  26. async function initMoralisSecretSDK() {
  27. await Moralis.start({ serverUrl, appId, moralisSecret });
  28. }
  29. function toJson(code_, obj_, errMsg_) {
  30. return utils.toJson(code_, obj_, errMsg_);
  31. }
  32. const transfer = async (obj) => {
  33. console.debug("fun transfer in ", obj);
  34. console.debug("fun transfer serverUrl ", serverUrl);
  35. console.debug("fun transfer appId ", serverUrl);
  36. console.debug("fun transfer moralisSecret ", moralisSecret);
  37. await initMoralisSecretSDK();
  38. // initSDK(moralisSecret);
  39. console.debug("fun transfer start ok ");
  40. const opts = {};
  41. opts.chainId = '0x61';
  42. opts.privateKey = config.moralis_config.test_server.DEFAULT_PRIVATE_KEY;
  43. opts.type = "erc20"; //native erc20
  44. if (!obj.receiver || !obj.amount || parseFloat(obj.amount) <= 0) {
  45. console.error("fun transfer parameter error.");
  46. return toJson(ERROR_CODE_001, null, "please check receiver or amount parameter is ok ?");
  47. }
  48. if (obj.chainId != null) {
  49. opts.chainId = obj.chainId;
  50. }
  51. if (obj.type != null) {
  52. opts.type = obj.type;
  53. }
  54. if (obj.from_block != null) {
  55. opts.from_block = obj.from_block;
  56. }
  57. if (obj.to_block != null) {
  58. opts.to_block = obj.to_block;
  59. }
  60. opts.contractAddress = obj.contractAddress;
  61. opts.receiver = obj.receiver;
  62. //调用者传入
  63. // opts.amount = Moralis.Units.Token(obj.amount, 18);
  64. opts.amount = obj.amount;
  65. console.log("NENET_CRYPT_KEY", process.env.NENET_CRYPT_KEY);
  66. if (obj.privateKey != null) {
  67. opts.privateKey = obj.privateKey;
  68. }
  69. //解密私钥
  70. var privateKey = utils.decryptPrivityKey(process.env.NENET_CRYPT_KEY, opts.privateKey);
  71. if (privateKey == null) {
  72. return toJson(-1, null, "decryptPrivityKey error.");
  73. }
  74. try {
  75. // sending 0.5 DAI tokens with 18 decimals on BSC testnet
  76. var options = Moralis.TransferOptions = {}
  77. if (opts.contractAddress) { //如果存在就是代币
  78. options = Moralis.TransferOptions = {
  79. type: opts.type,
  80. amount: opts.amount,
  81. receiver: opts.receiver, //接收钱包地址
  82. contractAddress: opts.contractAddress //用户合约地址
  83. };
  84. // console.log("options=",options);
  85. } else { //ETH or BNB
  86. options = Moralis.TransferOptions = {
  87. type: opts.type,
  88. amount: opts.amount,
  89. receiver: opts.receiver, //接收钱包地址
  90. };
  91. // console.log("options else=",options);
  92. }
  93. // Enable web3
  94. await Moralis.enableWeb3({
  95. //BSC mainnet = 0x38-56 testnet:0x61-97
  96. chainId: opts.chainId,
  97. privateKey: opts.privateKey,
  98. });
  99. var ret = await Moralis.transfer(options);
  100. return toJson(SUCCEED_CODE, ret, "");
  101. } catch (error) {
  102. console.log('transfer error:', error);
  103. if (error.reason != null) {
  104. return toJson(ERROR_CODE_001, null, error.toString());;
  105. } else {
  106. return toJson(ERROR_CODE_001, null, error);;
  107. }
  108. }
  109. };
  110. const getAllTokenWithdrawInfoLists = async (obj) => {
  111. await initMasterSDK();
  112. console.log('queryAllTokenBalance:', JSON.stringify(config.token_balance_config.TOKEN_BALANCE));
  113. return toJson(SUCCEED_CODE, JSON.stringify(config.token_balance_config.TOKEN_BALANCE), null);
  114. }
  115. /**
  116. * 获取代币价格 -> usdPrice
  117. */
  118. const getAllTotkenPrice = async (address, chain) => {
  119. await initMasterSDK();
  120. const options = {
  121. address: address,
  122. chain: chain,
  123. };
  124. try {
  125. console.debug("fun getTotkenPrice in ", options);
  126. if (Array.isArray(config.token_price_config)) {
  127. // const arrays = JSON.parse(config.token_price_config);
  128. return toJson(SUCCEED_CODE, config.token_price_config, null);
  129. }
  130. // var ret = await Moralis.Web3API.token.getTokenPrice(options);
  131. return toJson(ERROR_CODE_001, null, 'getTotkenPrice error.');
  132. } catch (error) {
  133. console.error("getTotkenPrice=", error);
  134. return toJson(ERROR_CODE_001, null, error);;
  135. }
  136. }
  137. //获取交易记录
  138. //hash 0xe09ba3a4c9f7a8902e01af68d0f1f91906f3f7db1195227e61c45c0e86b2630a
  139. async function getTokenTransfers(opt) {
  140. await initMasterSDK();
  141. console.debug("fun getTokenTransfers in ", opt);
  142. const options = {};
  143. options.type = 'all';
  144. options.chain = 'bsc';
  145. //todo 对必填参数做检验
  146. if (opt.chain != null) {
  147. options.chain = opt.chain;
  148. }
  149. if (opt.order != null) {
  150. options.order = opt.order;
  151. }
  152. if (opt.startTime != null) {
  153. options.from_date = opt.startTime;
  154. }
  155. if (opt.endTime != null) {
  156. options.to_date = opt.endTime;
  157. }
  158. if (opt.from_block != null) {
  159. options.from_block = opt.from_block;
  160. }
  161. if (opt.to_block != null) {
  162. options.to_block = opt.to_block;
  163. }
  164. if (opt.transaction_hash) {
  165. options.transaction_hash = opt.transaction_hash;
  166. options.type = 'transaction_hash';
  167. }
  168. console.debug('getTokenTransfers-->>>', options);
  169. if (options.type == 'all') {//查询主流币和 20 币所有的交易
  170. try {
  171. if (opt.address != null) {
  172. options.address = opt.address;
  173. } else {
  174. return toJson(ERROR_CODE_001, null, "please check address parameter is ok ?");
  175. }
  176. //主流币
  177. var t_1 = await Moralis.Web3API.account.getTransactions(options);
  178. //20币
  179. var t_2 = await Moralis.Web3API.account.getTokenTransfers(options);
  180. let arr = t_1.result;
  181. let arr1 = t_2.result;
  182. if (Array.isArray(arr1) && Array.isArray(arr)) {
  183. let arr2 = arr.concat(arr1);
  184. t_1.result = arr2;
  185. }
  186. return toJson(SUCCEED_CODE, t_1, null);
  187. } catch (error) {
  188. console.error("getTransactions error:", error)
  189. return toJson(ERROR_CODE_001, null, error);;
  190. }
  191. } else if (options.type == 'transaction_hash') {//根据哈希查询
  192. try {
  193. //native
  194. const transaction = await Moralis.Web3API.native.getTransaction(options);
  195. var arr = [];
  196. if (transaction)
  197. arr.push(transaction)
  198. var obj = { result: arr }
  199. return toJson(SUCCEED_CODE, obj, null);
  200. } catch (error) {
  201. console.error("native getTransaction error:", error)
  202. return toJson(ERROR_CODE_001, null, error);;
  203. }
  204. } else {
  205. return toJson(ERROR_CODE_001, null, "This type is not supported.");;
  206. }
  207. }
  208. module.exports = {
  209. transfer,
  210. getTokenTransfers,
  211. toJson,
  212. getAllTokenWithdrawInfoLists,
  213. getAllTotkenPrice,
  214. }