mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-15 19:31:37 +00:00
cmd/pushtx: add tx cost display to catch insufficient funds before broadcast
Co-authored-by: drQedwards <213266729+drQedwards@users.noreply.github.com>
This commit is contained in:
parent
cc2957508f
commit
84f37d3915
2 changed files with 22 additions and 0 deletions
|
|
@ -156,9 +156,17 @@ func printTxSummary(tx *types.Transaction) {
|
||||||
fmt.Println(" Value: ", formatWei(tx.Value()))
|
fmt.Println(" Value: ", formatWei(tx.Value()))
|
||||||
fmt.Println(" Gas limit:", tx.Gas())
|
fmt.Println(" Gas limit:", tx.Gas())
|
||||||
fmt.Println(" Gas price:", formatGwei(tx.GasPrice()))
|
fmt.Println(" Gas price:", formatGwei(tx.GasPrice()))
|
||||||
|
fmt.Println(" Tx cost: ", formatWei(txCost(tx)))
|
||||||
fmt.Println(" Chain ID: ", tx.ChainId())
|
fmt.Println(" Chain ID: ", tx.ChainId())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// txCost returns value + gas * gasPrice, i.e. the total ETH the sender
|
||||||
|
// must hold for the transaction to be accepted by the network.
|
||||||
|
func txCost(tx *types.Transaction) *big.Int {
|
||||||
|
gasCost := new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), tx.GasPrice())
|
||||||
|
return new(big.Int).Add(tx.Value(), gasCost)
|
||||||
|
}
|
||||||
|
|
||||||
// formatWei converts a wei amount to a human-readable string showing
|
// formatWei converts a wei amount to a human-readable string showing
|
||||||
// both the wei value and the ETH equivalent.
|
// both the wei value and the ETH equivalent.
|
||||||
func formatWei(wei *big.Int) string {
|
func formatWei(wei *big.Int) string {
|
||||||
|
|
|
||||||
|
|
@ -272,6 +272,20 @@ func TestFormatGwei(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTxCost(t *testing.T) {
|
||||||
|
tx := types.NewTx(&types.LegacyTx{
|
||||||
|
GasPrice: big.NewInt(1_000_000_000), // 1 Gwei
|
||||||
|
Gas: 21055,
|
||||||
|
Value: new(big.Int).Mul(big.NewInt(10), big.NewInt(1e18)), // 10 ETH
|
||||||
|
})
|
||||||
|
got := txCost(tx)
|
||||||
|
// Expected: 10 ETH + 21055 * 1 Gwei = 10000000000000000000 + 21055000000000 = 10000021055000000000
|
||||||
|
want, _ := new(big.Int).SetString("10000021055000000000", 10)
|
||||||
|
if got.Cmp(want) != 0 {
|
||||||
|
t.Errorf("txCost = %s, want %s", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunEqualsSyntax(t *testing.T) {
|
func TestRunEqualsSyntax(t *testing.T) {
|
||||||
srv := fakeRPC(t, false)
|
srv := fakeRPC(t, false)
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue