# Working with Staking

{% hint style="warning" %}
Make sure to implement the [prerequisite code](/interact-with-chiliz-chain/prerequisites.md), or else the examples in this page will not work!
{% endhint %}

Socios.com users have two possibilities when staking do in-app:

* Staking Fan Tokens.
* Staking CHZ.&#x20;

Furthermore, Socios.com partners can implement the Socios.com Fan Token staking mechanism on their own website.&#x20;

Of note: When working with CHZ tokens in your project, you must use thecorrect addresses in your dApp. See here:

{% content-ref url="/pages/D4uftGbnzOmnKXbxjg5X" %}
[Testnet staking smartcontracts](/interact-with-chiliz-chain/working-with-staking/testnet-staking-smartcontracts.md)
{% endcontent-ref %}

{% content-ref url="/pages/OwDloA38QmQujOKtWz0J" %}
[Mainnet staking smartcontracts](/interact-with-chiliz-chain/working-with-staking/mainnet-staking-smartcontracts.md)
{% endcontent-ref %}

## About "Stake & Earn"

By staking their Fan Tokens through their Socios.com Wallet, Socios.com users can earn Reward Points on a daily basis, and possibly get quick access to great Socios.com rewards and activities!

Each staked Fan Token will join an existing pool of rewards points, made of all the Fan Tokens staked by all Socios.com users.

The pool then works for all users, and the more Fan Tokens you have staked, the more reward points you can obtain in return.

{% hint style="info" %}
Fan Token staking is also possible from wallets other than Socios.com Wallet.&#x20;

This allows anyone to contribute to the Fan Token ecosystem, but staking from non-Socios wallets cannot earn Socios.com reward points.
{% endhint %}

{% hint style="success" %}
There is only one smart contract address to stake any of the Socios.com Fan Tokens: `0x5ff7f9724fd477d9a07dcdb894d0ca7f8fae1501`
{% endhint %}

## Implementing Socios.com staking/unstaking on your own site

Socios.com partners can implement Fan Token staking right into their own platform.&#x20;

This is done through a specific smart contract created by the Socios.com team. This Staking smart contract has 5 main features:

* Staking
* Locking
* Processing the locks
* Unstaking
* Claiming

To implement staking, unstaking, and the other necessary fixtures, we provide you with the following sample code listing, ready for you to adapt to your own codebase.

### Prerequisite: StakingABI JSON file <a href="#prerequisite-survey-json-file" id="prerequisite-survey-json-file"></a>

{% hint style="success" %}
When working with staking, and for some use-cases described in this page (the ones that import `stakingABI`), you will need the below **StakingABI.json** file saved in your project folder.

Make sure that you import that file in your code in order to achieve the wanted step (the samples already have the necessary `import` code).
{% endhint %}

{% file src="/files/OPONM7yZt7nqyAy9oRX6" %}

### Setting up your Chiliz Viem Client

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import { createPublicClient, http, createWalletClient } from 'viem'
import { chiliz, spicy } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts';

const publicClient = createPublicClient({
	chain: chiliz, // chiliz (mainnet) or spicy (testnet)
	transport: http() // you can overwrite the transport URL provided by Chain object here 
})

const privateKey = '0xSenderPrivateKey'; // Replace with the sender's private key
const account = privateKeyToAccount(privateKey);
const walletClient = createWalletClient({
	account: '0x',
	chain: chiliz, // chiliz (mainnet) or spicy (testnet)
	transport: http()
})
```

{% endcode %}

{% hint style="danger" %}
**Your private key must only be used in server-side code.** \
DO NOT release code on production with your private key shared on front-end side code.
{% endhint %}

### Staking

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import stakingABI from './StakingABI.json';

async function stake(amount, tokenAddress) {
  const txHash = await walletClient.writeContract({
    address: '0xFanTokenStakingAddress', // Replace with the Fan Token Staking contract address
    abi: stakingABI,
    functionName: 'stake', // Function to stake tokens
    args: [amount, tokenAddress],
  });
  return publicClient.waitForTransactionReceipt({ hash: txHash });
}

// Example usage. Replace 10 with the exact amount to stake, and 0xFanTokenAddress with the actual Fan Token contract address.
stake(10, '0xFanTokenAddress',).then(console.log);
```

{% endcode %}

### Knowing what's staked

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import stakingABI from './StakingABI.json';

async function getStakeDataLatest(staker, fanToken, perEventType) {
  const stakeData = await publicClient.readContract({
    address: '0xFanTokenStakingAddress', // Replace with the Fan Token Staking contract address,
    abi: stakingABI,
    functionName: 'getStakeDataLatest',
    args: [staker, fanToken, perEventType],
  });
  return stakeData;
}

// Example usage, Replace 0xStakerWalletAddress with the actual staker wallet address, and 0xFanTokenAddress with the actual Fan Token contract address.
getStakeDataLatest('0xStakerWalletAddress', '0xFanTokenAddress').then(console.log);
```

{% endcode %}

### Unstaking

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import stakingABI from './StakingABI.json';

async function unstake(amount, tokenAddress) {
  const txHash = await walletClient.writeContract({
    address: '0xFanTokenStakingAddress', // Replace with the Fan Token Staking contract address
    abi: stakingABI,
    functionName: 'unstake', // Function to unstake tokens
    args: [amount, tokenAddress],
  });
  return publicClient.waitForTransactionReceipt({ hash: txHash });
}

// Example usage. Replace 10 with the exact amount to unstake, and 0xFanTokenAddress with the actual Fan Token contract address.
unstake(10, '0xFanTokenAddress',).then(console.log);
```

{% endcode %}

### Checking the cooldown period

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import stakingABI from './StakingABI.json';

async function getCooldownPeriod() {
  const cooldownPeriod = await publicClient.readContract({
    address: '0xFanTokenStakingAddress', // Replace with the Fan Token Staking contract address,
    abi: stakingABI,
    functionName: 'unstakePeriod', // The function that returns the unstake period.
    args: [],
  });
  return cooldownPeriod.toString();;
}

// Example usage.
getCooldownPeriod().then(console.log);
```

{% endcode %}

### Claiming (after cooldown period)

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```javascript
import stakingABI from './StakingABI.json';

async function claim(amount, tokenAddress) {
  const txHash = await walletClient.writeContract({
    address: '0xFanTokenStakingAddress', // Replace with the Fan Token Staking contract address
    abi: stakingABI,
    functionName: 'claim', // Function to claim tokens
    args: [amount, tokenAddress],
  });
  return publicClient.waitForTransactionReceipt({ hash: txHash });
}

// Example usage. Replace 0xFanTokenAddress with the actual Fan Token contract address.
claim('0xFanTokenAddress',).then(console.log);
```

{% endcode %}


---

# 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://connect.socios.com/interact-with-chiliz-chain/working-with-staking.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.
