GMX v2 - Trading (#7)
* Move PrivateKeys.cs * Update gitignore * Update gitignore * updt * Extract GmxServiceTests.cs * Refact * update todo * Update code * Fix hashdata * Replace static token hashed datas * Set allowance * Add get orders * Add get orders tests * Add ignore * add close orders * revert * Add get gas limit * Start increasePosition. Todo: Finish GetExecutionFee and estimateGas * little refact * Update gitignore * Fix namespaces and clean repo * Add tests samples * Add execution fee * Add increase position * Handle backtest on the frontend * Add tests * Update increase * Test increase * fix increase * Fix size * Start get position * Update get positions * Fix get position * Update rpc and trade mappers * Finish close position * Fix leverage
This commit is contained in:
107
src/Managing.Infrastructure.Web3/Services/Gmx/GmxV2Mappers.cs
Normal file
107
src/Managing.Infrastructure.Web3/Services/Gmx/GmxV2Mappers.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System.Numerics;
|
||||
using Managing.ABI.GmxV2.SyntheticsReader.ContractDefinition;
|
||||
using Managing.Common;
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Infrastructure.Evm.Models.Gmx.v2;
|
||||
using Nethereum.Web3;
|
||||
|
||||
namespace Managing.Infrastructure.Evm.Services.Gmx;
|
||||
|
||||
internal static class GmxV2Mappers
|
||||
{
|
||||
public static Trade Map(GmxV2Position position, Enums.Ticker ticker)
|
||||
{
|
||||
var entryPrice = GmxV2Helpers.GetEntryPrice(position.SizeInUsd, position.SizeInTokens,
|
||||
position.TokenData.Decimals);
|
||||
var parsedEntryPrice = GmxHelpers.FormatAmount(entryPrice, Constants.GMX.Config.Decimals.USD - 2, 4);
|
||||
|
||||
var collateralLeverage = CalculateCollateralAndLeverage(position.SizeInUsd, position.CollateralAmount);
|
||||
var trade = new Trade(
|
||||
DateHelpers.GetFromUnixTimestamp((int)position.IncreasedAtTime),
|
||||
position.IsLong ? Enums.TradeDirection.Long : Enums.TradeDirection.Short,
|
||||
Enums.TradeStatus.Filled,
|
||||
Enums.TradeType.Limit,
|
||||
ticker,
|
||||
collateralLeverage.collateral / parsedEntryPrice,
|
||||
parsedEntryPrice,
|
||||
collateralLeverage.leverage,
|
||||
position.Key,
|
||||
""
|
||||
);
|
||||
|
||||
return trade;
|
||||
}
|
||||
|
||||
public static List<Trade> Map(List<GmxV2Order> orders)
|
||||
{
|
||||
var trades = new List<Trade>();
|
||||
|
||||
foreach (var order in orders)
|
||||
{
|
||||
var shortToken = TokenV2Service.GetTokenByAddress(order.InitialCollateralTokenAddress);
|
||||
var ticker = GmxV2Helpers.GetTicker(order.MarketAddress);
|
||||
var indexToken = TokenV2Service.GetByTicker(ticker);
|
||||
var sizeDelta = GmxHelpers.FormatAmount(order.SizeDeltaUsd, Constants.GMX.Config.Decimals.USD, 4);
|
||||
var triggerPrice = GmxV2Helpers.ParseContractPrice(order.ContractTriggerPrice, indexToken.Decimals, true);
|
||||
var quantity = sizeDelta / triggerPrice;
|
||||
var initialCollateral =
|
||||
GmxV2Helpers.ParseContractPrice(order.InitialCollateralDeltaAmount, shortToken.Decimals);
|
||||
var leverage = sizeDelta / initialCollateral;
|
||||
|
||||
var trade = new Trade(
|
||||
order.Date,
|
||||
order.IsLong ? Enums.TradeDirection.Long : Enums.TradeDirection.Short,
|
||||
Enums.TradeStatus.PendingOpen,
|
||||
GmxV2Helpers.GetTradeType(order.OrderType),
|
||||
GmxV2Helpers.GetTicker(order.MarketAddress),
|
||||
Convert.ToDecimal(quantity),
|
||||
Convert.ToDecimal(triggerPrice),
|
||||
leverage,
|
||||
"",
|
||||
""
|
||||
);
|
||||
|
||||
trades.Add(trade);
|
||||
}
|
||||
|
||||
return trades;
|
||||
}
|
||||
//
|
||||
// public static GmxV2Position MapPosition(GmxV2Service.PositionInfoDTO positionInfo)
|
||||
// {
|
||||
// return new GmxV2Position
|
||||
// {
|
||||
// PositionKey = GetPositionKeyV2(
|
||||
// positionInfo.Position.Account,
|
||||
// positionInfo.Position.Market,
|
||||
// positionInfo.Position.CollateralToken,
|
||||
// positionInfo.Position.IsLong
|
||||
// ),
|
||||
// SizeInUsd = positionInfo.Position.SizeInUsd,
|
||||
// CollateralAmount = positionInfo.Position.CollateralAmount,
|
||||
// IncreasedAtTime = positionInfo.Position.IncreasedAtTime,
|
||||
// IsLong = positionInfo.Position.IsLong,
|
||||
// Leverage = CalculateLeverage(positionInfo.Position.SizeInUsd, positionInfo.Position.CollateralAmount)
|
||||
// };
|
||||
// }
|
||||
|
||||
private static (decimal collateral, decimal leverage) CalculateCollateralAndLeverage(BigInteger sizeInUsd,
|
||||
BigInteger collateralAmount)
|
||||
{
|
||||
if (collateralAmount == 0) return (0m, 0m);
|
||||
var size = Web3.Convert.FromWei(sizeInUsd, 30);
|
||||
var collateral = Web3.Convert.FromWei(collateralAmount, 6); // USDC decimals
|
||||
return (collateral, Math.Round(size / collateral));
|
||||
}
|
||||
|
||||
public static List<MarketPrices> Map(List<ABI.GmxV2.Reader.ContractDefinition.MarketPrices> marketPrices)
|
||||
{
|
||||
return marketPrices.Select(mp => new MarketPrices
|
||||
{
|
||||
IndexTokenPrice = mp.IndexTokenPrice,
|
||||
LongTokenPrice = mp.LongTokenPrice,
|
||||
ShortTokenPrice = mp.ShortTokenPrice
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user