-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (68 loc) · 2.28 KB
/
Copy pathmain.go
File metadata and controls
83 lines (68 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"log"
"math/big"
"os"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
)
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
// Connect to an Ethereum client
client, err := ethclient.Dial("https://mainnet.infura.io/v3/" + os.Getenv("INFURA_PROJECT_ID"))
if err != nil {
log.Fatalf("Failed to connect to the Ethereum client: %v", err)
}
// Load the Uniswap contract ABI
uniswapABI := os.Getenv("UNISWAP_CONTRACT_ABI")
// Parse the contract ABI
contractABI, err := abi.JSON(strings.NewReader(uniswapABI))
if err != nil {
log.Fatalf("Failed to parse contract ABI: %v", err)
}
// Create a new Uniswap contract instance
contractAddress := os.Getenv("UNISWAP_CONTRACT_ADDRESS")
uniswapContract, err := NewUniswap(contractAddress, client)
if err != nil {
log.Fatalf("Failed to instantiate Uniswap contract: %v", err)
}
// Set up the transaction parameters
privateKey, err := crypto.HexToECDSA(os.Getenv("PRIVATE_KEY"))
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
fromAddress := os.Getenv("FROM_ADDRESS")
toAddress := os.Getenv("TO_ADDRESS")
amount := big.NewInt(0)
amount.SetString(os.Getenv("AMOUNT"), 10)
gasLimit := uint64(0)
gasLimit, _ = strconv.ParseUint(os.Getenv("GAS_LIMIT"), 10, 64)
gasPrice := big.NewInt(0)
gasPrice.SetString(os.Getenv("GAS_PRICE"), 10)
// Create a new transaction
nonce, err := client.PendingNonceAt(context.Background(), common.HexToAddress(fromAddress))
if err != nil {
log.Fatalf("Failed to retrieve nonce: %v", err)
}
tx := types.NewTransaction(nonce, common.HexToAddress(toAddress), amount, gasLimit, gasPrice, nil)
// Sign the transaction
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(big.NewInt(1)), privateKey)
if err != nil {
log.Fatalf("Failed to sign transaction: %v", err)
}
// Send the transaction
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatalf("Failed to send transaction: %v", err)
}
log.Printf("Transaction sent: %s", signedTx.Hash().Hex())
}