Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Created September 23, 2017 14:36
Show Gist options
  • Save Kikimora/c4625d49cefd251310f176d1ac0c1cac to your computer and use it in GitHub Desktop.
Save Kikimora/c4625d49cefd251310f176d1ac0c1cac to your computer and use it in GitHub Desktop.
public enum TransactionStatus { Pending, Mined }
public enum Network { Bitcoin, Ethereum }
public class TransactionDto
{
public string Id { get; set; }
public DateTime Timestamp { get; set; }
public decimal Amount { get; set; }
public TransactionStatus Status { get; set; }
public decimal Fee { get; set; }
public Network Network { get; set; }
//1. Eth specifics
public string EthGazPrice { get; set; }
public string EthGazLimit { get; set; }
//2. Bitcoin specifics
public string BtcMinerAddress { get; set; }
}
public class JavvyContext
{
public static User User { get; private set; }
public static IBitcoinService Bitcoins { get; }
public static IEthereumService Ethereum { get; }
public static ITransactionService TransactionsService { get; }
public static User User { get; }
static JavvyContext()
{
Bitcoins = new BitcoinService();
Ethereum = new EthereumService();
User = new User();
TransactionService = new TransactionService(UserAccessor, Bitcoins, Ethereum);
}
}
public class User
{
public Deictionary<Network, string> Wallets {get;}
}
public interface ITransactionService
{
List<TransactionDto> GetTransactions(int count);
}
public interface IBitcoinService
{
List<BitcoinTransaction> GetTransactions(string userId, int n);
}
public interface IEthereumService
{
List<EthereumTransaction> GetTransactions(string userId, int n);
}
public class TransactionService : ITransactionService
{
User _user;
IEthereumService _ethereum;
IBitcoinService _bitcoin;
public TransactionService(User user, IBitcoinService bitcoin, IEthereumService ethereum)
{
_user = user;
_bitcoin = bitcoin;
_ethereum = ethereum;
}
public List<TransactionDto> GetTransactions(int count, Network? network, CancellationToken token)
{
var btcT = _bitcoin.GetTransactions(_user.Wallets[Network.Bitcoin], count)
.Select(ToTransaction).ToList();
var ethT = _ethereum.GetTransactions(_user.Wallets[Network.Ethereum], count)
.Select(ToTransaction).ToList();
//3. merge
return new List<TransactionDto>().Where(x => x == network || network == null).Take(count).ToList();
}
private TransactionDto ToTransaction(BitcoinTransaction b)
{
//Fill BtcMinerAddress among other things
}
public TransactionDto ToTransaction(EthereumTransaction e)
{
//Fill EthGazPrice, EthGazLimit among other things
}
}
public class TransactionsList : DataContext
{
public ObservableCollection<TransactionDto> Transaction { get; }
public ICommand LoadCommand { get; }
public ICommand SelectTransactionCommand {get;}
public TransactionsList()
{
LoadCommand = new Command(Load);
//Bind this command to select event
SelectTransactionCommand = new Command(SelectTransaciton);
Transaction = new List<TransactionDto>();
}
Network _network;
public Network? Network
{
get => _network;
set
{
if (_network != value)
{
_network = value;
RaisePropertyChanged(nameof(Network));
LoadCommand.Execute();
}
}
}
string _hashFilter;
public string HashFilter
{
get => _hashFilter;
set
{
if (_hashFilter != value)
{
_hashFilter = value;
RaisePropertyChanged(nameof(HashFilter));
LoadCommand.Execute();
}
}
}
int _hasActivity = 0;
public bool HasActivity
{
get => _hasActivity > 0;
}
private void ReportActivity(bool activity)
{
_hasActivity = _hasActivity + (activity ? 1 : -1);
RaisePropertyChanged(nameof(HasActivity));
}
System.Threading.CancellationTokenSource _tcs;
private async void Load(object arg)
{
try
{
ReportActivity(true);
if (_tcs != null) _tcs.Cancel();
_tcs = new CancellationTokenSource();
var tx = JavvyContext.TransactionService;
var token = _tcs.Token;
var txs = await tx.GetTransactions(10, Network, HashFilter, token);
token.ThrowIfCancelled();
Transaction.Clear();
Transaction.AddAll();
}
catch (OperationCancelledException e)
{
//operation cancelled
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
ReportActivity(false);
}
}
private void SelectTransaciton(object arg)
{
SelectTransaciton = Transaction[(int)arg];
}
TransactionDto _selectedTransaction;
public TransactionDto SelectedTransaction
{
get => _selectedTransaction;
private set
{
if (_selectedTransaction != value)
{
_selectedTransaction = value;
RaisePropertyChanged(nameof(selectedTransaction));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment