// filename: Transactions.ts
import { toChainFormat } from '@injectivelabs/utils'
import {
MsgSend,
MsgCreateSpotLimitOrder,
MsgCreateDerivativeMarketOrder,
} from '@injectivelabs/sdk-ts/core/modules'
import {
spotPriceToChainPriceToFixed,
spotQuantityToChainQuantityToFixed,
derivativePriceToChainPriceToFixed,
derivativeQuantityToChainQuantityToFixed,
derivativeMarginToChainMarginToFixed,
getDefaultSubaccountId
} from '@injectivelabs/sdk-ts/utils'
// used to send assets from one address to another
export const makeMsgSend = ({
sender,
recipient,
amount,
denom
}: {
sender: string,
recipient: string,
amount: string, // human readable amount
denom: string
}) => {
const amount = {
denom,
amount: toChainFormat(amount, /** denom's decimals */).toFixed()
}
return MsgSend.fromJSON({
amount,
srcInjectiveAddress: sender,
dstInjectiveAddress: recipient,
})
}
// used to create a spot limit order
export const makeMsgCreateSpotLimitOrder = ({
price, // human readable number
quantity, // human readable number
orderType, // OrderType enum
injectiveAddress,
}) => {
const subaccountId = getDefaultSubaccountId(injectiveAddress)
const market = {
marketId: '0x...',
baseDecimals: 18,
quoteDecimals: 6,
minPriceTickSize: '', /* fetched from the chain */
minQuantityTickSize: '', /* fetched from the chain */
priceTensMultiplier: '', /** can be fetched from getSpotMarketTensMultiplier */
quantityTensMultiplier: '', /** can be fetched from getSpotMarketTensMultiplier */
}
return MsgCreateSpotLimitOrder.fromJSON({
subaccountId,
injectiveAddress,
orderType: orderType,
price: spotPriceToChainPriceToFixed({
value: price,
tensMultiplier: market.priceTensMultiplier,
baseDecimals: market.baseDecimals,
quoteDecimals: market.quoteDecimals
}),
quantity: spotQuantityToChainQuantityToFixed({
value: quantity,
tensMultiplier: market.quantityTensMultiplier,
baseDecimals: market.baseDecimals
}),
marketId: market.marketId,
feeRecipient: injectiveAddress,
})
}
// used to create a derivative market order
export const makeMsgCreateDerivativeMarketOrder = ({
price, // human readable number
margin, // human readable number
quantity, // human readable number
orderType, // OrderType enum
injectiveAddress,
}) => {
const subaccountId = getDefaultSubaccountId(injectiveAddress)
const market = {
marketId: '0x...',
baseDecimals: 18,
quoteDecimals: 6,
minPriceTickSize: '', /* fetched from the chain */
minQuantityTickSize: '', /* fetched from the chain */
priceTensMultiplier: '', /** can be fetched from getDerivativeMarketTensMultiplier */
quantityTensMultiplier: '', /** can be fetched from getDerivativeMarketTensMultiplier */
}
return MsgCreateDerivativeMarketOrder.fromJSON({
orderType: orderType,
triggerPrice: '0',
injectiveAddress,
price: derivativePriceToChainPriceToFixed({
value: price,
tensMultiplier: market.priceTensMultiplier,
quoteDecimals: market.quoteDecimals
}),
quantity: derivativeQuantityToChainQuantityToFixed({
value: quantity,
tensMultiplier: market.quantityTensMultiplier,
}),
margin: derivativeMarginToChainMarginToFixed({
value: margin,
quoteDecimals: market.quoteDecimals,
tensMultiplier: market.priceTensMultiplier,
}),
marketId: market.marketId,
feeRecipient: injectiveAddress,
subaccountId: subaccountId
})
}