Skip to content

Staking API

Overview

Staking is a process that involves holding funds in a cryptocurrency wallet to support the operations of a blockchain network. In return, users receive rewards for their contributions to the network. These rewards are also referred to as staking yields or interest.

XDEFI offers a staking API that enables developers to engage with the staking features across multiple blockchain networks. This API is crafted for simplicity and ease of use, emphasizing a high degree of abstraction from the complexities of the underlying blockchains.

The base URL for all API endpoints is: https://gql-router.dev.xdefi.services/graphql

Below are the services provided by the staking API.

Cosmos Chains

Our staking API extends comprehensive support to a wide array of Cosmos chains using STRIDE, enabling seamless interactions with their staking functionalities. Supported assets include ATOM, OSMO, JUNO, STARS, LUNA, SEI, AXL, KAVA, AKASH, STRD, KUJI, and CRE.

Liquid staking (STRIDE)

Stride is a liquid staking solution within the Cosmos ecosystem, designed to enhance the utility and liquidity of staked tokens. By offering liquid staking services, Stride enables users across any Cosmos chain to stake their tokens and receive liquid staked tokens in return.

Get balance of staked asset on STRIDE

You can use the getStrideStakedAssetBalance query to get the balance of a staked asset on STRIDE. The query takes the following parameters:

  • strideAddress: The STRIDE address for which to get the balance.
  • asset: The asset for which to get the balance.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query GetStrideStakedAssetBalance($strideAddress: String!, $asset: SupportedAssets!) {
  staking {
    getStrideStakedAssetBalance(asset: $asset, strideAddress: $strideAddress) {
      amount
      decimal
      denom
    }
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      asset: asset, // Asset selected
      strideAddress: address, // Input address
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Create Staking Transaction on Stride from any Cosmos Chain

You can use the createStrideLiquidStakingTx mutation to create a staking transaction on STRIDE from any Cosmos chain. The mutation takes the following parameters:

  • recieverStrideAddr: The STRIDE address of the recipient.
  • amount: The amount of the asset to stake.
  • senderAddr: The address of the sender.
  • senderPubkeyHex: The public key of the sender.
  • asset: The asset to stake.
  • timeoutHeight: The timeout height for the transaction.
  • gasLimit: The gas limit for the transaction.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query CreateStrideLiquidStakingTx($input: StrideStakingInput!) {
  staking {
  createStrideLiquidStakingTx(input: $input)
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      input: {
        recieverStrideAddr: recieverStrideAddr, // Reciever STRIDE address
        amount: amount || null, // Amount to stake
        senderAddr: senderAddr, // Sender address
        senderPubkeyHex: senderPubkeyHex || null, // Sender public key
        asset: asset, // Asset selected
        timeoutHeight: timeoutHeight, // Timeout height
        gasLimit: gasLimit || null, // Gas limit
      },
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Native staking

Native staking allows users to delegate their assets to validators on the Cosmos network. The staking API provides support for native staking on the Cosmos network, enabling users to delegate their assets to validators.

Get Cosmos Delegations

You can use the getCosmosDelegations query to get the delegations of a Cosmos address. The query takes the following parameters:

  • asset: The asset for which to get the delegations.
  • address: The address for which to get the delegations.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query getCosmosDelegations($asset: SupportedAssets!, $address: String!) {
  staking {
    getCosmosDelegations(address: $address, asset: $asset) {
      amount
      decimal
      denom
      validatorAddress
      validatorName
    }
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      asset: asset.key, // Asset selected
      address: address, // Input address
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Create native staking TX and delegate to validator (Meria, StakeLab or Custom validator’s address)

You can use the createCosmosDelegateTx mutation to create a native staking transaction and delegate to a validator. The mutation takes the following parameters:

  • asset: The asset to stake.
  • validator: The validator to delegate to. Supported validators: Meria, StakeLab or null (Custom). If validator is Custom, you need to provide the validatorAddress parameter.
  • validatorAddress: The address of the custom validator.
  • amount: The amount to stake.
  • delegatorAddress: An address related to the chain which the asset is the native asset for.
  • delegatorPubkeyHex: The public key of the delegator.
  • memo: Some memo for.
  • gasLimit: The gas limit.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query CreateCosmosDelegateTx($delegationInput: CosmosDelegationInput!, $validatorAddress: String, $provider: Providers) {
  staking {
    createCosmosDelegateTx(
      delegationInput: $delegationInput
      provider: $provider
      validatorAddress: $validatorAddress
    )
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      delegationInput: {
        asset: asset.key, // Asset selected
        amount: amount, // Input amount
        delegatorAddress: delegatorAddress, // Input delegatorAddress
        delegatorPubkeyHex: delegatorPubkeyHex, // Input delegatorPubkeyHex
        memo: memo, // Input memo
        gasLimit: gasLimit, // Input Gas limit
      },
      provider: "Meria" || "StakeLab", 
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query CreateCosmosDelegateTx($delegationInput: CosmosDelegationInput!, $validatorAddress: String, $provider: Providers) {
  staking {
    createCosmosDelegateTx(
      delegationInput: $delegationInput
      provider: $provider
      validatorAddress: $validatorAddress
    )
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      delegationInput: {
        asset: asset.key, // Asset selected
        amount: amount, // Input amount
        delegatorAddress: delegatorAddress, // Input delegatorAddress
        delegatorPubkeyHex: delegatorPubkeyHex, // Input delegatorPubkeyHex
        memo: memo, // Input memo
        gasLimit: gasLimit, // Input Gas limit
      },
      provider: null,
      validatorAddress: validatorAddress, // Input validatorAddress
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Ethereum Chains

Our staking API extends comprehensive support to Ethereum chains using LIDO, enabling seamless interactions with their staking functionalities. Supported assets include ETH (Ethereum) and MATIC_ERC20 (Polygon).

Liquid staking (LIDO)

Lido (LDO) is a liquid staking protocol designed to bring enhanced liquidity and accessibility to staked assets on various Proof of Stake (PoS) blockchains, including Ethereum, and Polygon.

Get balance of stakes asset on LIDO for stETH

You can use the getLidoStakedAssetBalance query to get the balance of a staked asset on LIDO. The query takes the following parameters:

  • asset: ETH
  • address: The address for which to get the balance.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query GetLidoStakedBalance($asset: SupportedAssets!, $address: String!) {
  staking {
    getLidoStakedBalance(address: $address, asset: $asset) {
      amount
      asset
      chain
      decimal
    }
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      asset: asset.key, // Asset selected
      address: address, // Input address
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Create staking Tx for ETH/ERC20 on LIDO

You can use the createLidoStakeTx query to create a staking transaction on LIDO. The query takes the following parameters:

  • asset: The asset to stake.
  • address: The address of the sender.
  • stakeValue: The amount to stake.
  • nonce: The nonce for the transaction.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query CreateLidoStakeTx($input: LidoStakingInput!) {
  staking {
    createLidoStakeTx(input: $input) {
      chainId
      data
      fromAddress
      nonce
      toAddress
      value
    }
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      input: {
        asset: asset.key, // Asset selected
        address: address, // Input address
        stakeValue: stakeValue, // Input stakeValue
        nonce: nonce, // Input nonce
      },
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Check the allowance for staking erc20 MATIC to stMATIC contract

You can use the lidoCheckErc20Allowance query to check the allowance for staking erc20 MATIC to the stMATIC contract. The query takes the following parameters:

  • asset: ETH
  • ownerAddress: The ethereum address which you try to stake erc20 MATIC on.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query LidoCheckErc20Allowance($ownerAddress: String!, $asset: SupportedAssets!) {
  staking {
    lidoCheckErc20Allowance(input: {ownerAddress: $ownerAddress, asset: $asset})
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      asset: asset.key, // Asset selected
      ownerAddress: address, // Input address
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });

Approve Tx to increase allowance for stMATIC to spend MATIC from the address

You can use the createErc20ApproveTx query to create an approval transaction to increase the allowance for stMATIC to spend MATIC from the address. The query takes the following parameters:

  • asset: MATIC_ERC20
  • fromAddress: The ethereum address which you try to stake erc20 MATIC on.
  • spenderAddress: The address of the spender.
  • amount: The amount to approve.
  • nonce: The nonce for the transaction.
javascript
const GRAPHQL_ENDPOINT = "https://gql-router.dev.xdefi.services/graphql";
const query = `query createErc20ApproveTx($input: Erc20ApproveInput!) {
  staking {
    createErc20ApproveTx(input: $input) {
      chainId
      data
      fromAddress
      nonce
      toAddress
      value
    }
  }
}`;

await fetch(GRAPHQL_ENDPOINT, {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "apollographql-client-name": "docs-staking-api",
    "apollographql-client-version": "v1.0",
  },
  body: JSON.stringify({
    query,
    variables: {
      input: {
        asset: asset.key, // Asset selected
        fromAddress: fromAddress, // Input fromAddress
        spenderAddress: spenderAddress, // Input spenderAddress
        amount: amount, // Input amount
        nonce: nonce, // Input nonce
      },
    },
  }),
})
  .then((response) => response.json())
  .then((result) => {
    console.log(result);
    // Do something with the result
  });