Skip to content

Commit

Permalink
Merge pull request #229 from autonomys/feat/modify-withdraw-stake-for…
Browse files Browse the repository at this point in the history
…-latest-changes

Modify withdraw stake for latest changes
  • Loading branch information
marc-aurele-besner authored Feb 6, 2025
2 parents acfded5 + 04cfdfd commit b0e7da9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
26 changes: 21 additions & 5 deletions packages/auto-consensus/src/staking.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion packages/auto-consensus/src/types/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions packages/auto-utils/src/utils/createType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit b0e7da9

Please sign in to comment.