-
Notifications
You must be signed in to change notification settings - Fork 294
feat: add batchUnstakingBuilder and withdrawUnbondedBuilder to support unstaking in polyx #6141
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
Open
abhijit0943
wants to merge
1
commit into
master
Choose a base branch
from
SC-1836
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
modules/sdk-coin-polyx/src/lib/batchUnstakingBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import { methods } from '@substrate/txwrapper-polkadot'; | ||
import { UnsignedTransaction, DecodedSigningPayload, DecodedSignedTx } from '@substrate/txwrapper-core'; | ||
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; | ||
import BigNumber from 'bignumber.js'; | ||
import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate'; | ||
import { BatchArgs } from './iface'; | ||
import { BatchUnstakingTransactionSchema } from './txnSchema'; | ||
|
||
export class BatchUnstakingBuilder extends TransactionBuilder { | ||
protected _amount: string; | ||
|
||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
} | ||
|
||
/** | ||
* Unbond tokens and chill (stop nominating validators) | ||
* | ||
* @returns {UnsignedTransaction} an unsigned Polyx transaction | ||
*/ | ||
protected buildTransaction(): UnsignedTransaction { | ||
const baseTxInfo = this.createBaseTxInfo(); | ||
|
||
const chillCall = methods.staking.chill({}, baseTxInfo.baseTxInfo, baseTxInfo.options); | ||
|
||
const unbondCall = methods.staking.unbond( | ||
{ | ||
value: this._amount, | ||
}, | ||
baseTxInfo.baseTxInfo, | ||
baseTxInfo.options | ||
); | ||
|
||
// Create batch all transaction (atomic execution) | ||
return methods.utility.batchAll( | ||
{ | ||
calls: [chillCall.method, unbondCall.method], | ||
}, | ||
baseTxInfo.baseTxInfo, | ||
baseTxInfo.options | ||
); | ||
} | ||
|
||
protected get transactionType(): TransactionType { | ||
return TransactionType.Batch; | ||
} | ||
|
||
/** | ||
* The amount to unstake. | ||
* | ||
* @param {string} amount | ||
* @returns {BatchUnstakingBuilder} This unstake builder. | ||
*/ | ||
amount(amount: string): this { | ||
this.validateValue(new BigNumber(amount)); | ||
this._amount = amount; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the amount to unstake | ||
*/ | ||
getAmount(): string { | ||
return this._amount; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx): void { | ||
const methodName = decodedTxn.method?.name as string; | ||
|
||
if (methodName === 'utility.batchAll') { | ||
const txMethod = decodedTxn.method.args as unknown as BatchArgs; | ||
const calls = txMethod.calls; | ||
|
||
if (calls.length !== 2) { | ||
throw new InvalidTransactionError( | ||
`Invalid batch unstaking transaction: expected 2 calls but got ${calls.length}` | ||
); | ||
} | ||
|
||
// Check that first call is chill | ||
if (calls[0].method !== 'staking.chill') { | ||
throw new InvalidTransactionError( | ||
`Invalid batch unstaking transaction: first call should be staking.chill but got ${calls[0].method}` | ||
); | ||
} | ||
|
||
// Check that second call is unbond | ||
if (calls[1].method !== 'staking.unbond') { | ||
throw new InvalidTransactionError( | ||
`Invalid batch unstaking transaction: second call should be staking.unbond but got ${calls[1].method}` | ||
); | ||
} | ||
|
||
// Validate unbond amount | ||
const unbondArgs = calls[1].args as { value: string }; | ||
const validationResult = BatchUnstakingTransactionSchema.validate({ | ||
value: unbondArgs.value, | ||
}); | ||
|
||
if (validationResult.error) { | ||
throw new InvalidTransactionError(`Invalid batch unstaking transaction: ${validationResult.error.message}`); | ||
} | ||
} else { | ||
throw new InvalidTransactionError(`Invalid transaction type: ${methodName}. Expected utility.batchAll`); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
protected fromImplementation(rawTransaction: string): Transaction { | ||
const tx = super.fromImplementation(rawTransaction); | ||
|
||
if (this._method && (this._method.name as string) === 'utility.batchAll') { | ||
const txMethod = this._method.args as unknown as BatchArgs; | ||
const calls = txMethod.calls; | ||
|
||
if (calls && calls.length === 2 && calls[1].method === 'staking.unbond') { | ||
const unbondArgs = calls[1].args as { value: string }; | ||
this.amount(unbondArgs.value); | ||
} | ||
} else { | ||
throw new InvalidTransactionError(`Invalid Transaction Type: ${this._method?.name}. Expected utility.batchAll`); | ||
} | ||
|
||
return tx; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateTransaction(_: Transaction): void { | ||
super.validateTransaction(_); | ||
this.validateFields(this._amount); | ||
} | ||
|
||
private validateFields(value: string): void { | ||
const validationResult = BatchUnstakingTransactionSchema.validate({ | ||
value, | ||
}); | ||
|
||
if (validationResult.error) { | ||
throw new InvalidTransactionError( | ||
`Batch Unstaking Builder Transaction validation failed: ${validationResult.error.message}` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Validates fields for testing | ||
*/ | ||
testValidateFields(): void { | ||
this.validateFields(this._amount); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
modules/sdk-coin-polyx/src/lib/withdrawUnbondedBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import { methods } from '@substrate/txwrapper-polkadot'; | ||
import { UnsignedTransaction, DecodedSigningPayload, DecodedSignedTx } from '@substrate/txwrapper-core'; | ||
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; | ||
import BigNumber from 'bignumber.js'; | ||
import { TransactionBuilder, Transaction } from '@bitgo/abstract-substrate'; | ||
import { WithdrawUnbondedTransactionSchema } from './txnSchema'; | ||
import { WithdrawUnbondedArgs } from './iface'; | ||
|
||
export class WithdrawUnbondedBuilder extends TransactionBuilder { | ||
protected _slashingSpans = 0; | ||
|
||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
} | ||
|
||
/** | ||
* Withdraw unbonded tokens after the unbonding period has passed | ||
* | ||
* @returns {UnsignedTransaction} an unsigned Polyx transaction | ||
*/ | ||
protected buildTransaction(): UnsignedTransaction { | ||
const baseTxInfo = this.createBaseTxInfo(); | ||
|
||
return methods.staking.withdrawUnbonded( | ||
{ | ||
numSlashingSpans: this._slashingSpans, | ||
}, | ||
baseTxInfo.baseTxInfo, | ||
baseTxInfo.options | ||
); | ||
} | ||
|
||
protected get transactionType(): TransactionType { | ||
return TransactionType.StakingWithdraw; | ||
} | ||
|
||
/** | ||
* The number of slashing spans, typically 0 for most users | ||
* | ||
* @param {number} slashingSpans | ||
* @returns {WithdrawUnbondedBuilder} This withdrawUnbonded builder. | ||
*/ | ||
slashingSpans(slashingSpans: number): this { | ||
this.validateValue(new BigNumber(slashingSpans)); | ||
this._slashingSpans = slashingSpans; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get the slashing spans | ||
*/ | ||
getSlashingSpans(): number { | ||
return this._slashingSpans; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx): void { | ||
if (decodedTxn.method?.name === 'staking.withdrawUnbonded') { | ||
const txMethod = decodedTxn.method.args as unknown as WithdrawUnbondedArgs; | ||
const slashingSpans = txMethod.numSlashingSpans; | ||
const validationResult = WithdrawUnbondedTransactionSchema.validate({ slashingSpans }); | ||
|
||
if (validationResult.error) { | ||
throw new InvalidTransactionError( | ||
`WithdrawUnbonded Transaction validation failed: ${validationResult.error.message}` | ||
); | ||
} | ||
} else { | ||
throw new InvalidTransactionError( | ||
`Invalid transaction type: ${decodedTxn.method?.name}. Expected staking.withdrawUnbonded` | ||
); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
protected fromImplementation(rawTransaction: string): Transaction { | ||
const tx = super.fromImplementation(rawTransaction); | ||
|
||
if (this._method && (this._method.name as string) === 'staking.withdrawUnbonded') { | ||
const txMethod = this._method.args as unknown as WithdrawUnbondedArgs; | ||
this.slashingSpans(txMethod.numSlashingSpans); | ||
} else { | ||
throw new InvalidTransactionError( | ||
`Invalid Transaction Type: ${this._method?.name}. Expected staking.withdrawUnbonded` | ||
); | ||
} | ||
|
||
return tx; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateTransaction(_: Transaction): void { | ||
super.validateTransaction(_); | ||
this.validateFields(this._slashingSpans); | ||
} | ||
|
||
private validateFields(slashingSpans: number): void { | ||
const validationResult = WithdrawUnbondedTransactionSchema.validate({ | ||
slashingSpans, | ||
}); | ||
|
||
if (validationResult.error) { | ||
throw new InvalidTransactionError( | ||
`WithdrawUnbonded Builder Transaction validation failed: ${validationResult.error.message}` | ||
); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider initializing '_amount' with a default value (e.g., an empty string) to avoid potential issues from its uninitialized state.
Copilot uses AI. Check for mistakes.