Skip to content

Commit 74d5bad

Browse files
Merge pull request #7718 from BitGo/WIN-8225
fix(sdk-coin-tao): support all valid netuids for transferStake tx
2 parents dfa7d21 + 1ac6041 commit 74d5bad

File tree

5 files changed

+45
-41
lines changed

5 files changed

+45
-41
lines changed

modules/abstract-substrate/src/lib/txnSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ export const UnstakeTransactionSchema = joi.object({
6161
export const TransferStakeTransactionSchema = joi.object({
6262
destinationColdkey: addressSchema.required(),
6363
hotkey: addressSchema.required(),
64-
originNetuid: joi.string().required(),
65-
destinationNetuid: joi.string().required(),
64+
originNetuid: joi.string().regex(/^\d+$/).required(),
65+
destinationNetuid: joi.string().regex(/^\d+$/).required(),
6666
alphaAmount: joi.string().required(),
6767
});
6868

modules/sdk-coin-tao/src/lib/tokenTransferBuilder.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class TokenTransferBuilder extends TransactionBuilder {
8080
* @returns {TokenTransferBuilder} This builder.
8181
*/
8282
originNetuid(netuid: string): this {
83+
this.validateNetuid(netuid);
8384
this._originNetuid = netuid;
8485
return this;
8586
}
@@ -90,6 +91,7 @@ export class TokenTransferBuilder extends TransactionBuilder {
9091
* @returns {TokenTransferBuilder} This builder.
9192
*/
9293
destinationNetuid(netuid: string): this {
94+
this.validateNetuid(netuid);
9395
this._destinationNetuid = netuid;
9496
return this;
9597
}
@@ -165,6 +167,16 @@ export class TokenTransferBuilder extends TransactionBuilder {
165167
}
166168
}
167169

170+
/**
171+
* Validate the netuid. Throws an error if the netuid is not a non-negative integer.
172+
* @param netuid The netuid to validate.
173+
*/
174+
validateNetuid(netuid: string): void {
175+
if (BigInt(netuid) < 0n) {
176+
throw new Error(`The netuid '${netuid}' is not a valid netuid`);
177+
}
178+
}
179+
168180
/**
169181
* Construct a transaction to transfer stake
170182
*

modules/sdk-coin-tao/src/lib/tokenTransferTransaction.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,6 @@ export class TokenTransferTransaction extends SubstrateTransaction {
2828
return result;
2929
}
3030

31-
/** @inheritdoc */
32-
loadInputsAndOutputs(): void {
33-
super.loadInputsAndOutputs();
34-
35-
const decodedTx = decode(this._substrateTransaction, {
36-
metadataRpc: this._substrateTransaction.metadataRpc,
37-
registry: this._registry,
38-
isImmortalEra: utils.isZeroHex(this._substrateTransaction.era),
39-
}) as unknown as SubstrateInterface.DecodedTx;
40-
const txMethod = decodedTx.method.args as SubstrateInterface.TransferStakeArgs;
41-
42-
this._inputs.push({
43-
address: this._sender,
44-
value: txMethod.alphaAmount,
45-
coin: utils.getTaoTokenBySubnetId(txMethod.originNetuid).name,
46-
});
47-
48-
this._outputs.push({
49-
address: txMethod.destinationColdkey,
50-
value: txMethod.alphaAmount,
51-
coin: utils.getTaoTokenBySubnetId(txMethod.destinationNetuid).name,
52-
});
53-
}
54-
5531
/** @inheritdoc */
5632
explainTransaction(): SubstrateInterface.TransactionExplanation {
5733
const result = this.toJson();

modules/sdk-coin-tao/test/resources/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export const rawTx = {
177177
},
178178
transferStake: {
179179
signed:
180-
'0xdd02840061b18c6dc02ddcabdeac56cb4f21a971cc41cc97640f6f85b073480008c53a0d00aadae7fa1f53e7a5c900b330ff71bee6782cf3c29a2c6f9599162381cd021ad581c74ded89f49ec79adefed64af8ff16649553523dda9cb4f017cbf15681e50ed5012103000007569f7b0675db59d19b4bd9c8c72eaabba75a9863d02b30115b8b3c3ca5c20f02548a90be061598f4b592afbd546bcb6beadb3c02f5c129df2e11b698f9543dbd41010001000300000000002000',
180+
'0xdd02840061b18c6dc02ddcabdeac56cb4f21a971cc41cc97640f6f85b073480008c53a0d00aadae7fa1f53e7a5c900b330ff71bee6782cf3c29a2c6f9599162381cd021ad581c74ded89f49ec79adefed64af8ff16649553523dda9cb4f017cbf15681e50ed5012103000007569f7b0675db59d19b4bd9c8c72eaabba75a9863d02b30115b8b3c3ca5c20f02548a90be061598f4b592afbd546bcb6beadb3c02f5c129df2e11b698f9543dbd410c000a000300000000002000',
181181
},
182182
};
183183

modules/sdk-coin-tao/test/unit/transactionBuilder/tokenTransferBuilder.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ describe('Tao Token Transfer Builder', function () {
4343

4444
SinonAssert.callCount(spyValidateAddress, 4);
4545
});
46+
47+
it('should validate netuid', function () {
48+
const spyValidateAddress = spy(builder, 'validateNetuid');
49+
assert.throws(
50+
() => builder.destinationNetuid('abc'),
51+
(e: Error) => e.message === `Cannot convert abc to a BigInt`
52+
);
53+
assert.throws(
54+
() => builder.destinationNetuid('-10'),
55+
(e: Error) => e.message === `The netuid '-10' is not a valid netuid`
56+
);
57+
should.doesNotThrow(() => builder.destinationNetuid('36'));
58+
59+
assert.throws(
60+
() => builder.originNetuid('abc'),
61+
(e: Error) => e.message === `Cannot convert abc to a BigInt`
62+
);
63+
assert.throws(
64+
() => builder.destinationNetuid('-1'),
65+
(e: Error) => e.message === `The netuid '-1' is not a valid netuid`
66+
);
67+
should.doesNotThrow(() => builder.originNetuid('64'));
68+
69+
SinonAssert.callCount(spyValidateAddress, 6);
70+
});
4671
});
4772

4873
describe('build transfer stake transaction', function () {
@@ -51,8 +76,8 @@ describe('Tao Token Transfer Builder', function () {
5176
.amount('9007199254740995')
5277
.destinationColdkey('5Ffp1wJCPu4hzVDTo7XaMLqZSvSadyUQmxWPDw74CBjECSoq')
5378
.hotkey('5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT')
54-
.originNetuid('1')
55-
.destinationNetuid('1')
79+
.originNetuid('12')
80+
.destinationNetuid('10')
5681
.sender({ address: sender.address })
5782
.validity({ firstValid: 3933, maxDuration: 64 })
5883
.referenceBlock(referenceBlock)
@@ -65,7 +90,7 @@ describe('Tao Token Transfer Builder', function () {
6590

6691
serializedTx.should.equal(rawTx.transferStake.signed);
6792
tx.toJson().should.deepEqual({
68-
id: '0xe5ce9ff1bbdf54d1dbd5adee8648027aa7efa99d319b041afb4b57be2042fc11',
93+
id: '0xe5932b507a53d351aabd520487ba55ba833e0968ad18642a28f54dabeb7abf2f',
6994
sender: '5EGoFA95omzemRssELLDjVenNZ68aXyUeqtKQScXSEBvVJkr',
7095
referenceBlock: '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d',
7196
blockNumber: 3933,
@@ -78,19 +103,10 @@ describe('Tao Token Transfer Builder', function () {
78103
tip: 0,
79104
destinationColdkey: '5Ffp1wJCPu4hzVDTo7XaMLqZSvSadyUQmxWPDw74CBjECSoq',
80105
hotkey: '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT',
81-
originNetuid: '1',
82-
destinationNetuid: '1',
106+
originNetuid: '12',
107+
destinationNetuid: '10',
83108
alphaAmount: '9007199254740995',
84109
});
85-
tx.explainTransaction().should.containDeep({
86-
outputs: [
87-
{
88-
address: '5Ffp1wJCPu4hzVDTo7XaMLqZSvSadyUQmxWPDw74CBjECSoq',
89-
amount: '9007199254740995',
90-
tokenName: 'ttao:apex',
91-
},
92-
],
93-
});
94110
});
95111

96112
it('should re-build from raw signed tx', async function () {

0 commit comments

Comments
 (0)