-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from cosmos/matt/301-lcd-client
Move LCD client into this repo
- Loading branch information
Showing
5 changed files
with
232 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict' | ||
|
||
const axios = require('axios') | ||
|
||
// returns an async function which makes a request for the given | ||
// HTTP method (GET/POST/DELETE/etc) and path (/foo/bar) | ||
function req (method, path) { | ||
return async function (data) { | ||
return await this.request(method, path, data) | ||
} | ||
} | ||
|
||
// returns an async function which makes a request for the given | ||
// HTTP method and path, which accepts arguments to be appended | ||
// to the path (/foo/{arg}/...) | ||
function argReq (method, path) { | ||
return async function (args, data) { | ||
// `args` can either be a single value or an array | ||
if (Array.isArray(args)) { | ||
args = args.join('/') | ||
} | ||
return await this.request(method, `${path}/${args}`, data) | ||
} | ||
} | ||
|
||
class Client { | ||
constructor (server = 'http://localhost:8998') { | ||
this.server = server | ||
} | ||
|
||
async request (method, path, data) { | ||
try { | ||
let res = await axios[method.toLowerCase()](this.server + path, data) | ||
return res.data | ||
} catch (resError) { | ||
if (!resError.response || !resError.response.data) { | ||
throw resError | ||
} | ||
let data = resError.response.data | ||
// server responded with error message, create an Error from that | ||
let error = Error(data.error) | ||
error.code = data.code | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
let fetchAccount = argReq('GET', '/query/account') | ||
let fetchNonce = argReq('GET', '/query/nonce') | ||
|
||
Object.assign(Client.prototype, { | ||
sign: req('POST', '/sign'), | ||
postTx: req('POST', '/tx'), | ||
|
||
// keys | ||
generateKey: req('POST', '/keys'), | ||
listKeys: req('GET', '/keys'), | ||
getKey: argReq('GET', '/keys'), | ||
updateKey: argReq('PUT', '/keys'), | ||
deleteKey: argReq('DELETE', '/keys'), | ||
recoverKey: req('POST', '/keys/recover'), | ||
|
||
// coins | ||
buildSend: req('POST', '/build/send'), | ||
async queryAccount (address) { | ||
try { | ||
return await fetchAccount.call(this, address) | ||
} catch (err) { | ||
// if account not found, return null instead of throwing | ||
if (err.message.includes('account bytes are empty')) { | ||
return null | ||
} | ||
throw err | ||
} | ||
}, | ||
coinTxs: argReq('GET', '/tx/coin'), | ||
|
||
// nonce | ||
async queryNonce (address) { | ||
try { | ||
return await fetchNonce.call(this, address) | ||
} catch (err) { | ||
// if nonce not found, return 0 instead of throwing | ||
if (err.message.includes('nonce empty')) { | ||
return 0 | ||
} | ||
throw err | ||
} | ||
}, | ||
|
||
// Tendermint RPC | ||
status: req('GET', '/tendermint/status'), | ||
|
||
// staking | ||
candidate: argReq('GET', '/query/stake/candidates'), | ||
candidates: req('GET', '/query/stake/candidates'), | ||
buildDelegate: req('POST', '/build/stake/delegate'), | ||
buildUnbond: req('POST', '/build/stake/unbond'), | ||
bondingsByDelegator: argReq('GET', '/tx/bondings/delegator'), | ||
bondingsByValidator: argReq('GET', '/tx/bondings/validator') | ||
|
||
// TODO: separate API registration for different modules | ||
}) | ||
|
||
module.exports = Client |
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
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
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,125 @@ | ||
let axios = require('axios') | ||
let LcdClient = require('../../../app/src/renderer/lcdClient.js') | ||
|
||
describe('LCD Client', () => { | ||
let client = new LcdClient() | ||
|
||
it('makes a GET request with no args', async () => { | ||
axios.get = jest.fn() | ||
.mockReturnValueOnce(Promise.resolve({ | ||
data: { foo: 'bar' } | ||
})) | ||
|
||
let res = await client.status() | ||
expect(res).toEqual({ foo: 'bar' }) | ||
expect(axios.get.mock.calls[0]).toEqual([ | ||
'http://localhost:8998/tendermint/status', | ||
undefined | ||
]) | ||
}) | ||
|
||
it('makes a GET request with one arg', async () => { | ||
axios.get = jest.fn() | ||
.mockReturnValueOnce(Promise.resolve({ | ||
data: { foo: 'bar' } | ||
})) | ||
|
||
let res = await client.getKey('myKey') | ||
expect(res).toEqual({ foo: 'bar' }) | ||
expect(axios.get.mock.calls[0]).toEqual([ | ||
'http://localhost:8998/keys/myKey', | ||
undefined | ||
]) | ||
}) | ||
|
||
it('makes a GET request with multiple args', async () => { | ||
axios.get = jest.fn() | ||
.mockReturnValueOnce(Promise.resolve({ | ||
data: { foo: 'bar' } | ||
})) | ||
|
||
let res = await client.bondingsByDelegator([ 'foo', 'bar' ]) | ||
expect(res).toEqual({ foo: 'bar' }) | ||
expect(axios.get.mock.calls[0]).toEqual([ | ||
'http://localhost:8998/tx/bondings/delegator/foo/bar', | ||
undefined | ||
]) | ||
}) | ||
|
||
it('makes a POST request', async () => { | ||
axios.post = jest.fn() | ||
.mockReturnValueOnce(Promise.resolve({ | ||
data: { foo: 'bar' } | ||
})) | ||
|
||
let res = await client.generateKey() | ||
expect(res).toEqual({ foo: 'bar' }) | ||
expect(axios.post.mock.calls[0]).toEqual([ | ||
'http://localhost:8998/keys', | ||
undefined | ||
]) | ||
}) | ||
|
||
it('makes a POST request with args and data', async () => { | ||
axios.put = jest.fn() | ||
.mockReturnValueOnce(Promise.resolve({ | ||
data: { foo: 'bar' } | ||
})) | ||
|
||
let res = await client.updateKey('myKey', { abc: 123 }) | ||
expect(res).toEqual({ foo: 'bar' }) | ||
expect(axios.put.mock.calls[0]).toEqual([ | ||
'http://localhost:8998/keys/myKey', | ||
{ abc: 123 } | ||
]) | ||
}) | ||
|
||
it('makes a GET request with an error', async () => { | ||
axios.get = jest.fn() | ||
.mockReturnValueOnce(Promise.reject({ | ||
response: { | ||
data: { | ||
error: 'foo', | ||
code: 123 | ||
} | ||
} | ||
})) | ||
|
||
try { | ||
await client.status() | ||
} catch (err) { | ||
expect(err.message).toBe('foo') | ||
expect(err.code).toBe(123) | ||
} | ||
expect(axios.get.mock.calls[0]).toEqual([ | ||
'http://localhost:8998/tendermint/status', | ||
undefined | ||
]) | ||
}) | ||
|
||
it('does not throw error for empty results', async () => { | ||
axios.get = jest.fn() | ||
.mockReturnValueOnce(Promise.reject({ | ||
response: { | ||
data: { | ||
error: 'account bytes are empty', | ||
code: 1 | ||
} | ||
} | ||
})) | ||
let res = await client.queryAccount('address') | ||
expect(res).toBe(null) | ||
|
||
axios.get = jest.fn() | ||
.mockReturnValueOnce(Promise.reject({ | ||
response: { | ||
data: { | ||
error: 'nonce empty', | ||
code: 2 | ||
} | ||
} | ||
})) | ||
res = await client.queryNonce('address') | ||
expect(res).toBe(0) | ||
}) | ||
}) |
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