Skip to content

Commit

Permalink
[Feature Proposal ]Add Axios query params in each API get method. (#113)
Browse files Browse the repository at this point in the history
* Add axios query params in each API get method.

Co-authored-by: Paul Kim <[email protected]>
  • Loading branch information
0xslipk and hanjukim authored Aug 13, 2021
1 parent 34fb928 commit b17807d
Show file tree
Hide file tree
Showing 17 changed files with 255 additions and 128 deletions.
9 changes: 7 additions & 2 deletions src/client/lcd/APIRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface APIResponse<T> {
result: T;
}

export type APIParams = Record<string, string | number | null | undefined>;

export class APIRequester {
private axios: AxiosInstance;
constructor(baseURL: string) {
Expand All @@ -17,11 +19,14 @@ export class APIRequester {
});
}

public async getRaw<T>(endpoint: string, params?: any): Promise<T> {
public async getRaw<T>(endpoint: string, params: APIParams = {}): Promise<T> {
return this.axios.get(endpoint, { params }).then(d => d.data);
}

public async get<T>(endpoint: string, params?: any): Promise<APIResponse<T>> {
public async get<T>(
endpoint: string,
params: APIParams = {}
): Promise<APIResponse<T>> {
return this.axios.get(endpoint, { params }).then(d => d.data);
}

Expand Down
8 changes: 5 additions & 3 deletions src/client/lcd/api/AuthAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LazyGradedVestingAccount,
} from '../../../core';
import { BaseAPI } from './BaseAPI';
import { APIParams } from '../APIRequester';

export class AuthAPI extends BaseAPI {
/**
Expand All @@ -14,16 +15,17 @@ export class AuthAPI extends BaseAPI {
* @param address address of account to look up
*/
public async accountInfo(
address: AccAddress
address: AccAddress,
params: APIParams = {}
): Promise<Account | LazyGradedVestingAccount> {
const { result } = await this.c.get<
Account.Data | LazyGradedVestingAccount.Data
>(`/auth/accounts/${address}`);
>(`/auth/accounts/${address}`, params);

// Until columbus-4 it used to return coins from /auth/accounts
if (!result.value.coins) {
result.value.coins = (
await this.c.get<Coins.Data>(`/bank/balances/${address}`)
await this.c.get<Coins.Data>(`/bank/balances/${address}`, params)
).result;
}

Expand Down
8 changes: 6 additions & 2 deletions src/client/lcd/api/BankAPI.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { BaseAPI } from './BaseAPI';
import { Coins, AccAddress } from '../../../core';
import { APIParams } from '../APIRequester';

export class BankAPI extends BaseAPI {
/**
* Look up the balance of an account by its address.
* @param address address of account to look up.
*/
public async balance(address: AccAddress): Promise<Coins> {
public async balance(
address: AccAddress,
params: APIParams = {}
): Promise<Coins> {
return this.c
.get<Coins.Data>(`/bank/balances/${address}`)
.get<Coins.Data>(`/bank/balances/${address}`, params)
.then(d => Coins.fromData(d.result));
}

Expand Down
37 changes: 27 additions & 10 deletions src/client/lcd/api/DistributionAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseAPI } from './BaseAPI';
import { Coins, AccAddress, Dec, ValAddress } from '../../../core';
import { APIParams } from '../APIRequester';

export interface DistributionParams {
/**
Expand Down Expand Up @@ -82,9 +83,15 @@ export class DistributionAPI extends BaseAPI {
* Gets a delegator's rewards.
* @param delegator delegator's account address
*/
public async rewards(delegator: AccAddress): Promise<Rewards> {
public async rewards(
delegator: AccAddress,
params: APIParams = {}
): Promise<Rewards> {
const rewardsData = await this.c
.get<Rewards.Data>(`/distribution/delegators/${delegator}/rewards`)
.get<Rewards.Data>(
`/distribution/delegators/${delegator}/rewards`,
params
)
.then(d => d.result);

const rewards: Rewards['rewards'] = {};
Expand All @@ -102,10 +109,14 @@ export class DistributionAPI extends BaseAPI {
* @param validator validator's operator address
*/
public async validatorRewards(
validator: ValAddress
validator: ValAddress,
params: APIParams = {}
): Promise<ValidatorRewards> {
return this.c
.get<ValidatorRewards.Data>(`/distribution/validators/${validator}`)
.get<ValidatorRewards.Data>(
`/distribution/validators/${validator}`,
params
)
.then(d => d.result)
.then(d => ({
self_bond_rewards: Coins.fromData(d.self_bond_rewards),
Expand All @@ -117,27 +128,33 @@ export class DistributionAPI extends BaseAPI {
* Gets the withdraw address of a delegator, the address to which rewards are withdrawn.
* @param delegator
*/
public async withdrawAddress(delegator: AccAddress): Promise<AccAddress> {
public async withdrawAddress(
delegator: AccAddress,
params: APIParams = {}
): Promise<AccAddress> {
return this.c
.get<AccAddress>(`/distribution/delegators/${delegator}/withdraw_address`)
.get<AccAddress>(
`/distribution/delegators/${delegator}/withdraw_address`,
params
)
.then(d => d.result);
}

/**
* Gets the current value of the community pool.
*/
public async communityPool(): Promise<Coins> {
public async communityPool(params: APIParams = {}): Promise<Coins> {
return this.c
.get<Coins.Data>(`/distribution/community_pool`)
.get<Coins.Data>(`/distribution/community_pool`, params)
.then(d => Coins.fromData(d.result));
}

/**
* Gets the current distribution parameters.
*/
public async parameters(): Promise<DistributionParams> {
public async parameters(params: APIParams = {}): Promise<DistributionParams> {
return this.c
.get<DistributionParams.Data>(`/distribution/parameters`)
.get<DistributionParams.Data>(`/distribution/parameters`, params)
.then(({ result: d }) => ({
base_proposer_reward: new Dec(d.base_proposer_reward),
community_tax: new Dec(d.community_tax),
Expand Down
62 changes: 40 additions & 22 deletions src/client/lcd/api/GovAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
VotingParams,
TallyParams,
} from '../../../core/gov/params';
import { APIParams } from '../APIRequester';

export interface GovParams {
/**
Expand Down Expand Up @@ -51,59 +52,74 @@ export class GovAPI extends BaseAPI {
/**
* Gets all proposals.
*/
public async proposals(): Promise<Proposal[]> {
public async proposals(params: APIParams = {}): Promise<Proposal[]> {
return this.c
.get<Proposal.Data[]>(`/gov/proposals`)
.get<Proposal.Data[]>(`/gov/proposals`, params)
.then(d => d.result.map(Proposal.fromData));
}

/**
* Get a specific proposal by its ID
* @param proposalId proposal's ID
*/
public async proposal(proposalId: number): Promise<Proposal> {
public async proposal(
proposalId: number,
params: APIParams = {}
): Promise<Proposal> {
return this.c
.get<Proposal.Data>(`/gov/proposals/${proposalId}`)
.get<Proposal.Data>(`/gov/proposals/${proposalId}`, params)
.then(d => Proposal.fromData(d.result));
}

/**
* Get the proposal's proposer
* @param proposalId proposal's ID
*/
public async proposer(proposalId: number): Promise<AccAddress> {
public async proposer(
proposalId: number,
params: APIParams = {}
): Promise<AccAddress> {
return this.c
.get<{ proposer: AccAddress }>(`/gov/proposals/${proposalId}/proposer`)
.get<{ proposer: AccAddress }>(
`/gov/proposals/${proposalId}/proposer`,
params
)
.then(d => d.result.proposer);
}

/**
* Get the deposits for a proposal
* @param proposalId proposal's ID
*/
public async deposits(proposalId: number): Promise<any> {
public async deposits(
proposalId: number,
params: APIParams = {}
): Promise<any> {
return this.c
.get(`/gov/proposals/${proposalId}/deposits`)
.get(`/gov/proposals/${proposalId}/deposits`, params)
.then(d => d.result);
}

/**
* Get the current votes for a proposal
* @param proposalId proposal's ID
*/
public async votes(proposalId: number): Promise<any> {
public async votes(proposalId: number, params: APIParams = {}): Promise<any> {
return this.c
.get(`/gov/proposals/${proposalId}/deposits`)
.get(`/gov/proposals/${proposalId}/deposits`, params)
.then(d => d.result);
}

/**
* Gets the current tally for a proposal.
* @param proposalId proposal's ID
*/
public async tally(proposalId: number): Promise<Tally> {
public async tally(
proposalId: number,
params: APIParams = {}
): Promise<Tally> {
return this.c
.get<Tally.Data>(`/gov/proposals/${proposalId}/tally`)
.get<Tally.Data>(`/gov/proposals/${proposalId}/tally`, params)
.then(({ result: d }) => ({
yes: new Int(d.yes),
no: new Int(d.no),
Expand All @@ -113,28 +129,30 @@ export class GovAPI extends BaseAPI {
}

/** Gets the Gov module's deposit parameters */
public async depositParameters(): Promise<DepositParams> {
public async depositParameters(
params: APIParams = {}
): Promise<DepositParams> {
return this.c
.get<DepositParams.Data>(`/gov/parameters/deposit`)
.get<DepositParams.Data>(`/gov/parameters/deposit`, params)
.then(({ result: d }) => ({
max_deposit_period: Number.parseInt(d.max_deposit_period),
min_deposit: Coins.fromData(d.min_deposit),
}));
}

/** Gets the Gov module's voting parameters */
public async votingParameters(): Promise<VotingParams> {
public async votingParameters(params: APIParams = {}): Promise<VotingParams> {
return this.c
.get<VotingParams.Data>(`/gov/parameters/voting`)
.get<VotingParams.Data>(`/gov/parameters/voting`, params)
.then(({ result: d }) => ({
voting_period: Number.parseInt(d.voting_period),
}));
}

/** Gets teh Gov module's tally parameters */
public async tallyParameters(): Promise<TallyParams> {
public async tallyParameters(params: APIParams = {}): Promise<TallyParams> {
return this.c
.get<TallyParams.Data>(`/gov/parameters/tallying`)
.get<TallyParams.Data>(`/gov/parameters/tallying`, params)
.then(({ result: d }) => ({
quorum: new Dec(d.quorum),
veto_threshold: new Dec(d.veto_threshold),
Expand All @@ -143,11 +161,11 @@ export class GovAPI extends BaseAPI {
}

/** Gets the Gov module's current parameters */
public async parameters(): Promise<GovParams> {
public async parameters(params: APIParams = {}): Promise<GovParams> {
const [deposit_params, voting_params, tally_params] = await Promise.all([
this.depositParameters(),
this.votingParameters(),
this.tallyParameters(),
this.depositParameters(params),
this.votingParameters(params),
this.tallyParameters(params),
]);
return {
deposit_params,
Expand Down
16 changes: 11 additions & 5 deletions src/client/lcd/api/MarketAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Coin, Dec, Numeric, Denom } from '../../../core';
import { APIParams } from '../APIRequester';
import { BaseAPI } from './BaseAPI';

export interface MarketParams {
Expand Down Expand Up @@ -28,8 +29,13 @@ export class MarketAPI extends BaseAPI {
* @param offerCoin coin to convert
* @param askDenom denomination to swap into
*/
public async swapRate(offerCoin: Coin, askDenom: Denom): Promise<Coin> {
public async swapRate(
offerCoin: Coin,
askDenom: Denom,
_params: APIParams = {}
): Promise<Coin> {
const params = {
..._params,
offer_coin: offerCoin.toString(),
ask_denom: askDenom,
};
Expand All @@ -42,18 +48,18 @@ export class MarketAPI extends BaseAPI {
/**
* Gets current value of the pool delta, which is used to determine Terra<>Luna swap rates.
*/
public async poolDelta(): Promise<Dec> {
public async poolDelta(params: APIParams = {}): Promise<Dec> {
return this.c
.get<Numeric.Input>(`/market/terra_pool_delta`)
.get<Numeric.Input>(`/market/terra_pool_delta`, params)
.then(d => new Dec(d.result));
}

/**
* Gets the current Market module's parameters.
*/
public async parameters(): Promise<MarketParams> {
public async parameters(params: APIParams = {}): Promise<MarketParams> {
return this.c
.get<MarketParams.Data>(`/market/parameters`)
.get<MarketParams.Data>(`/market/parameters`, params)
.then(({ result: d }) => ({
pool_recovery_period: Number.parseInt(d.pool_recovery_period),
base_pool: new Dec(d.base_pool),
Expand Down
13 changes: 7 additions & 6 deletions src/client/lcd/api/MintAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Dec, Numeric, Denom } from '../../../core';
import { APIParams } from '../APIRequester';
import { BaseAPI } from './BaseAPI';

export interface MintingParams {
Expand All @@ -25,27 +26,27 @@ export class MintAPI extends BaseAPI {
/**
* Gets the current minting inflation value
*/
public async inflation(): Promise<Dec> {
public async inflation(params: APIParams = {}): Promise<Dec> {
return this.c
.get<Numeric.Input>(`/minting/inflation`)
.get<Numeric.Input>(`/minting/inflation`, params)
.then(d => new Dec(d.result));
}

/**
* Gets the current minting annaul provisions value
*/
public async annualProvisions(): Promise<Dec> {
public async annualProvisions(params: APIParams = {}): Promise<Dec> {
return this.c
.get<Numeric.Input>(`minting/annual-provisions`)
.get<Numeric.Input>(`minting/annual-provisions`, params)
.then(d => new Dec(d.result));
}

/**
* Gets the current minting module's parameters.
*/
public async parameters(): Promise<MintingParams> {
public async parameters(params: APIParams = {}): Promise<MintingParams> {
return this.c
.get<MintingParams.Data>(`/minting/parameters`)
.get<MintingParams.Data>(`/minting/parameters`, params)
.then(({ result: d }) => ({
mint_denom: d.mint_denom,
inflation_rate_change: new Dec(d.inflation_rate_change),
Expand Down
Loading

0 comments on commit b17807d

Please sign in to comment.