Skip to content

Commit

Permalink
refactor(ui): remove rekeyed account simulate workaround
Browse files Browse the repository at this point in the history
Remove custom `makeEmptyTransactionSigner` implementation and `AuthAddressProvider`
since algosdk now correctly handles simulate calls from rekeyed accounts. This
was originally added as a workaround in #89 but is no longer needed after
algorand/go-algorand#5942.
  • Loading branch information
drichar committed Dec 5, 2024
1 parent f0ed7b0 commit ee8698f
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 155 deletions.
9 changes: 3 additions & 6 deletions ui/src/api/clients.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import algosdk from 'algosdk'
import algosdk, { makeEmptyTransactionSigner } from 'algosdk'
import { FEE_SINK } from '@/constants/accounts'
import { StakingPoolClient, StakingPoolFactory } from '@/contracts/StakingPoolClient'
import { ValidatorRegistryClient } from '@/contracts/ValidatorRegistryClient'
import { makeEmptyTransactionSigner } from '@/lib/makeEmptyTransactionSigner'
import { getRetiAppIdFromViteEnvironment } from '@/utils/env'
import { getAlgodConfigFromViteEnvironment } from '@/utils/network/getAlgoClientConfigs'
import { AlgorandClient } from '@algorandfoundation/algokit-utils'
Expand All @@ -29,11 +28,10 @@ export async function getValidatorClient(

export async function getSimulateValidatorClient(
senderAddr: string = FEE_SINK,
authAddr?: string,
): Promise<ValidatorRegistryClient> {
return algorandClient.client.getTypedAppClientById(ValidatorRegistryClient, {
defaultSender: senderAddr,
defaultSigner: makeEmptyTransactionSigner(authAddr),
defaultSigner: makeEmptyTransactionSigner(),
appId: RETI_APP_ID,
})
}
Expand All @@ -53,11 +51,10 @@ export async function getStakingPoolClient(
export async function getSimulateStakingPoolClient(
poolAppId: bigint,
senderAddr: string = FEE_SINK,
authAddr?: string,
): Promise<StakingPoolClient> {
return algorandClient.client.getTypedAppClientById(StakingPoolClient, {
defaultSender: senderAddr,
defaultSigner: makeEmptyTransactionSigner(authAddr),
defaultSigner: makeEmptyTransactionSigner(),
appId: poolAppId,
})
}
45 changes: 12 additions & 33 deletions ui/src/api/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
Validator,
ValidatorConfigInput,
} from '@/interfaces/validator'
import { makeEmptyTransactionSigner } from '@/lib/makeEmptyTransactionSigner'
import { BalanceChecker } from '@/utils/balanceChecker'
import { calculateValidatorPoolMetrics } from '@/utils/contracts'
import { ParamsCache } from '@/utils/paramsCache'
Expand Down Expand Up @@ -249,14 +248,13 @@ export async function addValidator(
nfdAppId: bigint,
signer: algosdk.TransactionSigner,
activeAddress: string,
authAddr?: string,
) {
const validatorClient = await getValidatorClient(signer, activeAddress)

const { addValidatorMbr } = (
await validatorClient.send.getMbrAmounts({
args: {},
signer: makeEmptyTransactionSigner(authAddr),
signer: algosdk.makeEmptyTransactionSigner(),
})
).return!

Expand Down Expand Up @@ -435,7 +433,7 @@ export async function doesStakerNeedToPayMbr(
authAddr?: string,
client?: ValidatorRegistryClient,
): Promise<boolean> {
const validatorClient = client || (await getSimulateValidatorClient(activeAddress, authAddr))
const validatorClient = client || (await getSimulateValidatorClient(activeAddress))

const result = await validatorClient.send.doesStakerNeedToPayMbr({
args: { staker: activeAddress },
Expand All @@ -454,7 +452,7 @@ export async function findPoolForStaker(
authAddr?: string,
client?: ValidatorRegistryClient,
): Promise<FindPoolForStakerResponse> {
const validatorClient = client || (await getSimulateValidatorClient(activeAddress, authAddr))
const validatorClient = client || (await getSimulateValidatorClient(activeAddress))

const result = await validatorClient
.newGroup()
Expand Down Expand Up @@ -495,7 +493,6 @@ export async function addStake(
rewardTokenId: bigint,
signer: algosdk.TransactionSigner,
activeAddress: string,
authAddr?: string,
): Promise<ValidatorPoolKey> {
const validatorClient = await getValidatorClient(signer, activeAddress)
const suggestedParams = await ParamsCache.getSuggestedParams()
Expand All @@ -507,7 +504,7 @@ export async function addStake(

const needsOptInTxn = rewardTokenId > 0 && !(await isOptedInToAsset(activeAddress, rewardTokenId))

const simulateValidatorClient = await getSimulateValidatorClient(activeAddress, authAddr)
const simulateValidatorClient = await getSimulateValidatorClient(activeAddress)

const simulateComposer = simulateValidatorClient
.newGroup()
Expand All @@ -534,7 +531,7 @@ export async function addStake(
suggestedParams,
})

simulateComposer.addTransaction(rewardTokenOptInTxn, makeEmptyTransactionSigner(authAddr))
simulateComposer.addTransaction(rewardTokenOptInTxn, algosdk.makeEmptyTransactionSigner())
}

const simulateResults = await simulateComposer.simulate({
Expand Down Expand Up @@ -762,15 +759,10 @@ export async function removeStake(
rewardTokenId: bigint,
signer: algosdk.TransactionSigner,
activeAddress: string,
authAddr?: string,
) {
const suggestedParams = await ParamsCache.getSuggestedParams()

const stakingPoolSimulateClient = await getSimulateStakingPoolClient(
poolAppId,
activeAddress,
authAddr,
)
const stakingPoolSimulateClient = await getSimulateStakingPoolClient(poolAppId, activeAddress)

const needsOptInTxn = rewardTokenId > 0 && !(await isOptedInToAsset(activeAddress, rewardTokenId))

Expand All @@ -792,7 +784,7 @@ export async function removeStake(
suggestedParams,
})

simulateComposer.addTransaction(rewardTokenOptInTxn, makeEmptyTransactionSigner(authAddr))
simulateComposer.addTransaction(rewardTokenOptInTxn, algosdk.makeEmptyTransactionSigner())
}

const simulateResult = await simulateComposer.simulate({
Expand Down Expand Up @@ -845,39 +837,28 @@ export async function epochBalanceUpdate(
poolAppId: bigint,
signer: algosdk.TransactionSigner,
activeAddress: string,
authAddr?: string,
): Promise<void> {
try {
const stakingPoolSimulateClient = await getSimulateStakingPoolClient(
poolAppId,
activeAddress,
authAddr,
)
const stakingPoolSimulateClient = await getSimulateStakingPoolClient(poolAppId, activeAddress)

const simulateResult = await stakingPoolSimulateClient
.newGroup()
.gas({
args: [],
note: '1',
staticFee: AlgoAmount.MicroAlgos(0),
signer: makeEmptyTransactionSigner(
'A7NMWS3NT3IUDMLVO26ULGXGIIOUQ3ND2TXSER6EBGRZNOBOUIQXHIBGDE',
),
signer: algosdk.makeEmptyTransactionSigner(),
})
.gas({
args: [],
note: '2',
staticFee: AlgoAmount.MicroAlgos(0),
signer: makeEmptyTransactionSigner(
'A7NMWS3NT3IUDMLVO26ULGXGIIOUQ3ND2TXSER6EBGRZNOBOUIQXHIBGDE',
),
signer: algosdk.makeEmptyTransactionSigner(),
})
.epochBalanceUpdate({
args: {},
staticFee: AlgoAmount.MicroAlgos(240_000),
signer: makeEmptyTransactionSigner(
'A7NMWS3NT3IUDMLVO26ULGXGIIOUQ3ND2TXSER6EBGRZNOBOUIQXHIBGDE',
),
signer: algosdk.makeEmptyTransactionSigner(),
})
.simulate({ allowEmptySignatures: true, allowUnnamedResources: true })

Expand Down Expand Up @@ -988,18 +969,16 @@ export async function claimTokens(
pools: PoolInfo[],
signer: algosdk.TransactionSigner,
activeAddress: string,
authAddr?: string,
) {
const [algorand, stakingFactory] = getStakingPoolFactory()

const feeComposer = algorand.newGroup()
const simSigner = makeEmptyTransactionSigner(authAddr)

for (const pool of pools) {
const client = stakingFactory.getAppClientById({
appId: pool.poolAppId,
defaultSender: activeAddress,
defaultSigner: simSigner,
defaultSigner: algosdk.makeEmptyTransactionSigner(),
})
feeComposer
.addAppCallMethodCall(
Expand Down
14 changes: 5 additions & 9 deletions ui/src/components/AddStakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import { Input } from '@/components/ui/input'
import { GatingType } from '@/constants/gating'
import { StakerPoolData, StakerValidatorData } from '@/interfaces/staking'
import { Validator } from '@/interfaces/validator'
import { useAuthAddress } from '@/providers/AuthAddressProvider'
import { InsufficientBalanceError } from '@/utils/balanceChecker'
import {
calculateMaxAvailableToStake,
Expand Down Expand Up @@ -82,7 +81,6 @@ export function AddStakeModal({
const queryClient = useQueryClient()
const router = useRouter()
const { transactionSigner, activeAddress } = useWallet()
const { authAddress, isReady } = useAuthAddress()

const accountInfoQuery = useQuery({
queryKey: ['account-info', activeAddress],
Expand Down Expand Up @@ -119,8 +117,8 @@ export function AddStakeModal({
// @todo: make this a custom hook, call from higher up and pass down as prop
const mbrRequiredQuery = useQuery({
queryKey: ['mbr-required', activeAddress],
queryFn: () => doesStakerNeedToPayMbr(activeAddress!, authAddress),
enabled: !!activeAddress && isReady,
queryFn: () => doesStakerNeedToPayMbr(activeAddress!),
enabled: !!activeAddress,
})
const mbrRequired = mbrRequiredQuery.data || false
const mbrAmount = mbrRequired ? addStakerMbr : 0n
Expand Down Expand Up @@ -227,24 +225,23 @@ export function AddStakeModal({
Number(validator.id),
BigInt(amountToStake),
activeAddress,
authAddress,
)
setTargetPoolId(Number(poolKey.poolId))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
console.error(`Error fetching target pool: ${error.message}`)
}
},
[activeAddress, authAddress, minimumStake, validator],
[activeAddress, minimumStake, validator],
)

React.useEffect(() => {
if (validator?.id && isReady) {
if (validator?.id) {
fetchTargetPoolId()
} else {
setTargetPoolId(null)
}
}, [fetchTargetPoolId, isReady, validator?.id])
}, [fetchTargetPoolId, validator?.id])

const debouncedFetchTargetPoolId = useDebouncedCallback(async (value) => {
const isValid = await form.trigger('amountToStake')
Expand Down Expand Up @@ -309,7 +306,6 @@ export function AddStakeModal({
validator!.config.rewardTokenId,
transactionSigner,
activeAddress,
authAddress,
)

toast.success(
Expand Down
4 changes: 0 additions & 4 deletions ui/src/components/AddValidatorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
import { Separator } from '@/components/ui/separator'
import { GatingType } from '@/constants/gating'
import { useBlockTime } from '@/hooks/useBlockTime'
import { useAuthAddress } from '@/providers/AuthAddressProvider'
import { InsufficientBalanceError } from '@/utils/balanceChecker'
import {
getEpochLengthBlocks,
Expand Down Expand Up @@ -74,10 +73,8 @@ export function AddValidatorForm({ constraints }: AddValidatorFormProps) {
const [isSigning, setIsSigning] = React.useState(false)

const { transactionSigner, activeAddress } = useWallet()
const { authAddress } = useAuthAddress()

const queryClient = useQueryClient()

const navigate = useNavigate({ from: '/add' })

const formSchema = z
Expand Down Expand Up @@ -322,7 +319,6 @@ export function AddValidatorForm({ constraints }: AddValidatorFormProps) {
nfdForInfoAppId,
transactionSigner,
activeAddress,
authAddress,
)

toast.success(
Expand Down
4 changes: 1 addition & 3 deletions ui/src/components/ClaimTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { toast } from 'sonner'
import { claimTokens } from '@/api/contracts'
import { DropdownMenuItem } from '@/components/ui/dropdown-menu'
import { Validator } from '@/interfaces/validator'
import { useAuthAddress } from '@/providers/AuthAddressProvider'
import { formatAssetAmount } from '@/utils/format'

interface ClaimTokensProps {
Expand All @@ -16,7 +15,6 @@ interface ClaimTokensProps {

export function ClaimTokens({ validator, rewardTokenBalance }: ClaimTokensProps) {
const { transactionSigner, activeAddress } = useWallet()
const { authAddress } = useAuthAddress()
const queryClient = useQueryClient()

const toastIdRef = React.useRef(`toast-${Date.now()}-${Math.random()}`)
Expand All @@ -40,7 +38,7 @@ export function ClaimTokens({ validator, rewardTokenBalance }: ClaimTokensProps)

toast.loading('Sign transactions to claim reward tokens...', { id: toastId })

await claimTokens(validator.pools, transactionSigner, activeAddress, authAddress)
await claimTokens(validator.pools, transactionSigner, activeAddress)

toast.success(
<div className="flex items-center gap-x-2">
Expand Down
3 changes: 0 additions & 3 deletions ui/src/components/StakingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
import { UnstakeModal } from '@/components/UnstakeModal'
import { StakerValidatorData } from '@/interfaces/staking'
import { Validator } from '@/interfaces/validator'
import { useAuthAddress } from '@/providers/AuthAddressProvider'
import {
calculateRewardEligibility,
canManageValidator,
Expand Down Expand Up @@ -78,7 +77,6 @@ export function StakingTable({
const [unstakeValidator, setUnstakeValidator] = React.useState<Validator | null>(null)

const { transactionSigner, activeAddress } = useWallet()
const { authAddress } = useAuthAddress()

const router = useRouter()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -276,7 +274,6 @@ export function StakingTable({
100,
transactionSigner,
activeAddress,
authAddress,
queryClient,
router,
)
Expand Down
3 changes: 0 additions & 3 deletions ui/src/components/UnstakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import {
} from '@/components/ui/select'
import { StakerPoolData, StakerValidatorData } from '@/interfaces/staking'
import { Validator } from '@/interfaces/validator'
import { useAuthAddress } from '@/providers/AuthAddressProvider'
import { InsufficientBalanceError } from '@/utils/balanceChecker'
import { setValidatorQueriesData } from '@/utils/contracts'
import { formatAlgoAmount } from '@/utils/format'
Expand Down Expand Up @@ -73,7 +72,6 @@ export function UnstakeModal({ validator, setValidator, stakesByValidator }: Uns
const queryClient = useQueryClient()
const router = useRouter()
const { transactionSigner, activeAddress } = useWallet()
const { authAddress } = useAuthAddress()

const formSchema = z.object({
amountToUnstake: z
Expand Down Expand Up @@ -203,7 +201,6 @@ export function UnstakeModal({ validator, setValidator, stakesByValidator }: Uns
validator!.config.rewardTokenId,
transactionSigner,
activeAddress,
authAddress,
)

toast.success(
Expand Down
3 changes: 0 additions & 3 deletions ui/src/components/ValidatorTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import { ValidatorRewards } from '@/components/ValidatorRewards'
import { useLocalStorage } from '@/hooks/useLocalStorage'
import { StakerValidatorData } from '@/interfaces/staking'
import { Validator } from '@/interfaces/validator'
import { useAuthAddress } from '@/providers/AuthAddressProvider'
import {
calculateMaxStake,
calculateSaturationPercentage,
Expand Down Expand Up @@ -88,7 +87,6 @@ export function ValidatorTable({
const [addPoolValidator, setAddPoolValidator] = React.useState<Validator | null>(null)

const { transactionSigner, activeAddress } = useWallet()
const { authAddress } = useAuthAddress()

const router = useRouter()
const queryClient = useQueryClient()
Expand Down Expand Up @@ -419,7 +417,6 @@ export function ValidatorTable({
100,
transactionSigner,
activeAddress!,
authAddress,
queryClient,
router,
)
Expand Down
Loading

0 comments on commit ee8698f

Please sign in to comment.