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

Add pool identifier to admin form #297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const NATIVE_ERRORS: ErrorCode[] = [
message:
'Insufficient funds. User does not have enough balance of token to complete the transaction',
},
{
code: '0',
message: 'Address already in use',
},
// token program errors
{
code: '0x1',
Expand Down
2 changes: 1 addition & 1 deletion components/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Admin() {
defer
data-domain="stake.cardinal.so"
src="https://plausible.io/js/plausible.js"
></script>
/>
</Head>

<HeaderSlim />
Expand Down
36 changes: 25 additions & 11 deletions components/StakePoolForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { tryFormatInput, tryParseInput } from 'common/units'
import { tryPublicKey } from 'common/utils'
import { asWallet } from 'common/Wallets'
import { useFormik } from 'formik'
import { validPoolIdentifier } from 'handlers/useHandleCreatePool'
import { useEnvironmentCtx } from 'providers/EnvironmentProvider'
import { useMemo, useState } from 'react'
import type { Account, Mint } from 'spl-token-v3'
Expand All @@ -25,6 +26,13 @@ export const publicKeyValidationTest = (value: string | undefined): boolean => {
return tryPublicKey(value) ? true : false
}

export const optionalPublicKeyValidationTest = (
value: string | undefined
): boolean => {
if (!value || value.length === 0) return true
return publicKeyValidationTest(value)
}

export const bnValidationTest = (value: string | undefined): boolean => {
if (value === undefined) return false
try {
Expand All @@ -38,7 +46,11 @@ export const bnValidationTest = (value: string | undefined): boolean => {
}

const creationFormSchema = Yup.object({
// overlayText: Yup.string(),
identifier: Yup.string().test(
'url-safe',
'Invalid identifier',
(value: string | undefined) => validPoolIdentifier(value ?? '')
),
requireCollections: Yup.array()
.of(
Yup.string().test(
Expand Down Expand Up @@ -68,7 +80,7 @@ const creationFormSchema = Yup.object({
rewardMintAddress: Yup.string().test(
'is-public-key',
'Invalid reward mint address',
publicKeyValidationTest
optionalPublicKeyValidationTest
),
rewardAmount: Yup.string()
.optional()
Expand Down Expand Up @@ -107,7 +119,7 @@ export function StakePoolForm({
const { connection } = useEnvironmentCtx()
const wallet = useWallet()
const initialValues: CreationForm = {
// overlayText: stakePoolData?.parsed.overlayText ?? 'STAKED',
identifier: stakePoolData?.parsed?.identifier ?? undefined,
requireCollections: (stakePoolData?.parsed?.allowedCollections ?? []).map(
(pk) => pk.toString()
),
Expand Down Expand Up @@ -237,22 +249,24 @@ export function StakePoolForm({

return (
<form className="w-full max-w-lg">
{/* <div className="-mx-3 flex flex-wrap">
<div className="-mx-3 flex flex-wrap">
<div className="mb-6 mt-4 w-full px-3 md:mb-0">
<FormFieldTitleInput
title={'Overlay Text'}
description={'Text to display over the receipt'}
title={'Pool ID (cannot contain spaces)'}
Copy link
Contributor

Choose a reason for hiding this comment

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

Add "cannot be changed later" too?

description={'This will be used in the URL'}
/>
<input
<FormInput
className="mb-3 block w-full appearance-none rounded border border-gray-500 bg-gray-700 py-3 px-4 leading-tight text-gray-200 placeholder-gray-500 focus:bg-gray-800 focus:outline-none"
type="text"
placeholder={'STAKED'}
name="overlayText"
value={values.overlayText}
placeholder={'Pool ID'}
name="identifier"
disabled={type === 'update'}
value={values.identifier}
error={!!errors.identifier}
onChange={handleChange}
/>
</div>
</div> */}
</div>
<div className="-mx-3 flex flex-wrap">
<div className="mb-6 mt-4 w-full px-3 md:mb-0">
<FormFieldTitleInput
Expand Down
13 changes: 3 additions & 10 deletions components/StakedTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,9 @@ export const StakedTokens = () => {
<div className="flex gap-5">
<Tooltip title={'Unstake will automatically claim reward for you.'}>
<button
onClick={() => {
if (stakedSelected.length === 0) {
notify({
message: `No tokens selected`,
type: 'error',
})
} else {
handleUnstake.mutate({ tokenDatas: stakedSelected })
}
}}
onClick={() =>
handleUnstake.mutate({ tokenDatas: stakedSelected })
}
style={{
background:
stakePoolMetadata?.colors?.secondary || defaultSecondaryColor,
Expand Down
19 changes: 6 additions & 13 deletions components/UnstakedTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,12 @@ export const UnstakedTokens = () => {
<div className="flex gap-5">
<Tooltip title="Click on tokens to select them">
<button
onClick={() => {
if (unstakedSelected.length === 0) {
notify({
message: `No tokens selected`,
type: 'error',
})
} else {
handleStake.mutate({
tokenDatas: unstakedSelected,
receiptType,
})
}
}}
onClick={() =>
handleStake.mutate({
tokenDatas: unstakedSelected,
receiptType,
})
}
style={{
background:
stakePoolMetadata?.colors?.secondary || defaultSecondaryColor,
Expand Down
7 changes: 6 additions & 1 deletion handlers/useHandleCreatePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { useMutation } from 'react-query'
import { useStakePoolData } from '../hooks/useStakePoolData'
import { useEnvironmentCtx } from '../providers/EnvironmentProvider'

export const validPoolIdentifier = (value: string) =>
/^[a-zA-Z0-9_-]*$/.test(value)

export const useHandleCreatePool = () => {
const wallet = asWallet(useWallet())
const { connection } = useEnvironmentCtx()
Expand Down Expand Up @@ -85,7 +88,9 @@ export const useHandleCreatePool = () => {

const transaction = new Transaction()
const program = rewardsCenterProgram(connection, wallet)
const identifier = `pool-name-${Math.random()}`
const identifier = values.identifier ?? ''
if (!validPoolIdentifier(identifier)) throw 'Invalid pool ID'

const stakePoolId = findStakePoolId(identifier)
const ix = await program.methods
.initPool({
Expand Down
1 change: 1 addition & 0 deletions handlers/useHandleUnstake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const useHandleUnstake = (callback?: () => void) => {
}): Promise<string[]> => {
if (!stakePoolId) throw 'Stake pool not found'
if (!stakePool || !stakePool.parsed) throw 'Stake pool not found'
if (tokenDatas.length <= 0) throw 'No tokens selected'

const ataTx = new Transaction()
if (rewardDistributorData.data && rewardDistributorData.data.parsed) {
Expand Down