package watcher import ( "math/big" "testing" token "github.com/mycompany/myapp/pkg/erc20" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethclient" "github.com/shopspring/decimal" log "github.com/sirupsen/logrus" ) func decimalsToWei(amount decimal.Decimal, decimals int) *big.Int { mul := decimal.NewFromFloat(float64(10)).Pow(decimal.NewFromFloat(float64(decimals))) result := amount.Mul(mul) wei := new(big.Int) wei.SetString(result.String(), 10) return wei } func sendTokens() { client, err := ethclient.Dial("https://rinkeby.infura.io:443") if err != nil { log.Fatal(err) } // rinkeby amount := decimalsToWei(decimal.NewFromFloat(1), int(18)) // send 1 tokens priv := "a1ea7b205141b739070598cbc7f152cc49f69dfc6ee3cc98363dcd5ae696cbbf" key, err := crypto.HexToECDSA(priv) if err != nil { log.Fatal(err) } auth := *bind.NewKeyedTransactor(key) auth.GasLimit = uint64(350000) // units auth.GasPrice = big.NewInt(40000000000) // wei (40 gwei) auth.Value = big.NewInt(0) auth.Nonce = nil // must be nil tokenAddress := common.HexToAddress("0x265712b02d982dca0ec2488aba707d06c1128baf") toAddress := common.HexToAddress("0x54282fcD78070ff03e20c56786fE46eaF4c48884") instance, err := token.NewToken(tokenAddress, client) if err != nil { log.Fatal(err) } tx, err := instance.Transfer(&auth, toAddress, amount) if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) } log.Printf("tx: %s", tx.Hash().Hex()) } func TestWatcher(t *testing.T) { t.Parallel() sendTokens() s := New(&NewInput{ ProviderURL: "https://rinkeby.infura.io:443", }) outputChannel := make(chan *WatchOnChainTransactionsOutput) err := s.WatchOnChainTransactions(&WatchOnChainTransactionsInput{ ToAddress: "0x54282fcD78070ff03e20c56786fE46eaF4c48884", OutputChannel: outputChannel, }) if err != nil { t.Fatal(err) } output := <-outputChannel log.Println(output) }