UNPKG

crypto-client

Version:

An unified client for all cryptocurrency exchanges.

195 lines (194 loc) 7.82 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.withdraw = exports.getWithdrawalFees = exports.getDepositAddresses = exports.queryBalance = exports.queryAllBalances = exports.queryOrder = exports.cancelOrder = exports.placeOrder = exports.createOrder = void 0; const assert_1 = require("assert"); const axios_1 = __importDefault(require("axios")); const crypto_pair_1 = require("crypto-pair"); const eos_token_info_1 = require("eos-token-info"); const eos_utils_1 = require("eos-utils"); const https_1 = __importDefault(require("https")); const blockchain_1 = require("../blockchain"); const config_1 = require("../config"); const util_1 = require("../util"); const dfuse_eos_1 = require("../util/dfuse_eos"); const promiseAny = require('promise.any'); function createOrder(market, price, quantity, sell) { assert_1.strict.ok(market); assert_1.strict.ok(config_1.USER_CONFIG.eosAccount); const [priceStr, quantityStr] = util_1.convertPriceAndQuantityToStrings(market, price, quantity, sell); const memo = { type: sell ? 'sell-limit' : 'buy-limit', symbol: market.id, price: priceStr, channel: 'dapp', ref: 'coinrace.com', }; let baseSymbol = market.base; if (baseSymbol === 'MYKEY') { baseSymbol = 'KEY'; } const quoteSymbol = market.quote; assert_1.strict.equal(baseSymbol, market.info.base_symbol.sym.split(',')[1]); assert_1.strict.equal(quoteSymbol, market.info.quote_symbol.sym.split(',')[1]); const quoteQuantity = util_1.numberToString(parseFloat(priceStr) * parseFloat(quantityStr), market.precision.quote, !sell); const action = sell ? dfuse_eos_1.createTransferAction(config_1.USER_CONFIG.eosAccount, 'newdexpublic', baseSymbol, quantityStr, JSON.stringify(memo)) : dfuse_eos_1.createTransferAction(config_1.USER_CONFIG.eosAccount, 'newdexpublic', quoteSymbol, quoteQuantity, JSON.stringify(memo)); return { exchange: 'Newdex', action, }; } exports.createOrder = createOrder; async function placeOrder(market, price, quantity, sell) { try { assert_1.strict.ok(market); assert_1.strict.ok(config_1.USER_CONFIG.eosAccount); assert_1.strict.ok(config_1.USER_CONFIG.eosPrivateKey); const actionExt = createOrder(market, price, quantity, sell); const response = await dfuse_eos_1.sendTransaction([actionExt.action], config_1.USER_CONFIG.eosPrivateKey).catch((e) => { return e; }); if (response instanceof Error) return response; return response.transaction_id; } catch (e) { return e; } } exports.placeOrder = placeOrder; async function cancelOrder(transactionId) { assert_1.strict.ok(transactionId); assert_1.strict.ok(config_1.USER_CONFIG.eosAccount); assert_1.strict.ok(config_1.USER_CONFIG.eosPrivateKey); const orderId = await blockchain_1.Bloks.getOrderId(transactionId); const action = { account: 'newdexpublic', name: 'cancelorder', authorization: [ { actor: config_1.USER_CONFIG.eosAccount, permission: 'active', }, ], data: orderId, }; const response = await dfuse_eos_1.sendTransaction([action], config_1.USER_CONFIG.eosPrivateKey); if (response instanceof Error) return response.message; return true; } exports.cancelOrder = cancelOrder; async function queryOrder(transactionId) { const orderId = await blockchain_1.Bloks.getOrderId(transactionId); let response = await promiseAny(eos_utils_1.EOS_API_ENDPOINTS.map((url) => eos_utils_1.getTableRows({ code: 'newdexpublic', scope: '...........u1', table: 'sellorder', lower_bound: orderId.order_id, upper_bound: orderId.order_id + 1, }, url))); if (response.rows.length === 0) { response = await promiseAny(eos_utils_1.EOS_API_ENDPOINTS.map((url) => eos_utils_1.getTableRows({ code: 'newdexpublic', scope: '...........u1', table: 'buyorder', lower_bound: orderId.order_id, upper_bound: orderId.order_id + 1, }, url))); } assert_1.strict.equal(response.more, false); if (response.rows.length === 0) return undefined; assert_1.strict.equal(response.rows.length, 1); const order = response.rows[0]; assert_1.strict.equal(order.owner, config_1.USER_CONFIG.eosAccount); return order; } exports.queryOrder = queryOrder; async function queryAllBalances() { const agent = new https_1.default.Agent({ rejectUnauthorized: false, }); const response = await axios_1.default.get('https://www.api.bloks.io/account/cryptoforest?type=getAccountTokens&coreSymbol=EOS', { httpsAgent: agent, timeout: 5000, headers: { Accept: 'application/json', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9', Origin: 'https://bloks.io', Referer: `https://bloks.io/account/${config_1.USER_CONFIG.eosAccount}`, 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36', }, }).catch((e) => { return e; }); if (response instanceof Error) return response; assert_1.strict.equal(response.status, 200); const arr = response.data.tokens; const result = {}; arr.forEach((x) => { const symbol = crypto_pair_1.normalizeSymbol(x.currency, 'Newdex'); result[symbol] = x.amount; }); result.EOS = await dfuse_eos_1.getCurrencyBalance(config_1.USER_CONFIG.eosAccount, 'EOS'); return result; } exports.queryAllBalances = queryAllBalances; async function queryBalance(symbol) { return dfuse_eos_1.getCurrencyBalance(config_1.USER_CONFIG.eosAccount, symbol); } exports.queryBalance = queryBalance; function getDepositAddresses(symbols) { const result = {}; symbols.forEach((symbol) => { if (eos_token_info_1.getTokenInfo(symbol === 'MYKEY' ? 'KEY' : symbol) === undefined) return; result[symbol] = {}; result[symbol].EOS = { symbol, platform: 'EOS', address: config_1.USER_CONFIG.eosAccount, }; }); return result; } exports.getDepositAddresses = getDepositAddresses; function getWithdrawalFees(symbols) { const result = {}; symbols.forEach((symbol) => { if (eos_token_info_1.getTokenInfo(symbol === 'MYKEY' ? 'KEY' : symbol) === undefined) return; result[symbol] = {}; result[symbol].EOS = { symbol, platform: 'EOS', fee: 0, min: 0, }; }); return result; } exports.getWithdrawalFees = getWithdrawalFees; async function withdraw(symbol, address, // only supports existing addresses in your withdrawal address list amount, platform, memo) { assert_1.strict.ok(memo); assert_1.strict.equal(platform, 'EOS'); const tokenInfo = eos_token_info_1.getTokenInfo(symbol); if (tokenInfo === undefined) { return new Error(`getTokenInfo(${symbol}) failed`); } const response = await dfuse_eos_1.transfer(config_1.USER_CONFIG.eosAccount, config_1.USER_CONFIG.eosPrivateKey, address, symbol, util_1.numberToString(amount, tokenInfo.decimals, false), memo).catch((e) => { return e; }); if (response instanceof Error) return response; return response.transaction_id || response.id; } exports.withdraw = withdraw;