Skip to content

Commit

Permalink
Merge pull request ccxt#12293 from samgermain/gateio-funding
Browse files Browse the repository at this point in the history
gateio fetchFundingHistory fixes
  • Loading branch information
kroitor authored Mar 11, 2022
2 parents ea0ee2e + a7531bf commit de635cb
Showing 1 changed file with 71 additions and 27 deletions.
98 changes: 71 additions & 27 deletions js/gateio.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,17 @@ module.exports = class gateio extends Exchange {
}

prepareRequest (market) {
if (market['contract']) {
return {
'contract': market['id'],
'settle': market['settleId'],
};
} else {
return {
'currency_pair': market['id'],
};
if (market !== undefined) {
if (market['contract']) {
return {
'contract': market['id'],
'settle': market['settleId'],
};
} else {
return {
'currency_pair': market['id'],
};
}
}
}

Expand Down Expand Up @@ -1312,41 +1314,83 @@ module.exports = class gateio extends Exchange {
}

async fetchFundingHistory (symbol = undefined, since = undefined, limit = undefined, params = {}) {
if (symbol === undefined) {
throw new ArgumentsRequired (this.id + ' fetchFundingHistory() requires a symbol argument');
}
await this.loadMarkets ();
// let defaultType = 'future';
const market = this.market (symbol);
const request = this.prepareRequest (market);
let market = undefined;
let request = {};
if (symbol !== undefined) {
market = this.market (symbol);
symbol = market['symbol'];
request = this.prepareRequest (market);
}
let type = undefined;
[ type, params ] = this.handleMarketTypeAndParams ('fetchFundingHistory', market, params);
if (market === undefined) {
const defaultSettle = (type === 'swap') ? 'usdt' : 'btc';
const settle = this.safeString (params, 'settle', defaultSettle);
request['settle'] = settle;
params = this.omit (params, 'settle');
}
request['type'] = 'fund'; // 'dnw' 'pnl' 'fee' 'refr' 'fund' 'point_dnw' 'point_fee' 'point_refr'
if (since !== undefined) {
request['from'] = since;
request['from'] = since / 1000;
}
if (limit !== undefined) {
request['limit'] = limit;
}
const method = this.getSupportedMapping (market['type'], {
const method = this.getSupportedMapping (type, {
'swap': 'privateFuturesGetSettleAccountBook',
'future': 'privateDeliveryGetSettleAccountBook',
});
const response = await this[method] (this.extend (request, params));
//
// [
// {
// "time": 1646899200,
// "change": "-0.027722",
// "balance": "11.653120591841",
// "text": "XRP_USDT",
// "type": "fund"
// },
// ...
// ]
//
return this.parseFundingHistories (response, symbol, since, limit);
}

parseFundingHistories (response, symbol, since, limit) {
const result = [];
for (let i = 0; i < response.length; i++) {
const entry = response[i];
const timestamp = this.safeTimestamp (entry, 'time');
result.push ({
'info': entry,
'symbol': symbol,
'code': this.safeCurrencyCode (this.safeString (entry, 'text')),
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'id': undefined,
'amount': this.safeNumber (entry, 'change'),
});
const funding = this.parseFundingHistory (entry);
result.push (funding);
}
const sorted = this.sortBy (result, 'timestamp');
return this.filterBySymbolSinceLimit (sorted, market['symbol'], since, limit);
return this.filterBySymbolSinceLimit (sorted, symbol, since, limit);
}

parseFundingHistory (info, market = undefined) {
//
// {
// "time": 1646899200,
// "change": "-0.027722",
// "balance": "11.653120591841",
// "text": "XRP_USDT",
// "type": "fund"
// }
//
const timestamp = this.safeTimestamp (info, 'time');
const marketId = this.safeString (info, 'text');
market = this.safeMarket (marketId, market);
return {
'info': info,
'symbol': this.safeString (market, 'symbol'),
'code': this.safeString (market, 'settle'),
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'id': undefined,
'amount': this.safeNumber (info, 'change'),
};
}

async fetchOrderBook (symbol, limit = undefined, params = {}) {
Expand Down

0 comments on commit de635cb

Please sign in to comment.