Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion modules/abstract-substrate/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export const MethodNames = {
* Transfer stake from one validator to another.
*/
TransferStake: 'transferStake' as const,
/**
* Move stake from one hotkey to another.
*/
MoveStake: 'moveStake' as const,
} as const;

/**
Expand Down Expand Up @@ -195,6 +199,14 @@ export interface TransferStakeArgs extends Args {
alphaAmount: string;
}

export interface MoveStakeArgs extends Args {
originHotkey: string;
destinationHotkey: string;
originNetuid: string;
destinationNetuid: string;
alphaAmount: string;
}

/**
* Decoded TxMethod from a transaction hex
*/
Expand All @@ -211,7 +223,8 @@ export interface TxMethod {
| UnbondArgs
| WithdrawUnbondedArgs
| BatchArgs
| TransferStakeArgs;
| TransferStakeArgs
| MoveStakeArgs;
name: MethodNamesValues;
pallet: string;
}
Expand Down
8 changes: 8 additions & 0 deletions modules/abstract-substrate/src/lib/txnSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,11 @@ export const TransferStakeTransactionSchema = joi.object({
destinationNetuid: joi.string().required(),
alphaAmount: joi.string().required(),
});

export const MoveStakeTransactionSchema = joi.object({
originHotkey: addressSchema.required(),
destinationHotkey: addressSchema.required(),
originNetuid: joi.string().required(),
destinationNetuid: joi.string().required(),
alphaAmount: joi.string().required(),
});
11 changes: 11 additions & 0 deletions modules/abstract-substrate/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
UnbondArgs,
WithdrawUnbondedArgs,
BatchArgs,
MoveStakeArgs,
} from './iface';

export class Utils implements BaseUtils {
Expand Down Expand Up @@ -263,6 +264,16 @@ export class Utils implements BaseUtils {
return (arg as BatchArgs).calls !== undefined && Array.isArray((arg as BatchArgs).calls);
}

isMoveStake(arg: TxMethod['args']): arg is MoveStakeArgs {
return (
(arg as MoveStakeArgs).originHotkey !== undefined &&
(arg as MoveStakeArgs).destinationHotkey !== undefined &&
(arg as MoveStakeArgs).originNetuid !== undefined &&
(arg as MoveStakeArgs).destinationNetuid !== undefined &&
(arg as MoveStakeArgs).alphaAmount !== undefined
);
}

/**
* extracts and returns the signature in hex format given a raw signed transaction
*
Expand Down
10 changes: 10 additions & 0 deletions modules/sdk-coin-tao/src/lib/constants.ts
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;
8 changes: 8 additions & 0 deletions modules/sdk-coin-tao/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ export interface TransferStakeTxData extends Interface.TxData {
destinationNetuid: string;
alphaAmount: string;
}

export interface MoveStakeTxData extends Interface.TxData {
originHotkey: string;
destinationHotkey: string;
originNetuid: string;
destinationNetuid: string;
alphaAmount: string;
}
4 changes: 4 additions & 0 deletions modules/sdk-coin-tao/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ export { TransactionBuilderFactory } from './transactionBuilderFactory';
export { TransferBuilder } from './transferBuilder';
export { StakingBuilder } from './stakingBuilder';
export { UnstakeBuilder } from './unstakeBuilder';
export { TokenTransferBuilder } from './tokenTransferBuilder';
export { TokenTransferTransaction } from './tokenTransferTransaction';
export { MoveStakeBuilder } from './moveStakeBuilder';
export { MoveStakeTransaction } from './moveStakeTransaction';
export { Utils, default as utils } from './utils';
220 changes: 220 additions & 0 deletions modules/sdk-coin-tao/src/lib/moveStakeBuilder.ts
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}.`
);
}
}
Comment on lines +144 to +151
Copy link
Preview

Copilot AI Sep 10, 2025

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.


/**
* 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
);
}
}
Loading