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

feat(staking): create a stake account on initial deposit #1887

Merged

Conversation

cprussin
Copy link
Collaborator

This PR includes a fairly significant refactor to restructure some hooks:

  • useStakeAccount has been replaced by a very similar hook called useApi. The biggest difference here, apart from some renaming, is that the states in useApi expose wrapped functions from api.ts, bound to the current stake account & client. So there's no need to interact with the stake account or staking client or api.ts anywhere else in the app any more.
  • useTransfer is replaced by a more generic hook useAsync, which takes an async callback and returns a function to trigger execution and the current execution satate. I did this because, once moving everything to useApi, everything specific to transferring, including busting the swr cache, got moved to the useApi wrapper functions, and what was left was just really a generic mechanism for running and tracking state of an async function.
  • useAccountHistory and useDashboardData have been replaced by useData, which takes useSwr and wraps it to bind the output to enum types. Both of the original hooks were doing mostly the same thing but with marginally different ways to set up their fetch functions; with useApi those things are now abstracted in the API states.

With those in place, I was able to use the states in useApi to abstract away how to handle depositing & fetching data when a stake account exists from when it doesn't, and I was able to leverage the type system to guarantee that an API method can't be called in an invalid state, which cleaned up a lot of other code too and removed the possibility of some class of runtime exceptions.

Copy link

vercel bot commented Sep 11, 2024

@cprussin is attempting to deploy a commit to the pyth-web Team on Vercel.

A member of the Team first needs to authorize it.

}
};

const loadPublisherData = async (client: PythStakingClient) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keyvankhademi FYI, this stuff is separated out because there are now two separate flows for fetching dashboard data -- one for when there is a stake account and one for when there isn't. The publisher data is common between the two.

@@ -221,6 +262,13 @@ export const loadAccountHistory = async (
return mkMockHistory();
};

export const createStakeAccountAndDeposit = async (
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDisabled={availableToWithdraw === 0n}
{...(api.type === ApiStateType.Loaded && {
transfer: api.withdraw,
})}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to reviewer: in this PR I made the <TransferButton> component render a disabled button if the max is 0n or if transfer is undefined. That makes it a lot easier to just use it all over and let it disable itself correctly if there's no function to call (generally meaning the API is in a state where this transfer is impossible) or if there's no tokens to transfer. So you'll see here and a few other places where I removed some checks that control the button states, since those are now handled by the button itself.

Copy link

vercel bot commented Sep 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api-reference ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 11, 2024 6:48pm
staking-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 11, 2024 6:48pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
xc-admin-frontend ⬜️ Ignored (Inspect) Visit Preview Sep 11, 2024 6:48pm

@cprussin cprussin force-pushed the create-stake-account-on-initial-deposit branch from 7c7aa8d to 20ce372 Compare September 11, 2024 18:41
@cprussin cprussin merged commit 55ec07a into pyth-network:main Sep 11, 2024
6 checks passed
@cprussin cprussin deleted the create-stake-account-on-initial-deposit branch September 11, 2024 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants