Skip to content

Commit

Permalink
feat: improve api
Browse files Browse the repository at this point in the history
- improve return types
- add axios config support
- allow return axios raw response

fix #1
  • Loading branch information
Juan Pablo Garcia authored and Juan Pablo Garcia committed Jan 30, 2021
1 parent 9c5098b commit 48e4d77
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 128 deletions.
192 changes: 119 additions & 73 deletions src/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import query, { QueryOptions } from './query'
import query, { QueryParams, RequestConfig } from './query'

type Options = Omit<QueryOptions, 'address' | 'contractAddress' | 'txhash' | 'module' | 'action'>
type AccountQueryParams = Omit<QueryParams, 'address' | 'contractAddress' | 'txhash' | 'module' | 'action'>

type BlockRange = {
startBlock?: number
Expand Down Expand Up @@ -72,98 +72,144 @@ type TokenTransferEvent = {
confirmations: string
}

function getBnbBalance(address: string, options?: Options): Promise<string> {
return query({
...options,
address,
module: 'account',
action: 'balance',
tag: 'latest',
})
function getBnbBalance(address: string, queryOptions?: AccountQueryParams, requestConfig?: RequestConfig) {
return query<string>(
{
...queryOptions,
address,
module: 'account',
action: 'balance',
tag: 'latest',
},
requestConfig
)
}

function getBnbBalanceForMultipleAddresses(addresses: string[], options?: Options): Promise<AddressBalance[]> {
return query({
...options,
address: addresses.join(','),
module: 'account',
action: 'balancemulti',
tag: 'latest',
})
function getBnbBalanceForMultipleAddresses(
addresses: string[],
queryOptions?: AccountQueryParams,
requestConfig?: RequestConfig
) {
return query<AddressBalance[]>(
{
...queryOptions,
address: addresses.join(','),
module: 'account',
action: 'balancemulti',
tag: 'latest',
},
requestConfig
)
}

function getTransactions(address: string, options?: Options): Promise<Transaction[]> {
return query({
...options,
address,
module: 'account',
action: 'txlist',
})
}

function getInternalTransactionsByAddress(address: string, options?: Options): Promise<InternalTransaction[]> {
return query({
...options,
address,
module: 'account',
action: 'txlistinternal',
})
function getInternalTransactionsByAddress(
address: string,
queryOptions?: AccountQueryParams,
requestConfig?: RequestConfig
) {
return query<InternalTransaction[]>(
{
...queryOptions,
address,
module: 'account',
action: 'txlistinternal',
},
requestConfig
)
}

function getInternalTransactionsByHash(txhash: string, options?: Options): Promise<InternalTransaction[]> {
return query({
...options,
txhash,
module: 'account',
action: 'txlistinternal',
})
function getInternalTransactionsByHash(
txhash: string,
queryOptions?: AccountQueryParams,
requestConfig?: RequestConfig
) {
return query<InternalTransaction[]>(
{
...queryOptions,
txhash,
module: 'account',
action: 'txlistinternal',
},
requestConfig
)
}

function getInternalTransactionsByBlockRange(
blockRange: BlockRange,
options?: Omit<Options, 'startBlock' | 'endBlock'>
): Promise<InternalTransaction[]> {
return query({
...options,
...blockRange,
module: 'account',
action: 'txlistinternal',
})
queryOptions?: Omit<AccountQueryParams, 'startBlock' | 'endBlock'>,
requestConfig?: RequestConfig
) {
return query<InternalTransaction[]>(
{
...queryOptions,
...blockRange,
module: 'account',
action: 'txlistinternal',
},
requestConfig
)
}

function getTokenTransferEventsByAddress(address: string, options?: Options): Promise<TokenTransferEvent[]> {
return query({
...options,
address,
module: 'account',
action: 'tokentx',
})
function getTokenTransferEventsByAddress(
address: string,
queryOptions?: AccountQueryParams,
requestConfig?: RequestConfig
) {
return query<TokenTransferEvent[]>(
{
...queryOptions,
address,
module: 'account',
action: 'tokentx',
},
requestConfig
)
}

function getTokenTransferEventsByContractAddress(
contractAddress: string,
options?: Options
): Promise<TokenTransferEvent[]> {
return query({
...options,
contractAddress,
module: 'account',
action: 'tokentx',
})
queryOptions?: AccountQueryParams,
requestConfig?: RequestConfig
) {
return query<TokenTransferEvent[]>(
{
...queryOptions,
contractAddress,
module: 'account',
action: 'tokentx',
},
requestConfig
)
}

function getTokenTransferEventsByAddressAndContractAddress(
address: string,
contractAddress: string,
options?: Options
): Promise<TokenTransferEvent[]> {
return query({
...options,
address,
contractAddress,
module: 'account',
action: 'tokentx',
})
queryOptions?: AccountQueryParams,
requestConfig?: RequestConfig
) {
return query<TokenTransferEvent[]>(
{
...queryOptions,
address,
contractAddress,
module: 'account',
action: 'tokentx',
},
requestConfig
)
}

function getTransactions(address: string, queryOptions?: AccountQueryParams, requestConfig?: RequestConfig) {
return query<Transaction[]>(
{
...queryOptions,
address,
module: 'account',
action: 'txlist',
},
requestConfig
)
}

export default {
Expand Down
36 changes: 19 additions & 17 deletions src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import query, { QueryOptions } from './query'
import query, { RequestConfig } from './query'

type SourceCode = {
SourceCode: string
Expand All @@ -16,24 +16,26 @@ type SourceCode = {
SwarmSource: string
}

function getContractAbi(address: string): Promise<string> {
const queryOptions: QueryOptions = {
address,
module: 'contract',
action: 'getabi',
}

return query(queryOptions)
function getContractAbi(address: string, requestConfig?: RequestConfig) {
return query<string>(
{
address,
module: 'contract',
action: 'getabi',
},
requestConfig
)
}

function getContractSourceCode(address: string): Promise<SourceCode[]> {
const queryOptions: QueryOptions = {
address,
module: 'contract',
action: 'getsourcecode',
}

return query(queryOptions)
function getContractSourceCode(address: string, requestConfig?: RequestConfig) {
return query<SourceCode[]>(
{
address,
module: 'contract',
action: 'getsourcecode',
},
requestConfig
)
}

export default {
Expand Down
34 changes: 28 additions & 6 deletions src/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios'
import axios, { AxiosRequestConfig } from 'axios'
import identity from 'lodash.identity'
import pickBy from 'lodash.pickby'
import qs from 'querystring'
Expand All @@ -13,7 +13,7 @@ type ContractAction = 'getabi' | 'getsourcecode'

type StatsAction = 'tokensupply' | 'tokenCsupply' | 'tokenbalance' | 'bnbsupply' | 'validators'

export type QueryOptions = {
export type QueryParams = {
module: Module
action: AccountAction | ContractAction | StatsAction
address?: string
Expand All @@ -28,7 +28,18 @@ export type QueryOptions = {
apiKey?: string
}

async function query(options: QueryOptions) {
export type RequestConfig = {
axiosConfig?: AxiosRequestConfig
rawAxiosResponse?: boolean
}

type Response<T> = {
status: '0' | '1'
message: string
result: T
}

async function query<T>(queryOptions: QueryParams, requestConfig?: RequestConfig) {
const {
address,
contractAddress,
Expand All @@ -41,7 +52,7 @@ async function query(options: QueryOptions) {
offset = 10000,
sort = 'asc',
apiKey = config.apiKey,
} = options
} = queryOptions

const queryParams = pickBy(
{
Expand All @@ -60,11 +71,22 @@ async function query(options: QueryOptions) {
identity
)

const { axiosConfig, rawAxiosResponse } = requestConfig || {}

const query = qs.stringify(queryParams)
const response = await axios.get<Response<T>>(`${config.url}/api?${query}`, axiosConfig)

if (rawAxiosResponse) {
return response
}

const { status, result } = response.data as Response<T>

const { data } = await axios.get(`${config.url}/api?${query}`)
if (status === '0') {
throw new Error(String(result))
}

return data.result
return result
}

export default query
28 changes: 17 additions & 11 deletions src/stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import query from './query'
import query, { RequestConfig } from './query'

type Validator = {
validatorAddress: string
Expand All @@ -8,18 +8,24 @@ type Validator = {
validatorVotingPowerProportion: string
}

function getBnbTotalSupply(): Promise<string> {
return query({
module: 'stats',
action: 'bnbsupply',
})
function getBnbTotalSupply(requestConfig?: RequestConfig) {
return query<string>(
{
module: 'stats',
action: 'bnbsupply',
},
requestConfig
)
}

function getValidatorsList(): Promise<Validator[]> {
return query({
module: 'stats',
action: 'validators',
})
function getValidatorsList(requestConfig?: RequestConfig) {
return query<Validator[]>(
{
module: 'stats',
action: 'validators',
},
requestConfig
)
}

export default {
Expand Down
Loading

0 comments on commit 48e4d77

Please sign in to comment.