-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1d96ae
commit 4c92bd2
Showing
5 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,15 @@ | ||
# uexchange-js | ||
Crypton Exchange API wrapper written in Javascript | ||
|
||
![logo](logo.png) | ||
|
||
The lib itself consists of two parts — `core.js` where's located API reference and core helpers; and `<platform-name>.js` where is located platform-depended code. | ||
Usage: | ||
1. Copy `core.js` to your project. | ||
1. Import API (default export) from `node.js`. Example: | ||
```js | ||
import API from '../api/browser.js'; | ||
``` | ||
1. Call methods directly from `API` object. Example: | ||
```js | ||
await API.login("pubkey", "password", "2fa_pin") | ||
``` | ||
That's it. Enjoy :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import CryptonAPI from './core.js'; | ||
|
||
function transport(method, url, body){ | ||
const options = { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
credentials: 'include', | ||
}; | ||
if(body) options.body = body; | ||
return fetch(url, options).then(v => v.text()); | ||
} | ||
|
||
function saveToken(token){ | ||
document.cookie = `auth_token=${token}`; | ||
} | ||
|
||
export default new CryptonAPI(transport, saveToken); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import CryptonAPI from './core.js'; | ||
|
||
function transport(method, url, body){ | ||
const options = { | ||
method, | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
credentials: 'include', | ||
}; | ||
if(body) options.body = body; | ||
return fetch('https://cors.io/?' + url, options).then(v => v.text()); | ||
} | ||
|
||
function saveToken(token){ | ||
document.cookie = `auth_token=${token}`; | ||
} | ||
|
||
export default new CryptonAPI(transport, saveToken); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const baseUrl = 'https://crp.is:8182/'; | ||
|
||
function body(obj){ | ||
let res = ''; | ||
for(const p in obj) if(obj[p]) res += `${encodeURIComponent(p)}=${encodeURIComponent(obj[p])}&`; | ||
return res.slice(0, -1); | ||
} | ||
|
||
function argsInUrl(method){ | ||
return method === 'GET'; | ||
} | ||
|
||
export default class CryptonAPI{ | ||
#transport | ||
#saveToken | ||
|
||
constructor(transport, saveToken){ | ||
this.#transport = transport; | ||
this.#saveToken = saveToken; | ||
} | ||
|
||
async #call(method, httpMethod, args){ | ||
const urlArgs = argsInUrl(method); | ||
const data = args ? body(args) : ''; | ||
let url = `${baseUrl}${method}`; | ||
if(urlArgs) url += `?${data}`; | ||
const parsed = JSON.parse(await this.#transport(httpMethod, url, urlArgs ? null : data)); | ||
if(parsed.success){ | ||
return parsed.result; | ||
} else { | ||
throw new Error(`${parsed.code}: ${parsed.text}`) | ||
} | ||
} | ||
|
||
async login(pubkey, password, pin){ | ||
const { auth_token, user_session: { session, user } } = await this.#call('user/login', 'POST', { | ||
PublicKey: pubkey, | ||
password, | ||
'2fa_pin': pin, | ||
}); | ||
this.session = session; | ||
this.user = user; | ||
await this.#saveToken(auth_token); | ||
} | ||
|
||
async logout(){ | ||
await this.#call('user/logout', 'POST'); | ||
} | ||
|
||
balance(){ | ||
return this.#call('user/balance', 'GET'); | ||
} | ||
|
||
buy(pair, amount, price){ | ||
return this.#call('market/buy', 'POST', { pair, amount, price }); | ||
} | ||
|
||
sell(pair, amount, price){ | ||
return this.#call('market/sell', 'POST', { pair, amount, price }); | ||
} | ||
|
||
hold(orderId){ | ||
return this.#call('market/hold', 'POST', { order_id: orderId }); | ||
} | ||
|
||
hold(orderId){ | ||
return this.#call('market/cancel', 'POST', { order_id: orderId }); | ||
} | ||
|
||
pairs(){ | ||
return this.#call('market/pairs', 'GET'); | ||
} | ||
|
||
panel(pair){ | ||
return this.#call('market/panel', 'POST', { pair }); | ||
} | ||
|
||
curlist(){ | ||
return this.#call('market/curlist', 'GET'); | ||
} | ||
|
||
orders(status, task){ | ||
return this.#call('orders', 'GET', { status, task }); | ||
} | ||
|
||
orderHistory(orderId){ | ||
return this.#call('orders/history', 'POST', { order_id: orderId }); | ||
} | ||
|
||
history(type){ | ||
return this.#call('history', 'POST', { type }); | ||
} | ||
|
||
tradeHistory(orderId){ | ||
return this.#call('history/trade', 'GET', { order_id: orderId }); | ||
} | ||
} |