private void OnCommandSendTx(string[] args)
        {
            string name = args[0];

            if (!wallets.TryGetValue(name, out Account sender))
            {
                BConsole.WriteLine("account name (", name, ") not found");
                return;
            }

            if (sender.Locked && !sender.Unlock(BConsole.ReadPassword("password: "******"can't unlock account");
                return;
            }

            Task.Run(async() =>
            {
                string to   = wallets.TryGetValue(args[1], out var receiver) ? (string)receiver.Address : args[1];
                ulong value = Coin.ToBeryl(decimal.Parse(args[2]));
                ulong gas   = args.Length > 3 ? Coin.ToBeryl(decimal.Parse(args[3])) : 0;
                ulong?nonce = args.Length > 4 ? (ulong?)Convert.ToInt64(args[4]) : null;

                var txid = await web4b.SendTransactionAsync(sender.Key, to, value, gas, nonce);

                BConsole.WriteLine("txid=", txid);
            });
        }
Exemple #2
0
        public async Task ShouldPassBe4TransactionTest(string from, string to)
        {
            PrivateKey sender = from, receiver = to;
            string     error, txid;
            bool?      mining;
            ulong?     balance;
            JArray     pending, history;
            JObject    receipt, tx;

            // sender should has at leat 1 brc for transaction test
            (balance, error) = await be4.GetBalanceAsync(sender.Address);

            Assert.True(string.IsNullOrEmpty(error));
            Assert.NotNull(balance);
            Assert.True(balance >= Coin.ToBeryl(1));

            // should mining
            (mining, error) = await be4.GetMiningAsync();

            Assert.True(string.IsNullOrEmpty(error));
            Assert.True(mining);

            // send transaction
            (txid, error) = await be4.SendTransactionAsync(sender, receiver.Address, Coin.ToBeryl(0.0001m));

            Assert.True(string.IsNullOrEmpty(error));
            Assert.False(string.IsNullOrEmpty(txid));

            // pending transaction
            (pending, error) = await be4.GetPendingTransactionsAsync();

            Assert.True(string.IsNullOrEmpty(error));
            Assert.False(pending is null);
            Assert.True(pending.ToObject <IEnumerable <JObject> >().Where(t => t.Value <string>("hash") == txid).Count() > 0);

            // get transaction receipt
            (receipt, error) = await be4.WaitTransactionReceiptAsync(txid);

            Assert.True(string.IsNullOrEmpty(error));
            Assert.False(receipt is null);
            Assert.True(receipt.Value <string>("hash") == txid);

            // get transaction
            (tx, error) = await be4.GetTransactionByHashAsync(txid);

            Assert.True(string.IsNullOrEmpty(error));
            Assert.False(tx is null);
            Assert.True(tx.Value <string>("hash") == txid);

            // get transaction by block hash and index
            (var tx1, string err1) = await be4.GetTransactionByBlockHashAndIndexAsync(tx.Value <string>("blockHash"), Hex.ToNumber <int>(tx.Value <string>("blockIndex")));

            Assert.True(string.IsNullOrEmpty(err1));
            Assert.False(tx1 is null);
            Assert.True(tx1.Value <string>("hash") == txid);

            // get transaction by block number and index
            (var tx2, string err2) = await be4.GetTransactionByBlockNumberAndIndexAsync(Hex.ToNumber <long>(tx.Value <string>("blockNumber")), Hex.ToNumber <int>(tx.Value <string>("blockIndex")));

            Assert.True(string.IsNullOrEmpty(err2));
            Assert.False(tx2 is null);
            Assert.True(tx2.Value <string>("hash") == txid);

            // get transaction history of sender
            (history, error) = await be4.GetTransactionsByAddressAsync(sender.Address, Hex.ToNumber <long>(tx.Value <string>("blockNumber")), true, 20);

            Assert.True(string.IsNullOrEmpty(error));
            Assert.False(history is null);
            Assert.True(history.ToObject <IEnumerable <JObject> >().Where(t => t.Value <string>("hash") == txid).Count() > 0);

            // get transaction history of receiver
            (history, error) = await be4.GetTransactionsByAddressAsync(receiver.Address, Hex.ToNumber <long>(tx.Value <string>("blockNumber")), false, 20);

            Assert.True(string.IsNullOrEmpty(error));
            Assert.False(history is null);
            Assert.True(history.ToObject <IEnumerable <JObject> >().Where(t => t.Value <string>("hash") == txid).Count() > 0);
        }