|
| 1 | +import assert from 'assert'; |
| 2 | +import should from 'should'; |
| 3 | +import { spy, assert as SinonAssert } from 'sinon'; |
| 4 | +import { UnstakeBuilder } from '../../../src/lib/unstakeBuilder'; |
| 5 | +import { accounts, mockTssSignature, genesisHash, chainName, rawTx } from '../../resources'; |
| 6 | +import { buildTestConfig } from './base'; |
| 7 | +import utils from '../../../src/lib/utils'; |
| 8 | +import { testnetMaterial } from '../../../src/resources'; |
| 9 | + |
| 10 | +describe('Tao Unstake Builder', function () { |
| 11 | + const referenceBlock = '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d'; |
| 12 | + let builder: UnstakeBuilder; |
| 13 | + const sender = accounts.account1; |
| 14 | + |
| 15 | + beforeEach(function () { |
| 16 | + const config = buildTestConfig(); |
| 17 | + const material = utils.getMaterial(config.network.type); |
| 18 | + builder = new UnstakeBuilder(config).material(material); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('setter validation', function () { |
| 22 | + it('should validate stake amount', function () { |
| 23 | + const spyValidateValue = spy(builder, 'validateValue'); |
| 24 | + assert.throws( |
| 25 | + () => builder.amount(-1), |
| 26 | + (e: Error) => e.message === 'Value cannot be less than zero' |
| 27 | + ); |
| 28 | + should.doesNotThrow(() => builder.amount(1000)); |
| 29 | + SinonAssert.calledTwice(spyValidateValue); |
| 30 | + }); |
| 31 | + it('should validate hotkey address', function () { |
| 32 | + const spyValidateAddress = spy(builder, 'validateAddress'); |
| 33 | + assert.throws( |
| 34 | + () => builder.hotkey({ address: 'abc' }), |
| 35 | + (e: Error) => e.message === `The address 'abc' is not a well-formed dot address` |
| 36 | + ); |
| 37 | + should.doesNotThrow(() => builder.hotkey({ address: '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT' })); |
| 38 | + SinonAssert.calledTwice(spyValidateAddress); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('build unstake transaction', function () { |
| 43 | + it('should build a unstake transaction', async function () { |
| 44 | + builder |
| 45 | + .amount(50000000000000) |
| 46 | + .hotkey({ address: '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT' }) |
| 47 | + .netuid(0) |
| 48 | + .sender({ address: sender.address }) |
| 49 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 50 | + .referenceBlock(referenceBlock) |
| 51 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 52 | + .fee({ amount: 0, type: 'tip' }) |
| 53 | + .addSignature({ pub: sender.publicKey }, Buffer.from(mockTssSignature, 'hex')); |
| 54 | + |
| 55 | + const tx = await builder.build(); |
| 56 | + const txJson = tx.toJson(); |
| 57 | + should.deepEqual(txJson.amount, '50000000000000'); |
| 58 | + should.deepEqual(txJson.to, '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT'); |
| 59 | + should.deepEqual(txJson.netuid, '0'); |
| 60 | + should.deepEqual(txJson.sender, sender.address); |
| 61 | + should.deepEqual(txJson.blockNumber, 3933); |
| 62 | + should.deepEqual(txJson.referenceBlock, referenceBlock); |
| 63 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 64 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 65 | + should.deepEqual(txJson.nonce, 200); |
| 66 | + should.deepEqual(txJson.tip, 0); |
| 67 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 68 | + should.deepEqual(txJson.chainName.toLowerCase(), chainName); |
| 69 | + should.deepEqual(txJson.eraPeriod, 64); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should build an unsigned unstake transaction', async function () { |
| 73 | + builder |
| 74 | + .amount(50000000000000) |
| 75 | + .hotkey({ address: '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT' }) |
| 76 | + .netuid(0) |
| 77 | + .sender({ address: sender.address }) |
| 78 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 79 | + .referenceBlock(referenceBlock) |
| 80 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 200 }) |
| 81 | + .fee({ amount: 0, type: 'tip' }); |
| 82 | + const tx = await builder.build(); |
| 83 | + const txJson = tx.toJson(); |
| 84 | + should.deepEqual(txJson.amount, '50000000000000'); |
| 85 | + should.deepEqual(txJson.to, '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT'); |
| 86 | + should.deepEqual(txJson.netuid, '0'); |
| 87 | + should.deepEqual(txJson.sender, sender.address); |
| 88 | + should.deepEqual(txJson.blockNumber, 3933); |
| 89 | + should.deepEqual(txJson.referenceBlock, referenceBlock); |
| 90 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 91 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 92 | + should.deepEqual(txJson.nonce, 200); |
| 93 | + should.deepEqual(txJson.tip, 0); |
| 94 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 95 | + should.deepEqual(txJson.chainName.toLowerCase(), chainName); |
| 96 | + should.deepEqual(txJson.eraPeriod, 64); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should build from raw signed tx', async function () { |
| 100 | + builder.from(rawTx.unstake.signed); |
| 101 | + builder.validity({ firstValid: 3933, maxDuration: 64 }).referenceBlock(referenceBlock); |
| 102 | + const tx = await builder.build(); |
| 103 | + const txJson = tx.toJson(); |
| 104 | + should.deepEqual(txJson.amount, '100000000'); |
| 105 | + should.deepEqual(txJson.to, '5FCPTnjevGqAuTttetBy4a24Ej3pH9fiQ8fmvP1ZkrVsLUoT'); |
| 106 | + should.deepEqual(txJson.netuid, '0'); |
| 107 | + should.deepEqual(txJson.sender, '5FvSWbV4hGC7GvXQKKtiVmmHSH3JELK8R3JS8Z5adnACFBwh'); |
| 108 | + should.deepEqual(txJson.blockNumber, 3933); |
| 109 | + should.deepEqual(txJson.referenceBlock, referenceBlock); |
| 110 | + should.deepEqual(txJson.genesisHash, genesisHash); |
| 111 | + should.deepEqual(txJson.specVersion, Number(testnetMaterial.specVersion)); |
| 112 | + should.deepEqual(txJson.nonce, 361); |
| 113 | + should.deepEqual(txJson.tip, 0); |
| 114 | + should.deepEqual(txJson.transactionVersion, Number(testnetMaterial.txVersion)); |
| 115 | + should.deepEqual(txJson.chainName.toLowerCase(), chainName); |
| 116 | + should.deepEqual(txJson.eraPeriod, 64); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments