mjolnir

package module
v0.0.0-...-dc9146d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2024 License: MIT Imports: 5 Imported by: 0

README

mjolnir

Go package to interact with EVM networks.

⚠️ Warning

The current implementation may not be suitable for production use!

🎨 Inspiration

This project is inspired by Viem. The design and structure of the client closely resemble their approach, aiming to bring similar ease of use and flexibility to Go developers working with EVM networks.

📖 Documentation

Documentation is coming soon! Stay tuned for detailed guides and examples.

📚 Example Usage

Here’s a basic example of how to use this package:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math/big"

	"github.com/sunsetlover36/mjolnir"
	"github.com/sunsetlover36/mjolnir/types"
)

func main() {
	// Convert your private key to an account
	account, err := mjolnir.PrivateKeyToAccount("YOUR_PRIVATE_KEY")
	if err != nil {
		log.Fatalf("Failed to convert private key to account: %v", err)
	}

	// Initialize a new wallet client with specified chain and RPC URL
	wc := mjolnir.NewWalletClient(types.NewWalletClientParams{
		Chain:   mjolnir.Chains["Base"],   // Set the correct chain
		RpcUrl:  "YOUR_RPC_URL",           // Replace with your actual RPC URL
		Account: account,
	})

	// Parse the ether value (in this case, 0.1 ETH)
	parsedEther, err := mjolnir.ParseEther("0.1")
	if err != nil {
		log.Fatalf("Failed to parse ether value: %v", err)
	}

	// Send a transaction
	txHash, err := wc.SendTx(&types.TxInteractionParams{
		TxData: &types.TxData{
			To:    "TO_ADDRESS", // Replace with the recipient address
			Value: parsedEther,
		},
	})
	if err != nil {
		log.Fatalf("Failed to send transaction: %v", err)
	}
	fmt.Println("Transaction Hash:", txHash)

	// Read the balance from a token contract
	balanceBytes, err := wc.ReadContract(types.ReadContractParams{
		Address:      "TOKEN_ADDRESS", // Replace with the token contract address
		Abi:          TOKEN_ABI,       // Replace with the ABI of the token contract
		FunctionName: "balanceOf",     // Function to call on the contract
		Args:         []interface{}{account.Address},
	})
	if err != nil {
		log.Fatalf("Failed to read contract: %v", err)
	}

	var balance *big.Int
	if err := json.Unmarshal(balanceBytes, &balance); err != nil {
		log.Fatalf("Failed to unmarshal balanceBytes: %v", err)
	}
	fmt.Printf("Token Balance: %v\n", balance)
}

✅ TODO

  • Refactor all methods to use pointers for params
  • Improved and more understandable aggregated error logs

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Chains = map[string]types.Chain{
	"Base": {
		Id:     8453,
		Name:   "Base Mainnet",
		RpcUrl: "https://base.llamarpc.com",
	},
	"Ethereum": {
		Id:     1,
		Name:   "Ethereum Mainnet",
		RpcUrl: "https://eth.llamarpc.com",
	},
	"Polygon": {
		Id:     137,
		Name:   "Polygon Mainnet",
		RpcUrl: "https://polygon.llamarpc.com",
	},
}

Functions

func FormatEther

func FormatEther(wei *big.Int) string

func FormatGwei

func FormatGwei(wei *big.Int) string

func GeneratePrivateKey

func GeneratePrivateKey() (string, error)

func ParseEther

func ParseEther(etherStr string) (*big.Int, error)

func ParseGwei

func ParseGwei(gweiStr string) (*big.Int, error)

func PrivateKeyToAccount

func PrivateKeyToAccount(privateKeyHex string) (*types.Account, error)

Types

This section is empty.

Directories

Path Synopsis
client

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL