Skip to content
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

feat(sdk-coin-tao): add netuid validation #5657

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions modules/abstract-substrate/src/lib/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
}
}

/**
* Helper method to check whether netuid is 0 (only root network is supported now)
* @param {string} netduid of the subnet
*/
public validateNetuid(netduid: string) {
const netuidBigNumber = new BigNumber(netduid);
if (!netuidBigNumber.isZero()) {
throw new BuildTransactionError('Only root network netuid(0) is supported');
}
}

/** @inheritdoc */
validateKey({ key }: BaseKey): void {
let isValidPrivateKeyFromBytes;
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-coin-tao/src/lib/stakingBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class StakingBuilder extends TransactionBuilder {
* @returns {StakingBuilder} This staking builder.
*/
netuid(netuid: string): this {
this.validateNetuid(netuid);
this._netuid = netuid;
return this;
}
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-coin-tao/src/lib/unstakeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class UnstakeBuilder extends TransactionBuilder {
* @returns {UnstakeBuilder} This unstaking builder
*/
netuid(netuid: string): this {
this.validateNetuid(netuid);
this._netuid = netuid;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ describe('Tao Stake Builder', function () {
should.doesNotThrow(() => builder.hotkey({ address: '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT' }));
SinonAssert.calledTwice(spyValidateAddress);
});

it('should validate netuid', function () {
const spyValidateNetuid = spy(builder, 'validateNetuid');
assert.throws(
() => builder.netuid('1'),
(e: Error) => e.message === 'Only root network netuid(0) is supported'
);
should.doesNotThrow(() => builder.netuid('0'));
SinonAssert.calledTwice(spyValidateNetuid);
});
});

describe('build stake transaction', function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ describe('Tao Unstake Builder', function () {
should.doesNotThrow(() => builder.hotkey({ address: '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT' }));
SinonAssert.calledTwice(spyValidateAddress);
});

it('should validate netuid', function () {
const spyValidateNetuid = spy(builder, 'validateNetuid');
assert.throws(
() => builder.netuid('1'),
(e: Error) => e.message === 'Only root network netuid(0) is supported'
);
should.doesNotThrow(() => builder.netuid('0'));
SinonAssert.calledTwice(spyValidateNetuid);
});
});

describe('build unstake transaction', function () {
Expand Down