Skip to content

Commit

Permalink
feat: updated unlock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
impelcrypto committed May 2, 2024
1 parent c547cf6 commit 4a38747
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/staking-v3/hooks/useDappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import {
import { Symbols } from 'src/v2/symbols';
import { ExtrinsicStatusMessage, IEventAggregator } from 'src/v2/messaging';
import { useStore } from 'src/store';
import { useAccount, useChainMetadata, useNetworkInfo, useLedger } from 'src/hooks';
import {
useAccount,
useChainMetadata,
useNetworkInfo,
useLedger,
ETHEREUM_EXTENSION,
} from 'src/hooks';
import { useI18n } from 'vue-i18n';
import { useDapps } from './useDapps';
import { ethers } from 'ethers';
Expand All @@ -42,12 +48,18 @@ export interface RewardsPerPeriod {
export function useDappStaking() {
const { t } = useI18n();
const store = useStore();
const { currentAccount } = useAccount();
const { currentAccount, currentAccountName } = useAccount();
const { registeredDapps, fetchStakeAmountsToStore, getDapp } = useDapps();
const { decimal } = useChainMetadata();
const { nativeTokenSymbol, isZkEvm } = useNetworkInfo();
const { isLedger } = useLedger();

const isH160 = computed<boolean>(() => store.getters['general/isH160Formatted']);

const isLockdropAccount = computed<boolean>(
() => !isH160.value && currentAccountName.value === ETHEREUM_EXTENSION
);

const currentBlock = computed<number>(() => store.getters['general/getCurrentBlock']);

const isDappStakingV3 = computed<boolean>(() => {
Expand Down Expand Up @@ -322,6 +334,7 @@ export function useDappStaking() {
await stakingService.unlockTokens(
currentAccount.value,
Number(ethers.utils.formatEther(amount)),
isLockdropAccount.value,
t('stakingV3.unlockSuccess')
);
}
Expand Down
19 changes: 14 additions & 5 deletions src/staking-v3/logic/repositories/DappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
EraInfo,
EraLengths,
EraRewardSpan,
InflationParam,
PeriodEndInfo,
PeriodType,
ProtocolState,
Expand Down Expand Up @@ -199,11 +198,17 @@ export class DappStakingRepository implements IDappStakingRepository {
}

//* @inheritdoc
public async getUnlockCall(amount: number): Promise<ExtrinsicPayload> {
public async getUnlockCall(amount: number, isLockdrop: boolean): Promise<ExtrinsicPayload> {
const api = await this.api.getApi();
const amountFormatted = this.getFormattedAmount(amount);
// Memo: address is ignored by runtime, but we need to pass something
// because runtime needs to keep the method signature.
const lockdropUnlock = api.tx.dappStaking.unbondAndUnstake(
getDappAddressEnum('ajYMsCKsEAhEvHpeA4XqsfiA9v1CdzZPrCfS6pEfeGHW9j8'),
amountFormatted
);

return api.tx.dappStaking.unlock(amountFormatted);
return isLockdrop ? lockdropUnlock : api.tx.dappStaking.unlock(amountFormatted);
}

private getFormattedAmount(amount: number): BigInt {
Expand All @@ -216,9 +221,13 @@ export class DappStakingRepository implements IDappStakingRepository {
amount: number
): Promise<ExtrinsicPayload[]> {
Guard.ThrowIfUndefined(contractAddress, 'contractAddress');
const api = await this.api.getApi();
// Memo: this shouldn't be the case for Lockdrop accounts as all the staked amount is under frozen (locked) balance already
const isLockdrop = false;

return [await this.getUnstakeCall(contractAddress, amount), await this.getUnlockCall(amount)];
return [
await this.getUnstakeCall(contractAddress, amount),
await this.getUnlockCall(amount, isLockdrop),
];
}

//* @inheritdoc
Expand Down
3 changes: 2 additions & 1 deletion src/staking-v3/logic/repositories/IDappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export interface IDappStakingRepository {
/**
* Gets unlock call.
* @param amount Amount of tokens to unlock.
* @param isLockdrop result of if it is a lockdrop account.
*/
getUnlockCall(amount: number): Promise<ExtrinsicPayload>;
getUnlockCall(amount: number, isLockdrop: boolean): Promise<ExtrinsicPayload>;

/**
* Gets batch call made of unstake and unlock calls.
Expand Down
3 changes: 2 additions & 1 deletion src/staking-v3/logic/services/DappStakingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,12 @@ export class DappStakingService extends SignerService implements IDappStakingSer
public async unlockTokens(
senderAddress: string,
amount: number,
isLockdrop: boolean,
successMessage: string
): Promise<void> {
Guard.ThrowIfUndefined(senderAddress, 'senderAddress');

const call = await this.dappStakingRepository.getUnlockCall(amount);
const call = await this.dappStakingRepository.getUnlockCall(amount, isLockdrop);
await this.signCall(call, senderAddress, successMessage);
}

Expand Down
3 changes: 2 additions & 1 deletion src/staking-v3/logic/services/DappStakingServiceEvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ export class DappStakingServiceEvm extends DappStakingService implements IDappSt
public async unlockTokens(
senderAddress: string,
amount: number,
isLockdrop: boolean,
successMessage: string
): Promise<void> {
Guard.ThrowIfUndefined(senderAddress, 'senderAddress');

const call = await this.dappStakingRepository.getUnlockCall(amount);
const call = await this.dappStakingRepository.getUnlockCall(amount, false);
await this.wallet.sendEvmTransaction({
from: senderAddress,
to: dispatch,
Expand Down
7 changes: 6 additions & 1 deletion src/staking-v3/logic/services/IDappStakingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ export interface IDappStakingService {
*/
relockUnlockingTokens(senderAddress: string, successMessage: string): Promise<void>;

unlockTokens(senderAddress: string, amount: number, successMessage: string): Promise<void>;
unlockTokens(
senderAddress: string,
amount: number,
isLockdrop: boolean,
successMessage: string
): Promise<void>;

getStakerInfo(
address: string,
Expand Down

0 comments on commit 4a38747

Please sign in to comment.