Ejemplo n.º 1
0
 public void TestFromXdrAmount()
 {
     Assert.AreEqual("0", Operation.FromXdrAmount(0L));
     Assert.AreEqual("0.0000001", Operation.FromXdrAmount(1L));
     Assert.AreEqual("1", Operation.FromXdrAmount(10000000L));
     Assert.AreEqual("1.1234567", Operation.FromXdrAmount(11234567L));
     Assert.AreEqual("72991284.3007381", Operation.FromXdrAmount(729912843007381L));
     Assert.AreEqual("101401671144.6800155", Operation.FromXdrAmount(1014016711446800155L));
     Assert.AreEqual("922337203685.4775807", Operation.FromXdrAmount(9223372036854775807L));
 }
Ejemplo n.º 2
0
        public async Task <string> BuildTransactionAsync(Guid operationId, AddressBalance from, string toAddress, string memoText, long amount)
        {
            var fromKeyPair = KeyPair.FromAccountId(from.Address);
            var fromAccount = new Account(fromKeyPair, from.Sequence);

            var toKeyPair = KeyPair.FromAccountId(toAddress);

            var transferableBalance = from.Balance - from.MinBalance;

            Operation operation;

            if (await _horizonService.AccountExists(toAddress))
            {
                if (amount <= transferableBalance)
                {
                    var asset = new AssetTypeNative();
                    operation = new PaymentOperation.Builder(toKeyPair, asset, Operation.FromXdrAmount(amount))
                                .SetSourceAccount(fromKeyPair)
                                .Build();
                }
                else if (!_balanceService.IsDepositBaseAddress(from.Address))
                {
                    operation = new AccountMergeOperation.Builder(toKeyPair)
                                .SetSourceAccount(fromKeyPair)
                                .Build();
                }
                else
                {
                    throw new BusinessException($"It isn't allowed to merge the entire balance from the deposit base into another account! Transfer less funds. transferable={transferableBalance}");
                }
            }
            else
            {
                if (amount <= transferableBalance)
                {
                    operation = new CreateAccountOperation.Builder(toKeyPair, Operation.FromXdrAmount(amount))
                                .SetSourceAccount(fromKeyPair)
                                .Build();
                }
                else
                {
                    throw new BusinessException($"It isn't possible to merge the entire balance into an unused account! Use a destination in existance. transferable={transferableBalance}");
                }
            }

            var builder = new TransactionBuilder(fromAccount)
                          .AddOperation(operation)
                          .SetFee(_appSettings.StellarApiService.OperationFee);

            if (!string.IsNullOrWhiteSpace(memoText))
            {
                var memo = new MemoText(memoText);
                builder = builder.AddMemo(memo);
            }

            var tx = builder.Build();

            var xdr               = tx.ToUnsignedEnvelopeXdr(TransactionBase.TransactionXdrVersion.V1);
            var expirationDate    = (DateTime.UtcNow + _transactionExpirationTime);
            var maxUnixTimeDouble = expirationDate.ToUnixTime() / 1000;//ms to seconds
            var maxTimeUnix       = (ulong)maxUnixTimeDouble;

            xdr.V1.Tx.TimeBounds = new TimeBounds()
            {
                MaxTime = new TimePoint(new Uint64(maxTimeUnix)),
                MinTime = new TimePoint(new Uint64(0)),
            };

            var writer = new XdrDataOutputStream();

            stellar_dotnet_sdk.xdr.TransactionEnvelope.Encode(writer, xdr);
            var xdrBase64 = Convert.ToBase64String(writer.ToArray());

            _log.Info("Transaction has been built", new
            {
                OperationId = operationId,
                From        = from,
                To          = toAddress,
                Memo        = memoText,
                Amount      = amount,
                Fee         = tx.Fee,
                Sequence    = tx.SequenceNumber,
                MaxTimeUnix = maxTimeUnix
            });

            var build = new TxBuild
            {
                OperationId = operationId,
                XdrBase64   = xdrBase64
            };
            await _buildRepository.AddAsync(build);

            return(xdrBase64);
        }