123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- /* import moralis */
- const Moralis = require("moralis/node");
- var utils = require('./utils.js');
- var config = require('../config/config.js');
- /* Moralis init code */
- var serverUrl = config.moralis_config.test_server.SERVER_URL;
- var appId = config.moralis_config.test_server.APP_ID;
- var masterKey = config.moralis_config.test_server.MASTER_KEY;
- var moralisSecret = config.moralis_config.test_server.MORALIS_SECRET;
- if (config.build_config.open_test == 0) {
- serverUrl = config.moralis_config.main_server.SERVER_URL;
- appId = config.moralis_config.main_server.APP_ID;
- masterKey = config.moralis_config.main_server.MASTER_KEY;
- moralisSecret = config.moralis_config.main_server.MORALIS_SECRET;
- }
- // 内部异常
- const ERROR_CODE_001 = -1;
- const SUCCEED_CODE = 0;
- /**
- * 初始化 moralis
- * https://st94nif1cq.feishu.cn/docs/doccnNxG2UwHPCdZXbywgbdy13f#
- */
- async function initMasterSDK() {
- await Moralis.start({ serverUrl, appId, masterKey });
- }
- async function initMoralisSecretSDK() {
- await Moralis.start({ serverUrl, appId, moralisSecret });
- }
- function toJson(code_, obj_, errMsg_) {
- return utils.toJson(code_, obj_, errMsg_);
- }
- const transfer = async (obj) => {
- console.debug("fun transfer in ", obj);
- console.debug("fun transfer serverUrl ", serverUrl);
- console.debug("fun transfer appId ", serverUrl);
- console.debug("fun transfer moralisSecret ", moralisSecret);
- await initMoralisSecretSDK();
- // initSDK(moralisSecret);
- console.debug("fun transfer start ok ");
- const opts = {};
- opts.chainId = '0x61';
- opts.privateKey = config.moralis_config.test_server.DEFAULT_PRIVATE_KEY;
- opts.type = "erc20"; //native erc20
- if (!obj.receiver || !obj.amount || parseFloat(obj.amount) <= 0) {
- console.error("fun transfer parameter error.");
- return toJson(ERROR_CODE_001, null, "please check receiver or amount parameter is ok ?");
- }
- if (obj.chainId != null) {
- opts.chainId = obj.chainId;
- }
- if (obj.type != null) {
- opts.type = obj.type;
- }
- if (obj.from_block != null) {
- opts.from_block = obj.from_block;
- }
- if (obj.to_block != null) {
- opts.to_block = obj.to_block;
- }
- opts.contractAddress = obj.contractAddress;
- opts.receiver = obj.receiver;
- //调用者传入
- // opts.amount = Moralis.Units.Token(obj.amount, 18);
- opts.amount = obj.amount;
- console.log("NENET_CRYPT_KEY", process.env.NENET_CRYPT_KEY);
- if (obj.privateKey != null) {
- opts.privateKey = obj.privateKey;
- }
- //解密私钥
- var privateKey = utils.decryptPrivityKey(process.env.NENET_CRYPT_KEY, opts.privateKey);
- if (privateKey == null) {
- return toJson(-1, null, "decryptPrivityKey error.");
- }
- try {
- // sending 0.5 DAI tokens with 18 decimals on BSC testnet
- var options = Moralis.TransferOptions = {}
- if (opts.contractAddress) { //如果存在就是代币
- options = Moralis.TransferOptions = {
- type: opts.type,
- amount: opts.amount,
- receiver: opts.receiver, //接收钱包地址
- contractAddress: opts.contractAddress //用户合约地址
- };
- // console.log("options=",options);
- } else { //ETH or BNB
- options = Moralis.TransferOptions = {
- type: opts.type,
- amount: opts.amount,
- receiver: opts.receiver, //接收钱包地址
- };
- // console.log("options else=",options);
- }
- // Enable web3
- await Moralis.enableWeb3({
- //BSC mainnet = 0x38-56 testnet:0x61-97
- chainId: opts.chainId,
- privateKey: opts.privateKey,
- });
- var ret = await Moralis.transfer(options);
- return toJson(SUCCEED_CODE, ret, "");
- } catch (error) {
- console.log('transfer error:', error);
- if (error.reason != null) {
- return toJson(ERROR_CODE_001, null, error.toString());;
- } else {
- return toJson(ERROR_CODE_001, null, error);;
- }
- }
- };
- const getAllTokenWithdrawInfoLists = async (obj) => {
- await initMasterSDK();
- console.log('queryAllTokenBalance:', JSON.stringify(config.token_balance_config.TOKEN_BALANCE));
- return toJson(SUCCEED_CODE, JSON.stringify(config.token_balance_config.TOKEN_BALANCE), null);
- }
- /**
- * 获取代币价格 -> usdPrice
- */
- const getAllTotkenPrice = async (address, chain) => {
- await initMasterSDK();
- const options = {
- address: address,
- chain: chain,
- };
- try {
- console.debug("fun getTotkenPrice in ", options);
- if (Array.isArray(config.token_price_config)) {
- // const arrays = JSON.parse(config.token_price_config);
- return toJson(SUCCEED_CODE, config.token_price_config, null);
- }
- // var ret = await Moralis.Web3API.token.getTokenPrice(options);
- return toJson(ERROR_CODE_001, null, 'getTotkenPrice error.');
- } catch (error) {
- console.error("getTotkenPrice=", error);
- return toJson(ERROR_CODE_001, null, error);;
- }
- }
- //获取交易记录
- //hash 0xe09ba3a4c9f7a8902e01af68d0f1f91906f3f7db1195227e61c45c0e86b2630a
- async function getTokenTransfers(opt) {
- await initMasterSDK();
- console.debug("fun getTokenTransfers in ", opt);
- const options = {};
- options.type = 'all';
- options.chain = 'bsc';
- //todo 对必填参数做检验
- if (opt.chain != null) {
- options.chain = opt.chain;
- }
- if (opt.order != null) {
- options.order = opt.order;
- }
- if (opt.startTime != null) {
- options.from_date = opt.startTime;
- }
- if (opt.endTime != null) {
- options.to_date = opt.endTime;
- }
- if (opt.from_block != null) {
- options.from_block = opt.from_block;
- }
- if (opt.to_block != null) {
- options.to_block = opt.to_block;
- }
- if (opt.transaction_hash) {
- options.transaction_hash = opt.transaction_hash;
- options.type = 'transaction_hash';
- }
- console.debug('getTokenTransfers-->>>', options);
- if (options.type == 'all') {//查询主流币和 20 币所有的交易
- try {
- if (opt.address != null) {
- options.address = opt.address;
- } else {
- return toJson(ERROR_CODE_001, null, "please check address parameter is ok ?");
- }
- //主流币
- var t_1 = await Moralis.Web3API.account.getTransactions(options);
- //20币
- var t_2 = await Moralis.Web3API.account.getTokenTransfers(options);
- let arr = t_1.result;
- let arr1 = t_2.result;
- if (Array.isArray(arr1) && Array.isArray(arr)) {
- let arr2 = arr.concat(arr1);
- t_1.result = arr2;
- }
- return toJson(SUCCEED_CODE, t_1, null);
- } catch (error) {
- console.error("getTransactions error:", error)
- return toJson(ERROR_CODE_001, null, error);;
- }
- } else if (options.type == 'transaction_hash') {//根据哈希查询
- try {
- //native
- const transaction = await Moralis.Web3API.native.getTransaction(options);
- var arr = [];
- if (transaction)
- arr.push(transaction)
- var obj = { result: arr }
- return toJson(SUCCEED_CODE, obj, null);
- } catch (error) {
- console.error("native getTransaction error:", error)
- return toJson(ERROR_CODE_001, null, error);;
- }
- } else {
- return toJson(ERROR_CODE_001, null, "This type is not supported.");;
- }
- }
- module.exports = {
- transfer,
- getTokenTransfers,
- toJson,
- getAllTokenWithdrawInfoLists,
- getAllTotkenPrice,
- }
|