Skip to content

Commit

Permalink
feat: implement closeAccount and getAccountInfo methods (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman authored Dec 20, 2022
1 parent 81fd642 commit e0829b0
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 72 deletions.
32 changes: 32 additions & 0 deletions Runtime/Helpers/ValidateKineticSdkConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Kinetic.Sdk.Interfaces;

// ReSharper disable once CheckNamespace

namespace Kinetic.Sdk.Transactions
{
public static class ValidateKineticSdkConfig
{
public static KineticSdkConfig Validate(
KineticSdkConfig config
)
{
// Throw error if endpoint does not start with http
if (!config.Endpoint.StartsWith("http"))
{
throw new ArgumentException("validateKineticSdkConfig: the endpoint should start with http or https.");
}

var commitment = config.Commitment ?? Commitment.Confirmed;
var endpoint = config.Endpoint.EndsWith("/") ? config.Endpoint.Substring(0, config.Endpoint.Length - 1) : config.Endpoint;

return new KineticSdkConfig(
endpoint,
config.Environment,
config.Index,
config.Logger,
commitment,
config.SolanaRpcEndpoint);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Helpers/ValidateKineticSdkConfig.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions Runtime/Interfaces/KineticSdkConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,33 @@

// ReSharper disable once CheckNamespace


namespace Kinetic.Sdk.Interfaces
{
public class KineticSdkConfig
{
public Kinetic.Sdk.Interfaces.Commitment? Commitment;
public readonly string Endpoint;
public readonly string Environment;
public readonly int Index;
public readonly Logger Logger;
public string SolanaRpcEndpoint;

public KineticSdkConfig(
int index,
string endpoint,
string environment,
int index,
Logger logger = null,
Commitment? commitment = null,
string solanaRpcEndpoint = null
)
{
Index = index;
Endpoint = endpoint;
Environment = environment;
Commitment = commitment;
Logger = logger;
SolanaRpcEndpoint = solanaRpcEndpoint;
}
}

public static class KineticSdkEndpoint
{
public const string Devnet = "devnet";
public const string Mainnet = "mainnet";
}
}
113 changes: 77 additions & 36 deletions Runtime/KineticSdk.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Kinetic.Sdk.Helpers;
using Kinetic.Sdk.Interfaces;
using Kinetic.Sdk.Transactions;
using Model;
using UnityEngine;
using Commitment = Kinetic.Sdk.Interfaces.Commitment;

// ReSharper disable once CheckNamespace

Expand Down Expand Up @@ -38,14 +37,25 @@ public AppConfig Config()

#region Utility

public BalanceResponse GetBalanceSync(string account)

public AccountInfo GetAccountInfoSync(string account, Commitment? commitment = null)
{
return _sdkInternal.GetAccountInfo(account, commitment);
}

public async Task<AccountInfo> GetAccountInfo(string account, Commitment? commitment = null )
{
return await Task.Run(() => GetAccountInfoSync(account, commitment));
}

public BalanceResponse GetBalanceSync(string account, Commitment? commitment = null)
{
return _sdkInternal.GetBalance(account);
return _sdkInternal.GetBalance(account, commitment);
}

public async Task<BalanceResponse> GetBalance(string account)
public async Task<BalanceResponse> GetBalance(string account, Commitment? commitment = null)
{
return await Task.Run(() => GetBalanceSync(account));
return await Task.Run(() => GetBalanceSync(account, commitment));
}

public string GetExplorerUrl(string path)
Expand All @@ -54,70 +64,95 @@ public string GetExplorerUrl(string path)
}


public List<HistoryResponse> GetHistorySync(string account, string mint = null)
public List<HistoryResponse> GetHistorySync(string account, string mint = null, Commitment? commitment = null)
{
return _sdkInternal.GetHistory(account, mint);
return _sdkInternal.GetHistory(account, mint, commitment);
}

public async Task<List<HistoryResponse>> GetHistory(string account, string mint = null)
public async Task<List<HistoryResponse>> GetHistory(string account, string mint = null, Commitment? commitment = null)
{
return await Task.Run(() => GetHistorySync(account, mint));
return await Task.Run(() => GetHistorySync(account, mint, commitment));
}

public GetTransactionResponse GetTransactionSync(string signature)
public GetTransactionResponse GetTransactionSync(string signature, Commitment? commitment = null)
{
return _sdkInternal.GetTransaction(signature);
return _sdkInternal.GetTransaction(signature, commitment);
}

public async Task<GetTransactionResponse> GetTransaction(string signature)
public async Task<GetTransactionResponse> GetTransaction(string signature, Commitment? commitment = null)
{
return await Task.Run(() => GetTransactionSync(signature));
return await Task.Run(() => GetTransactionSync(signature, commitment));
}


public List<string> GetTokenAccountsSync(
string account,
string mint = null)
string mint = null,
Commitment? commitment = null)
{
return _sdkInternal.GetTokenAccounts(account, mint);
return _sdkInternal.GetTokenAccounts(account, mint, commitment);
}

public async Task<List<string>> GetTokenAccounts(
string account,
string mint = null)
string mint = null,
Commitment? commitment = null)
{
return await Task.Run(() => GetTokenAccountsSync(account, mint));
return await Task.Run(() => GetTokenAccountsSync(account, mint, commitment));
}

public RequestAirdropResponse RequestAirdropSync(
string account,
string amount,
Commitment commitment = Commitment.Finalized,
string mint = null)
string mint = null,
Commitment? commitment = null
)
{
return _sdkInternal.RequestAirdrop(account, amount, commitment, mint);
return _sdkInternal.RequestAirdrop(account, amount, mint, commitment);
}

public async Task<RequestAirdropResponse> RequestAirdrop(
string account,
string amount,
Commitment commitment = Commitment.Finalized,
string mint = null
string mint = null,
Commitment? commitment = null
)
{
return await Task.Run(() => RequestAirdropSync(account, amount, commitment, mint));
return await Task.Run(() => RequestAirdropSync(account, amount, mint, commitment));
}

#endregion

#region Transactions

public Transaction CloseAccountSync(
string account,
string mint = null,
string referenceId = null,
string referenceType = null,
Commitment? commitment = null
)
{
return _sdkInternal.CloseAccount(account, mint, referenceId, referenceType, commitment);
}

public async Task<Transaction> CloseAccount(
string account,
string mint = null,
string referenceId = null,
string referenceType = null,
Commitment? commitment = null
)
{
return await Task.Run(() => CloseAccountSync(account, mint, referenceId, referenceType, commitment));
}

public Transaction CreateAccountSync(
Keypair owner,
string mint = null,
string referenceId = null,
string referenceType = null,
Commitment commitment = default
Commitment? commitment = null
)
{
return _sdkInternal.CreateAccount(owner, mint, referenceId, referenceType, commitment);
Expand All @@ -128,7 +163,7 @@ public async Task<Transaction> CreateAccount(
string mint = null,
string referenceId = null,
string referenceType = null,
Commitment commitment = Commitment.Confirmed
Commitment? commitment = null
)
{
return await Task.Run(() => CreateAccountSync(owner, mint, referenceId, referenceType, commitment));
Expand All @@ -142,12 +177,12 @@ public Transaction MakeTransferSync(
string referenceId = null,
string referenceType = null,
bool senderCreate = false,
Commitment commitment = Commitment.Confirmed,
TransactionType type = TransactionType.None
TransactionType type = TransactionType.None,
Commitment? commitment = null
)
{
return _sdkInternal.MakeTransfer(owner, amount, destination, mint, referenceId, referenceType,
senderCreate, commitment, type);
senderCreate, type, commitment);
}

public async Task<Transaction> MakeTransfer(
Expand All @@ -158,11 +193,12 @@ public async Task<Transaction> MakeTransfer(
string referenceId = null,
string referenceType = null,
bool senderCreate = false,
Commitment commitment = Commitment.Confirmed, TransactionType type = TransactionType.None)
TransactionType type = TransactionType.None,
Commitment? commitment = null
)
{
return await Task.Run(() =>
MakeTransferSync(owner, amount, destination, mint, referenceId, referenceType, senderCreate,
commitment, type));
MakeTransferSync(owner, amount, destination, mint, referenceId, referenceType, senderCreate, type, commitment));
}

#endregion
Expand All @@ -171,11 +207,16 @@ public async Task<Transaction> MakeTransfer(

private AppConfig Init()
{
// Error if SdkConfig is not set
if (SdkConfig == null)
{
throw new Exception("SdkConfig is not set. Please use the setup method to initialize the SDK.");
}
try
{
SdkConfig?.Logger?.Log("KineticSdk: initializing");
var config = _sdkInternal.GetAppConfig(SdkConfig.Environment, SdkConfig.Index);
SdkConfig.SolanaRpcEndpoint = SdkConfig.SolanaRpcEndpoint != null
SdkConfig!.Logger?.Log("KineticSdk: initializing");
var config = _sdkInternal.GetAppConfig();
SdkConfig!.SolanaRpcEndpoint = SdkConfig.SolanaRpcEndpoint != null
? SdkConfig.SolanaRpcEndpoint.GetSolanaRpcEndpoint()
: config.Environment.Cluster.Endpoint.GetSolanaRpcEndpoint();
Solana = new Solana(SdkConfig.SolanaRpcEndpoint, SdkConfig.Logger);
Expand All @@ -195,7 +236,7 @@ private AppConfig Init()

public static KineticSdk SetupSync(KineticSdkConfig config)
{
var sdk = new KineticSdk(config);
var sdk = new KineticSdk(config: ValidateKineticSdkConfig.Validate(config));
try
{
sdk.Init();
Expand Down
Loading

0 comments on commit e0829b0

Please sign in to comment.