-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CardanoSharp.Koios.SDK integration for important query and transactio…
…n commands New commands via Koios integration: - query tip --network <network> - query protocol-parameters --network <network> - query info account --network <network>--stake-address <bech32_stake_address> - query asset account --network <network> --stake-address <bech32_stake_address> - query info address --network <network> --address <bech32_payment_address> - query info transaction --network <network> --tx-id <transaction_hash> - transaction submit --network <network> --cbor-hex <hex_string>
- Loading branch information
1 parent
aecfa61
commit 7625b2e
Showing
47 changed files
with
1,520 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using CardanoSharp.Wallet.Enums; | ||
using Refit; | ||
|
||
namespace Cscli.ConsoleTool.Koios | ||
{ | ||
public static class BackendGateway | ||
{ | ||
public static T GetBackendClient<T>(NetworkType networkType) => | ||
RestService.For<T>(GetBaseUrlForNetwork(networkType)); | ||
|
||
private static string GetBaseUrlForNetwork(NetworkType networkType) => networkType switch | ||
{ | ||
NetworkType.Mainnet => "https://api.koios.rest/api/v0", | ||
NetworkType.Testnet => "https://testnet.koios.rest/api/v0", | ||
_ => throw new ArgumentException($"{nameof(networkType)} {networkType} is invalid", nameof(networkType)) | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nsoleTool/Commands/DecodeBech32Command.cs → ...ConsoleTool/Crypto/DecodeBech32Command.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...nsoleTool/Commands/EncodeBech32Command.cs → ...ConsoleTool/Crypto/EncodeBech32Command.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Cscli.ConsoleTool; | ||
|
||
public record struct Utxo(string TxHash, int OutputIndex, TokenBundle TokenBundle); | ||
|
||
public record struct TokenBundle(long LovelaceValue, NativeAssetValue[] NativeAssets); | ||
|
||
public record struct NativeAssetValue(string PolicyId, string AssetNameHex, long Quantity); | ||
|
||
public record WalletInfo(AccountInfo[] Accounts); | ||
|
||
public record AccountInfo(string StakeAddress, string PaymentAddress, Utxo[] Utxos); | ||
|
||
public record AddressInfo(string PaymentAddress, string? StakeAddress, Utxo[] Utxos); | ||
|
||
public record TextEnvelope(string? Type, string? Description, string? CborHex); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"Cscli.ConsoleTool": { | ||
"commandName": "Project", | ||
"commandLineArgs": "bech32 encode --value 00e3f81c986990cfd80283944858ae50c2d82f6a79d330ce7014e0b7fd9df9179beb0ce89f84025e02ae11c18b3003e7690149caa662fafd01 --prefix addr_test" | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using CardanoSharp.Koios.Sdk; | ||
using CardanoSharp.Wallet; | ||
using CardanoSharp.Wallet.Encoding; | ||
using CardanoSharp.Wallet.Enums; | ||
using CardanoSharp.Wallet.Models.Addresses; | ||
using Cscli.ConsoleTool.Koios; | ||
using System.Text.Json; | ||
using static Cscli.ConsoleTool.Constants; | ||
|
||
namespace Cscli.ConsoleTool.Query; | ||
|
||
public class QueryAccountAssetCommand : ICommand | ||
{ | ||
public string StakeAddress { get; init; } = string.Empty; | ||
public string Address { get; init; } = string.Empty; | ||
public string? Network { get; init; } | ||
|
||
public async ValueTask<CommandResult> ExecuteAsync(CancellationToken ct) | ||
{ | ||
var (isValid, networkType, stakeAddress, errors) = Validate(); | ||
if (!isValid || stakeAddress == null) | ||
{ | ||
return CommandResult.FailureInvalidOptions( | ||
string.Join(Environment.NewLine, errors)); | ||
} | ||
|
||
var accountClient = BackendGateway.GetBackendClient<IAccountClient>(networkType); | ||
try | ||
{ | ||
var assets = await accountClient.GetStakeAssets(stakeAddress.ToString()).ConfigureAwait(false); | ||
var json = JsonSerializer.Serialize(assets, SerialiserOptions); | ||
return CommandResult.Success(json); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return CommandResult.FailureUnhandledException("Unexpected error", ex); | ||
} | ||
} | ||
|
||
private ( | ||
bool isValid, | ||
NetworkType derivedNetworkType, | ||
Address? derivedStakeAddress, | ||
IReadOnlyCollection<string> validationErrors) Validate() | ||
{ | ||
var validationErrors = new List<string>(); | ||
if (!Enum.TryParse<NetworkType>(Network, ignoreCase: true, out var networkType)) | ||
{ | ||
validationErrors.Add( | ||
$"Invalid option --network must be either testnet or mainnet"); | ||
} | ||
var stakeAddressArgumentExists = !string.IsNullOrWhiteSpace(StakeAddress); | ||
var paymentAddressArgumentExists = !string.IsNullOrWhiteSpace(Address); | ||
if (!stakeAddressArgumentExists && !paymentAddressArgumentExists | ||
|| stakeAddressArgumentExists && paymentAddressArgumentExists) | ||
{ | ||
validationErrors.Add( | ||
"Invalid option, one of either --stake-address or --address is required"); | ||
return (!validationErrors.Any(), networkType, null, validationErrors); | ||
} | ||
if (stakeAddressArgumentExists) | ||
{ | ||
if (!Bech32.IsValid(StakeAddress)) | ||
{ | ||
validationErrors.Add( | ||
$"Invalid option --stake-address {StakeAddress} is invalid"); | ||
} | ||
else | ||
{ | ||
var stakeAddress = new Address(StakeAddress); | ||
if (stakeAddress.AddressType != AddressType.Reward || stakeAddress.NetworkType != networkType) | ||
{ | ||
validationErrors.Add( | ||
$"Invalid option --stake-address {StakeAddress} is invalid for {Network}"); | ||
} | ||
else | ||
{ | ||
return (!validationErrors.Any(), networkType, stakeAddress, validationErrors); | ||
} | ||
} | ||
} | ||
if (paymentAddressArgumentExists) | ||
{ | ||
if (!Bech32.IsValid(Address)) | ||
{ | ||
validationErrors.Add( | ||
$"Invalid option --address {Address} is not a base address with attached staking credentials"); | ||
} | ||
else | ||
{ | ||
var addressService = new AddressService(); | ||
var address = new Address(Address); | ||
if (address.AddressType != AddressType.Base || address.NetworkType != networkType) | ||
{ | ||
validationErrors.Add( | ||
$"Invalid option --address {Address} is not a base address with attached staking credentials for {Network}"); | ||
} | ||
else | ||
{ | ||
var stakeAddress = addressService.ExtractRewardAddress(address); | ||
return (!validationErrors.Any(), networkType, stakeAddress, validationErrors); | ||
} | ||
} | ||
} | ||
return (!validationErrors.Any(), networkType, null, validationErrors); | ||
} | ||
} |
Oops, something went wrong.