-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a new ethereum client in flow and connected to it
- Loading branch information
1 parent
963b358
commit 32aef99
Showing
5 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package blockchain | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/ethclient" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
type Client struct { | ||
ethClient *ethclient.Client | ||
privateKey *ecdsa.PrivateKey | ||
} | ||
|
||
func NewClient() (*Client, error) { | ||
// Load .env file | ||
err := godotenv.Load() | ||
if err != nil { | ||
return nil, fmt.Errorf("error loading .env file: %v", err) | ||
} | ||
|
||
// Connect to Ethereum client | ||
alchemyURL := os.Getenv("ALCHEMY_API_URL") | ||
if alchemyURL == "" { | ||
return nil, fmt.Errorf("ALCHEMY_API_URL is not set in .env file") | ||
} | ||
|
||
ethClient, err := ethclient.Dial(alchemyURL) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to the Ethereum client: %v", err) | ||
} | ||
|
||
// Load private key | ||
privateKeyHex := os.Getenv("PRIVATE_KEY") | ||
privateKey, err := crypto.HexToECDSA(privateKeyHex) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid private key: %v", err) | ||
} | ||
|
||
return &Client{ethClient: ethClient, privateKey: privateKey}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters