|
| 1 | +"use strict"; |
| 2 | +var querystring = require('querystring'); |
| 3 | +var verify = require('@you21979/simple-verify'); |
| 4 | +var objectutil = require('@you21979/object-util'); |
| 5 | +var constant = require('./constant'); |
| 6 | +var lp = require('./system').lp; |
| 7 | +var HttpApiError = require('@you21979/http-api-error'); |
| 8 | + |
| 9 | +var createHeader = function(api_key, secret_key, user_agent, postdata){ |
| 10 | + var qstring = querystring.stringify(postdata); |
| 11 | + return { |
| 12 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 13 | + 'Content-Length': qstring.length, |
| 14 | + 'User-Agent': user_agent, |
| 15 | + 'Sign': verify.sign('sha512', secret_key, qstring), |
| 16 | + 'Key': api_key, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +var createPostOption = function(url, api_key, secret_key, user_agent, nonce, timeout, method, params){ |
| 21 | + var post = objectutil.keyMerge({nonce:nonce, method:method}, params); |
| 22 | + return { |
| 23 | + url: url, |
| 24 | + method: 'POST', |
| 25 | + forever: constant.OPT_KEEPALIVE, |
| 26 | + form: post, |
| 27 | + headers: createHeader(api_key, secret_key, user_agent, post), |
| 28 | + timeout: timeout, |
| 29 | + transform2xxOnly : true, |
| 30 | + transform: function(body){ |
| 31 | + return JSON.parse(body) |
| 32 | + }, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +var req = function(params){ |
| 37 | + return lp.req(params) |
| 38 | +} |
| 39 | + |
| 40 | +var FuturesTradeApi = function(api_key, secret_key, user_agent, nonce_func){ |
| 41 | + this.name = "ZAIF-FUTURES"; |
| 42 | + this.url = constant.OPT_TLAPI_URL; |
| 43 | + this.api_key = api_key; |
| 44 | + this.secret_key = secret_key; |
| 45 | + this.user_agent = user_agent; |
| 46 | + this.timeout = Math.floor(constant.OPT_TIMEOUT_SEC * 1000); |
| 47 | + |
| 48 | + var initnonce = new Date() / 1000; |
| 49 | + this.nonce_func = nonce_func || function(){ return initnonce = initnonce + 0.01; } |
| 50 | +} |
| 51 | + |
| 52 | +FuturesTradeApi.prototype.query = function(method, mustparams, options){ |
| 53 | + var params = objectutil.keyMerge(mustparams, options); |
| 54 | + return req(createPostOption(this.url, this.api_key, this.secret_key, this.user_agent, this.nonce_func(), this.timeout, method, params)). |
| 55 | + then(function(v){ |
| 56 | + if(v.success === 1){ |
| 57 | + return v['return']; |
| 58 | + }else{ |
| 59 | + var error_code = (v.error).toUpperCase().replace(' ', '_'); |
| 60 | + throw new HttpApiError(v.error, "API", error_code, v); |
| 61 | + } |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +FuturesTradeApi.prototype.getPositions = function(type, options){ |
| 66 | + return this.query('get_positions', { |
| 67 | + type : type, |
| 68 | + }, options) |
| 69 | +} |
| 70 | + |
| 71 | +FuturesTradeApi.prototype.activePositions = function(type, options){ |
| 72 | + return this.query('active_positions', { |
| 73 | + type : type, |
| 74 | + }, options) |
| 75 | +} |
| 76 | + |
| 77 | +FuturesTradeApi.prototype.positionHistory = function(type, leverage_id, options){ |
| 78 | + return this.query('position_history', { |
| 79 | + type : type, |
| 80 | + leverage_id : leverage_id, |
| 81 | + }, options) |
| 82 | +} |
| 83 | + |
| 84 | +FuturesTradeApi.prototype.createPosition = function(type, currency_pair, action, price, amount, leverage, options){ |
| 85 | + return this.query('create_position', { |
| 86 | + type : type, |
| 87 | + currency_pair : currency_pair.toLowerCase(), |
| 88 | + action : action.toLowerCase(), |
| 89 | + price : price, |
| 90 | + amount : amount, |
| 91 | + leverage : leverage |
| 92 | + }, options) |
| 93 | +} |
| 94 | + |
| 95 | +FuturesTradeApi.prototype.changePosition = function(type, leverage_id, price, options){ |
| 96 | + return this.query('change_position', { |
| 97 | + type : type, |
| 98 | + leverage_id : leverage_id, |
| 99 | + price : price, |
| 100 | + }, options) |
| 101 | +} |
| 102 | + |
| 103 | +FuturesTradeApi.prototype.cancelPosition = function(type, leverage_id, options){ |
| 104 | + return this.query('cancel_position', { |
| 105 | + type : type, |
| 106 | + leverage_id : leverage_id, |
| 107 | + }, options) |
| 108 | +} |
| 109 | + |
| 110 | +var createFuturesPrivateApi = module.exports = function(api_key, secret_key, user_agent, nonce_func){ |
| 111 | + return new FuturesTradeApi(api_key, secret_key, user_agent, nonce_func) |
| 112 | +} |
| 113 | + |
0 commit comments