Exemplo n.º 1
0
        private void OnCommandTransfer(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, string error) = await web4b.GetAddressByUidAsync(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;

                (string txid, string err) = await web4b.SendTransferAsync(sender.Key, to, value, gas, nonce);
                BConsole.WriteLine("txid=", txid);
            });
        }
Exemplo n.º 2
0
        public async Task ShouldPassCyprusTransactionTest(string from, string to)
        {
            PrivateKey sender = from, receiver = to;
            string     error, txid;
            bool?      mining;
            ulong?     balance;
            JObject    receipt, tx;
            JArray     history;

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

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

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

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

            // send transfer
            (txid, error) = await cyprus.SendTransferAsync(sender, receiver.Address, Coin.ToBeryl(0.0001m));

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

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

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

            // get transaction
            (tx, error) = await cyprus.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 cyprus.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 cyprus.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 cyprus.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 cyprus.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);

            // send withdraw
            (txid, error) = await cyprus.SendWithdrawAsync(sender, sender.Address, Coin.ToBeryl(0.1m));

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

            // wait for withdraw confirm
            (receipt, error) = await cyprus.WaitTransactionReceiptAsync(txid);

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