Skip to content

Commit

Permalink
upload code
Browse files Browse the repository at this point in the history
  • Loading branch information
UtopistMan committed Feb 2, 2022
1 parent f1d96ae commit 4c92bd2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
17 changes: 15 additions & 2 deletions README.md
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 :)
19 changes: 19 additions & 0 deletions browser.js
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);
19 changes: 19 additions & 0 deletions browser_cors_io.js
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);
97 changes: 97 additions & 0 deletions core.js
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 });
}
}
Binary file added logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4c92bd2

Please sign in to comment.