import Decimal from "decimal.js" ; import * as querystring from 'querystring' ; import * as rp from 'request-promise-native' ; import { AuthenticationProvider } from "./authentication-provider" ; import { IAccount , IAccountBalance , IAllocation , IApiKeyPermissions , IApiKeys , IAssetInsight , IBacktestAsset , IBacktestResult , ICandlestick , IExchangeAsset , IExchangeInfo , IHistoricalCandlestick , IHistoricalCount , IHistoricalOrderBook , IHistoricalInstrument , IHistoricalTrade , ILimitOrder , ILimitOrderStatus , IManagementCredits , IManagementStatus , IManagementUsage , IMarketOrderBooks , IPredictions , IStrategy , ITicker , ITotalBalanceHistoryItem , ITrade , ITradeChanges , ITradingPair , ITrend , IUser , } from "../models" ; import { IAccountBalanceDto , IAccountDto , IAllocationDto , IApiKeyPermissionsDto , IApiKeysDto , IAssetInsightDto , IBacktestResultDto , IBacktestAssetDto , ICandlestickDto , IExchangeAssetDto , IExchangeInfoDto , IGuidIdResultDto , IHistoricalCandlestickDto , IHistoricalCountDto , IHistoricalOrderBookDto , IHistoricalInstrumentDto , IHistoricalTradeDto , ILimitOrderDto , ILimitOrderStatusDto , IMarketOrderBooksDto , INumberIdResultDto , IPredictionsDto , IRebalancePeriodResultDto , IStrategyDto , ITickerDto , ITotalBalanceHistoryItemDto , ITradeChangesDto , ITradeDto , ITradingPairDto , ITrendDto , IUserDto , IWebsocketTokenDto , } from "../dtos" ; import { AccountBalanceDtoConverter , AllocationDtoConverter , AssetInsightDtoConverter , BacktestAssetDtoConverter , BacktestDataPointDtoConverter , CandlestickDtoConverter , DateDtoConverter , DecimalDtoConverter , HistoricalCandlestickDtoConverter , HistoricalOrderBooksDtoConverter , HistoricalInstrumentsDtoConverter , HistoricalTradesDtoConverter , LimitOrderDtoConverter , LimitOrderStatusDtoConverter , MarketOrderBooksDtoConverter , PredictionsDtoConverter , StrategyDtoConverter , TickerDtoConverter , TotalBalanceHistoryItemDtoConverter , TradeChangesDtoConverter , TradeDtoConverter , UserDtoConverter , TrendDtoConverter , } from "../dto-converters" ; export class ShrimpyApiClient { private _accountBalanceDtoConverter = new AccountBalanceDtoConverter ( ) ; private _allocationDtoConverter = new AllocationDtoConverter ( ) ; private _assetInsightDtoConverter = new AssetInsightDtoConverter ( ) ; private _backtestAssetDtoConveter = new BacktestAssetDtoConverter ( ) ; private _backtestDataPointDtoConverter = new BacktestDataPointDtoConverter ( ) ; private _candlestickDtoConverter = new CandlestickDtoConverter ( ) ; private _dateDtoConverter = new DateDtoConverter ( ) ; private _decimalDtoConverter = new DecimalDtoConverter ( ) ; private _historicalCandlestickDtoConverter = new HistoricalCandlestickDtoConverter ( ) ; private _historicalOrderBooksDtoConverter = new HistoricalOrderBooksDtoConverter ( ) ; private _historicalInstrumentsDtoConverter = new HistoricalInstrumentsDtoConverter ( ) ; private _historicalTradesDtoConverter = new HistoricalTradesDtoConverter ( ) ; private _limitOrderDtoConverter = new LimitOrderDtoConverter ( ) ; private _limitOrderStatusDtoConverter = new LimitOrderStatusDtoConverter ( ) ; private _marketOrderBooksDtoConverter = new MarketOrderBooksDtoConverter ( ) ; private _predictionsDtoConverter = new PredictionsDtoConverter ( ) ; private _strategyDtoConverter = new StrategyDtoConverter ( ) ; private _tickerDtoConverter = new TickerDtoConverter ( ) ; private _totalBalanceHistoryItemDtoConverter = new TotalBalanceHistoryItemDtoConverter ( ) ; private _tradeChangesDtoConverter = new TradeChangesDtoConverter ( ) ; private _tradeDtoConverter = new TradeDtoConverter ( ) ; private _trendDtoConverter = new TrendDtoConverter ( ) ; private _userDtoConverter = new UserDtoConverter ( ) ; private _authenticationProvider : AuthenticationProvider | null = null ; constructor ( publicKey ? , privateKey ? ) { if ( publicKey && privateKey ) { this . _setApiCredentials ( publicKey , privateKey ) ; } } public async getSupportedExchanges ( ) { const endpoint = `list_exchanges` ; return await this . _callEndpoint < IExchangeInfoDto [ ] > ( endpoint , 'GET' , null , false ) ; } public async getExchangeAssets ( exchange ) { const endpoint = `exchanges/${ exchange }/assets` ; return await this . _callEndpoint < IExchangeAssetDto [ ] > ( endpoint , 'GET' , null , false ) ; } public async getTradingPairs ( exchange ) { const endpoint = `exchanges/${ exchange }/trading_pairs` ; return await this . _callEndpoint < ITradingPairDto [ ] > ( endpoint , 'GET' , null , false ) ; } public async getTicker ( exchange ) { const endpoint = `exchanges/${ exchange }/ticker` ; const tickerDtos = await this . _callEndpoint < ITickerDto [ ] > ( endpoint , 'GET' , null , false ) ; return tickerDtos . map ( ( tickerDto ) => { return this . _tickerDtoConverter . convertFromDto ( tickerDto ) ; } ) ; } public async getOrderBooks ( exchange , baseSymbol ? , quoteSymbol ? , limit ? ) { const endpoint = `orderbooks` ; let exchangeString ; if ( Array . isArray ( exchange ) ) { exchangeString = exchange . join ( ',' ) ; } else { exchangeString = exchange ; } const parameters = { exchange : exchangeString } ; if ( baseSymbol ) { if ( Array . isArray ( baseSymbol ) ) { parameters . baseSymbol = baseSymbol . join ( ',' ) ; } else { parameters . baseSymbol = baseSymbol ; } } if ( quoteSymbol ) { if ( Array . isArray ( quoteSymbol ) ) { parameters . quoteSymbol = quoteSymbol . join ( ',' ) ; } else { parameters . quoteSymbol = quoteSymbol ; } } if ( limit ) { parameters . limit = limit ; } const orderBooksListDto = await this . _callEndpoint < IMarketOrderBooksDto [ ] > ( endpoint , 'GET' , parameters , false ) ; return orderBooksListDto . map ( ( orderBooksDto ) => { return this . _marketOrderBooksDtoConverter . convertFromDto ( orderBooksDto ) ; } ) ; } public async getCandles ( exchange , baseTradingSymbol , quoteTradingSymbol , interval , startTime ? ) { const endpoint = `exchanges/${ exchange }/candles` ; const parameters = { baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol , interval : interval , } ; if ( startTime ) { parameters . startTime = this . _dateDtoConverter . convertToDto ( startTime ) ; } const candlestickDtos = await this . _callEndpoint < ICandlestickDto [ ] > ( endpoint , 'GET' , parameters , false ) ; const result = candlestickDtos . map ( ( candlestickDto ) => { return this . _candlestickDtoConverter . convertFromDto ( candlestickDto ) ; } ) ; return result ; } public async getUsers ( ) { const endpoint = 'users' ; const userDtos = await this . _callEndpoint < IUserDto [ ] > ( endpoint , 'GET' , null , true ) ; return userDtos . map ( ( userDto ) => { return this . _userDtoConverter . convertFromDto ( userDto ) ; } ) ; } public async getUser ( userId ) { const endpoint = `users/${ userId }` ; const userDto = await this . _callEndpoint < IUserDto > ( endpoint , 'GET' , null , true ) ; return this . _userDtoConverter . convertFromDto ( userDto ) ; } public async createUser ( name ? ) { const endpoint = 'users' ; let parameters = null ; if ( name ) { parameters = { name : name } ; } const result = await this . _callEndpoint < IGuidIdResultDto > ( endpoint , 'POST' , parameters , true ) ; return result . id ; } public async setUserName ( userId , name ) { const endpoint = `users/${ userId }/name` ; const parameters = { name : name } ; await this . _callEndpoint < any > ( endpoint , 'POST' , parameters , true ) ; } public async removeUser ( userId ) { const endpoint = `users/${ userId }` ; await this . _callEndpoint < any > ( endpoint , 'DELETE' , { } , true ) ; } public async enableUser ( userId ) { const endpoint = `users/${ userId }/enable` ; await this . _callEndpoint < any > ( endpoint , 'POST' , null , true ) ; } public async disableUser ( userId ) { const endpoint = `users/${ userId }/disable` ; await this . _callEndpoint < any > ( endpoint , 'POST' , null , true ) ; } public async getApiKeys ( userId ) { const endpoint = `users/${ userId }/keys` ; return await this . _callEndpoint < string [ ] > ( endpoint , 'GET' , null , true ) ; } public async createApiKeys ( userId ) { const endpoint = `users/${ userId }/keys` ; return await this . _callEndpoint < IApiKeysDto > ( endpoint , 'POST' , null , true ) ; } public async deleteApiKeys ( userId , publicKey ) { const endpoint = `users/${ userId }/keys/${ publicKey }` ; return await this . _callEndpoint < any > ( endpoint , 'DELETE' , null , true ) ; } public async getPermissions ( userId , publicKey ) { const endpoint = `users/${ userId }/keys/${ publicKey }/permissions` ; return await this . _callEndpoint < IApiKeyPermissionsDto > ( endpoint , 'GET' , null , true ) ; } public async setPermissions ( userId , publicKey , account , trade ) { const endpoint = `users/${ userId }/keys/${ publicKey }/permissions` ; const parameters = { account : account , trade : trade } ; await this . _callEndpoint < any > ( endpoint , 'POST' , parameters , true ) ; } public async getAccounts ( userId ) { const endpoint = `users/${ userId }/accounts` ; return await this . _callEndpoint < IAccountDto [ ] > ( endpoint , 'GET' , null , true ) ; } public async getAccount ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }` ; return await this . _callEndpoint < IAccountDto > ( endpoint , 'GET' , null , true ) ; } public async createAccount ( userId , exchange , publicKey , privateKey , passphrase ? ) { const endpoint = `users/${ userId }/accounts` ; const parameters = { exchange : exchange , publicKey : publicKey , privateKey : privateKey , passphrase : passphrase , } ; const result = await this . _callEndpoint < INumberIdResultDto > ( endpoint , 'POST' , parameters , true ) ; return result . id ; } public async deleteAccount ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }` ; await this . _callEndpoint < any > ( endpoint , 'DELETE' , null , true ) ; } public async getIpWhitelistAddresses ( userId ) { const endpoint = `users/${ userId }/whitelist` ; return await this . _callEndpoint < string [ ] > ( endpoint , 'GET' , null , true ) ; } public async createTrade ( userId , accountId , fromSymbol , toSymbol , amount , smartRouting ? , maxSpreadPercent ? , maxSlippagePercent ? ) { const endpoint = `users/${ userId }/accounts/${ accountId }/trades` ; let parameters = { fromSymbol : fromSymbol , toSymbol : toSymbol , amount : this . _decimalDtoConverter . convertToDto ( amount ) , } ; if ( smartRouting ) { parameters . smartRouting = smartRouting ; } if ( maxSpreadPercent ) { parameters . maxSpreadPercent = this . _decimalDtoConverter . convertToDto ( maxSpreadPercent ) ; } if ( maxSlippagePercent ) { parameters . maxSlippagePercent = this . _decimalDtoConverter . convertToDto ( maxSlippagePercent ) ; } const result = await this . _callEndpoint < IGuidIdResultDto > ( endpoint , 'POST' , parameters , true ) ; return result . id ; } public async getTrade ( userId , accountId , tradeId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/trades/${ tradeId }` ; const tradeChangesDto = await this . _callEndpoint < ITradeChangesDto > ( endpoint , 'GET' , null , true ) ; return this . _tradeChangesDtoConverter . convertFromDto ( tradeChangesDto ) ; } public async getActiveTrades ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/trades` ; const tradeDtos = await this . _callEndpoint < ITradeDto [ ] > ( endpoint , 'GET' , null , true ) ; return tradeDtos . map ( ( tradeDto ) => { return this . _tradeDtoConverter . convertFromDto ( tradeDto ) ; } ) ; } public async getBalance ( userId , accountId , date ? ) { const endpoint = `users/${ userId }/accounts/${ accountId }/balance` ; let parameters = null ; if ( date ) { parameters = { date : this . _dateDtoConverter . convertToDto ( date ) } ; } const accountBalanceDto = await this . _callEndpoint < IAccountBalanceDto > ( endpoint , 'GET' , parameters , true ) ; return this . _accountBalanceDtoConverter . convertFromDto ( accountBalanceDto ) ; } public async getTotalBalanceHistory ( userId , accountId , startTime , endTime ) { const endpoint = `users/${ userId }/accounts/${ accountId }/total_balance_history` ; let parameters = null ; if ( startTime || endTime ) { parameters = { } ; if ( startTime ) { parameters . startTime = this . _dateDtoConverter . convertToDto ( startTime ) ; } if ( endTime ) { parameters . endTime = this . _dateDtoConverter . convertToDto ( endTime ) ; } } const totalBalanceHistoryDtos = await this . _callEndpoint < ITotalBalanceHistoryItemDto [ ] > ( endpoint , 'GET' , parameters , true ) ; return totalBalanceHistoryDtos . map ( ( totalBalanceHistoryDto ) => { return this . _totalBalanceHistoryItemDtoConverter . convertFromDto ( totalBalanceHistoryDto ) ; } ) ; } public async rebalance ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/rebalance` ; await this . _callEndpoint < any > ( endpoint , 'POST' , null , true ) ; } public async getRebalancePeriod ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/rebalance_period` ; const result = await this . _callEndpoint < IRebalancePeriodResultDto > ( endpoint , 'GET' , null , true ) ; return result . rebalancePeriod ; } public async setRebalancePeriod ( userId , accountId , rebalancePeriodHours ) { if ( ! Number . isInteger ( rebalancePeriodHours ) ) { throw new Error ( "Invalid rebalance period. Rebalance period must be an integer" ) ; } const endpoint = `users/${ userId }/accounts/${ accountId }/rebalance_period` ; const parameters = { rebalancePeriod : rebalancePeriodHours } ; await this . _callEndpoint < any > ( endpoint , 'POST' , parameters , true ) ; } public async getStrategy ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/strategy` ; const strategyDto = await this . _callEndpoint < IStrategyDto > ( endpoint , 'GET' , null , true ) ; return this . _strategyDtoConverter . convertFromDto ( strategyDto ) ; } public async setStrategy ( userId , accountId , strategy ) { const endpoint = `users/${ userId }/accounts/${ accountId }/strategy` ; let parameters = this . _strategyDtoConverter . convertToDto ( strategy ) ; await this . _callEndpoint < any > ( endpoint , 'POST' , parameters , true ) ; } public async clearStrategy ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/strategy` ; await this . _callEndpoint < any > ( endpoint , 'DELETE' , null , true ) ; } public async allocate ( userId , accountId , strategy ) { const endpoint = `users/${ userId }/accounts/${ accountId }/allocate` ; let parameters = this . _strategyDtoConverter . convertToDto ( strategy ) ; await this . _callEndpoint < any > ( endpoint , 'POST' , parameters , true ) ; } public async createOrder ( userId , accountId , baseSymbol , quoteSymbol , quantity , price , side , timeInForce ) { const endpoint = `users/${ userId }/accounts/${ accountId }/orders` ; let parameters = { baseSymbol : baseSymbol , quoteSymbol : quoteSymbol , quantity : this . _decimalDtoConverter . convertToDto ( quantity ) , price : this . _decimalDtoConverter . convertToDto ( price ) , side : side , timeInForce : timeInForce } ; const result = await this . _callEndpoint < IGuidIdResultDto > ( endpoint , 'POST' , parameters , true ) ; return result . id ; } public async getOrder ( userId , accountId , orderId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/orders/${ orderId }` ; const limitOrderStatusDto = await this . _callEndpoint < ILimitOrderStatusDto > ( endpoint , 'GET' , null , true ) ; return this . _limitOrderStatusDtoConverter . convertFromDto ( limitOrderStatusDto ) ; } public async getOrders ( userId , accountId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/orders` ; const limitOrderDtos = await this . _callEndpoint < ILimitOrderDto [ ] > ( endpoint , 'GET' , null , true ) ; return limitOrderDtos . map ( ( limitOrderDto ) => { return this . _limitOrderDtoConverter . convertFromDto ( limitOrderDto ) ; } ) ; } public async cancelOrder ( userId , accountId , orderId ) { const endpoint = `users/${ userId }/accounts/${ accountId }/orders/${ orderId }` ; await this . _callEndpoint < any > ( endpoint , 'DELETE' , null , true ) ; } public async getBacktestAssets ( exchange , startTime ? , endTime ? ) { const endpoint = `analytics/backtest/${ exchange }/assets` ; const parameters = { } ; if ( startTime ) { parameters . startTime = this . _dateDtoConverter . convertToDto ( startTime ) ; } if ( endTime ) { parameters . endTime = this . _dateDtoConverter . convertToDto ( endTime ) ; } const backtestAssetDtos = await this . _callEndpoint < IBacktestAssetDto [ ] > ( endpoint , 'GET' , parameters , true ) ; const result = backtestAssetDtos . map ( ( backtestAssetDto ) => { return this . _backtestAssetDtoConveter . convertFromDto ( backtestAssetDto ) ; } ) ; return result ; } public async runBacktest ( exchange , rebalancePeriodHours , fee , startTime , endTime , initialValue , allocations ) { const endpoint = `analytics/backtest/${ exchange }/run` ; const allocationsDto = allocations . map ( ( allocation ) => { return this . _allocationDtoConverter . convertToDto ( allocation ) ; } ) ; const parameters = { rebalancePeriod : rebalancePeriodHours , fee : fee . toString ( ) , startTime : this . _dateDtoConverter . convertToDto ( startTime ) , endTime : this . _dateDtoConverter . convertToDto ( endTime ) , initialValue : initialValue . toString ( ) , allocations : allocationsDto , } ; const resultDto = await this . _callEndpoint < IBacktestResultDto > ( endpoint , 'POST' , parameters , true ) ; const rebalanceData = resultDto . rebalanceData . map ( ( dataPointDto ) => { return this . _backtestDataPointDtoConverter . convertFromDto ( dataPointDto ) ; } ) ; const holdingData = resultDto . holdingData . map ( ( dataPointDto ) => { return this . _backtestDataPointDtoConverter . convertFromDto ( dataPointDto ) ; } ) ; const result = { rebalanceData : rebalanceData , holdingData : holdingData , } ; return result ; } public async getPredictions ( exchange , baseTradingSymbol , quoteTradingSymbol ) { const endpoint = `analytics/predict` ; const parameters = { exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol } ; const predictionsListDto = await this . _callEndpoint < IPredictionsDto > ( endpoint , 'GET' , parameters , true ) ; return this . _predictionsDtoConverter . convertFromDto ( predictionsListDto ) ; } public async getTrend ( exchange , baseTradingSymbol , quoteTradingSymbol ) { const endpoint = `analytics/trend` ; const parameters = { exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol } ; const trendDto = await this . _callEndpoint < ITrendDto > ( endpoint , 'GET' , parameters , true ) ; return this . _trendDtoConverter . convertFromDto ( trendDto ) ; } public async getAssetDominance ( ) { const endpoint = `insights/asset_dominance` ; const assetInsightDtos = await this . _callEndpoint < IAssetInsightDto [ ] > ( endpoint , 'GET' , null , true ) ; return assetInsightDtos . map ( ( assetInsightDto ) => { return this . _assetInsightDtoConverter . convertFromDto ( assetInsightDto ) ; } ) ; } public async getAssetPopularity ( ) { const endpoint = `insights/asset_popularity` ; const assetInsightDtos = await this . _callEndpoint < IAssetInsightDto [ ] > ( endpoint , 'GET' , null , true ) ; return assetInsightDtos . map ( ( assetInsightDto ) => { return this . _assetInsightDtoConverter . convertFromDto ( assetInsightDto ) ; } ) ; } public async getHistoricalTrades ( exchange , baseTradingSymbol , quoteTradingSymbol , startTime , endTime , limit ) { const endpoint = `historical/trades` ; const parameters = { exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol , startTime : this . _dateDtoConverter . convertToDto ( startTime ) , endTime : this . _dateDtoConverter . convertToDto ( endTime ) , limit : limit , } ; const resultDto = await this . _callEndpoint < IHistoricalTradeDto [ ] > ( endpoint , 'GET' , parameters , true ) ; return this . _historicalTradesDtoConverter . convertFromDto ( resultDto ) ; } public async getHistoricalOrderBooks ( exchange , baseTradingSymbol , quoteTradingSymbol , startTime , endTime , limit ) { const endpoint = `historical/orderbooks` ; const parameters = { exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol , startTime : this . _dateDtoConverter . convertToDto ( startTime ) , endTime : this . _dateDtoConverter . convertToDto ( endTime ) , limit : limit , } ; const resultDto = await this . _callEndpoint < IHistoricalOrderBookDto [ ] > ( endpoint , 'GET' , parameters , true ) ; return this . _historicalOrderBooksDtoConverter . convertFromDto ( resultDto ) ; } public async getHistoricalCandles ( exchange , baseTradingSymbol , quoteTradingSymbol , startTime , endTime , limit , interval ) { const endpoint = `historical/candles` ; const parameters = { exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol , startTime : this . _dateDtoConverter . convertToDto ( startTime ) , endTime : this . _dateDtoConverter . convertToDto ( endTime ) , limit : limit , interval : interval } ; const resultDto = await this . _callEndpoint < IHistoricalCandlestickDto [ ] > ( endpoint , 'GET' , parameters , true ) ; const result = resultDto . map ( ( candlestick ) => { return this . _historicalCandlestickDtoConverter . convertFromDto ( candlestick ) ; } ) ; return result ; } public async getHistoricalInstruments ( exchange ? , baseTradingSymbol ? , quoteTradingSymbol ? ) { const endpoint = `historical/instruments` ; const parameters = { exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol } ; const resultDto = await this . _callEndpoint < IHistoricalInstrumentDto [ ] > ( endpoint , 'GET' , parameters , true ) ; return this . _historicalInstrumentsDtoConverter . convertFromDto ( resultDto ) ; } public async getHistoricalCount ( type , exchange , baseTradingSymbol , quoteTradingSymbol , startTime , endTime ) { const endpoint = `historical/count` ; const parameters = { type : type , exchange : exchange , baseTradingSymbol : baseTradingSymbol , quoteTradingSymbol : quoteTradingSymbol , startTime : this . _dateDtoConverter . convertToDto ( startTime ) , endTime : this . _dateDtoConverter . convertToDto ( endTime ) } ; const countDto = await this . _callEndpoint < IHistoricalCountDto > ( endpoint , 'GET' , parameters , true ) ; const result = { count : countDto . count } ; return result ; } public async getStatus ( ) { const endpoint = `management/status` ; return await this . _callEndpoint < IManagementStatus > ( endpoint , 'GET' , null , true ) ; } public async getCredits ( ) { const endpoint = `management/credits` ; return await this . _callEndpoint < IManagementCredits > ( endpoint , 'GET' , null , true ) ; } public async getUsage ( ) { const endpoint = `management/usage` ; return await this . _callEndpoint < IManagementUsage > ( endpoint , 'GET' , null , true ) ; } public async getToken ( ) { const endpoint = `ws/token` ; const websocketTokenResult = await this . _callEndpoint < IWebsocketTokenDto > ( endpoint , 'GET' , null , true ) ; return websocketTokenResult . token ; } private _setApiCredentials ( publicKey , privateKey ) { this . _authenticationProvider = new AuthenticationProvider ( publicKey , privateKey ) ; } private async _callEndpoint < T > ( endPoint , method , parameters , isSignRequired ) { let requestPath = "/v1/" + endPoint ; let options = { uri : "https://dev-api.shrimpy.io" + requestPath , headers : { 'content-type' : 'application/json' , } , method : method } ; if ( method === 'GET' && parameters && Object . keys ( parameters ) . length > 0 ) { const qs = '?' + querystring . stringify ( parameters ) ; options . uri += qs ; requestPath += qs ; } if ( method === 'POST' ) { if ( parameters ) { options . body = JSON . stringify ( parameters ) ; } else { options . body = "" ; } } if ( isSignRequired && ! this . _authenticationProvider ) { throw new Error ( `Cannot send a request to ${ endPoint } without api keys. Make sure to pass api keys to the ShrimpyApiClient constructor.` ) ; } if ( this . _authenticationProvider ) { const nonce = Date . now ( ) ; const bodyString = options . body ? options . body : "" ; const prehashString = requestPath + method + nonce + bodyString ; const signature = this . _authenticationProvider . sign ( prehashString ) ; options . headers [ 'DEV-SHRIMPY-API-KEY' ] = this . _authenticationProvider . publicKey ; options . headers [ 'DEV-SHRIMPY-API-NONCE' ] = nonce ; options . headers [ 'DEV-SHRIMPY-API-SIGNATURE' ] = signature ; } const response = await rp ( options ) ; return JSON . parse ( response ) ; } }