Infura currently doesn't support WebSockets (required for events using Web3 v1, otherwise you get the error "The current provider doesn't support subscriptions" when using infura as HttpProvider), so what you have to do is run a local geth node that connects and syncs to the network.
Here we enable the websocket flag and allow any origin to connect to the local geth node that is syncing with the rinkeby testnet:
geth --rinkeby --ws --wsport=8546 --wsorigins="*" --datadir=$HOME/.rinkeby --cache=512 --rpc --rpcapi="personal,eth,network" --rpcport=8545 --fast --bootnodes=enode://a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf@52.169.42.101:30303
To check the syncing status:
geth attach ipc:$HOME/.rinkeby/geth.ipc
> eth.syncing
Once it's done syncing you can connect using the WebsocketProvider
:
const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'))
You should now be able to receive events:
myContract.events.allEvents((error, event) => {
if (error) {
console.error(error)
return false
}
console.log(event)
})
Tested this with geth v1.6.5
and web3 v1.0.0
.
Update Oct 11, 2017:
Infura is experimenting with websocket support. You can ask for beta access in this github issue thread:
https://github.com/INFURA/infura/issues/29
Here's a working example of sending a raw transaction using the go-ethereum package to the ropsten infura gateway:
package main
import (
"context"
"crypto/ecdsa"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://ropsten.infura.io")
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
value := new(big.Int)
value.SetString("100000000000000000", 10) // in wei (0.1 eth)
gasLimit := uint64(21000) // in units
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
toAddress := common.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d")
var data []byte
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, privateKey)
if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s", signedTx.Hash().Hex())
}
You can see the transaction here https://ropsten.etherscan.io/tx/0xdf72fd028cc0d49a2acd854d922422cb629492a2db2248150cb44e0f228cccfb
Best Answer
For anyone using ethers.js V6, you can use the provider.broadcastTransaction function.