private async Task <AuthorizationAPIResult> ReceiveTransferAsync(NewTransferAPIResult2 new_transfer_info) { if (await GetLocalAccountHeightAsync() == 0) // if this is new account with no blocks { return(await OpenStandardAccountWithReceiveBlockAsync(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 latestBalances = latestBlock.Balances.ToDecimalDict(); var recvBalances = latestBlock.Balances.ToDecimalDict(); foreach (var chg in new_transfer_info.Transfer.Changes) { if (recvBalances.ContainsKey(chg.Key)) { recvBalances[chg.Key] += chg.Value; } else { recvBalances.Add(chg.Key, chg.Value); } } receiveBlock.Balances = recvBalances.ToLongDict(); await receiveBlock.InitializeBlockAsync(latestBlock, _signer); if (!receiveBlock.ValidateTransaction(latestBlock)) { throw new Exception("ValidateTransaction failed"); } //receiveBlock.Signature = Signatures.GetSignature(PrivateKey, receiveBlock.Hash); var result = await _rpcClient.ReceiveTransferAsync(receiveBlock); 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); }