UNPKG

@bcswaper/apis

Version:

bcswaper apis

1 lines 1.34 kB
import{ClientSettings}from"../../client";import qs from"query-string";import crypto from"crypto";import axios from"axios";import{OkxConstants}from"./okxContext";export class OkxHttpClient{constructor(t,e){this.okxAccInfo=t,e=e||OkxConstants.HTTP_ENDPOINT,this.apiClient=axios.create({baseURL:e})}getSignedHeader(t,e,i=""){const s=this.sign(this.okxAccInfo.secretKey,e,i,t);return{"OK-ACCESS-KEY":this.okxAccInfo.apiKey,"OK-ACCESS-TIMESTAMP":s[0],"OK-ACCESS-SIGN":s[1],"OK-ACCESS-PASSPHRASE":this.okxAccInfo.passphrase}}sign(t,e,i="",s="GET"){const a=crypto.createHmac("sha256",t),n=(new Date).toISOString();return[n,a.update(`${n}${s}${e}${i&&JSON.stringify(i)}`).digest("base64")]}async get(t,e=""){if(e){for(const t of Object.keys(e||{}))null!==e[t]&&void 0!==e[t]||delete e[t];t+="?"+qs.stringify(e)}return(await this.apiClient.get(t,{headers:this.getSignedHeader("GET",t)})).data}async post(t,e){const i={...this.getSignedHeader("POST",t,e),"Content-Type":"application/json"};return(await this.apiClient.post(t,e,{headers:i})).data}async getCandles(t,e){const{data:{data:i}}=await this.apiClient.get("/api/v5/market/candles",{params:{instId:t,bar:e,limit:ClientSettings.ListLenths}});return i}async getInstInfo(t,e){const{data:{data:i}}=await this.apiClient.get("/api/v5/public/instruments",{params:{instType:t,instId:e}});return i}}