-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0d1ca0
commit 6107abc
Showing
4 changed files
with
145 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
modules/sdk-coin-tao/test/unit/transactionBuilder/unstakeBuilder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
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 { buildTestConfig } from './base'; | ||
import utils from '../../../src/lib/utils'; | ||
describe('Tao Unstake Builder', function () { | ||
const referenceBlock = '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'; | ||
let builder: UnstakeBuilder; | ||
const sender = accounts.account1; | ||
beforeEach(function () { | ||
const config = buildTestConfig(); | ||
const material = utils.getMaterial(config.network.type); | ||
// console.log('Material:', material); | ||
builder = new UnstakeBuilder(config).material(material); | ||
}); | ||
describe('setter validation', function () { | ||
it('should validate stake amount', function () { | ||
const spyValidateValue = spy(builder, 'validateValue'); | ||
assert.throws( | ||
() => builder.amount(-1), | ||
(e: Error) => e.message === 'Value cannot be less than zero' | ||
); | ||
should.doesNotThrow(() => builder.amount(1000)); | ||
SinonAssert.calledTwice(spyValidateValue); | ||
}); | ||
}); | ||
describe('build unstake transaction', function () { | ||
it('should build a unstake transaction', async function () { | ||
builder | ||
.amount(50000000000000) | ||
.hotkey('5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR') | ||
.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' }) | ||
.addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); | ||
|
||
const tx = await builder.build(); | ||
// 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); | ||
should.deepEqual(txJson.sender, sender.address); | ||
should.deepEqual(txJson.blockNumber, 3933); | ||
should.deepEqual(txJson.referenceBlock, referenceBlock); | ||
should.deepEqual(txJson.genesisHash, genesisHash); | ||
should.deepEqual(txJson.specVersion, specVersion); | ||
should.deepEqual(txJson.nonce, 200); | ||
should.deepEqual(txJson.tip, 0); | ||
should.deepEqual(txJson.transactionVersion, txVersion); | ||
should.deepEqual(txJson.chainName, chainName); | ||
should.deepEqual(txJson.eraPeriod, 64); | ||
}); | ||
it('should build an unsigned unstake transaction', async function () { | ||
builder | ||
.amount(50000000000000) | ||
.hotkey('5H56KVtb3sSMxuhFsH51iFi1gei7tnBQjpVmj6hu9tK7CBDR') | ||
.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('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.sender, sender.address); | ||
should.deepEqual(txJson.blockNumber, 3933); | ||
should.deepEqual(txJson.referenceBlock, referenceBlock); | ||
should.deepEqual(txJson.genesisHash, genesisHash); | ||
should.deepEqual(txJson.specVersion, specVersion); | ||
should.deepEqual(txJson.nonce, 200); | ||
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(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.hotkey, '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.hotkey, '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); | ||
}); | ||
}); | ||
}); |