Skip to content

Unit Conversions

Currency and unit conversion utilities for Ethereum and Starknet tokens.

ETH/Wei Conversions

ETHToWei

Converts an ETH amount to Wei.

Wei is the smallest denomination of Ether, where 1 ETH = 10^18 Wei.

func ETHToWei(eth float64) *felt.Felt
Parameters:
  • eth - The ETH amount to convert (as a float64)
Returns:
  • *felt.Felt - The Wei value as a Felt
Usage Example:
package main
 
import (
	"fmt"
 
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// Convert 1.5 ETH to Wei
	ethAmount := 1.5
	weiAmount := utils.ETHToWei(ethAmount)
	fmt.Printf("%.2f ETH = %s Wei\n", ethAmount, weiAmount)
 
	// Convert 0.001 ETH to Wei
	smallAmount := 0.001
	smallWei := utils.ETHToWei(smallAmount)
	fmt.Printf("%.3f ETH = %s Wei\n", smallAmount, smallWei)
 
	// Common use case: preparing transfer amount
	transferAmount := 0.5
	weiTransfer := utils.ETHToWei(transferAmount)
	fmt.Printf("Transfer amount: %s Wei\n", weiTransfer)
}
Expected Output:
1.50 ETH = 1500000000000000000 Wei
0.001 ETH = 1000000000000000 Wei
Transfer amount: 500000000000000000 Wei

WeiToETH

Converts a Wei amount to ETH.

Wei is the smallest denomination of Ether, where 1 ETH = 10^18 Wei.

func WeiToETH(wei *felt.Felt) float64
Parameters:
  • wei - The Wei amount to convert (as a Felt)
Returns:
  • float64 - The ETH value as a float64
Usage Example:
package main
 
import (
	"context"
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/rpc"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	provider, err := rpc.NewProvider("https://starknet-sepolia.public.blastapi.io/rpc/v0_8")
	if err != nil {
		log.Fatal(err)
	}
 
	// ETH contract on Sepolia
	ethContract, _ := utils.HexToFelt("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")
	accountAddress, _ := utils.HexToFelt("0x...")
 
	// Get balance in Wei
	balanceOfSelector := utils.GetSelectorFromNameFelt("balanceOf")
	result, err := provider.Call(
		context.Background(),
		rpc.FunctionCall{
			ContractAddress:    ethContract,
			EntryPointSelector: balanceOfSelector,
			Calldata:           []*felt.Felt{accountAddress},
		},
		rpc.BlockID{Tag: "latest"},
	)
	if err != nil {
		log.Fatal(err)
	}
 
	// Convert Wei to ETH for display
	weiBalance := result[0]
	ethBalance := utils.WeiToETH(weiBalance)
	fmt.Printf("Balance: %s Wei\n", weiBalance)
	fmt.Printf("Balance: %.6f ETH\n", ethBalance)
}
Example with manual conversion:
package main
 
import (
	"fmt"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// Create a Wei amount (1.5 ETH)
	weiAmount := new(felt.Felt).SetUint64(1500000000000000000)
 
	// Convert to ETH
	ethAmount := utils.WeiToETH(weiAmount)
	fmt.Printf("%s Wei = %.6f ETH\n", weiAmount, ethAmount)
 
	// Small amount
	smallWei := new(felt.Felt).SetUint64(1000000000000000)
	smallETH := utils.WeiToETH(smallWei)
	fmt.Printf("%s Wei = %.6f ETH\n", smallWei, smallETH)
}
Expected Output:
1500000000000000000 Wei = 1.500000 ETH
1000000000000000 Wei = 0.001000 ETH

STRK/FRI Conversions

STRKToFRI

Converts a STRK amount to FRI.

FRI (the smallest unit of STRK) is analogous to Wei for ETH, where 1 STRK = 10^18 FRI.

func STRKToFRI(strk float64) *felt.Felt
Parameters:
  • strk - The STRK amount to convert (as a float64)
Returns:
  • *felt.Felt - The FRI value as a Felt
Usage Example:
package main
 
import (
	"fmt"
 
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// Convert 10 STRK to FRI
	strkAmount := 10.0
	friAmount := utils.STRKToFRI(strkAmount)
	fmt.Printf("%.2f STRK = %s FRI\n", strkAmount, friAmount)
 
	// Convert 0.5 STRK to FRI
	smallAmount := 0.5
	smallFRI := utils.STRKToFRI(smallAmount)
	fmt.Printf("%.1f STRK = %s FRI\n", smallAmount, smallFRI)
 
	// Common use case: preparing fee payment
	feeAmount := 0.001
	feeFRI := utils.STRKToFRI(feeAmount)
	fmt.Printf("Fee amount: %s FRI\n", feeFRI)
}
Expected Output:
10.00 STRK = 10000000000000000000 FRI
0.5 STRK = 500000000000000000 FRI
Fee amount: 1000000000000000 FRI

FRIToSTRK

Converts a FRI amount to STRK.

FRI (the smallest unit of STRK) is analogous to Wei for ETH, where 1 STRK = 10^18 FRI.

func FRIToSTRK(fri *felt.Felt) float64
Parameters:
  • fri - The FRI amount to convert (as a Felt)
Returns:
  • float64 - The STRK value as a float64
Usage Example:
package main
 
import (
	"context"
	"fmt"
	"log"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/rpc"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	provider, err := rpc.NewProvider("https://starknet-sepolia.public.blastapi.io/rpc/v0_8")
	if err != nil {
		log.Fatal(err)
	}
 
	// STRK contract on Sepolia
	strkContract, _ := utils.HexToFelt("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d")
	accountAddress, _ := utils.HexToFelt("0x...")
 
	// Get balance in FRI
	balanceOfSelector := utils.GetSelectorFromNameFelt("balanceOf")
	result, err := provider.Call(
		context.Background(),
		rpc.FunctionCall{
			ContractAddress:    strkContract,
			EntryPointSelector: balanceOfSelector,
			Calldata:           []*felt.Felt{accountAddress},
		},
		rpc.BlockID{Tag: "latest"},
	)
	if err != nil {
		log.Fatal(err)
	}
 
	// Convert FRI to STRK for display
	friBalance := result[0]
	strkBalance := utils.FRIToSTRK(friBalance)
	fmt.Printf("Balance: %s FRI\n", friBalance)
	fmt.Printf("Balance: %.6f STRK\n", strkBalance)
}
Example with manual conversion:
package main
 
import (
	"fmt"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// Create a FRI amount (10 STRK)
	friAmount := new(felt.Felt).SetUint64(10000000000000000000)
 
	// Convert to STRK
	strkAmount := utils.FRIToSTRK(friAmount)
	fmt.Printf("%s FRI = %.6f STRK\n", friAmount, strkAmount)
 
	// Fee estimation result
	estimatedFee := new(felt.Felt).SetUint64(1000000000000000)
	strkFee := utils.FRIToSTRK(estimatedFee)
	fmt.Printf("Estimated fee: %s FRI = %.6f STRK\n", estimatedFee, strkFee)
}
Expected Output:
10000000000000000000 FRI = 10.000000 STRK
1000000000000000 FRI = 0.001000 STRK

Practical Examples

Example: Display Transaction Fee

package main
 
import (
	"context"
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/rpc"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	provider, err := rpc.NewProvider("https://starknet-sepolia.public.blastapi.io/rpc/v0_8")
	if err != nil {
		log.Fatal(err)
	}
 
	// Get transaction receipt
	txHash, _ := utils.HexToFelt("0x...")
	receipt, err := provider.TransactionReceipt(context.Background(), txHash)
	if err != nil {
		log.Fatal(err)
	}
 
	// Get actual fee from receipt
	var actualFee *felt.Felt
	switch r := receipt.(type) {
	case rpc.InvokeTransactionReceipt:
		actualFee = r.ActualFee.Amount
	case rpc.DeclareTransactionReceipt:
		actualFee = r.ActualFee.Amount
	case rpc.DeployAccountTransactionReceipt:
		actualFee = r.ActualFee.Amount
	}
 
	// Display fee in both FRI and STRK
	strkFee := utils.FRIToSTRK(actualFee)
	fmt.Printf("Transaction Fee:\n")
	fmt.Printf("  FRI: %s\n", actualFee)
	fmt.Printf("  STRK: %.6f\n", strkFee)
}

Example: Convert User Input to Wei

package main
 
import (
	"fmt"
	"log"
 
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// User wants to transfer 0.05 ETH
	userInput := 0.05
 
	// Convert to Wei for transaction
	weiAmount := utils.ETHToWei(userInput)
 
	fmt.Printf("User input: %.2f ETH\n", userInput)
	fmt.Printf("Transaction amount: %s Wei\n", weiAmount)
 
	// Verify conversion
	converted := utils.WeiToETH(weiAmount)
	fmt.Printf("Verification: %.6f ETH\n", converted)
}
Expected Output:
User input: 0.05 ETH
Transaction amount: 50000000000000000 Wei
Verification: 0.050000 ETH

Example: Calculate Fee in USD (Hypothetical)

package main
 
import (
	"fmt"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/utils"
)
 
func main() {
	// Fee in FRI
	feeFRI := new(felt.Felt).SetUint64(5000000000000000)
 
	// Convert to STRK
	feeSTRK := utils.FRIToSTRK(feeFRI)
 
	// Hypothetical STRK price in USD
	strkPriceUSD := 2.50
 
	// Calculate USD value
	feeUSD := feeSTRK * strkPriceUSD
 
	fmt.Printf("Fee: %s FRI\n", feeFRI)
	fmt.Printf("Fee: %.6f STRK\n", feeSTRK)
	fmt.Printf("Fee: $%.4f USD (@ $%.2f/STRK)\n", feeUSD, strkPriceUSD)
}
Expected Output:
Fee: 5000000000000000 FRI
Fee: 0.005000 STRK
Fee: $0.0125 USD (@ $2.50/STRK)

Related