public async Task <string> BuildTransactionAsync(Guid operationId, AddressBalance from, string toAddress, long amount) { var fromKeyPair = KeyPair.FromAddress(from.Address); var fromAccount = new Account(fromKeyPair, from.Sequence); var toKeyPair = KeyPair.FromAddress(toAddress); var transferableBalance = from.Balance - from.MinBalance; StellarBase.Operation operation; if (await _horizonService.AccountExists(toAddress)) { if (amount <= transferableBalance) { var asset = new StellarBase.Asset(); operation = new PaymentOperation.Builder(toKeyPair, asset, amount) .SetSourceAccount(fromKeyPair) .Build(); } else { operation = new AccountMergeOperation.Builder(toKeyPair) .SetSourceAccount(fromKeyPair) .Build(); } } else { if (amount <= transferableBalance) { operation = new CreateAccountOperation.Builder(toKeyPair, amount) .SetSourceAccount(fromKeyPair) .Build(); } else { throw new BusinessException($"Currently not possible to transfer entire balance to an unused account! Use a destination in existance. transferable={transferableBalance}"); } } fromAccount.IncrementSequenceNumber(); var tx = new StellarBase.Transaction.Builder(fromAccount) .AddOperation(operation) .Build(); var xdr = tx.ToXDR(); var writer = new ByteWriter(); StellarBase.Generated.Transaction.Encode(writer, xdr); var xdrBase64 = Convert.ToBase64String(writer.ToArray()); var build = new TxBuild { OperationId = operationId, XdrBase64 = xdrBase64 }; await _buildRepository.AddAsync(build); return(xdrBase64); }