diff --git a/packages/auto-consensus/src/staking.ts b/packages/auto-consensus/src/staking.ts index 3bfd19b..e2aef2a 100644 --- a/packages/auto-consensus/src/staking.ts +++ b/packages/auto-consensus/src/staking.ts @@ -1,7 +1,13 @@ // file: src/staking.ts -import type { Api } from '@autonomys/auto-utils' -import { signingKey as signingKeyFn } from '@autonomys/auto-utils' +import type { Api, Codec } from '@autonomys/auto-utils' +import { + createWithdrawStakeAll, + createWithdrawStakeByPercent, + createWithdrawStakeByShares, + createWithdrawStakeByStake, + signingKey as signingKeyFn, +} from '@autonomys/auto-utils' import type { NominateOperatorParams, RegisterOperatorParams, @@ -102,9 +108,19 @@ export const nominateOperator = (params: NominateOperatorParams) => { export const withdrawStake = (params: WithdrawStakeParams) => { try { - const { api, operatorId, shares } = params - - return api.tx.domains.withdrawStake(parseString(operatorId), parseString(shares)) + const { api, operatorId } = params + let param2: Codec | null = null + if (params.all) param2 = createWithdrawStakeAll(api) + else if (params.percent) param2 = createWithdrawStakeByPercent(api, parseString(params.percent)) + else if (params.stake) param2 = createWithdrawStakeByStake(api, parseString(params.stake)) + else if (params.shares) param2 = createWithdrawStakeByShares(api, parseString(params.shares)) + + if (param2 === null) + throw new Error( + 'Provide all(boolean), percent(string/number/bigint), stake(string/number/bigint) or shared(string/number/bigint) to withdraw stake', + ) + + return api.tx.domains.withdrawStake(parseString(operatorId), param2) } catch (error) { console.error('error', error) throw new Error('Error creating withdraw stake tx.' + error) diff --git a/packages/auto-consensus/src/types/staking.ts b/packages/auto-consensus/src/types/staking.ts index 9470564..a28137d 100644 --- a/packages/auto-consensus/src/types/staking.ts +++ b/packages/auto-consensus/src/types/staking.ts @@ -132,7 +132,10 @@ export type StakingParams = { } export interface WithdrawStakeParams extends StakingParams { - shares: StringNumberOrBigInt + all?: boolean + percent?: string | number + stake?: StringNumberOrBigInt + shares?: StringNumberOrBigInt } export interface NominateOperatorParams extends StakingParams { diff --git a/packages/auto-utils/src/utils/createType.ts b/packages/auto-utils/src/utils/createType.ts index 13baec6..d2244d3 100644 --- a/packages/auto-utils/src/utils/createType.ts +++ b/packages/auto-utils/src/utils/createType.ts @@ -75,4 +75,18 @@ export const createTransporterToDomainAccount32Type = ( return createTransporterLocationType(api, chainId, accountId) } +export const createWithdrawStakeAll = (api: ApiPromise) => + createType(api.registry, 'PalletDomainsStakingWithdrawStake', { All: null }) + +export const createWithdrawStakeByPercent = (api: ApiPromise, percent: string) => + createType(api.registry, 'PalletDomainsStakingWithdrawStake', { + Percent: percent, + }) + +export const createWithdrawStakeByStake = (api: ApiPromise, stake: string) => + createType(api.registry, 'PalletDomainsStakingWithdrawStake', { Stake: stake }) + +export const createWithdrawStakeByShares = (api: ApiPromise, shares: string) => + createType(api.registry, 'PalletDomainsStakingWithdrawStake', { Shares: shares }) + export { createType }