-
Notifications
You must be signed in to change notification settings - Fork 302
feat(staking): make wallet optional in staking sdk #1932
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,12 +54,12 @@ import type { Staking } from "../types/staking"; | |
|
|
||
| export type PythStakingClientConfig = { | ||
| connection: Connection; | ||
| wallet: AnchorWallet; | ||
| wallet: AnchorWallet | undefined; | ||
| }; | ||
|
|
||
| export class PythStakingClient { | ||
| connection: Connection; | ||
| wallet: AnchorWallet; | ||
| wallet: AnchorWallet | undefined; | ||
| provider: AnchorProvider; | ||
| stakingProgram: Program<Staking>; | ||
| integrityPoolProgram: Program<IntegrityPool>; | ||
|
|
@@ -68,7 +68,9 @@ export class PythStakingClient { | |
| constructor(config: PythStakingClientConfig) { | ||
| this.connection = config.connection; | ||
| this.wallet = config.wallet; | ||
| this.provider = new AnchorProvider(this.connection, this.wallet, { | ||
|
|
||
| // {} as AnchorWallet is a workaround for AnchorProvider requiring a wallet | ||
| this.provider = new AnchorProvider(this.connection, this.wallet ?? {} as AnchorWallet, { | ||
| skipPreflight: true, | ||
| }); | ||
| this.stakingProgram = new Program(StakingIdl as Staking, this.provider); | ||
|
|
@@ -82,7 +84,14 @@ export class PythStakingClient { | |
| ); | ||
| } | ||
|
|
||
| private assertWallet(): asserts this is { wallet: AnchorWallet } { | ||
| if (this.wallet === undefined) { | ||
| throw new Error("Wallet not set"); | ||
| } | ||
| } | ||
|
|
||
| async initGlobalConfig(config: GlobalConfig) { | ||
| this.assertWallet(); | ||
| const globalConfigAnchor = convertBigIntToBN(config); | ||
| const instruction = await this.stakingProgram.methods | ||
| .initConfig(globalConfigAnchor) | ||
|
|
@@ -176,6 +185,7 @@ export class PythStakingClient { | |
| poolData: PublicKey; | ||
| y: bigint; | ||
| }) { | ||
| this.assertWallet(); | ||
| const yAnchor = convertBigIntToBN(y); | ||
| const instruction = await this.integrityPoolProgram.methods | ||
| .initializePool(rewardProgramAuthority, yAnchor) | ||
|
|
@@ -189,6 +199,7 @@ export class PythStakingClient { | |
| } | ||
|
|
||
| public async getOwnerPythAtaAccount(): Promise<Account> { | ||
| this.assertWallet(); | ||
| const globalConfig = await this.getGlobalConfig(); | ||
| return getAccount( | ||
| this.connection, | ||
|
|
@@ -228,6 +239,7 @@ export class PythStakingClient { | |
| stakeAccountPositions: PublicKey, | ||
| amount: bigint, | ||
| ) { | ||
| this.assertWallet(); | ||
| const instruction = await this.stakingProgram.methods | ||
| .createPosition( | ||
| { | ||
|
|
@@ -248,6 +260,7 @@ export class PythStakingClient { | |
| positionState: PositionState.LOCKED | PositionState.LOCKING, | ||
| amount: bigint, | ||
| ) { | ||
| this.assertWallet(); | ||
| const stakeAccountPositionsData = await this.getStakeAccountPositions( | ||
| stakeAccountPositions, | ||
| ); | ||
|
|
@@ -303,6 +316,7 @@ export class PythStakingClient { | |
| positionState: PositionState.LOCKED | PositionState.LOCKING, | ||
| amount: bigint, | ||
| ) { | ||
| this.assertWallet(); | ||
| const stakeAccountPositionsData = await this.getStakeAccountPositions( | ||
| stakeAccountPositions, | ||
| ); | ||
|
|
@@ -355,6 +369,7 @@ export class PythStakingClient { | |
| } | ||
|
|
||
| public async hasGovernanceRecord(config: GlobalConfig): Promise<boolean> { | ||
| this.assertWallet(); | ||
| const tokenOwnerRecordAddress = await getTokenOwnerRecordAddress( | ||
| GOVERNANCE_ADDRESS, | ||
| config.pythGovernanceRealm, | ||
|
|
@@ -370,6 +385,7 @@ export class PythStakingClient { | |
| } | ||
|
|
||
| public async createStakeAccountAndDeposit(amount: bigint) { | ||
| this.assertWallet(); | ||
| const globalConfig = await this.getGlobalConfig(); | ||
|
|
||
| const senderTokenAccount = await getAssociatedTokenAddress( | ||
|
|
@@ -451,6 +467,7 @@ export class PythStakingClient { | |
| stakeAccountPositions: PublicKey, | ||
| amount: bigint, | ||
| ) { | ||
| this.assertWallet(); | ||
| const globalConfig = await this.getGlobalConfig(); | ||
| const mint = globalConfig.pythTokenMint; | ||
|
|
||
|
|
@@ -473,6 +490,7 @@ export class PythStakingClient { | |
| stakeAccountPositions: PublicKey, | ||
| amount: bigint, | ||
| ) { | ||
| this.assertWallet(); | ||
| const globalConfig = await this.getGlobalConfig(); | ||
| const mint = globalConfig.pythTokenMint; | ||
|
|
||
|
|
@@ -497,6 +515,7 @@ export class PythStakingClient { | |
| publisher: PublicKey, | ||
| amount: bigint, | ||
| ) { | ||
| this.assertWallet(); | ||
| const instruction = await this.integrityPoolProgram.methods | ||
| .delegate(convertBigIntToBN(amount)) | ||
| .accounts({ | ||
|
|
@@ -534,6 +553,7 @@ export class PythStakingClient { | |
| async getAdvanceDelegationRecordInstructions( | ||
| stakeAccountPositions: PublicKey, | ||
| ) { | ||
| this.assertWallet(); | ||
| const poolData = await this.getPoolDataAccount(); | ||
| const stakeAccountPositionsData = await this.getStakeAccountPositions( | ||
| stakeAccountPositions, | ||
|
|
@@ -589,6 +609,7 @@ export class PythStakingClient { | |
| } | ||
|
|
||
| public async advanceDelegationRecord(stakeAccountPositions: PublicKey) { | ||
| this.assertWallet(); | ||
| const instructions = await this.getAdvanceDelegationRecordInstructions( | ||
| stakeAccountPositions, | ||
| ); | ||
|
|
@@ -604,6 +625,7 @@ export class PythStakingClient { | |
| } | ||
|
|
||
| public async getClaimableRewards(stakeAccountPositions: PublicKey) { | ||
| this.assertWallet(); | ||
|
||
| const instructions = await this.getAdvanceDelegationRecordInstructions( | ||
| stakeAccountPositions, | ||
| ); | ||
|
|
@@ -650,6 +672,7 @@ export class PythStakingClient { | |
| stakeAccountPositions: PublicKey, | ||
| newStakeAccountPositions: PublicKey | undefined, | ||
| ) { | ||
| this.assertWallet(); | ||
| const instruction = await this.integrityPoolProgram.methods | ||
| .setPublisherStakeAccount() | ||
| .accounts({ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does {} === undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, but the check is for
this.walletnot the{}in the provider.