Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add params to initStakingPool ix #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions src/instructions/staking/initStakingPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import {
Keypair,
PublicKey,
SYSVAR_RENT_PUBKEY,
TransactionInstruction,
Expand All @@ -8,9 +9,9 @@ import * as BufferLayout from "@solana/buffer-layout";
import BN from "bn.js";

import * as Layout from "../../serialization/layout";
import { PORT_STAKING } from "../../constants";
import { StakingInstructions } from "./instruction";
import { AccessType, getAccess } from "../../utils/Instructions";
import { Optional } from "../../serialization";

// interface Data {
// instruction: number;
Expand All @@ -25,6 +26,7 @@ import { AccessType, getAccess } from "../../utils/Instructions";
const DataLayout = BufferLayout.struct([
BufferLayout.u8("instruction"),
Layout.uint64("supply"),
Optional.of(Layout.uint64("subSupply")),
Layout.uint64("duration"),
Layout.uint64("earliestRewardTime"),
BufferLayout.u8("bumpSeed"),
Expand All @@ -41,6 +43,9 @@ const DataLayout = BufferLayout.struct([
// 5. `[]` Staking program derived that owns reward token pool.
// 6. `[]` Rent sysvar .
// 7. `[]` Token program.
// 8. `[writable, optional]` Sub Reward token supply.
// 9. `[writable, optional]` Sub Reward token pool - uninitialized.
// 10. `[optional]` Sub Reward token mint.
export const initStakingPoolInstruction = (
supply: number | BN,
duration: number | BN,
Expand All @@ -54,31 +59,46 @@ export const initStakingPoolInstruction = (
derivedStakingProgram: PublicKey,
poolOwnerAuthority: PublicKey,
adminAuthority: PublicKey,
stakingProgramId: PublicKey = PORT_STAKING
stakingProgramId: PublicKey,
subReward?: {
supply: number | BN;
tokenSupply: PublicKey;
tokenPool: PublicKey;
rewardTokenMint: PublicKey;
}
): TransactionInstruction => {
const data = Buffer.alloc(DataLayout.span);
DataLayout.encode(
{
instruction: StakingInstructions.InitStakingPool,
supply: new BN(supply),
duration: new BN(duration),
earliestRewardTime: new BN(earliestRewardTime),
bumpSeed,
poolOwnerAuthority,
adminAuthority,
},
data
);

const params = {
instruction: StakingInstructions.InitStakingPool,
supply: new BN(supply),
duration: new BN(duration),
earliestRewardTime: new BN(earliestRewardTime),
bumpSeed,
poolOwnerAuthority,
adminAuthority,
};

if (subReward) {
params["subSupply"] = new BN(subReward.supply);
}

DataLayout.encode(params, data);

const dummy = () => Keypair.generate().publicKey;

const keys = [
// signer
getAccess(transferRewardSupply, AccessType.SIGNER),
// write accounts
getAccess(rewardTokenSupply, AccessType.WRITE),
getAccess(rewardTokenPool, AccessType.WRITE),
getAccess(subReward?.tokenSupply ?? dummy(), AccessType.WRITE),
getAccess(subReward?.tokenPool ?? dummy(), AccessType.WRITE),
getAccess(stakingPool, AccessType.WRITE),
// read accounts
getAccess(rewardTokenMint, AccessType.READ),
getAccess(subReward?.rewardTokenMint ?? dummy(), AccessType.READ),
getAccess(derivedStakingProgram, AccessType.READ),
getAccess(SYSVAR_RENT_PUBKEY, AccessType.READ),
getAccess(TOKEN_PROGRAM_ID, AccessType.READ),
Expand Down