Documentation v1.0 has been released! Staking Guide, Protocols, Glossary, FAQ, and Support sections have been added.

Cardano

Getting Started

The Cardano wallet SDK library provides class with batch of methods that helps to manage user's stake.

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

$ npm install @everstake/wallet-sdk-cardano

This code example how to make Cardano instance. The base account should be in correct format - bech32. To create instance also need to get a blockFrostProjectID. More details can be found here https://blockfrost.io/ . Be careful when use network. (preview and preprod are testnets). All code examples use meshjs library, but it's not required to reuse.

import {CardanoWeb3} from "cardano-web3-js"
import {Cardano} from "@everstake/wallet-sdk-cardano";

import {Cardano} from "@everstake/wallet-sdk-cardano";
import {BlockfrostProvider, MeshWallet} from "@meshsdk/core";

const blockFrostID = '...';
const mnemonic = '...';
const network = 'preview';
const networkID = 0;

const cardano = new Cardano(network, paymentWallet.addresses.baseAddressBech32!, blockFrostID);
await cardano.init();

cardano.init() must be called. it will not work without that.

(Optionally) Methods stake(), registerTx() and delegateTx() required information about pool. It automatically calls selectPool() methods, that is a bit slowly. But it can be preloaded if selectPool() will be lazy loaded before delegations methods to save some time.

await cardano.selectPool(); // preloaded
const tx = await registerTx() // selectPool() will skipped

Stake

The stakeTx method creates unsigned transaction for register stake account, delegate to staking pool and vote for DRep.

import {Cardano} from "@everstake/wallet-sdk-cardano";
import {BlockfrostProvider, MeshWallet} from "@meshsdk/core";

const blockFrostID = '...';
const mnemonic = '...';
const network = 'preview';
const networkID = 0;

const provider = new BlockfrostProvider(blockFrostID);
const paymentWallet = new MeshWallet({
    networkId: networkID,
    fetcher: provider,
    submitter: provider,
    key: {
        type: 'mnemonic',
        words: mnemonic.split(' ')
    },
    accountType: 'payment'
});
await paymentWallet.init();
const stakeWallet = new MeshWallet({
    networkId: networkID,
    fetcher: provider,
    submitter: provider,
    key: {
        type: 'mnemonic',
        words: mnemonic.split(' ')
    },
    accountType: 'stake'
});
await stakeWallet.init();

const cardano = new Cardano(network, paymentWallet.addresses.baseAddressBech32!, blockFrostID);
await cardano.init();

const tx = await cardano.delegateTx();
let signedTx = await paymentWallet.signTx(tx, true);
signedTx = await stakeWallet.signTx(signedTx, true);
const txHash = await paymentWallet.submitTx(signedTx);
console.log('txHash', txHash);

Also, an alternative option it can be called as separate methods.

const delegationTx = await cardano.registerTx();
const delegationTx = await cardano.delegateTx();
const voteTx = await cardano.voteDRep();

Unstake

The deregisterTx method creates unsigned transaction for deregistration stake. This method returns pledge (2 ADA) + stake (delegation) + rewards. So no need to claim rewards additionally.

import {Cardano} from "@everstake/wallet-sdk-cardano";
import {BlockfrostProvider, MeshWallet} from "@meshsdk/core";

const blockFrostID = '...';
const mnemonic = '...';
const network = 'preview';
const networkID = 0;

const provider = new BlockfrostProvider(blockFrostID);
const paymentWallet = new MeshWallet({
    networkId: networkID,
    fetcher: provider,
    submitter: provider,
    key: {
        type: 'mnemonic',
        words: mnemonic.split(' ')
    },
    accountType: 'payment'
});
await paymentWallet.init();
const stakeWallet = new MeshWallet({
    networkId: networkID,
    fetcher: provider,
    submitter: provider,
    key: {
        type: 'mnemonic',
        words: mnemonic.split(' ')
    },
    accountType: 'stake'
});
await stakeWallet.init();

const cardano = new Cardano(network, paymentWallet.addresses.baseAddressBech32!, blockFrostID);
await cardano.init();

const tx = await cardano.deregisterTx();
let signedTx = await paymentWallet.signTx(tx, true);
signedTx = await stakeWallet.signTx(signedTx, true);
const txHash = await paymentWallet.submitTx(signedTx);
console.log('txHash', txHash);

Get Info

Method getStakeInfo shows information about current stake. undefined value means that account is not registered.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.getStakeInfo());

Method getDelegations shows list of delegations of current account.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.getDelegations());

Method getStakeActivation shows more information about delegation status and time to active stake.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.getStakeActivation());

Method getRewardHistory shows users rewards history per epochs.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.getRewardHistory());

Method getPoolsInfos returns info about specified pools.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.getPoolsInfos(['pool...']));

Method getPoolIDs returns list of internal pools.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.getPoolIDs());

Method isInternalPoolDelegation returns boolean value that shows is current pool delegation is to internal pool.

const cardano = new Cardano('preview', baseAddress, blockForstProjectID);
console.log(await cardano.isInternalPoolDelegation());

Withdraw Rewards

The withdrawRewardsTx method creates unsigned transaction for withdraw rewards. Cardano has auto compound strategy, so it means no need claim rewards to increase APR. But if rewards should be spent need to call withdraw or deregister method.

import {Cardano} from "@everstake/wallet-sdk-cardano";
import {BlockfrostProvider, MeshWallet} from "@meshsdk/core";

const blockFrostID = '...';
const mnemonic = '...';
const network = 'preview';
const networkID = 0;

const provider = new BlockfrostProvider(blockFrostID);
const paymentWallet = new MeshWallet({
    networkId: networkID,
    fetcher: provider,
    submitter: provider,
    key: {
        type: 'mnemonic',
        words: mnemonic.split(' ')
    },
    accountType: 'payment'
});
await paymentWallet.init();
const stakeWallet = new MeshWallet({
    networkId: networkID,
    fetcher: provider,
    submitter: provider,
    key: {
        type: 'mnemonic',
        words: mnemonic.split(' ')
    },
    accountType: 'stake'
});
await stakeWallet.init();

const cardano = new Cardano(network, paymentWallet.addresses.baseAddressBech32!, blockFrostID);
await cardano.init();

const tx = await cardano.withdrawRewardsTx();
let signedTx = await paymentWallet.signTx(tx, true);
signedTx = await stakeWallet.signTx(signedTx, true);
const txHash = await paymentWallet.submitTx(signedTx);
console.log('txHash', txHash);