Skip to content

Commit

Permalink
Add slinky marketmap module
Browse files Browse the repository at this point in the history
  • Loading branch information
joon9823 committed Jun 11, 2024
1 parent a7f0ea4 commit afb8a09
Show file tree
Hide file tree
Showing 17 changed files with 960 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@initia/initia.js",
"version": "0.2.6",
"version": "0.2.7",
"description": "The JavaScript SDK for Initia",
"license": "MIT",
"author": "InitiaLabs",
Expand Down
3 changes: 3 additions & 0 deletions src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
IbcTransferAPI,
IbcPermAPI,
InterTxAPI,
MarketmapAPI,
MoveAPI,
MstakingAPI,
OpchildAPI,
Expand Down Expand Up @@ -101,6 +102,7 @@ export class LCDClient {
public ibcTransfer: IbcTransferAPI;
public ibcPerm: IbcPermAPI;
public interTx: InterTxAPI;
public marketmap: MarketmapAPI;
public move: MoveAPI;
public mstaking: MstakingAPI;
public opchild: OpchildAPI;
Expand Down Expand Up @@ -153,6 +155,7 @@ export class LCDClient {
this.ibcTransfer = new IbcTransferAPI(this.apiRequester);
this.ibcPerm = new IbcPermAPI(this.apiRequester);
this.interTx = new InterTxAPI(this.apiRequester);
this.marketmap = new MarketmapAPI(this.apiRequester);
this.move = new MoveAPI(this.apiRequester);
this.mstaking = new MstakingAPI(this.apiRequester);
this.opchild = new OpchildAPI(this.apiRequester);
Expand Down
61 changes: 61 additions & 0 deletions src/client/lcd/api/MarketmapAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { BaseAPI } from './BaseAPI';
import { APIParams } from '../APIRequester';
import { CurrencyPair, Market, MarketmapParams } from '../../../core';

export interface MarketMap {
markets: { [ticker: string]: Market };
}

export namespace MarketMap {
export interface Data {
markets: { [ticker: string]: Market.Data };
}
}

export class MarketmapAPI extends BaseAPI {
public async marketMap(params: APIParams = {}): Promise<MarketMap> {
return this.c
.get<{ market_map: MarketMap.Data }>(
`/slinky/marketmap/v1/marketmap`,
params
)
.then(d => {
const markets: { [ticker: string]: Market } = {};
for (const [ticker, market] of Object.entries(d.market_map.markets)) {
markets[ticker] = Market.fromData(market);
}
return { markets };
});
}

public async market(
pair: CurrencyPair,
params: APIParams = {}
): Promise<Market> {
return this.c
.get<{ market: Market.Data }>(`/slinky/marketmap/v1/market`, {
...params,
'currency_pair.Base': pair.Base,
'currency_pair.Quote': pair.Quote,
})
.then(d => Market.fromData(d.market));
}

public async lastUpdated(params: APIParams = {}): Promise<number> {
return this.c
.get<{ last_updated: string }>(
`/slinky/marketmap/v1/last_updated`,
params
)
.then(d => Number.parseInt(d.last_updated));
}

public async parameters(params: APIParams = {}): Promise<MarketmapParams> {
return this.c
.get<{ params: MarketmapParams.Data }>(
`/slinky/marketmap/v1/params`,
params
)
.then(d => MarketmapParams.fromData(d.params));
}
}
1 change: 1 addition & 0 deletions src/client/lcd/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './IbcNftAPI';
export * from './IbcTransferAPI';
export * from './IbcPermAPI';
export * from './InterTxAPI';
export * from './MarketmapAPI';
export * from './MoveAPI';
export * from './MstakingAPI';
export * from './OpchildAPI';
Expand Down
41 changes: 41 additions & 0 deletions src/core/Msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ import {
MsgUpdateIbcConnectionParams,
} from './ibc/core/connection/msgs';
import { InterTxMsg, MsgRegisterAccount, MsgSubmitTx } from './intertx';
import {
MarketmapMsg,
MsgCreateMarkets,
MsgUpdateMarkets,
MsgRemoveMarketAuthorities,
MsgUpdateMarketmapParams,
} from './marketmap/msgs';
import {
MoveMsg,
MsgPublish,
Expand Down Expand Up @@ -240,6 +247,7 @@ export type Msg =
| IbcClientMsg
| IbcConnectionMsg
| InterTxMsg
| MarketmapMsg
| MoveMsg
| MstakingMsg
| OpchildMsg
Expand Down Expand Up @@ -272,6 +280,7 @@ export namespace Msg {
| IbcPermMsg.Amino
| IbcTransferMsg.Amino
| InterTxMsg.Amino
| MarketmapMsg.Amino
| MoveMsg.Amino
| MstakingMsg.Amino
| OpchildMsg.Amino
Expand Down Expand Up @@ -308,6 +317,7 @@ export namespace Msg {
| IbcClientMsg.Data
| IbcConnectionMsg.Data
| InterTxMsg.Data
| MarketmapMsg.Data
| MoveMsg.Data
| MstakingMsg.Data
| OpchildMsg.Data
Expand Down Expand Up @@ -344,6 +354,7 @@ export namespace Msg {
| IbcClientMsg.Proto
| IbcConnectionMsg.Proto
| InterTxMsg.Proto
| MarketmapMsg.Proto
| MoveMsg.Proto
| MstakingMsg.Proto
| OpchildMsg.Proto
Expand Down Expand Up @@ -521,6 +532,16 @@ export namespace Msg {
case 'intertx/MsgSubmitTx':
return MsgSubmitTx.fromAmino(data);

// marketmap
case 'slinky/x/marketmap/MsgCreateMarkets':
return MsgCreateMarkets.fromAmino(data);
case 'slinky/x/marketmap/MsgUpdateMarkets':
return MsgUpdateMarkets.fromAmino(data);
case 'slinky/x/marketmap/MsgRemoveMarketAuthorities':
return MsgRemoveMarketAuthorities.fromAmino(data);
case 'slinky/x/marketmap/MsgParams':
return MsgUpdateMarketmapParams.fromAmino(data);

// move
case 'move/MsgPublish':
return MsgPublish.fromAmino(data);
Expand Down Expand Up @@ -921,6 +942,16 @@ export namespace Msg {
case '/initia.intertx.v1.MsgSubmitTx':
return MsgSubmitTx.fromData(data);

// marketmap
case '/slinky.marketmap.v1.MsgCreateMarkets':
return MsgCreateMarkets.fromData(data);
case '/slinky.marketmap.v1.MsgUpdateMarkets':
return MsgUpdateMarkets.fromData(data);
case '/slinky.marketmap.v1.MsgRemoveMarketAuthorities':
return MsgRemoveMarketAuthorities.fromData(data);
case '/slinky.marketmap.v1.MsgParams':
return MsgUpdateMarketmapParams.fromData(data);

// move
case '/initia.move.v1.MsgPublish':
return MsgPublish.fromData(data);
Expand Down Expand Up @@ -1324,6 +1355,16 @@ export namespace Msg {
case '/initia.intertx.v1.MsgSubmitTx':
return MsgSubmitTx.unpackAny(proto);

// marketmap
case '/slinky.marketmap.v1.MsgCreateMarkets':
return MsgCreateMarkets.unpackAny(proto);
case '/slinky.marketmap.v1.MsgUpdateMarkets':
return MsgUpdateMarkets.unpackAny(proto);
case '/slinky.marketmap.v1.MsgRemoveMarketAuthorities':
return MsgRemoveMarketAuthorities.unpackAny(proto);
case '/slinky.marketmap.v1.MsgParams':
return MsgUpdateMarketmapParams.unpackAny(proto);

// move
case '/initia.move.v1.MsgPublish':
return MsgPublish.unpackAny(proto);
Expand Down
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export * from './group';
export * from './ibc';
export * from './ibchooks';
export * from './intertx';
export * from './marketmap';
export * from './move';
export * from './mstaking';
export * from './opchild';
Expand Down
82 changes: 82 additions & 0 deletions src/core/marketmap/Market.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { JSONSerializable } from '../../util/json';
import { ProviderConfig } from './ProviderConfig';
import { Ticker } from './Ticker';
import { Market as Market_pb } from '@initia/initia.proto/slinky/marketmap/v1/market';

export class Market extends JSONSerializable<
Market.Amino,
Market.Data,
Market.Proto
> {
/**
* @param ticker the price feed for a given asset pair
* @param provider_configs the list of provider-specific configs for this Market
*/
constructor(
public ticker: Ticker,
public provider_configs: ProviderConfig[]
) {
super();
}

public static fromAmino(data: Market.Amino): Market {
const { ticker, provider_configs } = data;
return new Market(
Ticker.fromAmino(ticker),
provider_configs.map(ProviderConfig.fromAmino)
);
}

public toAmino(): Market.Amino {
const { ticker, provider_configs } = this;
return {
ticker: ticker.toAmino(),
provider_configs: provider_configs.map(config => config.toAmino()),
};
}

public static fromData(data: Market.Data): Market {
const { ticker, provider_configs } = data;
return new Market(
Ticker.fromData(ticker),
provider_configs.map(ProviderConfig.fromData)
);
}

public toData(): Market.Data {
const { ticker, provider_configs } = this;
return {
ticker: ticker.toData(),
provider_configs: provider_configs.map(config => config.toData()),
};
}

public static fromProto(proto: Market.Proto): Market {
return new Market(
Ticker.fromProto(proto.ticker as Ticker.Proto),
proto.providerConfigs.map(ProviderConfig.fromProto)
);
}

public toProto(): Market.Proto {
const { ticker, provider_configs } = this;
return Market_pb.fromPartial({
ticker: ticker.toProto(),
providerConfigs: provider_configs.map(config => config.toProto()),
});
}
}

export namespace Market {
export interface Amino {
ticker: Ticker.Amino;
provider_configs: ProviderConfig.Amino[];
}

export interface Data {
ticker: Ticker.Data;
provider_configs: ProviderConfig.Data[];
}

export type Proto = Market_pb;
}
64 changes: 64 additions & 0 deletions src/core/marketmap/MarketmapParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { AccAddress } from '../bech32';
import { JSONSerializable } from '../../util/json';
import { Params as Params_pb } from '@initia/initia.proto/slinky/marketmap/v1/params';

export class MarketmapParams extends JSONSerializable<
MarketmapParams.Amino,
MarketmapParams.Data,
MarketmapParams.Proto
> {
/**
* @param market_authorities the list of authority accounts that are able to control updating the marketmap
* @param admin the address that can remove addresses from the MarketAuthorities list
*/
constructor(
public market_authorities: AccAddress[],
public admin: AccAddress
) {
super();
}

public static fromAmino(data: MarketmapParams.Amino): MarketmapParams {
return new MarketmapParams(data.market_authorities, data.admin);
}

public toAmino(): MarketmapParams.Amino {
const { market_authorities, admin } = this;
return { market_authorities, admin };
}

public static fromData(data: MarketmapParams.Data): MarketmapParams {
return new MarketmapParams(data.market_authorities, data.admin);
}

public toData(): MarketmapParams.Data {
const { market_authorities, admin } = this;
return { market_authorities, admin };
}

public static fromProto(data: MarketmapParams.Proto): MarketmapParams {
return new MarketmapParams(data.marketAuthorities, data.admin);
}

public toProto(): MarketmapParams.Proto {
const { market_authorities, admin } = this;
return Params_pb.fromPartial({
marketAuthorities: market_authorities,
admin,
});
}
}

export namespace MarketmapParams {
export interface Amino {
market_authorities: AccAddress[];
admin: AccAddress;
}

export interface Data {
market_authorities: AccAddress[];
admin: AccAddress;
}

export type Proto = Params_pb;
}
Loading

0 comments on commit afb8a09

Please sign in to comment.