# Cosmos

## Getting Started <a href="#getting-started" id="getting-started"></a>

You can use two different options to implement staking for Everstake validator.

**Option 1: REST API**

You can use REST API to call methods which are described in [Swagger](https://wallet-sdk-api.everstake.one/swagger/#/Cosmos) with detailed examples

```
https://wallet-sdk-api.everstake.one
```

**Option 2: JavaScript library**

You can install and import Wallet SDK for Javascript.

#### Step. 1: Installing the Library <a href="#step-1-installing-the-library" id="step-1-installing-the-library"></a>

Install the npm library  or yarn by copying the code below.

{% tabs %}
{% tab title="npm" %}

```sh
$ npm install @everstake/wallet-sdk
```

{% endtab %}

{% tab title="yarn" %}

```sh
$ yarn add @everstake/wallet-sdk
```

{% endtab %}
{% endtabs %}

#### Step. 2: Import Wallet SDK <a href="#step-2-import-wallet-sdk" id="step-2-import-wallet-sdk"></a>

After installing the app, you can import module of needed blockchain (Ethereum, Aptos, Solana, Cosmos, Polygon are available) and use the SDK:

**Import ES6**

```typescript
// import module
import { Cosmos } from '@everstake/wallet-sdk';
// or you can also use
import * as Cosmos from '@everstake/wallet-sdk/cosmos';
// import needed function
import { delegate } from '@everstake/wallet-sdk/cosmos';
```

**Import ES5**

```typescript
// import module
const { Cosmos } = require("@everstake/wallet-sdk");
// or you can also use
const { delegate } = require("@everstake/wallet-sdk/cosmos");
```

#### Step. 3: Create Auth Token <a href="#step-3-create-auth-token" id="step-3-create-auth-token"></a>

In order to access all the features of the Wallet SDK, it's necessary for you to generate an authentication token. This token will be required in all of the methods you use.

**Using JS Library**

```typescript
// import
const { CreateToken } = require("@everstake/wallet-sdk");

// Project name
const name = 'Everstake Wallet SDK';
// wallet | Dapp | Other
const type = 'SDK';

// Create Token - return token ID
const token = await CreateToken(name, type);
console.log(token); // 3155f389-d943-4966-8e18-f159c2a6ef66
```

**Using REST API** [**(Swagger)**](https://wallet-sdk-api.everstake.one/swagger/#/Auth/post_token_create)

```typescript
curl -X 'POST' \
  'https://wallet-sdk-api.everstake.one/token/create' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Everstake Wallet SDK",
  "type": "SDK"
}'
```

## Stake <a href="#delegate-cosmos" id="delegate-cosmos"></a>

The <mark style="color:yellow;">`delegate`</mark> namespace contains method used for sending transactions on delegation. The unique method to the <mark style="color:yellow;">`delegate`</mark> namespace is:

* <mark style="color:yellow;">`delegate(token, address, amount, sourceID)`</mark>: Delegate user tokens.

**Delegate Code Example**

```typescript
// Import SDK
import { Cosmos } from '@everstake/wallet-sdk';

// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66

// User wallet address.
const address = 'examplecosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhs';

// The amount that the user delegates.
const amountDelegate = 10; // min value 0.01 (ATOM)

// Your source identificator
const sourceID = '99'

// Delegate - return transaction
const delegate = await Cosmos.delegate(token, address, amountDelegate, sourceID);
console.log(delegate); // { result: { address, msg, fee, memo } }
```

## Unstake <a href="#undelegate-cosmos" id="undelegate-cosmos"></a>

The <mark style="color:yellow;">`undelegate`</mark> namespace contains method used for sending transactions on delegation. The unique method to the <mark style="color:yellow;">`undelegate`</mark> namespace is:

* <mark style="color:yellow;">`undelegate(token, address, amount)`</mark>: Undelegate user tokens.

**Undelegate Code Example**

```typescript
// Import SDK
import { Cosmos } from '@everstake/wallet-sdk';

// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66

// User wallet address.
const address = 'examplecosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhs';

// The amount that the user undelegates.
const amountUndelegate = 10; // min value 0.01 (ATOM)

// Your source identificator
const sourceID = '99'

// undelegate - return transaction
const undelegate = await Cosmos.undelegate(token, address, amountUndelegate, sourceID);
console.log(undelegate); // { result: { address, msg, fee, memo } }
```

## Redelegate <a href="#redelegate-cosmos" id="redelegate-cosmos"></a>

The <mark style="color:yellow;">`redelegate`</mark> namespace contains method used for sending transactions on delegation. The unique method to the <mark style="color:yellow;">`redelegate`</mark> namespace is:

* <mark style="color:yellow;">`redelegate(token, address, amount, validatorSrcAddress)`</mark>: Redelegate user tokens.

**Redelegate Code Example**

```typescript
// Import SDK
import { Cosmos } from '@everstake/wallet-sdk';

// Token ID.
const token = process.env.TOKEN; // 3155f389-d943-4966-8e18-f159c2a6ef66

// Optional: You can get all user delegations.
// User wallet address.
const address = 'examplecosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhs';
// Get all user delegations.
const getDelegations = await Cosmos.getDelegations(address);

// The amount that the user redelegates.
const amountRedelegate = getDelegations.result[0].balance.amount || 10; 
// min value 0.01 (ATOM)

// Validator Address from Redelegation.
const validatorSrcAddress = 
    getDelegations.result[0].delegation.validator_address 
    || 'cosmosvaloper1tflk30mq5vgqjdly92kkhhq3raev2hnz6eete3';

// Your source identificator
const sourceID = '99'

// redelegate - return transaction hash
const redelegate = await Cosmos.redelegate(
    token,
    address,
    amountRedelegate,
    validatorSrcAddress,
    sourceID
);
console.log(redelegate); // { result: { address, msg, fee, memo } }
```

## Withdraw Rewards <a href="#withdraw-rewards-cosmos" id="withdraw-rewards-cosmos"></a>

The <mark style="color:yellow;">`withdrawRewards`</mark> namespace contains method used for sending transactions on delegation. The unique method to the <mark style="color:yellow;">`withdrawRewards`</mark> namespace is:

* <mark style="color:yellow;">`withdrawRewards(address)`</mark>: Withdraw user rewards.

**Withdraw Rewards Code Example**

```typescript
// Import SDK
import { Cosmos } from '@everstake/wallet-sdk';

// User wallet address.
const address = 'examplecosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhs';

// Your source identificator
const sourceID = '99'

// reward - return transaction
const reward = await Cosmos.withdrawRewards(address, sourceID);
console.log(reward); // { result: { address, msg, fee, memo } }
```

## Getting Info <a href="#getting-info-cosmos" id="getting-info-cosmos"></a>

You can use <mark style="color:yellow;">`getDelegations`</mark> method to get all user delegations to use them for <mark style="color:yellow;">`redelegate`</mark> functionality.

The <mark style="color:yellow;">`get`</mark> namespace contains method used for getting info. The unique method to the <mark style="color:yellow;">`get`</mark> namespace is:

* <mark style="color:yellow;">`getDelegations(address)`</mark>: Gets all user delegation.

**Getting Info Code Example**

```typescript
// Import SDK
import { Cosmos } from '@everstake/wallet-sdk';

// User wallet address.
const address = 'examplecosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhs';

// Get all user delegation.
const getDelegationsResult = await Cosmos.getDelegations(address);
console.log(getDelegationsResult); // see the response below
```

**Response Example**

```typescript
getDelegationsResult = {
  "result": [
    {
      "delegation": {
        "delegator_address": "cosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhs6ujz05e",
        "validator_address": "cosmosvaloper1ptyzewnns2kn37ewtmv6ppsvhdnmeapvtfc9y5",
        "shares": "1000.000000000000000000"
      },
      "balance": {
        "denom": "uatom",
        "amount": "1000"
      },
      "operator_address": "cosmosvaloper1ptyzewnns2kn37ewtmv6ppsvhdnmeapvtfc9y5",
      "consensus_pubkey": {
        "type": "tendermint/PubKeyEd25519",
        "value": "XiGz/D6eg3KdjaFB0uYIJwkOTW5xZcFRxJmHcQYB3zg="
      },
      "status": 3,
      "tokens": "513203840253",
      "delegator_shares": "513203840253.000000000000000000",
      "description": {
        "moniker": "WeStaking",
        "identity": "DA9C5AD3E308E426",
        "website": "https://www.westaking.io",
        "details": "Delegate your atom to us for the staking rewards. We will do our best as secure and stable validator."
      },
      "unbonding_time": "2019-09-07T12:24:58.270714195Z",
      "commission": {
        "commission_rates": {
          "rate": "0.030000000000000000",
          "max_rate": "0.200000000000000000",
          "max_change_rate": "0.010000000000000000"
        },
        "update_time": "2020-03-29T00:37:22.163935678Z"
      },
      "min_self_delegation": "1"
    },
    {
      "delegation": {
        "delegator_address": "cosmos1hgmhc4hajlh8u47wmmls6ze4yrfekhsexample",
        "validator_address": "cosmosvaloper1tflk30mq5vgqjdly92kkhhq3raev2hnz6eete3",
        "shares": "2000.000000000000000000"
      },
      "balance": {
        "denom": "uatom",
        "amount": "2000"
      },
      "operator_address": "cosmosvaloper1tflk30mq5vgqjdly92kkhhq3raev2hnz6eete3",
      "consensus_pubkey": {
        "type": "tendermint/PubKeyEd25519",
        "value": "dhhD3I5QbtC870Il4IzML5Q2AwVDiSn9/HJ9w09Rgdg="
      },
      "status": 3,
      "tokens": "7087394678764",
      "delegator_shares": "7087394678764.000000000000000000",
      "description": {
        "moniker": "Everstake",
        "identity": "EF5AC70C00BECEDC",
        "website": "https://everstake.one",
        "details": "Reliable and experienced staking service provider from Ukraine. Visit our website for more details."
      },
      "unbonding_time": "2019-12-07T19:10:59.878559804Z",
      "commission": {
        "commission_rates": {
          "rate": "0.060000000000000000",
          "max_rate": "0.200000000000000000",
          "max_change_rate": "0.010000000000000000"
        },
        "update_time": "2022-12-31T22:25:10.09405574Z"
      },
      "min_self_delegation": "1"
    }
  ]
}
```

**Getting Undelegation Info Code Example**

You can use <mark style="color:yellow;">`getUndelegations`</mark> method to get all user undelegations.

* <mark style="color:yellow;">`getUndelegations(address)`</mark>: Gets all user undelegations.

```typescript
// Import SDK
import { Cosmos } from '@everstake/wallet-sdk';

// User wallet address.
const address = 'cosmos176f4pukjdnsjmvwsngxa6re8r50rkuf7dya90m';

// Get all user delegation.
const getUndelegationsResult = await Cosmos.getUndelegations(address);
console.log(getUndelegationsResult); // see the response below
```

**Response Example**

```typescript
getDelegationsResult = {
    "result":[
        {
            "delegator_address":"cosmos176f4pukjdnsjmvwsngxa6re8r50rkuf7dya90m",
            "validator_address":"cosmosvaloper1tflk30mq5vgqjdly92kkhhq3raev2hnz6eete3",
            "entries":[
                {
                    "creation_height":"22341651",
                    "completion_time":"2024-10-16T15:18:43.146385254Z",
                    "initial_balance":"100000",
                    "balance":"100000",
                    "unbonding_id":"882090",
                    "unbonding_on_hold_ref_count":"0"
                }
            ]
        },
        {
            "delegator_address":"cosmos176f4pukjdnsjmvwsngxa6re8r50rkuf7dya90m",
            "validator_address":"cosmosvaloper1clpqr4nrk4khgkxj78fcwwh6dl3uw4epsluffn",
            "entries":[
                {
                    "creation_height":"22366902",
                    "completion_time":"2024-10-18T08:47:50.123024310Z",
                    "initial_balance":"10000",
                    "balance":"10000",
                    "unbonding_id":"884765",
                    "unbonding_on_hold_ref_count":"0"
                }
            ]
        }
    ]
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.everstake.one/integrations/everstake-products/wallet-sdk/protocols/cosmos.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
