I want to send and broadcast a signed transaction by sender to the Ropsten network through INFURA in Golang.
I was able to send a signed transaction and recieved a response from INFURA as per below, however, this transaction hash seems not to be in Ropsten network because this cannot be found in https://ropsten.etherscan.io/.
{"jsonrpc":"2.0","id":69,"result":"0x44814c40f6193cb73d6f7aa2d0109c57e4586730564e1b08c3477f1b4e33d45c"}
Much appreciate if someone give me advice how signed transaction can be sent to Ropsten through INFURA.
Please refer to below my sample code.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/big"
"net/http"
"strings"
"unsafe"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
func SendRawTransaction(amount int64) (address string) {
url := "https://api.infura.io/v1/jsonrpc/ropsten" //According to https://infura.io/docs/api/post/network
from := "0x98263A537a237364E9e835Af210acDF90efC2223"
to := "0xab60821b9894fF2787077E6d9ef6ee997b60e350"
//Preparing sign method
chainID := big.NewInt(3) //ropsten
prvKey := "7a01f6905db8252701220775de0190636b8956672417776e66c04670388df799" //PrivateKey of "from"
senderPrivKey, _ := crypto.HexToECDSA(prvKey)
recipientAddr := common.HexToAddress(to)
nonce := uint64(20)
amount := big.NewInt(100000000000000000) //ETH0.1
gasLimit := uint64(100000)
gasPrice := big.NewInt(20000000000)
/***** Preparing Data struct of request body for send_RawTransaction *****/
type Data struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []string `json:"params"`
ID int `json:"id"`
}
data := Data{}
data.Jsonrpc = "2.0"
data.Method = "eth_sendRawTransaction"
data.ID = 69
/***** Preparing params struct for Data.Params *****/
params := Params{}
params.From = from
params.To = to
/***** Preparing signed transaction *****/
tx := types.NewTransaction(nonce, recipientAddr, amount, gasLimit, gasPrice, nil)
signer := types.NewEIP155Signer(chainID)
signedTx, _ := types.SignTx(tx, signer, senderPrivKey)
/***** Encoding signed transaction by RLP *****/
var buff bytes.Buffer
signedTx.EncodeRLP(&buff)
tmp := fmt.Sprintf("0x%x", buff.Bytes())
/***** Adding Params to data *****/
data.Params = append(data.Params, tmp)
/***** *****/
jsonStrByte, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
}
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonStrByte))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(req)
defer func() {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}()
if err != nil {
fmt.Println("err: ", err)
}
type Response struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result string `json:"result"`
}
var response Response
temp := json.NewDecoder(res.Body)
err = temp.Decode(&response)
if err != nil {
fmt.Println("err: ", err)
panic(err.Error)
}
defer res.Body.Close()
fmt.Println(response)
return response.Result
}