Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Commit

Permalink
Merge branch 'master' into handle-missing-requests
Browse files Browse the repository at this point in the history
  • Loading branch information
taobun authored Nov 18, 2020
2 parents 6425222 + 8ec08a9 commit 943e76a
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@

### Helpers

- (impv) [\#2862](https://github.com/bandprotocol/bandchain/pull/2862) bandchain.js: Implement `getSignData`, `getTxData` and all `with_*` methods on Transaction module
- (feat) [\#2889](https://github.com/bandprotocol/bandchain/pull/2889) Bandchain.js: Started to create the client module with mock request test
- (feat) [\#2872](https://github.com/bandprotocol/bandchain/pull/2872) Bandchain.js: Added new Address class and added more fn on PublicKey class
- (feat) [\#2865](https://github.com/bandprotocol/bandchain/pull/2865) bandchain.js: Add Private Key and verify on Public Key on Wallet
- (impv) [\#2863](https://github.com/bandprotocol/bandchain/pull/2863) bandchain.js: Add Github action
- (impv) [\#2862](https://github.com/bandprotocol/bandchain/pull/2862) bandchain.js: Implement `getSignData`, `getTxData` and all `with_*` methods on Transaction module
- (feat) [\#2855](https://github.com/bandprotocol/bandchain/pull/2855) pyband: Implemented with_auto fn on tx module
- (impv) [\#2835](https://github.com/bandprotocol/bandchain/pull/2835) pyband: Add msg delegate
- (impv) [\#2830](https://github.com/bandprotocol/bandchain/pull/2830) pyband: Add msg send
Expand Down
50 changes: 50 additions & 0 deletions helpers/bandchain2.js/__tests__/client_test.test.ts
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))
})
})
28 changes: 26 additions & 2 deletions helpers/bandchain2.js/example.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, Data } from './src/index'
import { Message, Data, Client } from './src/index'
import { Address, PrivateKey } from './src/wallet'

const { MsgSend } = Message
Expand All @@ -16,6 +16,30 @@ const msgSend = new MsgSend(from_addr, to_addr, amount)
let result = msgSend.asJson()
console.log(JSON.stringify(result))

// console.log(PrivateKey.generate())
const client = new Client('https://poa-api.bandchain.org')

console.log(PrivateKey.generate())
const x = PrivateKey.fromMnemonic('s')
console.log(x.toHex())

console.log('---------------------------------------')

client
.getChainID()
.then((e) => console.log('chain ID: ', e))
.catch((err) => console.log(err.response.data.error))

client
.getDataSource(3)
.then((e) => console.log('data source: ', e))
.catch((err) => console.log(err))

client
.getLatestRequest(
8,
'000000190000000652454e4254430000000457425443000000034449410000000342544d00000004494f545800000003464554000000034a5354000000034d434f000000034b4d440000000342545300000003514b430000000559414d563200000003585a4300000003554f5300000004414b524f00000003484e5400000003484f54000000034b4149000000034f474e00000003575258000000034b4441000000034f524e00000003464f52000000034153540000000553544f524a000000003b9aca00',
3,
4,
)
.then((e) => console.log('latest request: ', e))
.catch((err) => console.log(err))
16 changes: 15 additions & 1 deletion helpers/bandchain2.js/example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import logo from './logo.svg'
import './App.css'
import { Message, Data, Wallet } from 'bandchain2.js'
import { Message, Data, Wallet, Client } from 'bandchain2.js'

function App() {
const { MsgSend } = Message
Expand All @@ -26,6 +26,20 @@ function App() {

console.log(pubkey.toAddress().toAccBech32())

const client = new Client('http://d3n-debug.bandprotocol.com/rest')

console.log('---------------------------------------')

client
.getChainID()
.then((e) => console.log('chain ID: ', e))
.catch((err) => console.log(err.response.data.error))

client
.getDataSource(3)
.then((e) => console.log('data source: ', e))
.catch((err) => console.log(err))

return (
<div className="App">
<header className="App-header">
Expand Down
1 change: 1 addition & 0 deletions helpers/bandchain2.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"singleQuote": true
},
"dependencies": {
"axios": "^0.21.0",
"bech32": "^1.1.4",
"bip32": "^2.0.6",
"bip39": "^3.0.2",
Expand Down
58 changes: 58 additions & 0 deletions helpers/bandchain2.js/src/client.ts
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
}
}
9 changes: 9 additions & 0 deletions helpers/bandchain2.js/src/data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Address } from './wallet'

export class Coin {
amount: number
denom: string
Expand All @@ -11,3 +13,10 @@ export class Coin {
return { amount: this.amount.toString(), denom: this.denom }
}
}

export interface DataSource {
owner: Address
name: string
description: string
fileName: string
}
1 change: 1 addition & 0 deletions helpers/bandchain2.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * as Data from './data'
export * as Message from './message'
export * as Wallet from './wallet'
export { default as Transaction } from './transaction'
export { default as Client } from './client'
12 changes: 12 additions & 0 deletions helpers/bandchain2.js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.11.0.tgz#d61f46d83b2519250e2784daf5b09479a8b41c59"
integrity sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==

axios@^0.21.0:
version "0.21.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.0.tgz#26df088803a2350dff2c27f96fef99fe49442aca"
integrity sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==
dependencies:
follow-redirects "^1.10.0"

babel-jest@^26.6.3:
version "26.6.3"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-26.6.3.tgz#d87d25cb0037577a0c89f82e5755c5d293c01056"
Expand Down Expand Up @@ -1581,6 +1588,11 @@ find-up@^4.0.0, find-up@^4.1.0:
locate-path "^5.0.0"
path-exists "^4.0.0"

follow-redirects@^1.10.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db"
integrity sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down

0 comments on commit 943e76a

Please sign in to comment.