Skip to content

Commit

Permalink
Merge pull request #13 from DennisPitallano/feature/#6
Browse files Browse the repository at this point in the history
Add Get 'Internal Transactions' by Transaction Hash
  • Loading branch information
DennisPitallano authored Feb 15, 2022
2 parents 41702f8 + 6f332fa commit dfff512
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 57 deletions.
30 changes: 8 additions & 22 deletions src/BscScan.NetCore/Constants/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
namespace BscScan.NetCore.Constants
{
internal static class DefaultAccountApiEndPoint
{
/// <summary>
/// Get BNB Balance for a Single Address
/// </summary>
public const string GET_BALANCE_FOR_SINGLE_ADDRESS =
"/api?module=account&action=balance&address={address}&apikey={apiKey}";

public const string GET_BALANCE_FOR_MULTIPLE_ADDRESSES =
"/api?module=account&action=balancemulti&address={address}&tag=latest&apikey={apiKey}";

public const string GET_A_LIST_OF_NORMAL_TRANSACTIONS_BY_ADDRESS =
"/api?module=account&action=txlist&address={address}&startblock={startblock}&endblock={endblock}" +
"&page={page}&offset={offset}&sort={sortOrder}&apikey={apiKey}";

public const string BASE_QUERY =
"/api?apikey={apiKey}";
}

internal static class BscModule
{
public const string ACCOUNT = "/api?module=account&apikey={apiKey}&";
public const string CONTRACT = "/api?module=contract&apikey={apiKey}&";
public const string TRANSACTIONS = "/api?module=transaction&apikey={apiKey}&";
public const string ACCOUNT = "?module=account&apikey={apiKey}&";
public const string CONTRACT = "?module=contract&apikey={apiKey}&";
public const string TRANSACTIONS = "?module=transaction&apikey={apiKey}&";
}

internal static class AccountModuleAction
Expand All @@ -33,4 +14,9 @@ internal static class AccountModuleAction
public const string TRANSACTION_LIST = "txlist";
public const string TRANSACTION_LIST_INTERNAL = "txlistinternal";
}

internal static class BscQueryParam
{
public const string TxHash = "txhash={value}";
}
}
8 changes: 7 additions & 1 deletion src/BscScan.NetCore/Contracts/IBscScanAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public interface IBscScanAccountService
/// </summary>
/// <param name="request">InternalTransactionRequest Model</param>
/// <returns>Returns the list of internal transactions performed by an address, with optional pagination.</returns>
Task<InternalTransactions?> GetInternalTransactionsByAddressAsync(InternalTransactionRequest request);
Task<InternalTransactionsByAddress?> GetInternalTransactionsByAddressAsync(InternalTransactionRequest request);

/// <summary>
/// Get 'Internal Transactions' by Transaction Hash
/// </summary>
/// <param name="txHash">the string representing the transaction hash to check for internal transactions</param>
/// <returns>Returns the list of internal transactions performed within a transaction.</returns>
Task<InternalTransactionsByHash?> GetInternalTransactionsByTransactionHashAsync(string txHash);
}
}
20 changes: 18 additions & 2 deletions src/BscScan.NetCore/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ public static string AppendApiKey(this string value, string? apiKey)
return value.Replace("{apiKey}", apiKey);
}

public static string AddAction(this string url,string? action)
public static string AppendValue(this string query, string? value)
{
return $"{url}&action={action}";
return query.Replace("{value}", value);
}

public static string AddQuery(this string query, string key, string value)
{
return query.EndsWith('&') ? $"{query}{key}={value}" : $"{query}&{key}={value}";
}

public static string AddQuery(this string query, string parameter)
{
return query.EndsWith('&') ? $"{query}{parameter}" : $"{query}&{parameter}";
}


public static string AddAction(this string query, string? action)
{
return query.EndsWith('&') ? $"{query}action={action}" : $"{query}&action={action}";
}
}
}
6 changes: 1 addition & 5 deletions src/BscScan.NetCore/Models/Response/Account/BnbBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace BscScan.NetCore.Models.Response.Account;

public class BnbBalance
public class BnbBalance : BaseResponse
{
[JsonPropertyName("status")]
public string? Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
[JsonPropertyName("result")]
public string? Result { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace BscScan.NetCore.Models.Response.Account;

public class BnbMultipleBalances
public class BnbMultipleBalances : BaseResponse
{

[JsonPropertyName("status")]
public string? Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
[JsonPropertyName("result")]
public List<BnbMultipleBalanceData>? Result { get; set; }
}
Expand Down
31 changes: 21 additions & 10 deletions src/BscScan.NetCore/Models/Response/Account/InternalTransactions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace BscScan.NetCore.Models.Response.Account
{
public class InternalTransactions

public class InternalTransactionsByAddress: BaseResponse
{
[JsonPropertyName("result")]
public List<InternalTransactionDataByAddress>? Result { get; set; }
}

public class InternalTransactionsByHash : BaseResponse
{
[JsonPropertyName("status")]
public string? Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
[JsonPropertyName("result")]
public List<InternalTransactionData>? Result { get; set; }
public List<InternalTransactionDataByHash>? Result { get; set; }
}

public class InternalTransactionData
Expand All @@ -18,8 +21,6 @@ public class InternalTransactionData
public string? BlockNumber { get; set; }
[JsonPropertyName("timeStamp")]
public string? TimesStamp { get; set; }
[JsonPropertyName("hash")]
public string? Hash { get; set; }
[JsonPropertyName("from")]
public string? From { get; set; }
[JsonPropertyName("to")]
Expand All @@ -36,11 +37,21 @@ public class InternalTransactionData
public string? Gas { get; set; }
[JsonPropertyName("gasUsed")]
public string? GasUsed { get; set; }
[JsonPropertyName("traceId")]
public string? TraceId { get; set; }
[JsonPropertyName("isError")]
public string? IsError { get; set; }
[JsonPropertyName("errCode")]
public string? ErrCode { get; set; }
}

public class InternalTransactionDataByAddress : InternalTransactionData
{
[JsonPropertyName("hash")]
public string? Hash { get; set; }
[JsonPropertyName("traceId")]
public string? TraceId { get; set; }
}

public class InternalTransactionDataByHash : InternalTransactionData
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace BscScan.NetCore.Models.Response.Account
{
public class NormalTransactions
public class NormalTransactions : BaseResponse
{
[JsonPropertyName("status")]
public string? Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
[JsonPropertyName("result")]
public List<NormalTransactionData>? Result { get; set; }
}
Expand Down
12 changes: 12 additions & 0 deletions src/BscScan.NetCore/Models/Response/BaseResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace BscScan.NetCore.Models.Response
{
public class BaseResponse
{
[JsonPropertyName("status")]
public string? Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
}
}
17 changes: 15 additions & 2 deletions src/BscScan.NetCore/Services/BscScanAccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,28 @@ public BscScanAccountService(HttpClient bscScanHttpClient, BscScanConfiguration
return result;
}

public async Task<InternalTransactions?> GetInternalTransactionsByAddressAsync(InternalTransactionRequest request)
public async Task<InternalTransactionsByAddress?> GetInternalTransactionsByAddressAsync(InternalTransactionRequest request)
{
var queryParameters = $"{_bscScanModule}{request.ToRequestParameters(AccountModuleAction.TRANSACTION_LIST_INTERNAL)}";
using var response = await BscScanHttpClient.GetAsync($"{queryParameters}")
.ConfigureAwait(false);

response.EnsureSuccessStatusCode();
await using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
var result = await JsonSerializer.DeserializeAsync<InternalTransactions>(responseStream);
var result = await JsonSerializer.DeserializeAsync<InternalTransactionsByAddress>(responseStream);
return result;
}

public async Task<InternalTransactionsByHash?> GetInternalTransactionsByTransactionHashAsync(string txHash)
{
var queryParameters = $"{_bscScanModule}".AddAction(AccountModuleAction.TRANSACTION_LIST_INTERNAL)
.AddQuery(BscQueryParam.TxHash.AppendValue(txHash));
using var response = await BscScanHttpClient.GetAsync($"{queryParameters}")
.ConfigureAwait(false);

response.EnsureSuccessStatusCode();
await using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
var result = await JsonSerializer.DeserializeAsync<InternalTransactionsByHash>(responseStream);
return result;
}
}
9 changes: 5 additions & 4 deletions src/BscScanMvc/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ public async Task<IActionResult> Index()
// Address = "0xF426a8d0A94bf039A35CEE66dBf0227A7a12D11e"
//});

var txtIn = await _bscScanService.GetInternalTransactionsByAddressAsync(new InternalTransactionRequest
{
Address = "0x0000000000000000000000000000000000001004"
});
//var txtIn = await _bscScanService.GetInternalTransactionsByAddressAsync(new InternalTransactionRequest
//{
// Address = "0x0000000000000000000000000000000000001004"
//});
var txtIn = await _bscScanService.GetInternalTransactionsByTransactionHashAsync("0x4d74a6fc84d57f18b8e1dfa07ee517c4feb296d16a8353ee41adc03669982028");
}
catch (HttpRequestException e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/BscScanMvc/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
},
"BscScanOptions": {
"Token": "ZXD3CX98EJ2C5MIDYFPQJB6SKQ1BVJY1D934",
"Uri": "https://api.bscscan.com"
"Uri": "https://api.bscscan.com/api"
}
}

0 comments on commit dfff512

Please sign in to comment.