public async Task AddAsync(ulong receiverId, string reason, long amount, IUnitOfWork uow = null, bool gamble = false, IUser user = null) { if (amount < 0) { throw new ArgumentNullException(nameof(amount)); } var transaction = new CurrencyTransaction() { UserId = receiverId, Reason = reason, Amount = amount, }; if (uow == null) { using (uow = _db.UnitOfWork) { if (user != null) { uow.DiscordUsers.GetOrCreate(user); } uow.DiscordUsers.TryUpdateCurrencyState(receiverId, amount); uow.DiscordUsers.TryUpdateCurrencyState(_botId, -amount, true); uow.CurrencyTransactions.Add(transaction); await uow.CompleteAsync(); } } else { uow.DiscordUsers.TryUpdateCurrencyState(receiverId, amount); uow.DiscordUsers.TryUpdateCurrencyState(_botId, -amount, true); uow.CurrencyTransactions.Add(transaction); } }
private string PrepareNewTransaction(string aToAddress, decimal aAmount, decimal aTxFee, ICurrencyItem aCurrencyItem, TransactionUnit[] aUnspentOutputs, out CurrencyTransaction aCurrencyTransaction) { if (!FServerConnection.DirectCheckAddress(aCurrencyItem.Id, aToAddress)) { throw new ClientExceptions.InvalidAddressException("Address provided not valid. Please verify"); } var lTxOutputs = new List <TransactionUnit>(); lTxOutputs.Add(new TransactionUnit(0, aCurrencyItem.AmountToBigInteger(aAmount), aToAddress)); BigInteger lTotal = 0; foreach (var lOutput in aUnspentOutputs) { lTotal += lOutput.Amount; } var lSendTotal = aCurrencyItem.AmountToDecimal(lTotal); if (lSendTotal < (aAmount + aTxFee)) { throw new InvalidOperationException($"The amount to send '{aAmount + aTxFee}' is greater than the balance of transactions '{aCurrencyItem.AmountToDecimal(lTotal)}'."); } else if (lSendTotal > (aAmount + aTxFee)) { lTxOutputs.Add(new TransactionUnit(0, lTotal - aCurrencyItem.AmountToBigInteger(aAmount + aTxFee), FServerConnection.GetCoinAddress(aCurrencyItem.Id))); } aCurrencyTransaction = new CurrencyTransaction(aUnspentOutputs, lTxOutputs.ToArray(), aCurrencyItem.AmountToLong(aTxFee), aCurrencyItem.Id); return(FServerConnection.DirectCreateTransaction(aCurrencyTransaction)); }
public void TestAddCreateTransaction() { using (var lServerAccess = new ServerAccess.PandoraWalletServiceAccess("localhost", 20159, false)) if (lServerAccess.Logon("*****@*****.**", "davinci", "test")) { var lControl = CurrencyControl.GetCurrencyControl(); var lRootSeed = lControl.GenerateRootSeed("*****@*****.**", "davinci", "test"); var lAdvocacy = lControl.GetCryptoCurrency(1, ""); lAdvocacy.RootSeed = lRootSeed; lServerAccess.AddMonitoredAccount(1, lAdvocacy.GetAddress(10)); var lTransaction = new CurrencyTransaction(); var s = lServerAccess.GetTransactionRecords(1, 0); var lTxList = Newtonsoft.Json.JsonConvert.DeserializeObject <TransactionRecord[]>(s, new Pandora.Client.ClientLib.PandoraJsonConverter()); var lTx = lTxList[0]; lTransaction.AddInput(lTx.Outputs[0]); lTransaction.AddOutput(lTx.Outputs[0].Amount - 1000, "mzqD8sEGXfRMvcnNnkoGn6wyMN9AGUxaAB"); lTransaction.CurrencyId = 1; lTransaction.TxFee = 1000; var lTxData = lServerAccess.CreateTransaction(lTransaction); lTxData = lAdvocacy.SignTransaction(lTxData, lTransaction); //lServerAccess.SendTransaction(1, lTxData); lServerAccess.Logoff(); } else { throw new Exception("Basic Logon and log off failed."); } }
private bool InternalRemoveCurrency(ulong authorId, string reason, long amount, IUnitOfWork uow, bool addToBot) { var success = uow.DiscordUsers.TryUpdateCurrencyState(authorId, -amount); if (!success) { return(false); } var transaction = new CurrencyTransaction() { UserId = authorId, Reason = reason, Amount = -amount, }; if (addToBot) { var botTr = transaction.Clone(); botTr.UserId = _botId; botTr.Amount *= -1; uow.DiscordUsers.TryUpdateCurrencyState(_botId, amount); uow.CurrencyTransactions.Add(botTr); } uow.CurrencyTransactions.Add(transaction); return(true); }
public async Task AddAsync(ulong guildId, ulong receiverId, string reason, long amount, IUnitOfWork uow = null) { if (amount < 0) { throw new ArgumentException(null, nameof(amount)); } var transaction = new CurrencyTransaction { GuildId = guildId, UserId = receiverId, Reason = reason, Amount = amount, }; if (uow == null) { using var _uow = _db.UnitOfWork; _uow.Currency.TryAddCurrencyValue(guildId, receiverId, amount); _uow.CurrencyTransactions.Add(transaction); await _uow.SaveChangesAsync(); } else { uow.Currency.TryAddCurrencyValue(guildId, receiverId, amount); uow.CurrencyTransactions.Add(transaction); } }
public static async Task AddCurrencyAsync(ulong receiverId, string reason, long amount, IUnitOfWork uow = null) { if (amount < 0) { throw new ArgumentNullException(nameof(amount)); } var transaction = new CurrencyTransaction() { UserId = (long)receiverId, Reason = reason, Amount = amount, }; if (uow == null) { using (uow = DbHandler.UnitOfWork()) { uow.Currency.TryUpdateState(receiverId, amount); uow.CurrencyTransactions.Add(transaction); await uow.CompleteAsync(); } } else { uow.Currency.TryUpdateState(receiverId, amount); uow.CurrencyTransactions.Add(transaction); } }
protected virtual string SignTransactionData(string aTxData, CurrencyTransaction aCurrencyTransaction, ICryptoCurrencyAdvocacy aCurrencyAdvocacy) { //TODO: this is done because the object needs the address create. //So we should fix this somehow so its a bit more logical. aCurrencyAdvocacy.GetAddress(0); aCurrencyAdvocacy.GetAddress(1); return(aCurrencyAdvocacy.SignTransaction(aTxData, aCurrencyTransaction)); }
public CurrencyTransaction ToCurrencyTransaction() { return(CurrencyTransaction.Create(Id, Amount, CurrencyCode, TransactionDateLocal.ToUniversalTime(), CsvStatus.ToCurrencyTransactionStatus())); }
public static CurrencyTransactionWebApi FromDomainModel(CurrencyTransaction currencyTransaction) { return(new CurrencyTransactionWebApi() { Id = currencyTransaction.Id, Payment = $"{currencyTransaction.Amount:.00} {currencyTransaction.CurrencyCode}", Status = currencyTransaction.Status.ToString() }); }
public CurrencyTransaction ToCurrencyTransaction() { return (IsValid ? CurrencyTransaction.Create(Id, Amount, PaymentDetails.CurrencyCode, DateTime.Parse(TransactionDateLocalString).ToUniversalTime(), XmlStatus.ToCurrencyTransactionStatus()) : null); }
private string ThreadCreateTransaction(CurrencyTransaction aCurrencyTransaction) { string lTxData = aCurrencyTransaction.ToString(); long lHandle = (long)ReadServerResult(FServer.StartGetTransactionToSign(ConnectionId, lTxData)); do { System.Threading.Thread.Sleep(1000); lTxData = (string)ReadServerResult(FServer.EndGetTransactionToSign(ConnectionId, lHandle)); } while (string.IsNullOrEmpty(lTxData)); return(lTxData); }
public ActionResult Save(CurrencyTransaction data) { if (!ModelState.IsValid) { return(BadRequest("Some information are invalid")); } if (!_unitOfWork.RateTransactions.IsUserRegistered(data.UserID)) { return(BadRequest("Invalid UserID")); } var maxRate = _unitOfWork.RateTransactions.GetMaxRateSupported(data.UserID, data.CurrencyCode, DateTime.Now.ToString("yyyyMM")); if (maxRate < 0) { return(BadRequest($"Transaction not supported for this Code {data.CurrencyCode} in this period.")); } double rate = GetCurrencyRate(data.CurrencyCode); if (rate < 0) { return(BadRequest($"The ISO Code provided [{data.CurrencyCode}] is not supported for this API.")); } if (rate == 0) { return(BadRequest("An unexpected error was found in the request.")); } double exchange = data.Amount / rate; if (exchange > maxRate) { return(BadRequest($"The limit in {data.CurrencyCode} is {maxRate}.")); } Currency model = new Currency { UserID = data.UserID, Amount = data.Amount, Rate = rate, CurrencyCode = data.CurrencyCode, Total = exchange }; _unitOfWork.Currencies.Add(model); _unitOfWork.Complete(); return(Ok(exchange.ToString("#,###.##"))); }
public void GetOverLimitCurrencyExchange() { CurrencyTransaction data = new CurrencyTransaction { Amount = 205, CurrencyCode = "USD", UserID = "001" }; var result = defaulController.Save(data); var actual = result as BadRequestObjectResult; Assert.AreEqual("The limit in USD is 200.", actual.Value); }
public void GetInvalidUserForCurrencyExchange() { CurrencyTransaction data = new CurrencyTransaction { Amount = 100, CurrencyCode = "USD", UserID = "002" }; var result = defaulController.Save(data); var actual = result as BadRequestObjectResult; Assert.AreEqual("Invalid UserID", actual.Value); }
public void GetValidLimitCurrencyExchange() { CurrencyTransaction data = new CurrencyTransaction { Amount = 199, CurrencyCode = "USD", UserID = "001" }; var result = defaulController.Save(data); var actual = result as OkResult; Assert.IsNotNull(actual); }
public void SignTxTest() { var lTx = new CurrencyTransaction(); var lAdvocacy = new EthereumCurrencyAdvocacy(1000, FEthChainParams, () => "6A41F49D98D83DF2EEB89F8E895D22697A41F49D98D83DF2EEB89F8E895D2269"); var lString = lAdvocacy.GetAddress(0); Assert.AreEqual("0xFF3225E2c09280797F5848Ad204e9c0C331Af2c0", lString); // 1 eth is 1 Quintillion 1, or 1000 Quadrillion // 1,000,000,000,000,000,000 Wei (1018) lTx.AddInput(13689000000000000, lString); // how much I have lTx.TxFee = 420000000000000; // fee to pay lTx.AddOutput(1100000000000000, "0x596a2232d098965bc56b762549E045829CF43c8D"); // amount to move var ldata = lAdvocacy.SignTransaction("30", lTx); Assert.AreEqual("f8728087019945ca2620008704cbd15e72600094596a2232d098965bc56b762549e045829cf43c8d8703e871b540c000801ca0fb1f1e416a5b9d22b2b34242807e0c41994e1be4909cc16370b5187011a109fea03165afb74b0d47138e86e2faec57bce8079bdd7024937d64c8fdc454d2dfc512", ldata); }
public async Task AddToManyAsync(string reason, long amount, params ulong[] userIds) { using (var uow = _db.UnitOfWork) { foreach (var userId in userIds) { var transaction = new CurrencyTransaction() { UserId = userId, Reason = reason, Amount = amount, }; uow.Currency.TryUpdateState(userId, amount); uow.CurrencyTransactions.Add(transaction); } await uow.CompleteAsync(); } }
public async Task AddToManyAsync(ulong guildId, string reason, long amount, params ulong[] userIds) { using var uow = _db.UnitOfWork; foreach (var userId in userIds) { var transaction = new CurrencyTransaction { GuildId = guildId, UserId = userId, Reason = reason, Amount = amount, }; uow.Currency.TryAddCurrencyValue(guildId, userId, amount); uow.CurrencyTransactions.Add(transaction); } await uow.SaveChangesAsync().ConfigureAwait(false); }
private CurrencyTransaction CreateNewCurrencyTransaction(uint aCurrencyID, ulong aAmountToSend, string aToAddress, ulong aTxFee = 0) { CurrencyTransaction lCurrecyTransaction = new CurrencyTransaction(); lock (FUnspent) lCurrecyTransaction.AddInput(FUnspent[aCurrencyID]); lCurrecyTransaction.AddOutput(aAmountToSend, aToAddress); ulong lChangeAmount = FBalance[aCurrencyID].Total - aAmountToSend - aTxFee; if (lChangeAmount > 0) { lCurrecyTransaction.AddOutput(lChangeAmount, FAddresses[aCurrencyID].First()); } lCurrecyTransaction.TxFee = aTxFee; lCurrecyTransaction.CurrencyId = aCurrencyID; return(lCurrecyTransaction); }
public void FetchingDataTests() { PandorasServer TestServer = new PandorasServer(Path.Combine(Directory.GetCurrentDirectory(), "DBTEST"), "localhost", 20159); Assert.IsTrue(TestServer.Logon("*****@*****.**", "Dj", "test")); List <CurrencyItem> TestCurrencies = TestServer.FetchCurrencies(); Assert.IsTrue(TestCurrencies[1].Name == "Litecoin"); List <CurrencyStatusItem> TestStatus = TestServer.FetchCurrencyStatus(2); Assert.IsTrue(TestStatus[0].ExtendedInfo == "The coin is working fine with 3 servers running with consensus."); List <CurrencyStatusItem> TestStatus2 = TestServer.FetchCurrencyStatus(2); Assert.IsTrue(TestStatus2[0].ExtendedInfo == "The servers are under maintaince and should be back up soon."); List <CurrencyStatusItem> TestStatus3 = TestServer.FetchCurrencyStatus(3); Assert.IsTrue(TestStatus3[0].ExtendedInfo == "Coin may not come back up."); CurrencyTransaction TestCurr = new CurrencyTransaction(); TestCurr.AddInput(10000000000, "zBvBMSEYstWetqTFn5Au4m4GFgaxJaNVN3"); TestCurr.AddOutput(10000000000, "zJvBMSEYstWetqTFn5Au4m4GFgaxJaNVN3"); TestCurr.AddInput(10000000000, "zBvBMSEYstWetqTFn5Au4m4GFgaxJaNVN3"); TestCurr.AddOutput(10000000000, "zJvBMSEYstWetqTFn5Au4m4GFgaxJaNVN3"); TestCurr.TxFee = 100; string jsonstring = JsonConvert.SerializeObject(TestCurr); PandoraJsonConverter lJsonConverter = new PandoraJsonConverter(); var Response = Newtonsoft.Json.JsonConvert.DeserializeObject <CurrencyTransaction>(jsonstring, lJsonConverter); Assert.IsTrue(Response.TxFee == 100 && Response.Outputs[1].Address == "zJvBMSEYstWetqTFn5Au4m4GFgaxJaNVN3"); TestServer.Logoff(); }
public void TestSendTransaction() { using (var lServerAccess = new ServerAccess.PandoraWalletServiceAccess("localhost", 20159, false)) if (lServerAccess.Logon("*****@*****.**", "davinci", "test")) { var lTransaction = new CurrencyTransaction(); lTransaction.CurrencyId = 3; var lControl = CurrencyControl.GetCurrencyControl(); var lRootSeed = lControl.GenerateRootSeed("*****@*****.**", "davinci", "test"); var lAdvocacy = lControl.GetCryptoCurrency(lTransaction.CurrencyId, ""); lAdvocacy.RootSeed = lRootSeed; var lAddress = lAdvocacy.GetAddress(10); lServerAccess.AddMonitoredAccount(lTransaction.CurrencyId, lAddress); var s = lServerAccess.GetTransactionRecords(lTransaction.CurrencyId, 0); var lTxList = Newtonsoft.Json.JsonConvert.DeserializeObject <TransactionRecord[]>(s, new Pandora.Client.ClientLib.PandoraJsonConverter()); var lTx = lTxList[0]; lTransaction.AddInput(lTx.Outputs[0]); lTransaction.AddOutput(lTx.Outputs[0].Amount - 1000, "yPueC9mAxBj7yrPxj4M2vNY3rn4Yz3thrY"); lTransaction.TxFee = 1000; var lTxData = lServerAccess.CreateTransaction(lTransaction); lTxData = lAdvocacy.SignTransaction(lTxData, lTransaction); var lHandle = lServerAccess.SendTransaction(lTransaction.CurrencyId, lTxData); System.Threading.Thread.Sleep(5000); if (lServerAccess.IsTransactionSent(lHandle)) { var lTxId = lServerAccess.GetTransactionId(lHandle); } lServerAccess.Logoff(); } else { throw new Exception("Basic Logon and log off failed."); } }
public void PandoraServerTest() { TestServer = new PandorasServer(Path.Combine(Directory.GetCurrentDirectory(), "DBTEST"), "localhost", 20159); Assert.IsTrue(TestServer.Logon("*****@*****.**", "quality", "quality")); TestServer.OnTransactions += NewTransactionTestHandler; var TestUserStatus = (TestServer.GetUserStatus()); Assert.IsTrue(TestUserStatus.Active && TestUserStatus.ExtendedInfo == "Quality Testing User"); CurrencyItem[] Currencies = TestServer.GetCurrencyList(); Assert.IsTrue(Currencies.Count() > 0 && Currencies[0].Name == "Bitcoin"); CurrencyStatusItem Result2 = TestServer.GetCurrencyStatus(1); Assert.IsTrue(Result2.Status == CurrencyStatus.Active && Result2.ExtendedInfo == "The coin is working fine with 3 servers running with consensus."); CurrencyAccount[] Accounts = TestServer.MonitoredAccounts.GetById(1); Assert.AreEqual(Accounts[0].Address, "mk4DoTmTB8PnNgPyvzozTeNGxC1fduJqb5"); TestServer.StartTxUpdatingTask(); while (!Records.Any()) { Records = TestServer.GetTransactions(1); System.Threading.Thread.Sleep(1000); } TransactionRecord Record = Records.FirstOrDefault(); Assert.IsTrue(Record.TxId == "30c8e2f7f5cf8b00f8b8bff127a8ceb726e30ab354dda361199fc435c22275b9"); Assert.IsTrue(Record.Block == 1355565); Assert.IsTrue(Record.Outputs.Count() == 2); var Outputs = Record.Outputs.ToList(); Assert.IsTrue(Outputs.Exists(x => x.Address == "mk4DoTmTB8PnNgPyvzozTeNGxC1fduJqb5" && x.Amount == 150000000)); Assert.IsTrue(Outputs.Exists(x => x.Address == "2MwqZdwPUrmBACjacg8uwEZ8rVoCUX7GD2h" && x.Amount == 111418554)); var Transaction = new CurrencyTransaction(); Transaction.AddInput(150000000, "mk4DoTmTB8PnNgPyvzozTeNGxC1fduJqb5", 10151); Transaction.AddOutput(100000000, "mtCFoNLWbgHnB1SG7R7VqiDizGUP8K2uae"); Transaction.AddOutput(50000000, "mk4DoTmTB8PnNgPyvzozTeNGxC1fduJqb5"); Transaction.TxFee = 250000; Transaction.CurrencyId = 1; //var Response = TestServer.CreateTransaction(1, Transaction); //Assert.IsTrue(Response == "0100000001b97522c235c49f1961a3dd54b30ae326b7cea827f1bfb8f8008bcff5f7e2c830010000001976a91431ca57b301db0088e2d5912cff743210364b126c88acffffffff0200e1f505000000001976a9148b10583ecc2a455ab21342b3b34943ec711e694888ac80f0fa02000000001976a91431ca57b301db0088e2d5912cff743210364b126c88ac00000000"); TestServer.Logoff(); }
/// <summary> /// Creates the transaction data to be signed. /// </summary> /// <param name="aTransactionData">This is a CurrencyTransaction object.</param> /// <returns>returns the data that needs to be signed</returns> public string CreateTransaction(CurrencyTransaction aCurrencyTransaction) { CheckConnected(); return((string)this.Invoke(new CreateTransactionDelegate(ThreadCreateTransaction), aCurrencyTransaction)); }