Comparing version 0.1.4 to 0.2.0
# Change Log | ||
## v0.2.0 / 2018-10-08 | ||
### Added | ||
- gas advance feature. | ||
- disconnect method. | ||
### Fixed | ||
- duplicate results. | ||
## v0.1.4 / 2018-10-01 | ||
@@ -4,0 +12,0 @@ ### Fixed |
{ | ||
"name": "joyso", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "JOYSO API client library for trading.", | ||
@@ -5,0 +5,0 @@ "main": "src/joyso.js", |
@@ -309,2 +309,5 @@ # JOYSO | ||
### disconnect() | ||
Disconnect | ||
### subscription.unsubscribe() | ||
@@ -311,0 +314,0 @@ Unsubscribe |
@@ -102,3 +102,3 @@ const WebSocket = require('ws'); | ||
this.stopPolling(); | ||
document.removeEventListener("visibilitychange", this.visibilityDidChange); | ||
// document.removeEventListener("visibilitychange", this.visibilityDidChange); | ||
return ActionCable.log("ConnectionMonitor stopped"); | ||
@@ -105,0 +105,0 @@ } |
@@ -10,19 +10,25 @@ const rp = require('request-promise'); | ||
this.balances = {}; | ||
this.init = true; | ||
} | ||
subscribe() { | ||
this.cable = this.client.cable.subscriptions.create({ | ||
channel: 'BalancesChannel', | ||
contract: this.client.system.contract.substr(2), | ||
user: this.address.substr(2) | ||
}, { | ||
connected: async () => { | ||
const json = await this.get(); | ||
json.balances.forEach(balance => this.updateBalance(balance)); | ||
this.onReceived(this.balances); | ||
}, | ||
received: balances => { | ||
balances.forEach(balance => this.updateBalance(balance)); | ||
this.onReceived(this.balances); | ||
} | ||
return new Promise(resolve => { | ||
this.cable = this.client.cable.subscriptions.create({ | ||
channel: 'BalancesChannel', | ||
contract: this.client.system.contract.substr(2), | ||
user: this.address.substr(2) | ||
}, { | ||
connected: async () => { | ||
await this.get(); | ||
this.onReceived(this.balances); | ||
if (this.init) { | ||
this.init = false; | ||
resolve(); | ||
} | ||
}, | ||
received: balances => { | ||
balances.forEach(balance => this.updateBalance(balance)); | ||
this.onReceived(this.balances); | ||
} | ||
}); | ||
}); | ||
@@ -32,5 +38,3 @@ } | ||
unsubscribe() { | ||
this.cable.unsubscribe(); | ||
delete this.cable; | ||
this.balances = {}; | ||
this.onReceived = (() => {}); | ||
} | ||
@@ -48,4 +52,4 @@ | ||
get() { | ||
return rp(this.client.createRequest('balances', { | ||
async get() { | ||
const json = await rp(this.client.createRequest('balances', { | ||
qs: { | ||
@@ -56,2 +60,3 @@ contract: this.client.system.contract.substr(2), | ||
})); | ||
json.balances.forEach(balance => this.updateBalance(balance)); | ||
} | ||
@@ -58,0 +63,0 @@ } |
@@ -14,2 +14,4 @@ const rp = require('request-promise'); | ||
this.funds = []; | ||
this.requesting = 0; | ||
this.requestId = 0; | ||
} | ||
@@ -35,2 +37,3 @@ | ||
case 'new': | ||
++this.requestId; | ||
this.update(); | ||
@@ -62,2 +65,6 @@ break; | ||
async update() { | ||
if (this.requesting) { | ||
return; | ||
} | ||
this.requesting = this.requestId; | ||
const after = this.funds.length ? this.funds[0].id : null; | ||
@@ -72,2 +79,8 @@ const json = await this.get(after); | ||
this.onReceived(this.funds); | ||
if (this.requesting !== this.requestId) { | ||
this.requesting = 0; | ||
this.update(); | ||
} else { | ||
this.requesting = 0; | ||
} | ||
} | ||
@@ -74,0 +87,0 @@ |
@@ -14,4 +14,5 @@ const rp = require('request-promise'); | ||
const Funds = require('./funds'); | ||
const Account = require('./account'); | ||
BigNumber.config({ DECIMAL_PLACES: 36 }); | ||
BigNumber.config({ DECIMAL_PLACES: 36, ROUNDING_MODE: 1 }); | ||
@@ -47,2 +48,6 @@ const keys = {}; | ||
this.tokenManager.subscribe(); | ||
this.account = new Account(this, this.address); | ||
await this.account.subscribe(); | ||
this.balances = new Balances({ client: this, address: this.address }); | ||
await this.balances.subscribe(); | ||
await this.updateAccessToken(); | ||
@@ -64,2 +69,7 @@ this.orders = new Orders({ | ||
async disconnect() { | ||
this.cable.disconnect(); | ||
this.connected = false; | ||
} | ||
async updateAccessToken() { | ||
@@ -101,2 +111,11 @@ const nonce = Math.floor(Date.now() / 1000); | ||
repayWithdrawFee(token) { | ||
if (this.account.advanceReal !== 0 && this.account.advanceInOrder === 0) { | ||
const ratio = this.tokenManager.eth.withdrawFee.div(token.withdrawFee); | ||
return new BigNumber(this.account.advanceReal).div(ratio).truncated().add(token.withdrawFee); | ||
} else { | ||
return token.withdrawFee; | ||
} | ||
} | ||
createWithdraw({ token, amount, fee }) { | ||
@@ -119,3 +138,3 @@ this.validateWithdraw(amount, fee); | ||
} | ||
const withdrawFee = tokenFee.withdrawFee; | ||
const withdrawFee = this.repayWithdrawFee(tokenFee); | ||
let rawAmount = this.tokenManager.toRawAmount(token, amount); | ||
@@ -222,2 +241,24 @@ if (token === tokenFee) { | ||
repayGasFee(token) { | ||
const ratio = new BigNumber(this.tokenManager.eth.gasFee).div(token.gasFee); | ||
return new BigNumber(this.account.advanceReal).div(ratio).truncated().add(token.gasFee); | ||
} | ||
receivableGasFee(side, paymentMethod, token) { | ||
let ethBalance, gasFee; | ||
ethBalance = this.tokenManager.toRawAmount(this.tokenManager.eth, this.balances.balances.ETH.available || 0); | ||
gasFee = this.tokenManager.eth.gasFee; | ||
if ( | ||
side !== 'buy' && paymentMethod === 'base' && gasFee.gt(ethBalance) | ||
&& this.account.advanceReal === 0 && this.account.advanceInOrder === 0 | ||
) { | ||
return new BigNumber(0); | ||
} else if (this.account.advanceReal !== 0 && this.account.advanceInOrder === 0) { | ||
return this.repayGasFee(token); | ||
} else { | ||
return token.gasFee; | ||
} | ||
} | ||
createOrder({ pair, price, amount, fee, side }) { | ||
@@ -268,3 +309,3 @@ this.validateOrder(price, amount, fee, side); | ||
} | ||
const gasFee = tokenFee.gasFee; | ||
const gasFee = this.receivableGasFee(side, fee, tokenFee); | ||
@@ -404,11 +445,4 @@ let amountSell, amountBuy, tokenSell, tokenBuy; | ||
} | ||
if (this.balances) { | ||
this.balances.unsubscribe(); | ||
} | ||
this.balances = new Balances({ | ||
client: this, | ||
address: this.address, | ||
onReceived: callback | ||
}); | ||
this.balances.subscribe(); | ||
this.balances.onReceived = callback; | ||
this.balances.onReceived(this.balances.balances); | ||
return this.balances; | ||
@@ -415,0 +449,0 @@ } |
@@ -13,2 +13,4 @@ const rp = require('request-promise'); | ||
this.trades = []; | ||
this.requesting = 0; | ||
this.requestId = 0; | ||
} | ||
@@ -33,2 +35,3 @@ | ||
case 'new': | ||
++this.requestId; | ||
return this.update(); | ||
@@ -59,2 +62,6 @@ case 'update': | ||
async update() { | ||
if (this.requesting) { | ||
return; | ||
} | ||
this.requesting = this.requestId; | ||
const after = this.trades.length ? this.trades[0].id : null; | ||
@@ -69,2 +76,8 @@ const json = await this.get(after); | ||
this.onReceived(this.trades); | ||
if (this.requesting !== this.requestId) { | ||
this.requesting = 0; | ||
this.update(); | ||
} else { | ||
this.requesting = 0; | ||
} | ||
} | ||
@@ -71,0 +84,0 @@ |
@@ -12,2 +12,4 @@ const rp = require('request-promise'); | ||
this.orders = []; | ||
this.requesting = 0; | ||
this.requestId = 0; | ||
} | ||
@@ -28,2 +30,3 @@ | ||
case 'new': | ||
++this.requestId; | ||
return this.update(); | ||
@@ -57,2 +60,6 @@ case 'update': | ||
async update() { | ||
if (this.requesting) { | ||
return; | ||
} | ||
this.requesting = this.requestId; | ||
const after = this.orders.length ? this.orders[0].id : null; | ||
@@ -67,2 +74,8 @@ const result = await this.get(after); | ||
this.onReceived(this.orders); | ||
if (this.requesting !== this.requestId) { | ||
this.requesting = 0; | ||
this.update(); | ||
} else { | ||
this.requesting = 0; | ||
} | ||
} | ||
@@ -69,0 +82,0 @@ |
@@ -12,2 +12,4 @@ const rp = require('request-promise'); | ||
this.trades = []; | ||
this.requesting = 0; | ||
this.requestId = 0; | ||
} | ||
@@ -23,3 +25,6 @@ | ||
connected: () => this.update(), | ||
received: () => this.update() | ||
received: () => { | ||
++this.requestId; | ||
this.update(); | ||
} | ||
}); | ||
@@ -35,2 +40,6 @@ } | ||
async update() { | ||
if (this.requesting) { | ||
return; | ||
} | ||
this.requesting = this.requestId; | ||
const after = this.trades.length ? this.trades[0].id : null; | ||
@@ -45,2 +54,8 @@ const json = await this.get(after); | ||
this.onReceived(this.trades); | ||
if (this.requesting !== this.requestId) { | ||
this.requesting = 0; | ||
this.update(); | ||
} else { | ||
this.requesting = 0; | ||
} | ||
} | ||
@@ -47,0 +62,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67022
17
1773
324