Skip to content

Commit

Permalink
Merge pull request #35 from terra-money/feat/token/factory/api
Browse files Browse the repository at this point in the history
feat: token factory query API
  • Loading branch information
emidev98 authored Jul 18, 2023
2 parents e5fc221 + b951345 commit 29c8043
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 9 deletions.
47 changes: 47 additions & 0 deletions src/client/lcd/api/TokenFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { LCDClient } from '../LCDClient';
import { TokenFactory } from './TokenFactoryAPI';

// has a default list of endpoints that we can select with the first parameter
// 'mainnet' | 'testnet' | 'local'
// we can add more chains or overwrite the default settings with the second parameter
const lcd = LCDClient.fromDefaultConfig('testnet');
const tokenFactory = new TokenFactory(lcd);

describe('TokenFactory', () => {
it('params', async () => {
const param = await tokenFactory.params('pisco-1');

expect(param).toMatchObject({
params: {
denom_creation_fee: [
{
denom: 'uluna',
amount: '10000000',
},
],
},
});
});

it('denoms for creator', async () => {
const res = await tokenFactory.denomsFromCreator(
'terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je'
);

expect(res.denoms[res.denoms.length - 1]).toEqual(
'factory/terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je/utest766e'
);
});

/*
The following request does not work yet because the endpoint
does recognize the url encoded denom.
it('authority metadata', async () => {
const res = await tokenFactory.authorityMetadata("pisco-1","factory/terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je/utest766e");
expect(res)
.toEqual("factory/terra1zdpgj8am5nqqvht927k3etljyl6a52kwqup0je/utest766e")
});
*/
});
53 changes: 53 additions & 0 deletions src/client/lcd/api/TokenFactoryAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { APIParams } from '../APIRequester';
import { LCDClient } from '../LCDClient';
import { BaseAPI } from './BaseAPI';
import { QueryParamsResponse } from '@terra-money/terra.proto/cosmwasm/wasm/v1/query';
import { AccAddress } from 'core';

export interface DenomsFromCreatorResponse {
denoms: string[];
}

export interface AuthorityMetadataResponse {
authority_metadata: {
admin: AccAddress;
};
}

export class TokenFactory extends BaseAPI {
constructor(public lcd: LCDClient) {
super(lcd.apiRequesters, lcd.config);
}

public params(
chainID: string,
params: APIParams = {}
): Promise<QueryParamsResponse> {
return this.getReqFromChainID(chainID).get<QueryParamsResponse>(
`/osmosis/tokenfactory/v1beta1/params`,
params
);
}

public denomsFromCreator(
creator: AccAddress,
params: APIParams = {}
): Promise<DenomsFromCreatorResponse> {
const req = this.getReqFromAddress(creator);

return req.get<DenomsFromCreatorResponse>(
`/osmosis/tokenfactory/v1beta1/denoms_from_creator/${creator}`,
params
);
}

/*
The following request does not work yet because the endpoint
does recognize the url encoded denom.
public authorityMetadata(chainID: string, denom: string, params: APIParams = {}): Promise<AuthorityMetadataResponse> {
return this.getReqFromChainID(chainID)
.get<AuthorityMetadataResponse>(`/osmosis/tokenfactory/v1beta1/denoms/${denom}/authority_metadata`, params);
}
*/
}
17 changes: 8 additions & 9 deletions src/client/lcd/api/WasmAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { LCDClient } from '../LCDClient';
import { HistoryEntry } from '../../../core/wasm/HistoryEntry';
import { AbsoluteTxPosition } from '../../../core/wasm/AbsoluteTxPosition';
import { AccessConfig } from '../../../core/wasm';
import { getChainIDFromAddress } from '../../../util/bech32';

export interface CodeInfo {
code_id: number;
Expand Down Expand Up @@ -161,7 +160,7 @@ export class WasmAPI extends BaseAPI {
.get<{ data: T }>(endpoint, {
...params,
})
.then((d) => d.data);
.then(d => d.data);
}

public async parameters(
Expand All @@ -187,7 +186,7 @@ export class WasmAPI extends BaseAPI {
params
)
.then(({ pinned_code: d }) => ({
code_ids: d.code_ids.map((code_id) => Number.parseInt(code_id)),
code_ids: d.code_ids.map(code_id => Number.parseInt(code_id)),
}));
}

Expand Down Expand Up @@ -236,8 +235,8 @@ export class WasmAPI extends BaseAPI {
entries: HistoryEntry.Data[];
pagination: Pagination;
}>(`/cosmwasm/wasm/v1/contract/${contractAddress}/history`, params)
.then((d) => [
d.entries.map((entry) => HistoryEntry.fromData(entry)),
.then(d => [
d.entries.map(entry => HistoryEntry.fromData(entry)),
d.pagination,
]);
}
Expand All @@ -251,8 +250,8 @@ export class WasmAPI extends BaseAPI {
models: Model.Data[];
pagination: Pagination;
}>(`/cosmwasm/wasm/v1/contract/${contractAddress}/state`, params)
.then((d) => [
d.models.map((model) => {
.then(d => [
d.models.map(model => {
return {
key: model.key,
value: model.value,
Expand All @@ -271,8 +270,8 @@ export class WasmAPI extends BaseAPI {
codeInfos: CodeInfo.DataV2[];
pagination: Pagination;
}>(`/cosmwasm/wasm/v1/code`, params)
.then((d) => [
d.codeInfos.map((codeInfo) => {
.then(d => [
d.codeInfos.map(codeInfo => {
return {
code_id: +codeInfo.code_id,
code_hash: codeInfo.data_hash,
Expand Down
1 change: 1 addition & 0 deletions src/client/lcd/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export * from './TendermintAPI';
export * from './TxAPI';
export * from './WasmAPI';
export * from './MintAPI';
export * from './TokenFactoryAPI';
export * from './IbcAPI';
export * from './IbcTransferAPI';

0 comments on commit 29c8043

Please sign in to comment.