Skip to content

Commit

Permalink
Add dedicated eth staking example (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
drohit-cb authored Sep 23, 2024
1 parent ad1c2f8 commit fa2890f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions examples/ethereum/dedicated-eth-stake/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"context"
"log"
"math/big"
"os"

"github.com/coinbase/coinbase-sdk-go/gen/client"
"github.com/coinbase/coinbase-sdk-go/pkg/coinbase"
)

var (
networkID = client.NETWORKIDENTIFIER_ETHEREUM_HOLESKY
assetID = coinbase.Eth
)

/*
* This example code stakes ETH on the Holesky network via Dedicated ETH Staking.
* Run the code with 'go run examples/ethereum/dedicated-eth-stake/main.go <api_key_file_path> <funding_wallet_address>'
*/

func main() {
ctx := context.Background()

client, err := coinbase.NewClient(
coinbase.WithAPIKeyFromJSON(os.Args[1]),
)
if err != nil {
log.Fatalf("error creating coinbase client: %v", err)
}

address := coinbase.NewExternalAddress(string(networkID), os.Args[2])

stakeableBalance, err := client.GetStakeableBalance(ctx, assetID, address, coinbase.WithStakingBalanceMode(coinbase.StakingOperationModeNative))
if err != nil {
log.Fatalf("error getting stakeable balance: %v", err)
}

log.Printf("stakeable balance: %s\n", stakeableBalance)

listMyValidators(ctx, client, string(networkID), assetID)

stakeOperation, err := client.BuildStakeOperation(
ctx,
big.NewFloat(64),
assetID,
address,
coinbase.WithStakingOperationMode(coinbase.StakingOperationModeNative),
)
if err != nil {
log.Fatalf("error building staking operation: %v", err)
}

stakeOperation, err = client.Wait(ctx, stakeOperation, coinbase.WithWaitTimeoutSeconds(600))
if err != nil {
log.Fatalf("error waiting for staking operation: %v", err)
}

log.Printf("staking operation ID: %s\n", stakeOperation.ID())

for index, tx := range stakeOperation.Transactions() {
log.Printf("Deposit tx %d: %s\n", index+1, tx.UnsignedPayload())
}

listMyValidators(ctx, client, string(networkID), assetID)
}

func listMyValidators(ctx context.Context, client *coinbase.Client, networkID string, assetID string) {
validators, err := client.ListValidators(ctx, networkID, assetID)
if err != nil {
log.Fatalf("error listing validators: %v", err)
}

for _, validator := range validators {
log.Printf(validator.ToString())
}
}
File renamed without changes.

0 comments on commit fa2890f

Please sign in to comment.