Skip to content

Commit

Permalink
Rebased version
Browse files Browse the repository at this point in the history
  • Loading branch information
joshisakshi committed Feb 21, 2025
1 parent f02c1bb commit 0b74459
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 60 deletions.
12 changes: 10 additions & 2 deletions modules/abstract-substrate/src/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export enum MethodNames {
*/
TransferKeepAlive = 'transferKeepAlive',

AddStake = 'addStake',

RemoveStake = 'removeStake',
}

Expand Down Expand Up @@ -76,7 +78,13 @@ export interface TransferAllArgs {
dest: { id: string };
keepAlive: boolean;
}
export interface UnstakeArgs extends Args {

export interface AddStakeArgs extends Args {
amountStaked: number;
hotkey: string;
netuid: number;
}
export interface RemoveStakeArgs extends Args {
amountUnstaked: number;
hotkey: string;
netuid: number;
Expand All @@ -85,7 +93,7 @@ export interface UnstakeArgs extends Args {
* Decoded TxMethod from a transaction hex
*/
export interface TxMethod {
args: TransferArgs | TransferAllArgs | UnstakeArgs;
args: TransferArgs | TransferAllArgs | AddStakeArgs | RemoveStakeArgs;
name: MethodNames;
pallet: string;
}
Expand Down
6 changes: 6 additions & 0 deletions modules/abstract-substrate/src/lib/txnSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export const TransferAllTransactionSchema = joi.object({
to: addressSchema.required(),
});

export const StakeTransactionSchema = joi.object({
amountStaked: joi.number().required(),
hotkey: joi.string().required(),
netuid: joi.number().required(),
});

export const UnstakeTransactionSchema = joi.object({
amount: joi.number().required(),
hotkey: joi.string().required(),
Expand Down
6 changes: 3 additions & 3 deletions 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, UnstakeArgs } from './iface';
import { HexString, Material, TransferAllArgs, TransferArgs, TxMethod, RemoveStakeArgs } from './iface';

export class Utils implements BaseUtils {
/** @inheritdoc */
Expand Down Expand Up @@ -195,8 +195,8 @@ 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;
isUnstake(arg: TxMethod['args']): arg is RemoveStakeArgs {
return (arg as RemoveStakeArgs).hotkey !== undefined && (arg as RemoveStakeArgs).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/lib/iface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface CreateBaseTxInfo {
* Decoded TxMethod from a transaction hex
*/
export interface TxMethod {
args: AddStakeArgs | UnstakeArgs;
args: AddStakeArgs | RemoveStakeArgs;
name: MethodNames;
pallet: string;
}
Expand All @@ -39,7 +39,7 @@ export interface AddStakeArgs extends Args {
netuid: string;
}

export interface UnstakeArgs extends Args {
export interface RemoveStakeArgs extends Args {
amount_staked: string;
hotkey: string;
netuid: string;
Expand Down
10 changes: 5 additions & 5 deletions modules/sdk-coin-tao/src/lib/unstakeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class UnstakeBuilder extends TransactionBuilder {
protected fromImplementation(rawTransaction: string): Transaction {
const tx = super.fromImplementation(rawTransaction);
if (this._method?.name === Interface.MethodNames.RemoveStake) {
const txMethod = this._method.args as Interface.UnstakeArgs;
const txMethod = this._method.args as Interface.RemoveStakeArgs;
this.amount(txMethod.amountUnstaked);
this.hotkey(txMethod.hotkey);
this.netuid(txMethod.netuid);
Expand Down Expand Up @@ -105,11 +105,11 @@ export class UnstakeBuilder extends TransactionBuilder {
/** @inheritdoc */
validateDecodedTransaction(decodedTxn: DecodedSigningPayload | DecodedSignedTx, rawTransaction: string): void {
if (decodedTxn.method?.name === Interface.MethodNames.RemoveStake) {
const txMethod = decodedTxn.method.args as unknown as Interface.UnstakeArgs;
const amountStaked = `${txMethod.amountUnstaked}`;
const txMethod = decodedTxn.method.args as unknown as Interface.RemoveStakeArgs;
const amount = txMethod.amountUnstaked;
const hotkey = txMethod.hotkey;
const netuid = txMethod.netuid;
const validationResult = Schema.UnstakeTransactionSchema.validate({ amountStaked, hotkey, netuid });
const validationResult = Schema.UnstakeTransactionSchema.validate({ amount, hotkey, netuid });
if (validationResult.error) {
throw new InvalidTransactionError(`Transfer Transaction validation failed: ${validationResult.error.message}`);
}
Expand All @@ -121,7 +121,7 @@ export class UnstakeBuilder extends TransactionBuilder {
// * @param args - Arguments specific to this method.
// * @param info - Information required to construct the transaction.
// */
private removeStake(args: Interface.UnstakeArgs, info: Interface.CreateBaseTxInfo): UnsignedTransaction {
private removeStake(args: Interface.RemoveStakeArgs, info: Interface.CreateBaseTxInfo): UnsignedTransaction {
return defineMethod(
{
method: {
Expand Down
98 changes: 50 additions & 48 deletions modules/sdk-coin-tao/test/unit/transactionBuilder/unstakeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from 'assert';
import should from 'should';
import { spy, assert as SinonAssert } from 'sinon';
import { UnstakeBuilder } from '../../../src/lib/unstakeBuilder';
import { accounts, rawTx, mockTssSignature, genesisHash, specVersion, txVersion, chainName } from '../../resources';
import { accounts, mockTssSignature, genesisHash, specVersion, txVersion, chainName } from '../../resources';
import { buildTestConfig } from './base';
import utils from '../../../src/lib/utils';
describe('Tao Unstake Builder', function () {
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('Tao Unstake Builder', function () {
it('should build a unstake transaction', async function () {
builder
.amount(50000000000000)
.hotkey('5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR')
.hotkey('5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT')
.netuid(0)
.sender({ address: sender.address })
.validity({ firstValid: 3933, maxDuration: 64 })
Expand All @@ -44,7 +44,7 @@ describe('Tao Unstake Builder', function () {
const txJson = tx.toJson();
// console.log('Transaction JSON:', JSON.stringify(txJson, null, 2));
should.deepEqual(txJson.amount, '50000000000000');
should.deepEqual(txJson.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.to, '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, sender.address);
should.deepEqual(txJson.blockNumber, 3933);
Expand All @@ -60,18 +60,18 @@ describe('Tao Unstake Builder', function () {
it('should build an unsigned unstake transaction', async function () {
builder
.amount(50000000000000)
.hotkey('5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR')
.hotkey('5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT')
.netuid(0)
.sender({ address: sender.address })
.validity({ firstValid: 3933, maxDuration: 64 })
.referenceBlock(referenceBlock)
.sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 })
.fee({ amount: 0, type: 'tip' });
console.log('Building transaction...');
// console.log('Building transaction...');
const tx = await builder.build();
const txJson = tx.toJson();
should.deepEqual(txJson.amount, '50000000000000');
should.deepEqual(txJson.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.to, '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, sender.address);
should.deepEqual(txJson.blockNumber, 3933);
Expand All @@ -84,47 +84,49 @@ describe('Tao Unstake Builder', function () {
should.deepEqual(txJson.chainName, chainName);
should.deepEqual(txJson.eraPeriod, 64);
});
it('should build from raw signed tx', async function () {
builder.from(rawTx.unstake.signed);
// 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.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk');
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, referenceBlock);
should.deepEqual(txJson.genesisHash, genesisHash);
should.deepEqual(txJson.specVersion, specVersion);
should.deepEqual(txJson.nonce, 0);
should.deepEqual(txJson.tip, 0);
should.deepEqual(txJson.transactionVersion, txVersion);
should.deepEqual(txJson.chainName, chainName);
should.deepEqual(txJson.eraPeriod, 64);
});
it('should build from raw unsigned tx', async function () {
builder.from(rawTx.unstake.unsigned);
builder
.validity({ firstValid: 3933, maxDuration: 64 })
.referenceBlock(referenceBlock)
.sender({ address: '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk' })
.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.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
should.deepEqual(txJson.netuid, '0');
should.deepEqual(txJson.sender, '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk');
should.deepEqual(txJson.blockNumber, 3933);
should.deepEqual(txJson.referenceBlock, referenceBlock);
should.deepEqual(txJson.genesisHash, genesisHash);
should.deepEqual(txJson.specVersion, specVersion);
should.deepEqual(txJson.nonce, 0);
should.deepEqual(txJson.tip, 0);
should.deepEqual(txJson.transactionVersion, txVersion);
should.deepEqual(txJson.chainName, chainName);
should.deepEqual(txJson.eraPeriod, 64);
});
// it('should build from raw signed tx', async function () {
// builder.from('0x55028400aaa34f9f3c1f685e2bac444a4e2d50d302a16f0550f732dd799f854dda7ec77201223b6649a7d5e23c384a6deb8c76ff46a44958dbae8e640beb35b1d0e940f57185b2efc8497c848cd187f6f5fbf2cd199bf3f7a016085c45d7d68b05b3421a88b4019d05000007038a90be061598f4b592afbd546bcb6beadb3c02f5c129df2e11b698f9543dbd41000000e1f50500000000');
// // builder.validity({ firstValid: 3933, maxDuration: 64 }).referenceBlock(referenceBlock);
// 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.to, '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT');
// should.deepEqual(txJson.netuid, '0');
// should.deepEqual(txJson.sender, '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk');
// should.deepEqual(txJson.blockNumber,3933);
// should.deepEqual(txJson.referenceBlock, referenceBlock);
// should.deepEqual(txJson.genesisHash, genesisHash);
// should.deepEqual(txJson.specVersion, specVersion);
// should.deepEqual(txJson.nonce, 0);
// should.deepEqual(txJson.tip, 0);
// should.deepEqual(txJson.transactionVersion, txVersion);
// should.deepEqual(txJson.chainName, chainName);
// should.deepEqual(txJson.eraPeriod, 64);
// });
// it('should build from raw unsigned tx', async function () {
// builder.from(rawTx.unstake.unsigned);
// builder
// .validity({ firstValid: 3933, maxDuration: 64 })
// .referenceBlock(referenceBlock)
// .sender({ address: '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk' })
// .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.to, '5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR');
// should.deepEqual(txJson.netuid, '0');
// should.deepEqual(txJson.sender, '5F1mFBGhm7FrSKftDxzFPN8U1BqHKSAxEDhTV2Yx5JhCe2Nk');
// should.deepEqual(txJson.blockNumber, 3933);
// should.deepEqual(txJson.referenceBlock, referenceBlock);
// should.deepEqual(txJson.genesisHash, genesisHash);
// should.deepEqual(txJson.specVersion, specVersion);
// should.deepEqual(txJson.nonce, 0);
// should.deepEqual(txJson.tip, 0);
// should.deepEqual(txJson.transactionVersion, txVersion);
// should.deepEqual(txJson.chainName, chainName);
// should.deepEqual(txJson.eraPeriod, 64);
// });
});
});

0 comments on commit 0b74459

Please sign in to comment.