Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
raw tx create/send
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Sep 24, 2018
1 parent 2a442ae commit 0820058
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
5 changes: 3 additions & 2 deletions code/transaction_raw_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func main() {
}

ts := types.Transactions{signedTx}
rawTx := hex.EncodeToString(ts.GetRlp(0))
rawTxBytes := ts.GetRlp(0)
rawTxHex := hex.EncodeToString(rawTxBytes)

fmt.Printf(rawTx) // f86...772
fmt.Printf(rawTxHex) // f86...772
}
4 changes: 2 additions & 2 deletions code/transaction_raw_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func main() {

rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772"

var tx *types.Transaction

rawTxBytes, err := hex.DecodeString(rawTx)

tx := new(types.Transaction)
rlp.DecodeBytes(rawTxBytes, &tx)

err = client.SendTransaction(context.Background(), tx)
Expand Down
4 changes: 4 additions & 0 deletions en/GLOSSARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ Sets of formal rules describing how to transmit or exchange data, especially acr

A proof-of-authority testnet on the Ethereum blockchain. Supported by Geth only.

## RLP

[Recursive Length Prefix](https://github.com/ethereum/wiki/wiki/RLP) (RLP) is a standard to encode arbitrarily nested arrays of binary data. RLP is the main encoding method used to serialize objects in Ethereum.

## Ropsten

A proof-of-work testnet on the Ethereum blockchain which best simulates production environment. Supported by Geth and Parity.
Expand Down
41 changes: 39 additions & 2 deletions en/transaction-raw-create/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@ description: Tutorial on how to create a raw Ethereum transaction with Go.

# Create Raw Transaction

If you've read the [previous sections](../transfer-eth), then you know how to load your private key to sign transactions. We'll assume you know how to do that by now and now you want to get the raw transaction data to be able to broadcast it at a later time.

First construct the transaction object and sign it, for example:

```go
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)

signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
```

Now before we can get the transaction in raw bytes format we'll need to initialize a `types.Transactions` type with the signed transaction as the first value.

```go
ts := types.Transactions{signedTx}
```

The reason for doing this is because the `Transactions` type provides a `GetRlp` method for returning the transaction in RLP encoded format. RLP is a special encoding method Ethereum uses for serializing objects. The result of this is raw bytes.

```go
rawTxBytes := ts.GetRlp(0)
```

Finally we can very easily turn the raw bytes into a hex string.

```go
rawTxHex := hex.EncodeToString(rawTxBytes)

fmt.Printf(rawTxHex)
// f86d8202b38477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ba0699ff162205967ccbabae13e07cdd4284258d46ec1051a70a51be51ec2bc69f3a04e6944d508244ea54a62ebf9a72683eeadacb73ad7c373ee542f1998147b220e
```

And now you have the raw transaction data which you can use to broadcast at a future date. In the [next section](../transaction-raw-send) we'll learn how to do broadcast a raw transaction.

---

### Full code
Expand Down Expand Up @@ -72,8 +108,9 @@ func main() {
}

ts := types.Transactions{signedTx}
rawTx := hex.EncodeToString(ts.GetRlp(0))
rawTxBytes := ts.GetRlp(0)
rawTxHex := hex.EncodeToString(rawTxBytes)

fmt.Printf(rawTx) // f86...772
fmt.Printf(rawTxHex) // f86...772
}
```
34 changes: 32 additions & 2 deletions en/transaction-raw-send/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ description: Tutorial on how to send a raw Ethereum transaction with Go.

# Send Raw Transaction

In the [previous section](../transaction-raw-create) we learned how to do create a raw transaction. Now we'll learn how to broadcast it to the Ethereum network in order for it to get processed and mined.

First decode the raw transaction hex to bytes format.

```go
rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772"

rawTxBytes, err := hex.DecodeString(rawTx)
```

Now initialize a new `types.Transaction` pointer and call `DecodeBytes` from the go-ethereum `rlp` package passing it the raw transaction bytes and the pointer to the ethereum transaction type. RLP is an encoding method used by Ethereum to serialized and derialized data.

```go
tx := new(types.Transaction)
rlp.DecodeBytes(rawTxBytes, &tx)
```

Now we can easily broadcast the transaction with our ethereum client.

```go
err := client.SendTransaction(context.Background(), tx)
if err != nil {
log.Fatal(err)
}

fmt.Printf("tx sent: %s", tx.Hash().Hex()) // tx sent: 0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f
```

You can see the transaction on etherscan: [https://rinkeby.etherscan.io/tx/0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f](https://rinkeby.etherscan.io/tx/0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f)

---

### Full code
Expand Down Expand Up @@ -32,9 +62,9 @@ func main() {

rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772"

var tx *types.Transaction

rawTxBytes, err := hex.DecodeString(rawTx)

tx := new(types.Transaction)
rlp.DecodeBytes(rawTxBytes, &tx)

err = client.SendTransaction(context.Background(), tx)
Expand Down

0 comments on commit 0820058

Please sign in to comment.