-
Notifications
You must be signed in to change notification settings - Fork 298
feat(sdk-coin-tao): add moveStakeBuilder #6925
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
raksha-r7
wants to merge
1
commit into
master
Choose a base branch
from
SC-3017
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Constants for TAO network operations | ||
*/ | ||
export const TAO_CONSTANTS = { | ||
/** | ||
* Maximum supported netuid value for TAO subnets | ||
* Valid range is 0 to MAX_NETUID (inclusive) | ||
*/ | ||
MAX_NETUID: 128, | ||
} as const; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
import { Interface, Schema, Transaction, TransactionBuilder } from '@bitgo/abstract-substrate'; | ||
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import { DecodedSignedTx, DecodedSigningPayload, defineMethod, UnsignedTransaction } from '@substrate/txwrapper-core'; | ||
import BigNumber from 'bignumber.js'; | ||
import { TAO_CONSTANTS } from './constants'; | ||
import { MoveStakeTransaction } from './moveStakeTransaction'; | ||
|
||
export class MoveStakeBuilder extends TransactionBuilder { | ||
protected _originHotkey: string; | ||
protected _destinationHotkey: string; | ||
protected _originNetuid: string; | ||
protected _destinationNetuid: string; | ||
protected _alphaAmount: string; | ||
|
||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
this._transaction = new MoveStakeTransaction(_coinConfig); | ||
} | ||
|
||
/** | ||
* Construct a transaction to move stake | ||
* @returns {UnsignedTransaction} an unsigned move stake transaction | ||
*/ | ||
protected buildTransaction(): UnsignedTransaction { | ||
const baseTxInfo = this.createBaseTxInfo(); | ||
return this.moveStake( | ||
{ | ||
originHotkey: this._originHotkey, | ||
destinationHotkey: this._destinationHotkey, | ||
originNetuid: this._originNetuid, | ||
destinationNetuid: this._destinationNetuid, | ||
alphaAmount: this._alphaAmount, | ||
}, | ||
baseTxInfo | ||
); | ||
} | ||
|
||
/** @inheritdoc */ | ||
protected get transactionType(): TransactionType { | ||
return TransactionType.StakingRedelegate; | ||
} | ||
|
||
/** | ||
* Set the amount to move | ||
* @param {string} amount to move | ||
* @returns {MoveStakeBuilder} This builder. | ||
*/ | ||
amount(amount: string): this { | ||
const value = new BigNumber(amount); | ||
this.validateAmount(value); | ||
this._alphaAmount = amount; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the origin hot key address | ||
* @param {string} address of origin hotkey | ||
* @returns {MoveStakeBuilder} This builder. | ||
*/ | ||
originHotkey(address: string): this { | ||
this.validateAddress({ address }); | ||
this._originHotkey = address; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the destination hot key address | ||
* @param {string} address of destination hotkey | ||
* @returns {MoveStakeBuilder} This builder. | ||
*/ | ||
destinationHotkey(address: string): this { | ||
this.validateAddress({ address }); | ||
this._destinationHotkey = address; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the origin netuid of the subnet (root network is 0) | ||
* @param {string} netuid of subnet | ||
* @returns {MoveStakeBuilder} This builder. | ||
*/ | ||
originNetuid(netuid: string): this { | ||
this.validateNetuid(netuid); | ||
this._originNetuid = netuid; | ||
return this; | ||
} | ||
|
||
/** | ||
* Set the destination netuid of the subnet (root network is 0) | ||
* @param {string} netuid of subnet | ||
* @returns {MoveStakeBuilder} This builder. | ||
*/ | ||
destinationNetuid(netuid: string): this { | ||
this.validateNetuid(netuid); | ||
this._destinationNetuid = netuid; | ||
return this; | ||
} | ||
|
||
/** @inheritdoc */ | ||
protected fromImplementation(rawTransaction: string): Transaction { | ||
const tx = super.fromImplementation(rawTransaction); | ||
if (this._method?.name === Interface.MethodNames.MoveStake) { | ||
const txMethod = this._method.args as Interface.MoveStakeArgs; | ||
this.amount(txMethod.alphaAmount); | ||
this.originHotkey(txMethod.originHotkey); | ||
this.destinationHotkey(txMethod.destinationHotkey); | ||
this.originNetuid(txMethod.originNetuid); | ||
this.destinationNetuid(txMethod.destinationNetuid); | ||
} else { | ||
throw new InvalidTransactionError( | ||
`Invalid Transaction Type: ${this._method?.name}. Expected ${Interface.MethodNames.MoveStake}` | ||
); | ||
} | ||
return tx; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateTransaction(_: Transaction): void { | ||
super.validateTransaction(_); | ||
this.validateFields( | ||
this._originHotkey, | ||
this._destinationHotkey, | ||
this._originNetuid, | ||
this._destinationNetuid, | ||
this._alphaAmount | ||
); | ||
} | ||
|
||
/** | ||
* @param {BigNumber} amount amount to validate | ||
* @throws {InvalidTransactionError} if amount is less than or equal to zero | ||
*/ | ||
private validateAmount(amount: BigNumber): void { | ||
if (amount.isLessThanOrEqualTo(0)) { | ||
throw new InvalidTransactionError('Amount must be greater than zero'); | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} netuid netuid to validate | ||
* @throws {InvalidTransactionError} if netuid is out of range | ||
*/ | ||
private validateNetuid(netuid: string): void { | ||
const netuidNum = Number(netuid); | ||
if (isNaN(netuidNum) || !Number.isInteger(netuidNum) || netuidNum < 0 || netuidNum > TAO_CONSTANTS.MAX_NETUID) { | ||
throw new InvalidTransactionError( | ||
`Invalid netuid: ${netuid}. Netuid must be between 0 and ${TAO_CONSTANTS.MAX_NETUID}.` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Helper method to validate whether tx params have the correct type and format | ||
* @param {string} originHotkey origin hotkey address | ||
* @param {string} destinationHotkey destination hotkey address | ||
* @param {string} originNetuid netuid of the origin subnet | ||
* @param {string} destinationNetuid netuid of the destination subnet | ||
* @param {string} alphaAmount amount to move | ||
* @throws {InvalidTransactionError} if validation fails | ||
*/ | ||
private validateFields( | ||
originHotkey: string, | ||
destinationHotkey: string, | ||
originNetuid: string, | ||
destinationNetuid: string, | ||
alphaAmount: string | ||
): void { | ||
// Validate netuid ranges | ||
this.validateNetuid(originNetuid); | ||
this.validateNetuid(destinationNetuid); | ||
|
||
const validationResult = Schema.MoveStakeTransactionSchema.validate({ | ||
originHotkey, | ||
destinationHotkey, | ||
originNetuid, | ||
destinationNetuid, | ||
alphaAmount, | ||
}); | ||
|
||
if (validationResult.error) { | ||
throw new InvalidTransactionError(`Transaction validation failed: ${validationResult.error.message}`); | ||
} | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx, rawTransaction: string): void { | ||
if (decodedTxn.method?.name === Interface.MethodNames.MoveStake) { | ||
const txMethod = decodedTxn.method.args as unknown as Interface.MoveStakeArgs; | ||
|
||
const validationResult = Schema.MoveStakeTransactionSchema.validate(txMethod); | ||
if (validationResult.error) { | ||
throw new InvalidTransactionError( | ||
`Move Stake Transaction validation failed: ${validationResult.error.message}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Construct a transaction to move stake | ||
* | ||
* @param {Interface.MoveStakeArgs} args arguments to be passed to the moveStake method | ||
* @param {Interface.CreateBaseTxInfo} info txn info required to construct the moveStake txn | ||
* @returns {UnsignedTransaction} an unsigned move stake transaction | ||
*/ | ||
private moveStake(args: Interface.MoveStakeArgs, info: Interface.CreateBaseTxInfo): UnsignedTransaction { | ||
return defineMethod( | ||
{ | ||
method: { | ||
args, | ||
name: 'moveStake', | ||
pallet: 'subtensorModule', | ||
}, | ||
...info.baseTxInfo, | ||
}, | ||
info.options | ||
); | ||
} | ||
} |
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.
The validation allows decimal numbers that convert to integers (e.g., '5.0' becomes 5). Use
parseInt()
with radix 10 and check if the result equals the original input, or use a regex to ensure the input contains only digits.Copilot uses AI. Check for mistakes.