nemild / staking-client-library-ts

Programmatic access to Coinbase's best-in-class staking infrastructure and services. πŸ”΅

Home Page:https://github.com/coinbase/staking-client-library-ts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Coinbase Staking API

Programmatic access to Coinbase's best-in-class staking infrastructure and services. πŸ”΅

npm version Current version GitHub contributors GitHub Stars GitHub

Overview

staking-client-library-ts is the Typescript SDK for the Coinbase Staking API πŸ”΅.

The Coinbase Staking API empowers developers to deliver a fully-featured staking experience in their Web2 apps, wallets, or dApps using one common interface across protocols.

A traditional infrastructure-heavy staking integration can take months. Coinbase's Staking API enables onboarding within hours ✨.

Quick Start

Prerequisite: Node 20+

  1. Install this package: npm install @coinbase/staking-client-library-ts
  2. Create and download an API key from the Cloud Platform.
  3. Place the key named .coinbase_cloud_api_key.json at the root of this repository.
  4. Run one of the code samples below or any of our provided examples πŸš€.

Stake Partial ETH πŸ’ 

This code sample creates an ETH staking workflow. View the full code sample here

Code Sample
// examples/ethereum/create-workflow.ts
import { StakingClient } from "@coinbase/staking-client-library-ts";

const client = new StakingClient();

client.Ethereum.stake(
  'holesky',
  true,
  '0xdb816889F2a7362EF242E5a717dfD5B38Ae849FE', // replace with your wallet address
  '0xA55416de5DE61A0AC1aa8970a280E04388B1dE4b',
  '123',
)
  .then((workflow) => {
    console.log('Workflow created %s', workflow.name);
  })
  .catch(() => {
    throw new Error(`Error creating workflow`);
  });
Output
Workflow created: projects/62376b2f-3f24-42c9-9025-d576a3c06d6f/workflows/ffbf9b45-c57b-49cb-a4d5-fdab66d8cb25

View Ethereum Rewards πŸ’°

This code sample returns rewards for an Ethereum validator address. View the full code sample here.

Code Sample
import { StakingClient } from "@coinbase/staking-client-library-ts";

// Defines which address and rewards we want to see
const address: string =
  '0xac53512c39d0081ca4437c285305eb423f474e6153693c12fbba4a3df78bcaa3422b31d800c5bea71c1b017168a60474';
const filter: string = `address='${address}' AND period_end_time > '2024-02-25T00:00:00Z' AND period_end_time < '2024-02-27T00:00:00Z'`;

const client = new StakingClient();

// Loops through rewards array and prints each reward
client.Ethereum.listRewards(filter).then((resp) => {
  resp.rewards!.forEach((reward) => {
    console.log(JSON.stringify(reward, null, 2));
  });
});
Output
{
   "address": "0xac53512c39d0081ca4437c285305eb423f474e6153693c12fbba4a3df78bcaa3422b31d800c5bea71c1b017168a60474",
   "date": "2024-02-25",
   "aggregationUnit": "DAY",
   "periodStartTime": "2024-02-25T00:00:00Z",
   "periodEndTime": "2024-02-25T23:59:59Z",
   "totalEarnedNativeUnit": {
      "amount": "0.002183619",
      "exp": "18",
      "ticker": "ETH",
      "rawNumeric": "2183619000000000"
   },
   "totalEarnedUsd": [
      {
         "source": "COINBASE_EXCHANGE",
         "conversionTime": "2024-02-26T00:09:00Z",
         "amount": {
            "amount": "6.79",
            "exp": "2",
            "ticker": "USD",
            "rawNumeric": "679"
         },
         "conversionPrice": "3105.780029"
      }
   ],
   "endingBalance": null,
   "protocol": "ethereum"
}
{
   "address": "0xac53512c39d0081ca4437c285305eb423f474e6153693c12fbba4a3df78bcaa3422b31d800c5bea71c1b017168a60474",
   "date": "2024-02-26",
   "aggregationUnit": "DAY",
   "periodStartTime": "2024-02-26T00:00:00Z",
   "periodEndTime": "2024-02-26T23:59:59Z",
   "totalEarnedNativeUnit": {
      "amount": "0.002182946",
      "exp": "18",
      "ticker": "ETH",
      "rawNumeric": "2182946000000000"
   },
   "totalEarnedUsd": [
      {
         "source": "COINBASE_EXCHANGE",
         "conversionTime": "2024-02-27T00:09:00Z",
         "amount": {
            "amount": "6.94",
            "exp": "2",
            "ticker": "USD",
            "rawNumeric": "694"
         },
         "conversionPrice": "3178.889893"
      }
   ],
   "endingBalance": null,
   "protocol": "ethereum"
}

Documentation

There are numerous examples in the examples directory to help get you started. For even more, refer to our documentation website for detailed definitions, API specifications, integration guides, and more!

About

Programmatic access to Coinbase's best-in-class staking infrastructure and services. πŸ”΅

https://github.com/coinbase/staking-client-library-ts

License:Apache License 2.0


Languages

Language:TypeScript 99.2%Language:Shell 0.8%