Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions packages/sdk-ts/src/core/modules/exchange/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import MsgBatchCancelSpotOrders from './msgs/MsgBatchCancelSpotOrders.js'
import MsgCancelDerivativeOrder from './msgs/MsgCancelDerivativeOrder.js'
import MsgCreateSpotMarketOrder from './msgs/MsgCreateSpotMarketOrder.js'
import MsgIncreasePositionMargin from './msgs/MsgIncreasePositionMargin.js'
import MsgDecreasePositionMargin from './msgs/MsgDecreasePositionMargin.js'
import MsgInstantSpotMarketLaunch from './msgs/MsgInstantSpotMarketLaunch.js'
import MsgUpdateDerivativeMarketV2 from './msgs/MsgUpdateDerivativeMarketV2.js'
import MsgCancelBinaryOptionsOrder from './msgs/MsgCancelBinaryOptionsOrder.js'
Expand Down Expand Up @@ -46,6 +47,7 @@ export {
MsgBatchCancelSpotOrders,
MsgCreateSpotMarketOrder,
MsgIncreasePositionMargin,
MsgDecreasePositionMargin,
MsgInstantSpotMarketLaunch,
MsgCancelBinaryOptionsOrder,
MsgUpdateDerivativeMarketV2,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import snakecaseKeys from 'snakecase-keys'
import { EIP712Version } from '@injectivelabs/ts-types'
import { mockFactory, prepareEip712 } from '@injectivelabs/utils/test-utils'
import MsgDecreasePositionMargin from './MsgDecreasePositionMargin.js'
import {
getEip712TypedData,
getEip712TypedDataV2,
} from '../../../tx/eip712/eip712.js'
import { IndexerGrpcWeb3GwApi } from './../../../../client/indexer/grpc/IndexerGrpcWeb3GwApi.js'

const params: MsgDecreasePositionMargin['params'] = {
marketId: mockFactory.injUsdtDerivativeMarket.marketId,
injectiveAddress: mockFactory.injectiveAddress,
srcSubaccountId: mockFactory.subaccountId,
dstSubaccountId: mockFactory.subaccountId2,
amount: '1000',
}

const protoType = '/injective.exchange.v1beta1.MsgDecreasePositionMargin'
const protoTypeShort = 'exchange/MsgDecreasePositionMargin'
const protoParams = {
marketId: params.marketId,
sender: params.injectiveAddress,
sourceSubaccountId: params.srcSubaccountId,
destinationSubaccountId: params.dstSubaccountId,
amount: params.amount,
}
const protoParamsAmino = snakecaseKeys(protoParams)
const message = MsgDecreasePositionMargin.fromJSON(params)

describe('MsgDecreasePositionMargin', () => {
it('generates proper proto', () => {
const proto = message.toProto()

expect(proto).toStrictEqual({
...protoParams,
amount: '1000000000000000000000',
})
})

it('generates proper data', () => {
const data = message.toData()

expect(data).toStrictEqual({
'@type': protoType,
...protoParams,
amount: '1000000000000000000000',
})
})

it('generates proper amino', () => {
const amino = message.toAmino()

expect(amino).toStrictEqual({
type: protoTypeShort,
value: protoParamsAmino,
})
})

it('generates proper web3Gw', () => {
const web3 = message.toWeb3Gw()

expect(web3).toStrictEqual({
'@type': protoType,
...protoParamsAmino,
})
})

describe('generates proper EIP712 compared to the Web3Gw (chain)', () => {
const { endpoints, eip712Args, prepareEip712Request } = prepareEip712({
messages: message,
})

// TODO: invalid Go type math.LegacyDec for field injective.exchange.v1beta1.MsgDecreasePositionMargin.amount
it.skip('EIP712 v1', async () => {
const eip712TypedData = getEip712TypedData(eip712Args)

const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({
...prepareEip712Request,
eip712Version: EIP712Version.V1,
})

expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
})

it('EIP712 v2', async () => {
const eip712TypedData = getEip712TypedDataV2(eip712Args)

const txResponse = await new IndexerGrpcWeb3GwApi(
endpoints.indexer,
).prepareEip712Request({
...prepareEip712Request,
eip712Version: EIP712Version.V2,
})

expect(eip712TypedData).toStrictEqual(JSON.parse(txResponse.data))
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { toChainFormat } from '@injectivelabs/utils'
import * as InjectiveExchangeV1Beta1TxPb from '@injectivelabs/core-proto-ts-v2/generated/injective/exchange/v1beta1/tx_pb'
import { MsgBase } from '../../MsgBase.js'
import { numberToCosmosSdkDecString } from '../../../../utils/numbers.js'

export declare namespace MsgDecreasePositionMargin {
export interface Params {
marketId: string
injectiveAddress: string
srcSubaccountId: string
dstSubaccountId: string
amount: string
}

export type Proto = InjectiveExchangeV1Beta1TxPb.MsgDecreasePositionMargin
}

const createMessage = (params: MsgDecreasePositionMargin.Params) => {
const message = InjectiveExchangeV1Beta1TxPb.MsgDecreasePositionMargin.create(
{
sender: params.injectiveAddress,
sourceSubaccountId: params.srcSubaccountId,
destinationSubaccountId: params.dstSubaccountId,
marketId: params.marketId,
amount: params.amount,
},
)

return message
}

/**
* @category Messages
*/
export default class MsgDecreasePositionMargin extends MsgBase<
MsgDecreasePositionMargin.Params,
MsgDecreasePositionMargin.Proto
> {
static fromJSON(
params: MsgDecreasePositionMargin.Params,
): MsgDecreasePositionMargin {
return new MsgDecreasePositionMargin(params)
}

public toProto() {
const { params: initialParams } = this

const params = {
...initialParams,
amount: toChainFormat(initialParams.amount).toFixed(),
} as MsgDecreasePositionMargin.Params

return createMessage(params)
}

public toData() {
const proto = this.toProto()

return {
'@type': '/injective.exchange.v1beta1.MsgDecreasePositionMargin',
...proto,
}
}

public toAmino() {
const { params } = this
const message = {
sender: params.injectiveAddress,
source_subaccount_id: params.srcSubaccountId,
destination_subaccount_id: params.dstSubaccountId,
market_id: params.marketId,
amount: params.amount,
}

return {
type: 'exchange/MsgDecreasePositionMargin',
value: message,
}
}

public toWeb3Gw() {
const amino = this.toAmino()
const { value } = amino

return {
'@type': '/injective.exchange.v1beta1.MsgDecreasePositionMargin',
...value,
}
}

public toEip712() {
const amino = this.toAmino()
const { type, value } = amino

const messageAdjusted = {
...value,
amount: toChainFormat(value.amount).toFixed(),
}

return {
type,
value: messageAdjusted,
}
}

public toEip712V2() {
const { params } = this
const web3gw = this.toWeb3Gw()

const messageAdjusted = {
...web3gw,
amount: numberToCosmosSdkDecString(params.amount),
}

return messageAdjusted
}

public toDirectSign() {
const proto = this.toProto()

return {
type: '/injective.exchange.v1beta1.MsgDecreasePositionMargin',
message: proto,
}
}

public toBinary(): Uint8Array {
return InjectiveExchangeV1Beta1TxPb.MsgDecreasePositionMargin.toBinary(
this.toProto(),
)
}
}
2 changes: 2 additions & 0 deletions packages/sdk-ts/src/core/modules/msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import type MsgBatchCancelSpotOrders from './exchange/msgs/MsgBatchCancelSpotOrd
import type MsgCancelDerivativeOrder from './exchange/msgs/MsgCancelDerivativeOrder.js'
import type MsgCreateSpotMarketOrder from './exchange/msgs/MsgCreateSpotMarketOrder.js'
import type MsgIncreasePositionMargin from './exchange/msgs/MsgIncreasePositionMargin.js'
import type MsgDecreasePositionMargin from './exchange/msgs/MsgDecreasePositionMargin.js'
import type MsgInstantSpotMarketLaunch from './exchange/msgs/MsgInstantSpotMarketLaunch.js'
import type MsgCancelBinaryOptionsOrder from './exchange/msgs/MsgCancelBinaryOptionsOrder.js'
import type MsgUpdateDerivativeMarketV2 from './exchange/msgs/MsgUpdateDerivativeMarketV2.js'
Expand Down Expand Up @@ -107,6 +108,7 @@ export type ExchangeV1Msgs =
| MsgBatchCancelSpotOrders
| MsgCancelDerivativeOrder
| MsgCreateSpotMarketOrder
| MsgDecreasePositionMargin
| MsgIncreasePositionMargin
| MsgInstantSpotMarketLaunch
| MsgCancelBinaryOptionsOrder
Expand Down
16 changes: 16 additions & 0 deletions packages/sdk-ts/src/core/tx/eip712/MsgDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getInjectiveAddress } from '../../../utils/index.js'
import { toUtf8, uint8ArrayToHex } from '../../../utils/encoding.js'
import {
MsgSignData,
MsgDecreasePositionMargin,
MsgIncreasePositionMargin,
} from '../../modules/exchange/index.js'
import type * as GoogleProtobufAnyPb from '@injectivelabs/core-proto-ts-v2/generated/google/protobuf/any_pb'
Expand All @@ -28,6 +29,21 @@ export class MsgDecoder {
})
}

case type.includes('MsgDecreasePositionMargin'): {
const msg =
InjectiveExchangeV1Beta1TxPb.MsgDecreasePositionMargin.fromBinary(
message.value,
)

return MsgDecreasePositionMargin.fromJSON({
marketId: msg.marketId,
injectiveAddress: msg.sender,
srcSubaccountId: msg.sourceSubaccountId,
dstSubaccountId: msg.destinationSubaccountId,
amount: msg.amount,
})
}

case type.includes('MsgSignData'): {
const msg = InjectiveExchangeV1Beta1TxPb.MsgSignData.fromBinary(
message.value,
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk-ts/src/core/tx/eip712/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ export const protoTypeToAminoType = (type: string): string => {
return 'exchange/MsgExternalTransfer'
case 'injective.exchange.v1beta1.MsgIncreasePositionMargin':
return 'exchange/MsgIncreasePositionMargin'
case 'injective.exchange.v1beta1.MsgDecreasePositionMargin':
return 'exchange/MsgDecreasePositionMargin'
case 'injective.exchange.v1beta1.MsgLiquidatePosition':
return 'exchange/MsgLiquidatePosition'
case 'injective.exchange.v1beta1.MsgBatchUpdateOrders':
Expand Down
1 change: 1 addition & 0 deletions packages/utils/tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineConfig({
outDir: 'dist',
external: [
// External workspace dependencies
'bignumber.js',
'@injectivelabs/exceptions',
'@injectivelabs/networks',
'@injectivelabs/ts-types',
Expand Down
1 change: 1 addition & 0 deletions scripts/eslint-rules/no-sdk-ts-barrel.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const SUBPATH_MAPPINGS = {
MsgBatchCancelDerivativeOrders: '/core/modules',
MsgBatchUpdateOrders: '/core/modules',
MsgIncreasePositionMargin: '/core/modules',
MsgDecreasePositionMargin: '/core/modules',
MsgLiquidatePosition: '/core/modules',
MsgSubaccountTransfer: '/core/modules',
MsgExternalTransfer: '/core/modules',
Expand Down