コード例 #1
0
        public void TestAccountMergeOperation()
        {
            // GC5SIC4E3V56VOHJ3OZAX5SJDTWY52JYI2AFK6PUGSXFVRJQYQXXZBZF
            var source = KeyPair.FromSecretSeed("SC4CGETADVYTCR5HEAVZRB3DZQY5Y4J7RFNJTRA6ESMHIPEZUSTE2QDK");
            // GDW6AUTBXTOC7FIKUO5BOO3OGLK4SF7ZPOBLMQHMZDI45J2Z6VXRB5NR
            var destination = KeyPair.FromSecretSeed("SDHZGHURAYXKU2KMVHPOXI6JG2Q4BSQUQCEOY72O3QQTCLR2T455PMII");

            var operation = new AccountMergeOperation.Builder(destination)
                            .SetSourceAccount(source)
                            .Build();

            var xdr = operation.ToXdr();

            var parsedOperation = (AccountMergeOperation)Operation.FromXdr(xdr);

            Assert.AreEqual(destination.AccountId, parsedOperation.Destination.AccountId);

            Assert.AreEqual(
                "AAAAAQAAAAC7JAuE3XvquOnbsgv2SRztjuk4RoBVefQ0rlrFMMQvfAAAAAgAAAAA7eBSYbzcL5UKo7oXO24y1ckX+XuCtkDsyNHOp1n1bxA=",
                operation.ToXdrBase64());
        }
コード例 #2
0
        static void MergeAccount(Account source, KeyPair destination)
        {
            var operation =
                new AccountMergeOperation.Builder(destination)
                .SetSourceAccount(source.KeyPair)
                .Build();

            source.IncrementSequenceNumber();

            StellarBase.Transaction transaction =
                new StellarBase.Transaction.Builder(source)
                .AddOperation(operation)
                .Build();

            transaction.Sign(source.KeyPair);

            var tx = transaction.ToEnvelopeXdrBase64();

            var response = PostResult(tx);

            Console.WriteLine(response.ReasonPhrase);
        }
コード例 #3
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);
        }
コード例 #4
0
        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);
        }