Exemplo n.º 1
0
        private async Task <TransactionExtention> CreateTransactionAsync(string from, string to, long amount)
        {
            var fromAddress = Base58Encoder.DecodeFromBase58Check(from);
            var toAddress   = Base58Encoder.DecodeFromBase58Check(to);

            var transferContract = new TransferContract
            {
                OwnerAddress = ByteString.CopyFrom(fromAddress),
                ToAddress    = ByteString.CopyFrom(toAddress),
                Amount       = amount
            };

            var transaction = new Transaction();

            var contract = new Transaction.Types.Contract();

            try
            {
                contract.Parameter = Google.Protobuf.WellKnownTypes.Any.Pack(transferContract);
            }
            catch (Exception)
            {
                return(new TransactionExtention
                {
                    Result = new Return {
                        Result = false, Code = Return.Types.response_code.OtherError
                    },
                });
            }
            var newestBlock = await _wallet.GetNowBlock2Async(new EmptyMessage());

            contract.Type       = Transaction.Types.Contract.Types.ContractType.TransferContract;
            transaction.RawData = new Transaction.Types.raw();
            transaction.RawData.Contract.Add(contract);
            transaction.RawData.Timestamp  = DateTime.Now.Ticks;
            transaction.RawData.Expiration = newestBlock.BlockHeader.RawData.Timestamp + 10 * 60 * 60 * 1000;
            var blockHeight = newestBlock.BlockHeader.RawData.Number;
            var blockHash   = Sha256Sm3Hash.Of(newestBlock.BlockHeader.RawData.ToByteArray()).GetBytes();

            var bb = ByteBuffer.Allocate(8);

            bb.PutLong(blockHeight);

            var refBlockNum = bb.ToArray();

            transaction.RawData.RefBlockHash  = ByteString.CopyFrom(blockHash.SubArray(8, 8));
            transaction.RawData.RefBlockBytes = ByteString.CopyFrom(refBlockNum.SubArray(6, 2));

            var transactionExtension = new TransactionExtention
            {
                Transaction = transaction,
                Txid        = ByteString.CopyFromUtf8(transaction.GetTxid()),
                Result      = new Return {
                    Result = true, Code = Return.Types.response_code.Success
                },
            };

            return(transactionExtension);
        }
Exemplo n.º 2
0
        public async Task TestTransferAsync()
        {
            var transactionClient = _record.ServiceProvider.GetService <ITransactionClient>();
            var privateKey        = "8e812436a0e3323166e1f0e8ba79e19e217b2c4a53c970d4cca0cfb1078979df";
            var tronKey           = new TronECKey(privateKey, _record.Options.Value.Network);
            var from   = tronKey.GetPublicAddress();
            var to     = "TGehVcNhud84JDCGrNHKVz9jEAVKUpbuiv";
            var amount = 1_000_000L; // 1 TRX, api only receive trx in Sun, and 1 trx = 1000000 Sun

            var fromAddress = Base58Encoder.DecodeFromBase58Check(from);
            var toAddress   = Base58Encoder.DecodeFromBase58Check(to);

            var block = await _wallet.GetNowBlock2Async(new EmptyMessage());

            var transaction = await transactionClient.CreateTransactionAsync(from, to, amount);

            Assert.True(transaction.Result.Result);

            var transactionSigned = transactionClient.GetTransactionSign(transaction.Transaction, privateKey);

            var result = await transactionClient.BroadcastTransactionAsync(transactionSigned);

            Assert.True(result.Result);
        }