Skip to content

Commit

Permalink
fix(ui): add reward token opt-in to add/rm stake simulate call
Browse files Browse the repository at this point in the history
  • Loading branch information
drichar committed May 17, 2024
1 parent 6251100 commit 5793f46
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
32 changes: 28 additions & 4 deletions ui/src/api/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ export async function addStake(
suggestedParams,
})

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

const simulateValidatorClient = await getSimulateValidatorClient(activeAddress, authAddr)

const simulateComposer = simulateValidatorClient
Expand All @@ -556,6 +558,18 @@ export async function addStake(
{ sendParams: { fee: AlgoAmount.MicroAlgos(240_000) } },
)

if (needsOptInTxn) {
const rewardTokenOptInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: activeAddress,
to: activeAddress,
amount: 0,
assetIndex: rewardTokenId,
suggestedParams,
})

simulateComposer.addTransaction(rewardTokenOptInTxn)
}

const simulateResults = await simulateComposer.simulate({
allowEmptySignatures: true,
allowUnnamedResources: true,
Expand All @@ -568,8 +582,6 @@ export async function addStake(
2000 + 1000 * ((simulateResults.simulateResponse.txnGroups[0].appBudgetAdded as number) / 700),
)

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

const composer = validatorClient
.compose()
.gas({})
Expand Down Expand Up @@ -834,6 +846,8 @@ export async function removeStake(
authAddr,
)

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

const simulateComposer = stakingPoolSimulateClient
.compose()
.gas({}, { note: '1', sendParams: { fee: AlgoAmount.MicroAlgos(0) } })
Expand All @@ -846,6 +860,18 @@ export async function removeStake(
{ sendParams: { fee: AlgoAmount.MicroAlgos(240_000) } },
)

if (needsOptInTxn) {
const rewardTokenOptInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({
from: activeAddress,
to: activeAddress,
amount: 0,
assetIndex: rewardTokenId,
suggestedParams,
})

simulateComposer.addTransaction(rewardTokenOptInTxn)
}

const simulateResult = await simulateComposer.simulate({
allowEmptySignatures: true,
allowUnnamedResources: true,
Expand All @@ -859,8 +885,6 @@ export async function removeStake(
),
)

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

const stakingPoolClient = await getStakingPoolClient(poolAppId, signer, activeAddress)

const composer = stakingPoolClient
Expand Down
1 change: 1 addition & 0 deletions ui/src/components/AddStakeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export function AddStakeModal({
// Invalidate other queries to update UI
queryClient.invalidateQueries({ queryKey: ['stakes', { staker: activeAddress }] })
queryClient.invalidateQueries({ queryKey: ['staked-info'] })
queryClient.invalidateQueries({ queryKey: ['pools-info'] })
router.invalidate()
} catch (error) {
toast.error('Failed to add stake to pool', { id: toastId })
Expand Down

0 comments on commit 5793f46

Please sign in to comment.