> ## Documentation Index
> Fetch the complete documentation index at: https://docs.powfi.alephium.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Stake ALPH for xALPH

> Stake, show the exchange rate, start an unstake, and claim as it unlocks.

export const VersionBanner = ({network = 'mainnet', products = [], lang = 'en'}) => {
  const SDK_VERSION = '1.0.2';
  const ADDRESSES = {
    mainnet: {
      cpmm: {
        Router: 'ze15rnAwTyVCPnDhN7J8An3SvNeWBsZHAVMeBx1ehXeP',
        TokenPairFactory: '24CXRLvj1QiDH6riwr1EPERG7BcduHKZtktygbq97MfYb'
      },
      clmm: {
        PoolFactory: '24gqd4DMQXVzjm5QDAGxUtYupx3WM2PSu3vZUseuBCV9h',
        PositionManager: 'vFyKSqnJHS9MicrojBcLgztyRFqhS4hB349Ci3LJCELw'
      },
      staking: {
        XAlphToken: '225WevmFp5ZgzPsyVJTvyp2v2uyKrvp329HfrVmzffnWj',
        RewardFeeCollector: '22CE7vG6u64zjjsLZLxeJqQuPDydjPvbP9iG1d861j2G7'
      }
    },
    testnet: {
      cpmm: {
        Router: 'xaueT8kPMvpnaFJEfHv6CJLgqsSPpoNmHjF1QgTxoF9R',
        TokenPairFactory: '29hdd9b9Gp7oXuamwHKfUqBaRXP487HoeTcutS3RhZJEj'
      },
      clmm: {
        PoolFactory: 'z2QFT7qBQrfm8UwAYG3VHWoHs7B56ZDFNdSCcDu5j53m',
        PositionManager: '21p6egW8d6FEyVVuH17WqvHCARsCsD61GcBmeCr75XdGT'
      },
      staking: {
        XAlphToken: '23psDicimC5VdpM56wrgqgCzQzcyXWGCkQGSYWbtZAPRq',
        RewardFeeCollector: '29qQ1LPJ5uqawRviXLVDMeo19JUygRrNNTQy5RUBh817D'
      }
    }
  };
  const EXPLORERS = {
    mainnet: 'https://explorer.alephium.org/addresses/',
    testnet: 'https://testnet.alephium.org/addresses/'
  };
  const LABELS = {
    en: {
      sdk: 'SDK',
      network: 'Network',
      contracts: 'Contracts',
      all: 'All addresses',
      allNetworks: 'mainnet · testnet · devnet',
      devnet: 'Local deployment (addresses vary per devnet)'
    },
    zh: {
      sdk: 'SDK 版本',
      network: '网络',
      contracts: '合约',
      all: '全部地址',
      allNetworks: 'mainnet · testnet · devnet',
      devnet: '本地部署（每个 devnet 的地址不同）'
    },
    fr: {
      sdk: 'SDK',
      network: 'Réseau',
      contracts: 'Contrats',
      all: 'Toutes les adresses',
      allNetworks: 'mainnet · testnet · devnet',
      devnet: 'Déploiement local (adresses propres à chaque devnet)'
    }
  };
  const t = LABELS[lang] ?? LABELS.en;
  const contractsPage = lang === 'en' ? '/reference/contracts' : `/${lang}/reference/contracts`;
  const short = a => `${a.slice(0, 6)}…${a.slice(-4)}`;
  const entries = [];
  const addrs = ADDRESSES[network];
  if (addrs) {
    for (const p of products) {
      for (const [name, address] of Object.entries(addrs[p] ?? ({}))) entries.push({
        name,
        address
      });
    }
  }
  let contractsCell;
  if (network === 'devnet' && products.length > 0) {
    contractsCell = <span>{t.devnet}</span>;
  } else if (entries.length > 0) {
    contractsCell = entries.map(({name, address}, i) => <span key={name}>
        {i > 0 && <span className="mx-1 opacity-50">·</span>}
        {name}{' '}
        <a href={EXPLORERS[network] + address} target="_blank" rel="noreferrer" title={address}>
          <code>{short(address)}</code>
        </a>
      </span>);
  }
  const row = (label, value) => <div className="flex flex-wrap gap-x-2">
      <span className="font-semibold min-w-[5.5rem]">{label}</span>
      <span className="flex-1 min-w-0 break-words">{value}</span>
    </div>;
  return <div className="not-prose mb-6 rounded-xl border border-zinc-950/10 dark:border-white/10 bg-zinc-50 dark:bg-white/5 px-4 py-3 text-sm leading-6 text-zinc-700 dark:text-zinc-300">
      {row(t.sdk, <code>@alephium/powfi-sdk@{SDK_VERSION}</code>)}
      {row(t.network, network === 'all' ? t.allNetworks : network)}
      {contractsCell && row(t.contracts, <span>
            {contractsCell}
            <span className="mx-1 opacity-50">·</span>
            <a href={contractsPage}>{t.all} →</a>
          </span>)}
    </div>;
};

<VersionBanner network="mainnet" products={['staking']} lang="en" />

## Stake

```ts theme={null}
import { ONE_ALPH } from '@alephium/web3'

const tx = await powfi.staking.stakeAlph(100n * ONE_ALPH)
```

The transaction attaches `amount + MINIMAL_CONTRACT_DEPOSIT` ALPH. To attribute the stake to a partner address, use `stakeAlphWithReferral(amount, referral)`. Contact the team before using a referral: see [Referral staking & partner commission](/guides/referral-staking).

## Show the rate and a user's value

```ts theme={null}
import Decimal from 'decimal.js'

const { fields } = await powfi.staking.getXAlphTokenState()
const rate = new Decimal(fields.totalDepositedAlph.toString()).div(fields.totalXAlphSupply.toString()) // ALPH per xALPH

const xAlphId = powfi.staking.getConfig().xAlphTokenId
const bal = await powfi.nodeProvider.addresses.getAddressesAddressBalance(address)
const xAlph = BigInt(bal.tokenBalances?.find((t) => t.id === xAlphId)?.amount ?? '0')
const valueInAlph = rate.mul(xAlph.toString()).div(1e18) // xALPH has 18 decimals, like ALPH
```

## Unstake

```ts theme={null}
await powfi.staking.startUnstake(50n * ONE_ALPH) // amount of xALPH to burn
```

This opens a new unstake vault. The ALPH unlocks linearly over `getSettings().unstakeDuration` (30 days on mainnet): you can claim the unlocked part at any time, and the full amount once the period ends. A user can have at most 16 active requests.

## List pending requests

```ts theme={null}
const indexes = await powfi.staking.getActiveUnstakeVaultIndexes(address)

const requests = await Promise.all(
  indexes.map(async (i) => {
    const { fields } = await powfi.staking.getAlphUnstakeVaultState(address, i)
    return {
      index: i,
      total: fields.totalUnstakeAmount,
      withdrawn: fields.withdrawnAmount,
      fullyUnlockedAt: new Date(Number(fields.unstakeStartTime + fields.unstakeDuration)),
      claimable: await powfi.staking.getClaimableAmount(address, i)
    }
  })
)
```

## Claim or cancel

```ts theme={null}
for (const r of requests) {
  if (r.claimable > 0n) await powfi.staking.claimUnstaked(r.index, r.claimable)
}

// change of mind: unlocked ALPH is paid out, the rest is re-staked as xALPH
await powfi.staking.cancelUnstake(requests[0].index)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.