Skip to content

feat: add claiming of vesting and staking positions #25

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 2 commits into from
May 12, 2025
Merged
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
93 changes: 67 additions & 26 deletions components/TableOwnedPositions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,24 @@
</template>
</td>
<td class="table-positions__td">
<BaseTooltip v-if="stake.isVesting" :tooltip-text="upgradeToStakeTooltipText">
<template #trigger>
<button :disabled="isUpgradingToStake" class="table-positions__upgrade-btn" @click="upgradeToStake(stake.unlockEpoch, DEFAULT_VESTING_UPGRADE_EPOCH_LOCKUP)">
{{ isUpgradingToStake ? 'Upgrading...' : 'Upgrade to stake' }}
</button>
</template>
</BaseTooltip>
<span
v-else
class="link link--no-underline link--primary"
@click="openModal(stake.id, stake.epochsRemaining, stake.amount)">
Increase Duration
</span>
<div class="table-positions__actions-wrapper">
<BaseTooltip v-if="stake.isVesting && stake.unlocksIn > 0" :tooltip-text="upgradeToStakeTooltipText">
<template #trigger>
<button :disabled="isUpgradingToStake" class="table-positions__upgrade-btn" @click="upgradeToStake(stake.unlockEpoch, DEFAULT_VESTING_UPGRADE_EPOCH_LOCKUP)">
{{ isUpgradingToStake ? 'Upgrading...' : 'Upgrade to stake' }}
</button>
</template>
</BaseTooltip>
<span
v-if="!stake.isVesting"
class="link link--no-underline link--primary"
@click="openModal(stake.id, stake.epochsRemaining, stake.amount)">
Increase Duration
</span>
<button v-if="stake.unlocksIn === 0" class="table-positions__upgrade-btn" @click="claim(stake)" :disabled="isClaiming">
Claim
</button>
</div>
</td>
</tr>
</tbody>
Expand All @@ -94,16 +99,15 @@

<script setup lang="ts">
import { useLocalStorage } from '@vueuse/core';
import { useAccount, useWriteContract } from '@wagmi/vue';
import { useAccount } from '@wagmi/vue';
import { formatUnits, parseUnits } from 'viem';
import { SECONDS_IN_EPOCH, MIN_STAKE_DURATION_IN_EPOCH } from '~/constants/contracts';
import { formatSeconds } from '@/utils/date';
import { TooltipBorderColor } from './BaseTooltip.vue';
import { useChainIdTypesafe } from '~/constants/chain';
import {useUserVestedTokens, useCurrentEpoch, useAllBeneficiaries} from '~/utils/hooks';
import { wagmiAdapter } from '~/wagmi';
import { PWN_VESTING_MANAGER } from '~/constants/addresses';
import { PWN_VESTING_MANAGER_ABI } from '~/constants/abis';
import {useUserVestedTokens, useAllBeneficiaries} from '~/utils/hooks';
import { PWN_VESTING_MANAGER, VE_PWN_TOKEN } from '~/constants/addresses';
import { PWN_VESTING_MANAGER_ABI, VE_PWN_TOKEN_ABI } from '~/constants/abis';
import type {Address} from "abitype";
import {shortenAddress} from "../utils/web3";
import {getFormattedVotingPower} from "~/utils/parsing";
Expand Down Expand Up @@ -358,15 +362,20 @@ const handleSortingIconClick = (newSortingProp: SortingProp) => {

const DEFAULT_VESTING_UPGRADE_EPOCH_LOCKUP = MIN_STAKE_DURATION_IN_EPOCH

const { writeContractAsync: _writeContractUpgradeToStake, isPending: isUpgradingToStake } = useWriteContract()
const isUpgradingToStake = ref(false)
const upgradeToStake = async (unlockEpoch: number | bigint, stakeLockUpEpochs: number | bigint) => {
return await _writeContractUpgradeToStake({
abi: PWN_VESTING_MANAGER_ABI,
address: PWN_VESTING_MANAGER[1],
functionName: 'upgradeToStake',
chainId: 1,
args: [BigInt(unlockEpoch), BigInt(stakeLockUpEpochs)]
})
isUpgradingToStake.value = true
try {
return await sendTransaction({
abi: PWN_VESTING_MANAGER_ABI,
address: PWN_VESTING_MANAGER[1],
functionName: 'upgradeToStake',
chainId: 1,
args: [BigInt(unlockEpoch), BigInt(stakeLockUpEpochs)]
})
} finally {
Copy link
Member

Choose a reason for hiding this comment

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

It would be great to also add error handling here

isUpgradingToStake.value = false
}
}

const upgradeToStakeTooltipText = computed(() => {
Expand All @@ -387,6 +396,32 @@ const upgradeToStakeTooltipText = computed(() => {
For a longer stake lock-up period, please contact us on Discord for assistance.
`
})

const isClaiming = ref(false)
const claim = async (stake: TableRowData) => {
try {
isClaiming.value = true
if (stake.isVesting) {
return await sendTransaction({
abi: PWN_VESTING_MANAGER_ABI,
address: PWN_VESTING_MANAGER[1],
functionName: 'claimVesting',
chainId: 1,
args: [BigInt(stake.unlockEpoch)]
})
} else {
return await sendTransaction({
abi: VE_PWN_TOKEN_ABI,
address: VE_PWN_TOKEN[chainId.value],
functionName: 'withdrawStake',
chainId: chainId.value,
args: [BigInt(stake.id), stake.votingDelegate || address.value!]
})
}
} finally {
isClaiming.value = false
}
}
</script>

<style scoped>
Expand Down Expand Up @@ -500,5 +535,11 @@ const upgradeToStakeTooltipText = computed(() => {
}
}
}

&__actions-wrapper {
display: flex;
align-items: center;
column-gap: 1rem;
}
}
</style>
2 changes: 2 additions & 0 deletions constants/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export const VE_PWN_TOKEN_ABI = [
"stateMutability": "nonpayable",
"type": "function"
},
{ "inputs": [{ "internalType": "uint256", "name": "stakeId", "type": "uint256" }, { "internalType": "address", "name": "stakeBeneficiary", "type": "address" }], "name": "withdrawStake", "outputs": [], "stateMutability": "nonpayable", "type": "function" },
{ "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "inputs": [], "name": "ApproveDisabled", "type": "error" }, { "inputs": [], "name": "DelegateBySigDisabled", "type": "error" }, { "inputs": [], "name": "DelegateDisabled", "type": "error" }, { "inputs": [], "name": "EpochStillRunning", "type": "error" }, { "inputs": [], "name": "InvalidAmount", "type": "error" }, { "inputs": [], "name": "InvalidLockUpPeriod", "type": "error" }, { "inputs": [], "name": "LockUpPeriodMismatch", "type": "error" }, { "inputs": [], "name": "NotStakeOwner", "type": "error" }, { "inputs": [], "name": "NothingToIncrease", "type": "error" }, { "inputs": [{ "internalType": "uint256", "name": "lastCalculatedEpoch", "type": "uint256" }], "name": "PowerAlreadyCalculated", "type": "error" }, { "inputs": [], "name": "SameBeneficiary", "type": "error" }, { "inputs": [{ "internalType": "uint256", "name": "stakeId", "type": "uint256" }], "name": "StakeNotFound", "type": "error" }, { "inputs": [], "name": "TransferDisabled", "type": "error" }, { "inputs": [], "name": "TransferFromDisabled", "type": "error" }, { "inputs": [], "name": "WithrawalBeforeLockUpEnd", "type": "error" }
] as const

export const EPOCH_CLOCK_ABI = [
Expand Down
5 changes: 4 additions & 1 deletion utils/useTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import to from '@/utils/await-to-js'
import type { Abi, Address, ContractFunctionArgs, ContractFunctionName, TransactionReceipt } from 'viem'
import { getAccount, getBlockNumber, getPublicClient, getTransaction, getTransactionReceipt, switchChain, waitForTransactionReceipt, watchContractEvent, writeContract } from '@wagmi/vue/actions'
import { getAccount, getBlockNumber, getPublicClient, getTransaction, getTransactionReceipt, simulateContract, switchChain, waitForTransactionReceipt, watchContractEvent, writeContract } from '@wagmi/vue/actions'
import type { WriteContractVariables } from '@wagmi/vue/query'
import type { AnyFunction, IntervalId } from '@/types/customTypes'
// import type { ToastStep } from '@/modules/common/notifications/useToastsStore'
Expand Down Expand Up @@ -66,6 +66,9 @@ export async function sendTransaction<

const { address: userAddress } = getAccount(wagmiAdapter.wagmiConfig)

// @ts-expect-error probably some type mismatch between writeContract and simulateContract
await simulateContract(wagmiAdapter.wagmiConfig, transaction)


// txHash:
// 1) EOA: real transaction hash
Expand Down