A staking program built on Solana using the Anchor framework. This program allows users to stake SPL tokens, claim rewards, and unstake their tokens after a defined duration. It provides the following features:
- Initialize staking pools with customizable parameters.
- Stake SPL tokens and earn rewards based on a reward rate.
- Claim accumulated rewards at regular intervals.
- Unstake tokens after the staking duration ends.
To set up and run a local Solana validator:
-
Ensuring Solana CLI has been installed.
-
Set the local validator as your cluster:
solana config set --url localhost -
Start the local validator on a separate terminal:
solana-test-validator
-
Create a new SPL token using the Solana CLI:
spl-token create-token
-
Create a token account for the mint:
spl-token create-account <TOKEN_MINT_ADDRESS>
-
Mint tokens to the token account:
spl-token mint <TOKEN_MINT_ADDRESS> <AMOUNT> <RECIPIENT_ACCOUNT_ADDRESS>
-
Replace mint in tests/simple_stake.ts
const mint = new PublicKey("replace_with_newly_deployed_mint_address"); -
Install dependencies:
npm install
-
Build the program:
anchor build
-
Deploy the program to the local validator:
anchor deploy
-
Generata a new programID and synced it across.
solana-keygen new --outfile target/deploy/simple_stake-keypair.json
anchor keys sync
Also make sure you manually replace in this too.
const programId = new PublicKey("replace_by_newly_generated");
-
Run the test cases ( make sure local validaor running on a separate terminal ):
anchor test --skip-local-validator --skip-deploy
Stakes a specified amount of tokens in the staking pool.
it("Simple Stake", async () => {
let amountToStake = new anchor.BN(2000000000);
let tx = await program.methods.stakeSpl(amountToStake)
.accounts({
...
})
});Allows a staker to claim rewards from the staking pool.
it("Simple Claim", async () => {
let tx = await program.methods.claimSpl()
.accounts({
...
})
.signers([staker1]).rpc();
console.log("Claim tx", tx);
});Unstakes a specified amount of tokens from the staking pool.
it("Simple UnStake", async () => {
let amountToUnStake = new anchor.BN(200000000);
let tx = await program.methods.unstakeSpl(amountToUnStake)
.accounts({
...
})
.signers([staker1]).rpc();
console.log("Unstake tx", tx);
});Feel free to contribute or raise issues for improvements!