// filename: Transactions.ts
import { BigNumberInWei } from '@injectivelabs/utils'
import {
MsgSend,
MsgCreateSpotLimitOrder,
spotPriceToChainPriceToFixed,
MsgCreateDerivativeMarketOrder,
spotQuantityToChainQuantityToFixed
} from '@injectivelabs/sdk-ts'
// used to send assets from one address to another
export const makeMsgSend = ({
sender: string,
recipient: string,
amount: string, // human readable amount
denom: string
}) => {
const amount = {
denom,
amount: new BigNumberInBase(amount).toWei(/** denom's decimals */)
}
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 getDerivativeMarketTensMultiplier */
quantityTensMultiplier: '', /** can be fetched from getDerivativeMarketTensMultiplier */
}
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: orderPrice,
triggerPrice: '0',
injectiveAddress,
price: derivativePriceToChainPriceToFixed({
value: order.price,
tensMultiplier: market.priceTensMultiplier,
quoteDecimals: market.quoteDecimals
}),
quantity: derivativeQuantityToChainQuantityToFixed({
value: order.quantity,
tensMultiplier: market.quantityTensMultiplier,
}),
margin: derivativeMarginToChainMarginToFixed({
value: order.margin,
quoteDecimals: market.quoteDecimals,
tensMultiplier: priceTensMultiplier,
}),
marketId: market.marketId,
feeRecipient: feeRecipient,
subaccountId: subaccountI
})
}