Skip to content

Commit

Permalink
Add BarterDEX API client
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Jan 8, 2018
1 parent e84d917 commit 3fa1085
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
52 changes: 52 additions & 0 deletions app/renderer/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Api {
constructor({endpoint, passphrase}) {
this.endpoint = endpoint;
this.passphrase = passphrase;
}

_request(data) {
return fetch(this.endpoint, {
method: 'post',
body: JSON.stringify(data)
}).then(res => res.json());
}

async _userpass() {
const {userpass} = await this._request({
method: 'passphrase',
passphrase: this.passphrase
});

return userpass;
}

async request(data) {
if (!this.userpass) {
this.userpass = await this._userpass();
}

return this._request(Object.assign({}, data, {userpass: this.userpass}));
}

botList() {
return this.request({method: 'bot_list'});
}

enableCoin(coin) {
return this.request({method: 'enable', coin});
}

portfolio() {
return this.request({method: 'portfolio'});
}

coins() {
return this.request({method: 'getcoins'});
}

ticker() {
return this.request({method: 'ticker'});
}
}

export default Api;
15 changes: 15 additions & 0 deletions app/renderer/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import electron from 'electron';
import React from 'react';
import {render} from 'react-dom';
import Api from './api';

require('electron-unhandled')();

Expand Down Expand Up @@ -29,3 +30,17 @@ class App extends React.Component {
}

render(<App/>, document.querySelector('#app'));

// TODO: This is only temporary for testing
// Make sure BarterDEX is running first:
// docker run -e PASSPHRASE="secure passphrase" -p 127.0.0.1:7783:7783 lukechilds/barterdex-api
const PASSPHRASE = 'secure passphrase';
const api = new Api({
endpoint: `http://localhost:7783`,
passphrase: PASSPHRASE
});

(async () => {
console.log('Portfolio:', await api.portfolio());
console.log('Coins:', (await api.coins())[0]);
})();

0 comments on commit 3fa1085

Please sign in to comment.