Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fix): unhide spot/perp protojs #250

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/nibijs/docs/classes/StableSwap.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ y()
Calculate x[j] if one makes x[i] = x

Done by solving quadratic equation iteratively.
x_1**2 + x1 * (sum' - (A*n**n - 1) _ D / (A _ n**n)) = D ** (n+1)/(n ** (2 _ n) _ prod' \* A)
x_1**2 + b\*x_1 = c
x*1\*\*2 + x1 * (sum' - (A*n\*\*n - 1) * D / (A _ n**n)) = D ** (n+1)/(n \*\* (2 _ n) _ prod' _ A)
x_1\*\*2 + b\*x_1 = c

x_1 = (x_1\**2 + c) / (2*x_1 + b)

Expand Down
2 changes: 1 addition & 1 deletion packages/nibijs/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class CustomChain implements Chain {

private initChainId = () => {
const { prefix, shortName, number } = this.chainIdParts
return [prefix, shortName, number].filter(Boolean).join("-")
return [prefix, shortName, number].join("-")
cgilbe27 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
18 changes: 9 additions & 9 deletions packages/nibijs/src/msg/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// import { PerpMsgFactory } from "./perp"
// import { SpotMsgFactory } from "./spot"
import { PerpMsgFactory } from "./perp"
import { SpotMsgFactory } from "./spot"

// export class MsgFactory {
// spot = SpotMsgFactory
export class MsgFactory {
spot = SpotMsgFactory

// perp = PerpMsgFactory
// }
perp = PerpMsgFactory
}

// export const Msg = new MsgFactory()
export const Msg = new MsgFactory()

export * from "./encode-types"
// export * from "./perp"
// export * from "./spot"
export * from "./perp"
export * from "./spot"
350 changes: 175 additions & 175 deletions packages/nibijs/src/msg/perp.ts
Original file line number Diff line number Diff line change
@@ -1,175 +1,175 @@
// import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"
// import {
// MsgAddMargin,
// MsgClosePosition,
// MsgDonateToEcosystemFund,
// MsgMultiLiquidate,
// MsgMarketOrder,
// MsgRemoveMargin,
// MsgPartialClose,
// protobufPackage,
// } from "@nibiruchain/protojs/dist/nibiru/perp/v2/tx"
// import { Direction } from "@nibiruchain/protojs/dist/nibiru/perp/v2/state"
// import { toSdkDec, toSdkInt } from "../chain"
// import { TxMessage } from "./encode-types"

// export const PERP_MSG_TYPE_URLS = {
// MsgAddMargin: `/${protobufPackage}.MsgAddMargin`,
// MsgRemoveMargin: `/${protobufPackage}.MsgRemoveMargin`,
// MsgMultiLiquidate: `/${protobufPackage}.MsgMultiLiquidate`,
// MsgMarketOrder: `/${protobufPackage}.MsgMarketOrder`,
// MsgClosePosition: `/${protobufPackage}.MsgClosePosition`,
// MsgDonateToEcosystemFund: `/${protobufPackage}.MsgDonateToEcosystemFund`,
// MsgPartialClose: `/${protobufPackage}.MsgPartialClose`,
// }

// export const perpTypes: ReadonlyArray<[string, GeneratedType]> = [
// [PERP_MSG_TYPE_URLS.MsgAddMargin, MsgAddMargin],
// [PERP_MSG_TYPE_URLS.MsgRemoveMargin, MsgRemoveMargin],
// [PERP_MSG_TYPE_URLS.MsgMultiLiquidate, MsgMultiLiquidate],
// [PERP_MSG_TYPE_URLS.MsgMarketOrder, MsgMarketOrder],
// [PERP_MSG_TYPE_URLS.MsgClosePosition, MsgClosePosition],
// [PERP_MSG_TYPE_URLS.MsgDonateToEcosystemFund, MsgDonateToEcosystemFund],
// [PERP_MSG_TYPE_URLS.MsgPartialClose, MsgPartialClose],
// ]

// export interface MsgAddMarginEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgAddMargin>
// }

// export const isMsgAddMarginEncodeObject = (encodeObject: EncodeObject) =>
// (encodeObject as MsgAddMarginEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgAddMargin

// export interface MsgRemoveMarginEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgRemoveMargin>
// }

// export const isMsgRemoveMarginEncodeObject = (encodeObject: EncodeObject) =>
// (encodeObject as MsgRemoveMarginEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgRemoveMargin

// export interface MsgMultiLiquidateEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgMultiLiquidate>
// }

// export const isMsgMultiLiquidateEncodeObject = (encodeObject: EncodeObject) =>
// (encodeObject as MsgMultiLiquidateEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgMultiLiquidate

// export interface MsgOpenPositionEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgMarketOrder>
// }

// export const isMsgOpenPositionEncodeObject = (encodeObject: EncodeObject) =>
// (encodeObject as MsgOpenPositionEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgMarketOrder

// export interface MsgClosePositionEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgClosePosition>
// }

// export const isMsgClosePositionEncodeObject = (encodeObject: EncodeObject) =>
// (encodeObject as MsgClosePositionEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgClosePosition

// export interface MsgDonateToEcosystemFundEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgDonateToEcosystemFund>
// }

// export const isMsgDonateToEcosystemFundEncodeObject = (
// encodeObject: EncodeObject
// ) =>
// (encodeObject as MsgDonateToEcosystemFundEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgDonateToEcosystemFund

// export interface MsgPartialCloseEncodeObject extends EncodeObject {
// readonly typeUrl: string
// readonly value: Partial<MsgPartialClose>
// }

// export const isMsgPartialCloseEncodeObject = (encodeObject: EncodeObject) =>
// (encodeObject as MsgPartialCloseEncodeObject).typeUrl ===
// PERP_MSG_TYPE_URLS.MsgPartialClose

// // ----------------------------------------------------------------------------

// export class PerpMsgFactory {
// static removeMargin(msg: MsgRemoveMargin): TxMessage {
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgRemoveMargin,
// value: MsgRemoveMargin.fromPartial(msg),
// }
// }

// /**
// * Returns a 'TxMessage' for adding margin to a position
// *
// * @static
// * @param {MsgAddMargin} msg - Message to add margin
// * @returns {TxMessage} - formatted version of MsgAddMargin
// */
// static addMargin(msg: MsgAddMargin): TxMessage {
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgAddMargin,
// value: MsgAddMargin.fromPartial(msg),
// }
// }

// static liquidate(msg: MsgMultiLiquidate): TxMessage {
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgMultiLiquidate,
// value: MsgMultiLiquidate.fromPartial(msg),
// }
// }

// static openPosition(msg: {
// sender: string
// pair: string
// goLong: boolean
// quoteAssetAmount: number
// baseAssetAmountLimit?: number
// leverage: number
// }): TxMessage {
// const { quoteAssetAmount, baseAssetAmountLimit, leverage } = msg
// const pbMsg: MsgMarketOrder = {
// sender: msg.sender,
// pair: msg.pair,
// quoteAssetAmount: toSdkInt(quoteAssetAmount),
// baseAssetAmountLimit: toSdkInt(baseAssetAmountLimit ?? 0),
// leverage: toSdkDec(leverage.toString()),
// side: msg.goLong ? Direction.LONG : Direction.SHORT,
// }
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgMarketOrder,
// value: MsgMarketOrder.fromPartial(pbMsg),
// }
// }

// static closePosition(msg: MsgClosePosition): TxMessage {
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgClosePosition,
// value: MsgClosePosition.fromPartial(msg),
// }
// }

// static partialClosePosition(msg: MsgPartialClose): TxMessage {
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgPartialClose,
// value: MsgPartialClose.fromPartial(msg),
// }
// }

// static donateToPerpEF(msg: MsgDonateToEcosystemFund): TxMessage {
// return {
// typeUrl: PERP_MSG_TYPE_URLS.MsgDonateToEcosystemFund,
// value: MsgDonateToEcosystemFund.fromPartial(msg),
// }
// }
// }
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"
import {
MsgAddMargin,
MsgClosePosition,
MsgDonateToEcosystemFund,
MsgMultiLiquidate,
MsgMarketOrder,
MsgRemoveMargin,
MsgPartialClose,
protobufPackage,
} from "@nibiruchain/protojs/dist/nibiru/perp/v2/tx"
import { Direction } from "@nibiruchain/protojs/dist/nibiru/perp/v2/state"
import { toSdkDec, toSdkInt } from "../chain"
import { TxMessage } from "./encode-types"

export const PERP_MSG_TYPE_URLS = {
MsgAddMargin: `/${protobufPackage}.MsgAddMargin`,
MsgRemoveMargin: `/${protobufPackage}.MsgRemoveMargin`,
MsgMultiLiquidate: `/${protobufPackage}.MsgMultiLiquidate`,
MsgMarketOrder: `/${protobufPackage}.MsgMarketOrder`,
MsgClosePosition: `/${protobufPackage}.MsgClosePosition`,
MsgDonateToEcosystemFund: `/${protobufPackage}.MsgDonateToEcosystemFund`,
MsgPartialClose: `/${protobufPackage}.MsgPartialClose`,
}

export const perpTypes: ReadonlyArray<[string, GeneratedType]> = [
[PERP_MSG_TYPE_URLS.MsgAddMargin, MsgAddMargin],
[PERP_MSG_TYPE_URLS.MsgRemoveMargin, MsgRemoveMargin],
[PERP_MSG_TYPE_URLS.MsgMultiLiquidate, MsgMultiLiquidate],
[PERP_MSG_TYPE_URLS.MsgMarketOrder, MsgMarketOrder],
[PERP_MSG_TYPE_URLS.MsgClosePosition, MsgClosePosition],
[PERP_MSG_TYPE_URLS.MsgDonateToEcosystemFund, MsgDonateToEcosystemFund],
[PERP_MSG_TYPE_URLS.MsgPartialClose, MsgPartialClose],
]

export interface MsgAddMarginEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgAddMargin>
}

export const isMsgAddMarginEncodeObject = (encodeObject: EncodeObject) =>
(encodeObject as MsgAddMarginEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgAddMargin

export interface MsgRemoveMarginEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgRemoveMargin>
}

export const isMsgRemoveMarginEncodeObject = (encodeObject: EncodeObject) =>
(encodeObject as MsgRemoveMarginEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgRemoveMargin

export interface MsgMultiLiquidateEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgMultiLiquidate>
}

export const isMsgMultiLiquidateEncodeObject = (encodeObject: EncodeObject) =>
(encodeObject as MsgMultiLiquidateEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgMultiLiquidate

export interface MsgOpenPositionEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgMarketOrder>
}

export const isMsgOpenPositionEncodeObject = (encodeObject: EncodeObject) =>
(encodeObject as MsgOpenPositionEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgMarketOrder

export interface MsgClosePositionEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgClosePosition>
}

export const isMsgClosePositionEncodeObject = (encodeObject: EncodeObject) =>
(encodeObject as MsgClosePositionEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgClosePosition

export interface MsgDonateToEcosystemFundEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgDonateToEcosystemFund>
}

export const isMsgDonateToEcosystemFundEncodeObject = (
encodeObject: EncodeObject
) =>
(encodeObject as MsgDonateToEcosystemFundEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgDonateToEcosystemFund

export interface MsgPartialCloseEncodeObject extends EncodeObject {
readonly typeUrl: string
readonly value: Partial<MsgPartialClose>
}

export const isMsgPartialCloseEncodeObject = (encodeObject: EncodeObject) =>
(encodeObject as MsgPartialCloseEncodeObject).typeUrl ===
PERP_MSG_TYPE_URLS.MsgPartialClose

// ----------------------------------------------------------------------------

export class PerpMsgFactory {
static removeMargin(msg: MsgRemoveMargin): TxMessage {
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgRemoveMargin,
value: MsgRemoveMargin.fromPartial(msg),
}
}

/**
* Returns a 'TxMessage' for adding margin to a position
*
* @static
* @param {MsgAddMargin} msg - Message to add margin
* @returns {TxMessage} - formatted version of MsgAddMargin
*/
static addMargin(msg: MsgAddMargin): TxMessage {
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgAddMargin,
value: MsgAddMargin.fromPartial(msg),
}
}

static liquidate(msg: MsgMultiLiquidate): TxMessage {
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgMultiLiquidate,
value: MsgMultiLiquidate.fromPartial(msg),
}
}

static openPosition(msg: {
sender: string
pair: string
goLong: boolean
quoteAssetAmount: number
baseAssetAmountLimit?: number
leverage: number
}): TxMessage {
const { quoteAssetAmount, baseAssetAmountLimit, leverage } = msg
const pbMsg: MsgMarketOrder = {
sender: msg.sender,
pair: msg.pair,
quoteAssetAmount: toSdkInt(quoteAssetAmount),
baseAssetAmountLimit: toSdkInt(baseAssetAmountLimit ?? 0),
leverage: toSdkDec(leverage.toString()),
side: msg.goLong ? Direction.LONG : Direction.SHORT,
}
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgMarketOrder,
value: MsgMarketOrder.fromPartial(pbMsg),
}
}

static closePosition(msg: MsgClosePosition): TxMessage {
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgClosePosition,
value: MsgClosePosition.fromPartial(msg),
}
}

static partialClosePosition(msg: MsgPartialClose): TxMessage {
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgPartialClose,
value: MsgPartialClose.fromPartial(msg),
}
}

static donateToPerpEF(msg: MsgDonateToEcosystemFund): TxMessage {
return {
typeUrl: PERP_MSG_TYPE_URLS.MsgDonateToEcosystemFund,
value: MsgDonateToEcosystemFund.fromPartial(msg),
}
}
}
Loading
Loading