Skip to content

Commit

Permalink
unstaking builder test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijit0943 authored and joshisakshi committed Feb 21, 2025
1 parent 6107abc commit f02c1bb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
4 changes: 3 additions & 1 deletion modules/abstract-substrate/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ export interface TxData {
chainName: string;
method?: string;
specName?: string;
amount?: number;
amount?: string;
to?: string;
tip?: number;
eraPeriod?: number;
payee?: string;
keepAlive?: boolean;
netuid?: number;
hotkey?: string;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion modules/abstract-substrate/src/lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,20 @@ export class Transaction extends BaseTransaction {
} else {
throw new ParseTransactionError(`Serializing unknown Transfer type parameters`);
}
} else if (this.type === TransactionType.StakingDeactivate) {
// fill this up
const txMethod = decodedTx.method.args;
if (utils.isUnstake(txMethod)) {
const keypair = new KeyPair({
pub: Buffer.from(decodeAddress(txMethod.hotkey)).toString('hex'),
});
result.to = keypair.getAddress(this.getAddressFormat());
result.amount = txMethod.amountUnstaked.toString();
result.netuid = txMethod.netuid;
}
} else {
throw new ParseTransactionError(`Serializing unknown Transfer type parameters`);
}

return result;
}

Expand Down
5 changes: 4 additions & 1 deletion modules/abstract-substrate/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import bs58 from 'bs58';
import base32 from 'hi-base32';
import nacl from 'tweetnacl';
import { KeyPair } from '.';
import { HexString, Material, TransferAllArgs, TransferArgs, TxMethod } from './iface';
import { HexString, Material, TransferAllArgs, TransferArgs, TxMethod, UnstakeArgs } from './iface';

export class Utils implements BaseUtils {
/** @inheritdoc */
Expand Down Expand Up @@ -195,6 +195,9 @@ export class Utils implements BaseUtils {
return (arg as TransferAllArgs).dest?.id !== undefined && (arg as TransferAllArgs).keepAlive !== undefined;
}

isUnstake(arg: TxMethod['args']): arg is UnstakeArgs {
return (arg as UnstakeArgs).hotkey !== undefined && (arg as UnstakeArgs).netuid !== undefined;
}
/**
* extracts and returns the signature in hex format given a raw signed transaction
*
Expand Down
4 changes: 2 additions & 2 deletions modules/sdk-coin-tao/src/resources/testnet.ts

Large diffs are not rendered by default.

36 changes: 17 additions & 19 deletions modules/sdk-coin-tao/test/unit/transactionBuilder/unstakeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Tao Unstake Builder', function () {
beforeEach(function () {
const config = buildTestConfig();
const material = utils.getMaterial(config.network.type);
// console.log('Material:', material);
//console.log('Material:', material);
builder = new UnstakeBuilder(config).material(material);
});
describe('setter validation', function () {
Expand All @@ -38,14 +38,14 @@ describe('Tao Unstake Builder', function () {
.sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 })
.fee({ amount: 0, type: 'tip' })
.addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex'));

// console.log('Builder after setting properties:', builder);
const tx = await builder.build();
// console.log('Transaction built:', tx);
// console.log('Transaction built:', tx);
const txJson = tx.toJson();
console.log('Transaction JSON:', JSON.stringify(txJson, null, 2));
should.deepEqual(txJson.amount, 50000000000000);
should.deepEqual(txJson.hotkey, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, 0);
// console.log('Transaction JSON:', JSON.stringify(txJson, null, 2));
should.deepEqual(txJson.amount, '50000000000000');
should.deepEqual(txJson.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, sender.address);
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, referenceBlock);
Expand All @@ -67,14 +67,12 @@ describe('Tao Unstake Builder', function () {
.referenceBlock(referenceBlock)
.sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 })
.fee({ amount: 0, type: 'tip' });
// console.log('Builder after setting properties:', builder);
console.log('Building transaction...');
const tx = await builder.build();
const txJson = tx.toJson();
// console.log('Transaction JSON:', JSON.stringify(txJson, null, 2));
should.deepEqual(txJson.amount, 50000000000000);
should.deepEqual(txJson.hotkey, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, 0);
should.deepEqual(txJson.amount, '50000000000000');
should.deepEqual(txJson.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, sender.address);
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, referenceBlock);
Expand All @@ -88,12 +86,12 @@ describe('Tao Unstake Builder', function () {
});
it('should build from raw signed tx', async function () {
builder.from(rawTx.unstake.signed);
builder.validity({ firstValid: 3933, maxDuration: 64 }).referenceBlock(referenceBlock);
// builder.validity({ firstValid: 3933, maxDuration: 64 }).referenceBlock(referenceBlock);
const tx = await builder.build();
const txJson = tx.toJson();
should.deepEqual(txJson.amount, 50000000000000);
should.deepEqual(txJson.hotkey, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, 0);
should.deepEqual(txJson.amount, '50000000000000');
should.deepEqual(txJson.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk');
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, referenceBlock);
Expand All @@ -114,9 +112,9 @@ describe('Tao Unstake Builder', function () {
.addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex'));
const tx = await builder.build();
const txJson = tx.toJson();
should.deepEqual(txJson.amount, 50000000000000);
should.deepEqual(txJson.hotkey, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, 0);
should.deepEqual(txJson.amount, '50000000000000');
should.deepEqual(txJson.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk');
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, referenceBlock);
Expand Down

0 comments on commit f02c1bb

Please sign in to comment.