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:
@@ -1,34 +1,30 @@
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using System.Numerics;
|
||||
using Managing.Application.Abstractions.Repositories;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Evm;
|
||||
using Managing.Domain.Trades;
|
||||
using Managing.Infrastructure.Evm;
|
||||
using Managing.Infrastructure.Evm.Abstractions;
|
||||
using Managing.Infrastructure.Evm.Models.Gmx;
|
||||
using Managing.Infrastructure.Evm.Referentials;
|
||||
using Managing.Infrastructure.Evm.Services;
|
||||
using Managing.Infrastructure.Evm.Services.Gmx;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Nethereum.Contracts;
|
||||
using Nethereum.Contracts.Standards.ERC721.ContractDefinition;
|
||||
using Nethereum.Web3;
|
||||
using System.Numerics;
|
||||
using Xunit;
|
||||
using static Managing.Common.Enums;
|
||||
using Assert = Xunit.Assert;
|
||||
|
||||
namespace Managing.Infrastructure.Tests;
|
||||
|
||||
|
||||
public class EvmManagerTests
|
||||
public class EvmManagerTests
|
||||
{
|
||||
private readonly IEvmManager _manager;
|
||||
private readonly List<Domain.Evm.Chain> _chains;
|
||||
private readonly List<Chain> _chains;
|
||||
|
||||
public List<ISubgraphPrices> Subgraphs;
|
||||
public readonly string PublicAddress = "";
|
||||
|
||||
|
||||
public EvmManagerTests()
|
||||
public EvmManagerTests()
|
||||
{
|
||||
_manager = new EvmManager(Subgraphs);
|
||||
_chains = ChainService.GetChains();
|
||||
@@ -41,6 +37,7 @@ public class EvmManagerTests
|
||||
Assert.IsType<EvmManager>(manager);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
public async void Should_get_address_balance(string address)
|
||||
@@ -51,6 +48,7 @@ public class EvmManagerTests
|
||||
}
|
||||
|
||||
// Connect to nft contract
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData("0x17f4BAa9D35Ee54fFbCb2608e20786473c7aa49f")]
|
||||
public async void Should_return_holder_list_for_nft_collection(string contract)
|
||||
@@ -60,6 +58,7 @@ public class EvmManagerTests
|
||||
Assert.True(holders.Any());
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData("0xa435530d50d7D17Fd9fc6E1c897Dbf7C08E12d35", "0x17f4BAa9D35Ee54fFbCb2608e20786473c7aa49f")]
|
||||
public async void Should_return_event_transfer_nft(string owner, string contract)
|
||||
@@ -70,6 +69,8 @@ public class EvmManagerTests
|
||||
Assert.True(holders.Any());
|
||||
}
|
||||
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Should_return_date_of_block()
|
||||
{
|
||||
@@ -104,7 +105,7 @@ public class EvmManagerTests
|
||||
Assert.False(string.IsNullOrEmpty(keys.Secret));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact]
|
||||
public void Should_return_correct_account_for_mnemo()
|
||||
{
|
||||
var mnemo = "twist enemy flame exchange summer roast beyond friend image pyramid topple need";
|
||||
@@ -116,19 +117,21 @@ public class EvmManagerTests
|
||||
Assert.Equal(publicAddress, address);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
//[InlineData("0x0425dEAb364E9121F7CA284129dA854FD5cF22eD", Constants.Chains.Ethereum)]
|
||||
[InlineData("0x7002AE0Bae7fC67416230F025A32EfE086C0934E", Constants.Chains.Arbitrum)]
|
||||
[InlineData("0x0425dEAb364E9121F7CA284129dA854FD5cF22eD", Constants.Chains.Arbitrum)]
|
||||
// [InlineData("0x7002AE0Bae7fC67416230F025A32EfE086C0934E", Constants.Chains.Arbitrum)]
|
||||
public async void Should_return_balances(string publicAddress, string chainName)
|
||||
{
|
||||
var manager = new EvmManager(Subgraphs);
|
||||
var chain = _chains.First(c => c.Name == chainName);
|
||||
var balances = await manager.GetBalances(chain, 0, 30, publicAddress);
|
||||
var balances = await manager.GetBalances(chain, 0, 500, publicAddress);
|
||||
|
||||
Assert.IsType<List<EvmBalance>>(balances);
|
||||
Assert.NotEmpty(balances);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
//[InlineData("0x7002ae0bae7fc67416230f025a32efe086c0934e", Constants.Chains.Arbitrum)]
|
||||
[InlineData("0xc62F5499789b716Aa94a421A60c76c8c13A31ab6", Constants.Chains.Ethereum)]
|
||||
@@ -142,6 +145,7 @@ public class EvmManagerTests
|
||||
Assert.True(balances.Count > 1);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData("", Constants.Chains.Arbitrum, Ticker.GMX)]
|
||||
public async void Should_return_token_balance(string publicAddress, string chainName, Ticker ticker)
|
||||
@@ -153,6 +157,7 @@ public class EvmManagerTests
|
||||
Assert.True(balance.Balance > 0);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
public async void Should_return_balance_of_ethers(string publicAddress)
|
||||
@@ -164,6 +169,7 @@ public class EvmManagerTests
|
||||
Assert.IsType<EvmBalance>(balance);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
public async void Should_return_all_balance_for_all_chain(string publicAddress)
|
||||
@@ -175,6 +181,7 @@ public class EvmManagerTests
|
||||
Assert.True(balances.Count > 0);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Theory]
|
||||
[InlineData(Ticker.BTC, Timeframe.FiveMinutes)]
|
||||
public async void Get_Prices(Ticker ticker, Timeframe timeframe)
|
||||
@@ -186,10 +193,12 @@ public class EvmManagerTests
|
||||
{
|
||||
candles = await manager.GetCandles(SubgraphProvider.ChainlinkGmx, ticker, DateTime.UtcNow, timeframe);
|
||||
}
|
||||
|
||||
Assert.NotNull(candles);
|
||||
Assert.True(candles.Any());
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Get_Available_Tickers()
|
||||
{
|
||||
@@ -199,6 +208,7 @@ public class EvmManagerTests
|
||||
Assert.NotEmpty(tickers);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void GetLastCandle()
|
||||
{
|
||||
@@ -208,6 +218,7 @@ public class EvmManagerTests
|
||||
Assert.NotNull(candle);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Should_Init_Address_For_Trading()
|
||||
{
|
||||
@@ -217,6 +228,7 @@ public class EvmManagerTests
|
||||
Assert.True(accountInitilized);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Should_send_eth_from_account()
|
||||
{
|
||||
@@ -236,6 +248,7 @@ public class EvmManagerTests
|
||||
Assert.True(sendResult);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Should_send_Gmx_from_account()
|
||||
{
|
||||
@@ -248,132 +261,43 @@ public class EvmManagerTests
|
||||
chain,
|
||||
Ticker.GMX,
|
||||
balance.Balance / 2,
|
||||
PublicAddress,
|
||||
PublicAddress,
|
||||
"",
|
||||
receiverAddress);
|
||||
|
||||
Assert.True(sendResult);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Should_return_indexes_from_gmx()
|
||||
{
|
||||
var chain = ChainService.GetChain(Constants.Chains.Arbitrum);
|
||||
var web3 = new Web3(chain.RpcUrl);
|
||||
var indexes = await GmxService.GetLastIndex(web3, "");
|
||||
|
||||
Assert.IsType<GmxOrderIndexes>(indexes);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
public async void Should_return_gmx_orders(string publicAddress)
|
||||
{
|
||||
var chain = ChainService.GetChain(Constants.Chains.Arbitrum);
|
||||
var web3 = new Web3(chain.RpcUrl);
|
||||
var orders = await GmxService.GetOrders(web3, publicAddress, Ticker.BTC);
|
||||
|
||||
Assert.IsType<List<GmxOrder>>(orders);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Should_return_orders()
|
||||
public async void Should_return_allowance()
|
||||
{
|
||||
var manager = new EvmManager(Subgraphs);
|
||||
var account = GetAccount();
|
||||
var account = PrivateKeys.GetAccount();
|
||||
|
||||
var orders = await manager.GetOrders(account, Ticker.BTC);
|
||||
|
||||
Assert.IsType<List<Trade>>(orders);
|
||||
var allowance = await manager.GetAllowance(account.Key, Ticker.BTC);
|
||||
Assert.IsType<decimal>(allowance);
|
||||
}
|
||||
|
||||
[Ignore]
|
||||
[Fact]
|
||||
public async void Should_cancel_gmx_orders()
|
||||
public async void Should_set_allowance()
|
||||
{
|
||||
var manager = new EvmManager(Subgraphs);
|
||||
var account = GetAccount();
|
||||
var account = PrivateKeys.GetAccount();
|
||||
|
||||
var cancelled = await manager.CancelOrders(account, Ticker.BTC);
|
||||
// Get amount from balance
|
||||
var balance = await manager.GetTokenBalance(Constants.Chains.Arbitrum, Ticker.USDC, account.Key);
|
||||
|
||||
Assert.IsType<bool>(cancelled);
|
||||
}
|
||||
|
||||
private static Account GetAccount()
|
||||
{
|
||||
return new Account
|
||||
{
|
||||
Key = "PublicAddress",
|
||||
Secret = "PrivateKey"
|
||||
};
|
||||
var result = await manager.SetAllowance(account, Ticker.USDC, new BigInteger(balance.Balance));
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_convert_quantity()
|
||||
public async void Should_return_GmxGasPrice()
|
||||
{
|
||||
var quantity = Web3.Convert.ToWei(0.0019);
|
||||
|
||||
Assert.IsType<BigInteger>(quantity);
|
||||
var result =
|
||||
await EvmBase.GetGasPrice(new Web3(_chains.First(c => c.Name == Constants.Chains.Arbitrum).RpcUrl));
|
||||
Assert.True(result > 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Should_approve_order()
|
||||
{
|
||||
var chain = ChainService.GetChain(Constants.Chains.Arbitrum);
|
||||
var web3 = new Web3(chain.RpcUrl);
|
||||
var approval = await GmxService.ApproveOrder(web3, Ticker.BTC, PublicAddress, 0.0003m);
|
||||
|
||||
Assert.IsType<bool>(approval);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Should_check_approved_gmx_plugin()
|
||||
{
|
||||
var chain = ChainService.GetChain(Constants.Chains.Arbitrum);
|
||||
var web3 = new Web3(chain.RpcUrl);
|
||||
var isPluginAdded = await GmxService.IsPluginAdded(web3, "", Arbitrum.Address.OrderBook);
|
||||
|
||||
Assert.IsType<bool>(isPluginAdded);
|
||||
Assert.True(isPluginAdded);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_return_correct_acceptable_price()
|
||||
{
|
||||
var acceptablePrice = GmxHelpers.GetAcceptablePrice(16672.76m, true);
|
||||
var price = new BigInteger(1662274172);
|
||||
var expected = Web3.Convert.ToWei(price, 25);
|
||||
|
||||
Assert.NotNull(acceptablePrice);
|
||||
Assert.IsType<BigInteger>(acceptablePrice);
|
||||
Assert.Equal(expected, acceptablePrice);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Should_return_quantity_in_position()
|
||||
{
|
||||
var manager = new EvmManager(Subgraphs);
|
||||
var quantity = await manager.QuantityInPosition(Constants.Chains.Arbitrum, PublicAddress, Ticker.BTC);
|
||||
|
||||
Assert.NotNull(quantity);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Should_return_Gmx_position()
|
||||
{
|
||||
var chain = ChainService.GetChain(Constants.Chains.Arbitrum);
|
||||
var web3 = new Web3(chain.RpcUrl);
|
||||
var position = await GmxService.GetGmxPosition(web3, "", Ticker.BTC);
|
||||
|
||||
Assert.IsType<GmxPosition>(position);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async void Should_return_Trade()
|
||||
{
|
||||
var chain = ChainService.GetChain(Constants.Chains.Arbitrum);
|
||||
var web3 = new Web3(chain.RpcUrl);
|
||||
var position = await GmxService.GetTrade(web3, "", Ticker.ETH);
|
||||
|
||||
Assert.IsType<Trade>(position);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user