This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into handle-missing-requests
- Loading branch information
Showing
9 changed files
with
174 additions
and
4 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
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,50 @@ | ||
import axios from 'axios' | ||
import { Client } from '../src/index' | ||
import { Address } from '../src/wallet' | ||
|
||
jest.mock('axios') | ||
const mockedAxios = axios as jest.Mocked<typeof axios> | ||
|
||
const TEST_RPC = 'https://api-mock.bandprotocol.com/rest' | ||
|
||
const client = new Client(TEST_RPC) | ||
|
||
describe('Client get data', () => { | ||
it('get chain ID', () => { | ||
const resp = { data: { chain_id: 'bandchain' } } | ||
mockedAxios.get.mockResolvedValue(resp) | ||
|
||
const response = client.getChainID() | ||
response.then((e) => expect(e).toEqual('bandchain')) | ||
}) | ||
|
||
it('get data source by ID', () => { | ||
const resp = { | ||
data: { | ||
height: '651093', | ||
result: { | ||
owner: 'band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs', | ||
name: 'CoinGecko Cryptocurrency Price', | ||
description: | ||
'Retrieves current price of a cryptocurrency from https://www.coingecko.com', | ||
filename: | ||
'c56de9061a78ac96748c83e8a22330accf6ee8ebb499c8525613149a70ec49d0', | ||
}, | ||
}, | ||
} | ||
mockedAxios.get.mockResolvedValue(resp) | ||
|
||
const expected = { | ||
owner: Address.fromAccBech32( | ||
'band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs', | ||
), | ||
name: 'CoinGecko Cryptocurrency Price', | ||
description: | ||
'Retrieves current price of a cryptocurrency from https://www.coingecko.com', | ||
fileName: | ||
'c56de9061a78ac96748c83e8a22330accf6ee8ebb499c8525613149a70ec49d0', | ||
} | ||
const response = client.getDataSource(1) | ||
response.then((e) => expect(e).toEqual(expected)) | ||
}) | ||
}) |
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
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,58 @@ | ||
import axios from 'axios' | ||
import { Address } from './wallet' | ||
import { DataSource } from './data' | ||
|
||
export default class Client { | ||
rpcUrl: string | ||
constructor(rpcUrl: string) { | ||
this.rpcUrl = rpcUrl | ||
} | ||
|
||
private async get(path: string, param?: object) { | ||
const response = await axios.get(`${this.rpcUrl}${path}`, param) | ||
return response.data | ||
} | ||
|
||
private async getResult(path: string, param?: object) { | ||
const response = await this.get(`${path}`, param) | ||
return response.result | ||
} | ||
|
||
async getChainID(): Promise<string> { | ||
const response = await this.get('/bandchain/chain_id') | ||
return response.chain_id | ||
} | ||
|
||
/** | ||
* Get the data source by ID | ||
* @param id Data source ID | ||
* @returns A Promise of DataSoruce. | ||
*/ | ||
|
||
async getDataSource(id: number): Promise<DataSource> { | ||
const response = await this.getResult(`/oracle/data_sources/${id}`) | ||
return { | ||
owner: Address.fromAccBech32(response.owner), | ||
name: response.name, | ||
description: response.description, | ||
fileName: response.filename, | ||
} | ||
} | ||
|
||
async getLatestRequest( | ||
oid: number, | ||
calldata: string, | ||
minCount: number, | ||
askCount: number, | ||
) { | ||
const response = await this.getResult(`/oracle/request_search`, { | ||
params: { | ||
oid: oid, | ||
calldata: calldata, | ||
min_count: minCount, | ||
ask_count: askCount, | ||
}, | ||
}) | ||
return response | ||
} | ||
} |
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