public Task <NewTransferAPIResult> LookForNewTransfer(string AccountId, string Signature) { NewTransferAPIResult transfer_info = new NewTransferAPIResult(); try { SendTransferBlock sendBlock = BlockChain.Singleton.FindUnsettledSendBlock(AccountId); if (sendBlock != null) { TransactionBlock previousBlock = BlockChain.Singleton.FindBlockByHash(sendBlock.PreviousHash); if (previousBlock == null) { transfer_info.ResultCode = APIResultCodes.CouldNotTraceSendBlockChain; } else { transfer_info.Transfer = sendBlock.GetTransaction(previousBlock); //CalculateTransaction(sendBlock, previousSendBlock); transfer_info.SourceHash = sendBlock.Hash; transfer_info.NonFungibleToken = sendBlock.NonFungibleToken; transfer_info.ResultCode = APIResultCodes.Success; } } else { transfer_info.ResultCode = APIResultCodes.NoNewTransferFound; } } catch (Exception e) { transfer_info.ResultCode = APIResultCodes.UnknownError; } return(Task.FromResult(transfer_info)); }
public async Task <NewTransferAPIResult> LookForNewTransfer(string AccountId, string Signature) { NewTransferAPIResult transfer_info = new NewTransferAPIResult(); if (!await VerifyClientAsync(AccountId, Signature)) { transfer_info.ResultCode = APIResultCodes.APISignatureValidationFailed; return(transfer_info); } try { SendTransferBlock sendBlock = await BlockChain.Singleton.FindUnsettledSendBlockAsync(AccountId); if (sendBlock != null) { TransactionBlock previousBlock = await BlockChain.Singleton.FindBlockByHashAsync(sendBlock.PreviousHash) as TransactionBlock; if (previousBlock == null) { transfer_info.ResultCode = APIResultCodes.CouldNotTraceSendBlockChain; } else { transfer_info.Transfer = sendBlock.GetTransaction(previousBlock); //CalculateTransaction(sendBlock, previousSendBlock); transfer_info.SourceHash = sendBlock.Hash; transfer_info.NonFungibleToken = sendBlock.NonFungibleToken; transfer_info.ResultCode = APIResultCodes.Success; } } else { transfer_info.ResultCode = APIResultCodes.NoNewTransferFound; } } catch (Exception) { transfer_info.ResultCode = APIResultCodes.UnknownError; } return(transfer_info); }
private async Task <AuthorizationAPIResult> OpenStandardAccountWithReceiveBlock(NewTransferAPIResult new_transfer_info) { var svcBlockResult = await _rpcClient.GetLastServiceBlockAsync(); if (svcBlockResult.ResultCode != APIResultCodes.Success) { throw new Exception("Unable to get latest service block."); } var openReceiveBlock = new OpenWithReceiveTransferBlock { AccountType = AccountTypes.Standard, AccountID = _accountId, ServiceHash = svcBlockResult.GetBlock().Hash, SourceHash = new_transfer_info.SourceHash, Balances = new Dictionary <string, long>(), Fee = 0, FeeType = AuthorizationFeeTypes.NoFee, FeeCode = LyraGlobal.OFFICIALTICKERCODE, NonFungibleToken = new_transfer_info.NonFungibleToken, VoteFor = null }; openReceiveBlock.Balances.Add(new_transfer_info.Transfer.TokenCode, new_transfer_info.Transfer.Amount.ToBalanceLong()); openReceiveBlock.InitializeBlock(null, _signer); //openReceiveBlock.Signature = Signatures.GetSignature(PrivateKey, openReceiveBlock.Hash); var result = await _rpcClient.ReceiveTransferAndOpenAccountAsync(openReceiveBlock); //PrintCon(string.Format("{0}> ", AccountName)); return(result); }
private async Task <AuthorizationAPIResult> ReceiveTransfer(NewTransferAPIResult new_transfer_info) { if (await GetLocalAccountHeightAsync() == 0) // if this is new account with no blocks { return(await OpenStandardAccountWithReceiveBlock(new_transfer_info)); } var svcBlockResult = await _rpcClient.GetLastServiceBlockAsync(); if (svcBlockResult.ResultCode != APIResultCodes.Success) { throw new Exception("Unable to get latest service block."); } var receiveBlock = new ReceiveTransferBlock { AccountID = _accountId, VoteFor = null, ServiceHash = svcBlockResult.GetBlock().Hash, SourceHash = new_transfer_info.SourceHash, Balances = new Dictionary <string, long>(), Fee = 0, FeeType = AuthorizationFeeTypes.NoFee, FeeCode = LyraGlobal.OFFICIALTICKERCODE, NonFungibleToken = new_transfer_info.NonFungibleToken }; TransactionBlock latestBlock = await GetLatestBlockAsync(); var newBalance = new_transfer_info.Transfer.Amount; // if the recipient's account has this token already, add the transaction amount to the existing balance if (latestBlock.Balances.ContainsKey(new_transfer_info.Transfer.TokenCode)) { newBalance += latestBlock.Balances[new_transfer_info.Transfer.TokenCode].ToBalanceDecimal(); } receiveBlock.Balances.Add(new_transfer_info.Transfer.TokenCode, newBalance.ToBalanceLong()); // transfer unchanged token balances from the previous block foreach (var balance in latestBlock.Balances) { if (!(receiveBlock.Balances.ContainsKey(balance.Key))) { receiveBlock.Balances.Add(balance.Key, balance.Value); } } receiveBlock.InitializeBlock(latestBlock, _signer); if (!receiveBlock.ValidateTransaction(latestBlock)) { throw new ApplicationException("ValidateTransaction failed"); } //receiveBlock.Signature = Signatures.GetSignature(PrivateKey, receiveBlock.Hash); var result = await _rpcClient.ReceiveTransferAsync(receiveBlock); return(result); }