Example #1
0
        // For sure this should not be here at all. As soon as there is a decoupled way to abiEncode, got to extract this to a wrapper (e.g.: adapter or a factory method on TransactionRpc itself.
        private async Task <Rpc.Transaction> MapTransactionToRpc(TransactionRequest tx)
        {
            Rpc.Transaction result = new Rpc.Transaction();

            result.Data = tx.Data == null || tx.Data.Length < 2 ? "0x" : tx.Data;
            if (!String.IsNullOrEmpty(tx.Function))
            {
                string fnData = await AbiEncode(tx.Function, tx.Params);

                if (fnData != null && fnData.Length > 2 && fnData.StartsWith("0x"))
                {
                    result.Data += fnData.Substring(2 + (result.Data.Length > 2 ? 8 : 0));
                }
            }

            result.To   = tx.To;
            result.From = tx.From;
            if (tx.Value.HasValue)
            {
                result.Value = DataTypeConverter.BigIntToPrefixedHex(tx.Value.Value);
            }

            if (tx.Nonce.HasValue)
            {
                result.Nonce = DataTypeConverter.BigIntToPrefixedHex(tx.Nonce.Value);
            }

            if (tx.Gas.HasValue)
            {
                result.Gas = DataTypeConverter.BigIntToPrefixedHex(tx.Gas.Value);
            }

            if (tx.GasPrice.HasValue)
            {
                result.GasPrice = DataTypeConverter.BigIntToPrefixedHex(tx.GasPrice.Value);
            }

            if (result.Data == null || result.Data.Length < 2)
            {
                result.Data = "0x";
            }
            else
            {
                result.Data = DataTypeConverter.AddHexPrefixer(result.Data);
            }

            return(result);
        }