Example #1
0
        static KeyPair CreateRandomAccount(Account source, long nativeAmount)
        {
            var dest = KeyPair.Random();

            var operation =
                new CreateAccountOperation.Builder(dest, nativeAmount)
                .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:" + response.ReasonPhrase);
            Console.WriteLine(dest.Address);
            Console.WriteLine(dest.Seed);

            return(dest);
        }
Example #2
0
        static void Payment(KeyPair from, KeyPair to, long amount)
        {
            Account source = new Account(from, GetSequence(from.Address));

            // load asset
            Asset asset = new Asset();

            var operation =
                new PaymentOperation.Builder(to, asset, amount)
                .SetSourceAccount(from)
                .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);
        }
Example #3
0
        public async void TestSubmitTransactionMissingAmount()
        {
            StellarBase.Network.CurrentNetwork = network_passphrase;

            AccountCallBuilder accountBuilder = new AccountCallBuilder(horizon_url);

            accountBuilder.accountId("GBAB45XKXEGB74QALBHWP7HATWSWXVHWJKO7W6R3ETWJVCZN5FTASVHL");
            var t = await accountBuilder.Call();


            var     sourceKeyPair = KeyPair.FromSeed("SAEBUXJNNNZPMX3NDG4TTTV2OGUQIMFPPAHZG4FO4FQECKWK4BOSN5GZ");
            Account sourceAccount = new Account(sourceKeyPair, long.Parse(t.Sequence));

            var destinationKeyPair = KeyPair.FromAddress("GDGUDD3WNMAZD6GQXXJXZMJKMCADEZJDA74TAQJSEEPTLNL4PYHZVM4T");

            // make payment with amount > source balance
            double amount    = double.Parse(t.Balances[0].Balance);
            var    operation =
                new PaymentOperation.Builder(destinationKeyPair, new Asset(), (long)(amount * StellarBase.One.Value) + 10)
                .SetSourceAccount(sourceKeyPair)
                .Build();

            sourceAccount.IncrementSequenceNumber();
            var transaction = new StellarBase.Transaction.Builder(sourceAccount)
                              .AddOperation(operation)
                              .Build();

            transaction.Sign(sourceAccount.KeyPair);
            var txSigned = transaction.ToEnvelopeXdrBase64();

            TransactionCallBuilder txBuilder = new TransactionCallBuilder(horizon_url);

            txBuilder.submitTransaction(txSigned);
            try
            {
                var tx = await txBuilder.Call();

                Assert.Equal("Expected BadRequestException", null);
            }
            catch (BadRequestException e)
            {
                Assert.Equal(400, e.ErrorDetails.Status);
                Assert.Equal("tx_failed", e.ErrorDetails.Extras.ResultCodes.Transaction);
                Assert.Equal("op_underfunded", e.ErrorDetails.Extras.ResultCodes.Operations[0]);
            }
        }
Example #4
0
        public StellarBase.Transaction SampleTransaction(string destAccountId)
        {
            StellarBase.Network.CurrentNetwork = "";

            var master = KeyPair.Master();
            var dest   = string.IsNullOrEmpty(destAccountId) ? KeyPair.Random() : KeyPair.FromAccountId(destAccountId);

            var sourceAccount = new Account(master, 1);

            CreateAccountOperation operation =
                new CreateAccountOperation.Builder(dest, 1000)
                //.SetSourceAccount(master)
                .Build();

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

            return(transaction);
        }
Example #5
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);
        }
Example #6
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);
        }