-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from coinbase/example-code
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
Showing
4 changed files
with
115 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,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()) | ||
} | ||
} |
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,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()) | ||
} | ||
} |