Exemplo n.º 1
0
        protected override void ExecuteCommand(string[] args)
        {
            var sender   = WalletService.GetActiveKey().Data.Address;
            var receiver = WalletService.GetAddress(receiverArgument.Value);
            var currency = Currency.FromSymbol(currencyArgument.Value);
            var amount   = Amount.FromWholeDecimal(Convert.ToDecimal(amountArgument.Value));

            var input  = new TxInput(sender, currency, amount);
            var output = new TxOutput(receiver, currency, amount);

            var declarations = new List <TxDeclaration> {
            };
            var inputs       = new List <TxInput> {
                input
            };
            var outputs = new List <TxOutput> {
                output
            };

            var fees = WalletService.CreateFeesInput(sender);

            var transaction = new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.AddMinutes(1).ToUnixTimestamp(), fees);

            if (WalletService.SignAndSubmit(transaction))
            {
                Console.WriteLine($"Successfully sent transaction");
            }
        }
        protected override void ExecuteCommand(string[] args)
        {
            var owner   = WalletService.GetActiveKey().Data.Address;
            var @in     = Currency.FromSymbol(currencyInArgument.Value);
            var @out    = Currency.FromSymbol(currencyOutArgument.Value);
            var rate    = Amount.FromWholeDecimal(Decimal.Parse(rateArgument.Value));
            var machine = new VendingMachine(owner, @in, @out, rate);


            // TODO put this in a gobal function
            var alias = aliasArgument.Value;

            WalletService.ImportDeclaration(alias, machine);

            var amount = Amount.FromWholeDecimal(Convert.ToDecimal(amountArgument.Value));

            var sender   = owner;
            var receiver = machine.Address;
            var currency = @out;

            // TODO put this in a gobal function
            var input  = new TxInput(sender, currency, amount);
            var output = new TxOutput(receiver, currency, amount);

            var declarations = new List <TxDeclaration> {
                machine
            };
            var inputs = new List <TxInput> {
                input
            };
            var outputs = new List <TxOutput> {
                output
            };

            var fees = WalletService.CreateFeesInput(sender);

            var transaction = new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.AddMinutes(1).ToUnixTimestamp(), fees);

            if (WalletService.SignAndSubmit(transaction))
            {
                Console.WriteLine($"Successfully sent transaction");
            }
            // --
        }
Exemplo n.º 3
0
        protected override void ExecuteCommand(string[] args)
        {
            // verify the vending machine
            var address = WalletService.GetAddress(addressArgument.Value);

            if (!LedgerService.LedgerManager.LedgerState.TryGetAccount(address, out var account))
            {
                Console.WriteLine($"The account {address} does not exist");
                return;
            }

            var type = account.Address.Type;

            if (type != AddressType.VendingMachine)
            {
                Console.WriteLine($"The account is not a vending machine : {Enum.GetName(type.GetType(), type)}");
                return;
            }

            var machine = account.GetDeclaration <VendingMachine>();

            if (machine == null)
            {
                Console.WriteLine("The account is not declared");
                return;
            }

            // compute the currency out
            var currency = Currency.FromSymbol(currencyArgument.Value);
            var amount   = Amount.FromWholeDecimal(Convert.ToDecimal(amountArgument.Value));

            Amount amountIn, amountOut;

            var rate = machine.Rate;

            if (machine.CurrencyIn == currency)
            {
                amountIn  = amount;
                amountOut = Amount.Divide(amount, rate);
            }
            else if (machine.CurrencyOut == currency)
            {
                amountOut = amount;
                amountIn  = Amount.Multiply(amount, rate);
            }
            else
            {
                Console.WriteLine($"The machine can only receive {machine.CurrencyIn} and will only send {machine.CurrencyOut}");
                return;
            }

            var currencyIn  = machine.CurrencyIn;
            var currencyOut = machine.CurrencyOut;

            // ask for confirmation
            Console.WriteLine($"Are you sure you cant to exchange {Amount.ToWholeDecimal(amountIn)} {Currency.ToSymbol(currencyIn)} against {Amount.ToWholeDecimal(amountOut)} {Currency.ToSymbol(currencyOut)} ? [y/n]");
            if (Console.ReadKey(true).Key != ConsoleKey.Y)
            {
                Console.WriteLine("Aborted...");
                return;
            }

            // create transaction
            var buyer  = WalletService.GetActiveKey().Data.Address;
            var vendor = machine.Address;

            var declarations = new List <TxDeclaration> {
            };
            var inputs       = new List <TxInput> {
                new TxInput(buyer, currencyIn, amountIn), new TxInput(vendor, currencyOut, amountOut)
            };
            var outputs = new List <TxOutput> {
                new TxOutput(vendor, currencyIn, amountIn), new TxOutput(buyer, currencyOut, amountOut)
            };

            var fees = WalletService.CreateFeesInput(buyer);

            var transaction = new Transaction(declarations, inputs, outputs, TransactionMessage.Empty, DateTime.UtcNow.AddMinutes(1).ToUnixTimestamp(), fees);

            if (WalletService.SignAndSubmit(transaction))
            {
                Console.WriteLine($"Successfully sent transaction");
            }
        }