Add Privy type wallet

This commit is contained in:
2025-03-05 10:36:54 +07:00
parent 30bf9d26f6
commit 988cc9eb61
21 changed files with 287 additions and 53 deletions

View File

@@ -793,6 +793,7 @@ public class GmxV2Service
var exchangeRouterService = new ExchangeRouterService(web3, Arbitrum.AddressV2.ExchangeRouter);
var receipt = await exchangeRouterService.MulticallRequestAndWaitForReceiptAsync(multiCallFunction);
// Call privy api instead and send the txn
var trade = new Trade(DateTime.UtcNow,
direction,
Enums.TradeStatus.Requested,

View File

@@ -0,0 +1,85 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using Managing.Infrastructure.Evm.Abstractions;
using Managing.Infrastructure.Evm.Models;
public class PrivyService : IPrivyService
{
private readonly HttpClient _privyClient;
private readonly string _appId;
private readonly string _appSecret;
public PrivyService(IPrivySettings settings)
{
_privyClient = new HttpClient();
_appId = settings.AppId;
_appSecret = settings.AppSecret;
ConfigureHttpClient();
}
private void ConfigureHttpClient()
{
_privyClient.BaseAddress = new Uri("https://api.privy.io/");
var authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_appId}:{_appSecret}"));
// _privyClient.DefaultRequestHeaders.Authorization =
// new AuthenticationHeaderValue("Basic", $"{_appId}:{_appSecret}");
_privyClient.DefaultRequestHeaders.Add("privy-app-id", _appId);
// add custom header
_privyClient.DefaultRequestHeaders.Add("Authorization", authToken);
_privyClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<PrivyWallet> CreateWalletAsync(string chainType = "ethereum")
{
try
{
var json = JsonSerializer.Serialize(new { chain_type = chainType });
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _privyClient.PostAsJsonAsync("/v1/wallets", content);
var result = new PrivyWallet();
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadFromJsonAsync<PrivyWallet>();
}
else
{
throw new Exception(await response.Content.ReadAsStringAsync());
}
return result;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public async Task<HttpResponseMessage> SendTransactionAsync(string walletId, string recipientAddress, long value,
string caip2 = "eip155:84532")
{
var requestBody = new
{
method = "eth_sendTransaction",
caip2,
@params = new
{
transaction = new
{
to = recipientAddress,
value
}
}
};
return await _privyClient.PostAsJsonAsync(
$"/v1/wallets/{walletId}/rpc",
requestBody
);
}
}