Ethereum Transaction


Ethereum uses an accounting system where values in WEI (which is smallest unit of ethereum, 1 WEI = 0.000000000000000001 ETH) are debited from accounts and credited to another, as opposed to Bitcoin's UTXO system, which is more analogous to spending cash and receiving change in return.

Generally, What makes Ethereum different is that the transaction also has a DATA field. This DATA field enables three types of transactions:

Transfer of ETH value
  • TO recipient address
  • DATA field empty or containing any message you want to attach
  • FROM you
  • VALUE is ETH amount you want to send
  • GAS LIMIT varies
    1) For normal ETH send to EOA address, gas limit is fixed with 21,000.
    2) For ETH send to contract address with simplest form of accept ETH deposit function() external payable { }, gas limit consume 21,076 .
    3) Continue point no. 2. The more complicated the ETH deposit function, the higher the gas will be used. you may use tool to estimate it.
Create smart contract
  • TO field is empty (this is what triggers the creation of a smart contract)
  • DATA field contains smart contract code compiled to byte-code
  • FROM you
  • VALUE can be zero or ETH amount you might want to give to your contract
Call smart contract
  • TO field is the address of the smart contract account
  • DATA field contains function name and parameters - how to call the smart contract
  • FROM you
  • VALUE can be zero or ETH amount you want pay for contract service.


Raw Tx Generator

<?php 
use kornrunner\Ethereum\Transaction;
use kornrunner\Keccak;

include_once "../libraries/vendor/autoload.php";
include_once("html_iframe_header.php");
include_once("eth_utils.php");

define("GWEI_TO_WEI",'1000000000');
define("ETH_TO_WEI",'1000000000000000000');

$supportChains = ['1'=>"Ethereum Mainnet", '3'=>"Ethereum Testnet Ropsten", '56'=>"BSC Mainnet", "97"=>"BSC Testnet"];

unset($_REQUEST);
$_REQUEST = array("chain"=>"","nonce"=>"","gas_price"=>"","gas_limit"=>"","to"=>"","data"=>"","value"=>"","privkey"=>"");
$disableFields = explode(",", $_GET["disable_fields"]);
foreach($_REQUEST as $k=>$v) {
	if (isset($_POST[$k])) {
		$_REQUEST[$k] = $_POST[$k];
	} else {
		$_REQUEST[$k] = $_GET[$k];
	}
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    try {
		
		$nonce = bcdechex($_REQUEST['nonce']);
		$gasPrice = bcdechex(bcmul($_REQUEST['gas_price'],GWEI_TO_WEI, 18));
		$gasLimit = bcdechex($_REQUEST['gas_limit']);
		$to = $_REQUEST['to'];
		$value = bcdechex(bcmul($_REQUEST['value'],ETH_TO_WEI, 18));
		
		if (strlen(trim($_REQUEST['data']))) {
			$data = trim($_REQUEST['data']);
		} else {
			$data = "";
		}
		
		$transaction = new Transaction ($nonce, $gasPrice, $gasLimit, $to, $value,$data);
		$rawTx = $transaction->getRaw($_REQUEST['privkey'], $_REQUEST['chain']);
		$txHash = Keccak::hash(hex2bin($rawTx), 256);
    ?>
       <div class="alert alert-success">
			<h6 class="mt-3">Final TX Hex</h6>
			<textarea class="form-control" rows="5" id="comment"><?php echo $rawTx;?></textarea>
			<p>
			<?php
			
			if ($_REQUEST['chain'] == "1") {
			?>
				* <a href="https://etherscan.io/pushTx" target="_blank">https://etherscan.io/pushTx</a>
			<?php
			} else {
			?>
			
				* <a href="https://ropsten.etherscan.io/pushTx" target="_blank">https://ropsten.etherscan.io/pushTx</a>
				
			<?php
			}
			?>
			</p>
			
			<h6 class="mt-3">TX Hash</h6>
			<input class="form-control" readonly value="<?php echo $txHash?>"/>
		</div>
<?php 
    } catch (Exception $e) {
        $errmsg .= "Problem found. " . $e->getMessage();

    }
} 

if ($errmsg) {
?>
    <div class="alert alert-danger">
        <strong>Error!</strong> <?php echo $errmsg?>
    </div>
<?php
}

?>
<form action='<?php echo $_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : ""?>' method='post'>

	
	<div class="form-group">
		<label for="chain">Chain:</label>
		<select id="chain" name="chain" class="form-control"<?php echo in_array("chain", $disableFields) ? " readonly" : ""?>>
			<?php
			foreach($supportChains as $k=>$v) {
				echo "<option value='{$k}'".($k == $_REQUEST['chain'] ? " selected": "").">{$v}</option>";
			}
			?>
		</select>
	</div>
	
	 <div class="form-group">
        <label for="nonce">Nonce:</label>
        <input class="form-control" type='text' name='nonce' id='nonce' value='<?php echo $_REQUEST['nonce']?>' <?php echo in_array("nonce", $disableFields) ? " readonly" : ""?>>
		
		<small>
			Learn more <a href='eth_tx_nonce.php' target='_blank'>here</a>.
		</small>
    </div>
	
	<div class="form-group">
		<label for="gas_price">Gas Price:</label>
		
		<div class="input-group mb-3">
			<input class="form-control" type='text' name='gas_price' id='gas_price' value='<?php echo $_REQUEST['gas_price']?>' <?php echo in_array("gas_price", $disableFields) ? " readonly" : ""?>>
			<div class="input-group-append">
			  <span class="input-group-text">GWEI</span>
			</div>
		</div>
	</div>
	
	 <div class="form-group">
        <label for="gas_limit">Gas Limit:</label>
        <input class="form-control" type='text' name='gas_limit' id='gas_limit' value='<?php echo $_REQUEST['gas_limit']?>' <?php echo in_array("gas_limit", $disableFields) ? " readonly" : ""?>>
    </div>
	
	
    <div class="form-group">
        <label for="to">To:</label>
        <input placeholder="Address" class="form-control" type='text' name='to' id='to' value='<?php echo $_REQUEST['to']?>' <?php echo in_array("to", $disableFields) ? " readonly" : ""?>>
    </div>
	
	<div class="form-group">
        <label for="data">Data (Hex):</label>
        <input class="form-control" type='text' name='data' id='data' value='<?php echo $_REQUEST['data']?>' <?php echo in_array("data", $disableFields) ? " readonly" : ""?>>
    </div>
   
   <div class="form-group">
		<label for="value">ETH Value:</label>
		
		<div class="input-group mb-3">
			<input class="form-control" type='text' name='value' id='value' value='<?php echo $_REQUEST['value']?>' <?php echo in_array("value", $disableFields) ? " readonly" : ""?>>
			<div class="input-group-append">
			  <span class="input-group-text">ETH</span>
			</div>
		</div>
	</div>
	
    <div class="form-group">
        <label for="privkey">From:</label>
        <input placeholder="Sender's Private Key (Hex)" class="form-control" type='text' name='privkey' id='privkey' value='<?php echo $_REQUEST['privkey']?>' <?php echo in_array("privkey", $disableFields) ? " readonly" : ""?>>
    </div>
   
    <input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");
go/eth_tx_form.go

package main

import(
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"math/big"
	"strconv"
	"encoding/hex"	
	"strings"
)

func genSignedTx(chainIdStr string, nonceStr string, valueStr string, gasLimitStr string, toStr string, dataStr string, gasPriceStr string, privKeyStr string) (map[string]string, error) {
	
	var thisError error 
	response := make(map[string]string)
	
	chainId     := chainIdStr
	privKey, _  := crypto.HexToECDSA(privKeyStr)
	to          := common.HexToAddress(toStr)
	nonce, _    := strconv.ParseUint(nonceStr, 10, 64)
	gasLimit, _ := strconv.ParseUint(gasLimitStr,10,64)
	
	
	value, _    := new(big.Int).SetString(EthToWei(valueStr),10)
	gasPrice, _ := new(big.Int).SetString(GweiToWei(gasPriceStr),10)
	signer      := types.LatestSigner( getChainsConfig()[chainId] )
	data, _     := hex.DecodeString(strings.TrimPrefix(dataStr, "0x"))

	tx := types.NewTransaction(uint64(nonce), to, value, gasLimit, gasPrice, data)
	signedTx, _ := types.SignTx(tx, signer, privKey)
	
	rawTx, _ := signedTx.MarshalBinary()
	
	response["hash"] = signedTx.Hash().Hex()
	response["rawTx"] = hex.EncodeToString(rawTx)
	
	return response, thisError
}

go/templates/eth_tx_form.html

{{ template "html_iframe_header.html" .}}

{{if .error}}
	<div class="alert alert-danger">
        <strong>Error!</strong> {{ .error }}
    </div>
{{else if .rawTx}}

	<div class="alert alert-success">
		<h6 class="mt-3">Final TX Hex</h6>
		<textarea class="form-control" rows="5" id="comment">{{.rawTx}}</textarea>
		<p>
		{{if eq .chain "1"}}
			* <a href="https://etherscan.io/pushTx" target="_blank">https://etherscan.io/pushTx</a>
		{{end}}
		
		{{if eq .chain "3"}}
			* <a href="https://ropsten.etherscan.io/pushTx" target="_blank">https://ropsten.etherscan.io/pushTx</a>
		{{end}}
		</p>

		<h6 class="mt-3">TX Hash</h6>
		<input class="form-control" readonly value="{{.hash}}"/>
	</div>
{{end}}

<form action='{{.queryString}}' method='post'>

	<div class="form-group">
		<label for="chain">Chain:</label>
		<select id="chain" name="chain" class="form-control"{{/*<?php echo in_array("chain", $disableFields) ? " readonly" : ""?>*/}}>
			
			{{ $chain := .chain }}
				
			{{if not $chain}}
				{{$chain = ""}}
			{{end}}
			
			{{ range $key, $value := .chains }}
				<option value='{{$key}}'
				{{if eq $chain $key}}
					selected
				{{end}}
				>{{$value}}</option>
			{{end}}
		</select>
	</div>
	
	 <div class="form-group">
        <label for="nonce">Nonce:</label>
        <input class="form-control" type='text' name='nonce' id='nonce' value='{{.nonce}}' {{/*<?php echo in_array("nonce", $disableFields) ? " readonly" : ""?>*/}}>
		
		<small>
			Learn more <a href='eth_tx_nonce.php' target='_blank'>here</a>.
		</small>
    </div>
	
	<div class="form-group">
		<label for="gas_price">Gas Price:</label>
		
		<div class="input-group mb-3">
			<input class="form-control" type='text' name='gas_price' id='gas_price' value='{{.gas_price}}' {{/*<?php echo in_array("gas_price", $disableFields) ? " readonly" : ""?>*/}}>
			<div class="input-group-append">
			  <span class="input-group-text">GWEI</span>
			</div>
		</div>
	</div>
	
	 <div class="form-group">
        <label for="gas_limit">Gas Limit:</label>
        <input class="form-control" type='text' name='gas_limit' id='gas_limit' value='{{.gas_limit}}' {{/*<?php echo in_array("gas_limit", $disableFields) ? " readonly" : ""?>*/}}>
    </div>
	
	
    <div class="form-group">
        <label for="to">To:</label>
        <input placeholder="Address" class="form-control" type='text' name='to' id='to' value='{{.to}}' {{/*<?php echo in_array("to", $disableFields) ? " readonly" : ""?>*/}}>
    </div>
	
	<div class="form-group">
        <label for="data">Data (Hex):</label>
        <input class="form-control" type='text' name='data' id='data' value='{{.data}}' {{/*<?php echo in_array("data", $disableFields) ? " readonly" : ""?>*/}}>
    </div>
   
   <div class="form-group">
		<label for="value">ETH Value:</label>
		
		<div class="input-group mb-3">
			<input class="form-control" type='text' name='value' id='value' value='{{.value}}' {{/*<?php echo in_array("value", $disableFields) ? " readonly" : ""?>*/}}>
			<div class="input-group-append">
			  <span class="input-group-text">ETH</span>
			</div>
		</div>
	</div>
	
    <div class="form-group">
        <label for="privkey">From:</label>
        <input placeholder="Sender's Private Key (Hex)" class="form-control" type='text' name='privkey' id='privkey' value='{{.privkey}}' {{/*<?php echo in_array("privkey", $disableFields) ? " readonly" : ""?>*/}}>
    </div>
   
    <input type='submit' class="btn btn-success btn-block"/>
</form>

{{ template "html_iframe_footer.html" .}}








Tutorials
About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Community
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.