Skip to content

Commit

Permalink
Merge pull request #31 from coinbase/example-code
Browse files Browse the repository at this point in the history
This PR adds more examples for the newest features of the SDK, namely StakingRewards and HistoricalStakingBalances.

It also reorganized the existing examples and added descriptions.
  • Loading branch information
ProfMoo authored Sep 17, 2024
2 parents 0b2933a + 3183ac3 commit dbefe81
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/coinbase/coinbase-sdk-go/pkg/coinbase"
)

/*
* This example code stakes ETH on the Holesky network.
* Run the code with 'go run examples/ethereum/build-staking-operation/main.go <api_key_file_path> <wallet_address>'
*/

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

Expand All @@ -21,7 +26,7 @@ func main() {
log.Fatalf("error creating coinbase client: %v", err)
}

address := coinbase.NewExternalAddress("ethereum-holesky", "0x57a063e1df096aaA6b2068C3C7FE6Ac4BC3c4F58")
address := coinbase.NewExternalAddress("ethereum-holesky", os.Args[2])

stakeableBalance, err := client.GetStakeableBalance(ctx, coinbase.Eth, address, coinbase.WithStakingBalanceMode(coinbase.StakingOperationModePartial))
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var (
defaultPrivKeyPath = filepath.Join(home(), ".config/solana/id.json")
)

/*
* This example code stakes SOL on the devnet network.
* Run the code with 'go run examples/solana/build-staking-operation/main.go <api_key_file_path> <wallet_address>'
*/

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

Expand Down
51 changes: 51 additions & 0 deletions examples/solana/list-staking-balances/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"context"
"log"
"os"
"time"

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

var (
networkID = "solana-mainnet"
)

/*
* This example code lists historical staking balances of any delegator on the Solana blockchain
* Run the code with 'go run examples/solana/list-staking-balances/main.go <api_key_file_path> <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)
}

// Create a new external address on the solana-mainnet-beta network for which you want to view staking balances.
address := coinbase.NewExternalAddress(networkID, os.Args[2])

// Get the balances earned from staking in the last 10 days.
// Note that it can take several hours for new balances to show up.
balances, err := client.ListHistoricalStakingBalances(
ctx,
coinbase.Sol,
address,
time.Now().Add(-10*24*time.Hour),
time.Now(),
)
if err != nil {
log.Fatalf("error fetching staking balances: %v", err)
}

// Loop through the balances and print each staking balance.
for _, balance := range balances {
log.Printf("Staking balance: %s", balance.String())
}
}
53 changes: 53 additions & 0 deletions examples/solana/list-staking-rewards/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"context"
"log"
"os"
"time"

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

var (
networkID = "solana-mainnet"
)

/*
* This example code lists historical staking rewards of any delegator on the Solana blockchain
* Run the code with 'go run examples/solana/list-staking-rewards/main.go <api_key_file_path> <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)
}

// Create a new external address on the solana-mainnet-beta network for which you want to view staking rewards.
address := coinbase.NewExternalAddress(networkID, os.Args[2])

// Get the rewards earned from staking in the last 10 days.
// Note that it can take several hours for new rewards to show up.
rewards, err := client.ListStakingRewards(
ctx,
coinbase.Sol,
[]coinbase.Address{*address},
time.Now().Add(-10*24*time.Hour),
time.Now(),
api.STAKINGREWARDFORMAT_USD,
)
if err != nil {
log.Fatalf("error fetching staking rewards: %v", err)
}

// Loop through the rewards and print each staking reward.
for _, reward := range rewards {
log.Printf("Staking reward: %s", reward.ToString())
}
}

0 comments on commit dbefe81

Please sign in to comment.