|
| 1 | +import { DecodedSigningPayload } from '@substrate/txwrapper-core'; |
| 2 | +import { decode } from '@substrate/txwrapper-polkadot'; |
| 3 | +import { coins } from '@bitgo/statics'; |
| 4 | +import should from 'should'; |
| 5 | +import sinon from 'sinon'; |
| 6 | +import { TransactionBuilderFactory, UnbondBuilder, Transaction } from '../../../src/lib'; |
| 7 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 8 | +import utils from '../../../src/lib/utils'; |
| 9 | + |
| 10 | +import { accounts, stakingTx } from '../../resources'; |
| 11 | + |
| 12 | +function createMockTransaction(txData: string): Partial<Transaction> { |
| 13 | + return { |
| 14 | + id: '123', |
| 15 | + type: TransactionType.StakingDeactivate, |
| 16 | + toBroadcastFormat: () => txData, |
| 17 | + inputs: [], |
| 18 | + outputs: [], |
| 19 | + signature: ['mock-signature'], |
| 20 | + toJson: () => ({ |
| 21 | + id: '123', |
| 22 | + type: 'StakingDeactivate', |
| 23 | + sender: accounts.account1.address, |
| 24 | + referenceBlock: '0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d', |
| 25 | + blockNumber: 100, |
| 26 | + genesisHash: '0x', |
| 27 | + nonce: 1, |
| 28 | + tip: 0, |
| 29 | + specVersion: 1, |
| 30 | + transactionVersion: 1, |
| 31 | + chainName: 'Polymesh', |
| 32 | + inputs: [], |
| 33 | + outputs: [], |
| 34 | + }), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('Polyx Unbond Builder', function () { |
| 39 | + let builder: UnbondBuilder; |
| 40 | + const factory = new TransactionBuilderFactory(coins.get('tpolyx')); |
| 41 | + |
| 42 | + const senderAddress = accounts.account1.address; |
| 43 | + const testAmount = '10000'; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + builder = factory.getUnbondBuilder(); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('setter validation', () => { |
| 50 | + it('should validate unbond amount', () => { |
| 51 | + const spy = sinon.spy(builder, 'validateValue'); |
| 52 | + should.throws(() => builder.amount('-1'), /Value cannot be less than zero/); |
| 53 | + should.doesNotThrow(() => builder.amount('1000')); |
| 54 | + sinon.assert.calledTwice(spy); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('Build and Sign', function () { |
| 59 | + it('should build an unbond transaction', async () => { |
| 60 | + builder |
| 61 | + .amount(testAmount) |
| 62 | + .sender({ address: senderAddress }) |
| 63 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 64 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 65 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 100 }); |
| 66 | + |
| 67 | + const mockTx = createMockTransaction(stakingTx.unbond.unsigned); |
| 68 | + sinon.stub(builder, 'build').resolves(mockTx as Transaction); |
| 69 | + |
| 70 | + const tx = await builder.build(); |
| 71 | + should.exist(tx); |
| 72 | + |
| 73 | + should.equal(builder.getAmount(), testAmount); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Transaction Validation', function () { |
| 78 | + it('should build, decode, and validate a real unbond transaction', () => { |
| 79 | + // Build the transaction with real parameters |
| 80 | + builder |
| 81 | + .amount(testAmount) |
| 82 | + .sender({ address: senderAddress }) |
| 83 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 84 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 85 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 100 }); |
| 86 | + |
| 87 | + // Set up material for decoding |
| 88 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 89 | + builder.material(material); |
| 90 | + |
| 91 | + // Build the actual unsigned transaction |
| 92 | + const unsignedTx = builder['buildTransaction'](); |
| 93 | + const registry = builder['_registry']; |
| 94 | + |
| 95 | + // Decode the actual built transaction |
| 96 | + const decodedTx = decode(unsignedTx, { |
| 97 | + metadataRpc: material.metadata, |
| 98 | + registry: registry, |
| 99 | + }); |
| 100 | + |
| 101 | + // Validate the decoded transaction structure |
| 102 | + should.equal(decodedTx.method.name, 'unbond'); |
| 103 | + should.equal(decodedTx.method.pallet, 'staking'); |
| 104 | + |
| 105 | + const unbondArgs = decodedTx.method.args as { value: string }; |
| 106 | + should.equal(unbondArgs.value, testAmount); |
| 107 | + |
| 108 | + // validate using the builder's validation method |
| 109 | + should.doesNotThrow(() => { |
| 110 | + builder.validateDecodedTransaction(decodedTx); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should reject non-unbond transactions', () => { |
| 115 | + const mockDecodedTx: DecodedSigningPayload = { |
| 116 | + method: { |
| 117 | + name: 'bond', |
| 118 | + pallet: 'staking', |
| 119 | + args: {}, |
| 120 | + }, |
| 121 | + } as unknown as DecodedSigningPayload; |
| 122 | + |
| 123 | + should.throws(() => { |
| 124 | + builder.validateDecodedTransaction(mockDecodedTx); |
| 125 | + }, /Invalid transaction type/); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should validate field validation', () => { |
| 129 | + builder.amount('1000'); |
| 130 | + should.doesNotThrow(() => { |
| 131 | + builder.testValidateFields(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe('From Raw Transaction', function () { |
| 137 | + it('should rebuild from real unbond transaction', async () => { |
| 138 | + // First build a transaction to get a real raw transaction |
| 139 | + const originalBuilder = factory.getUnbondBuilder(); |
| 140 | + originalBuilder |
| 141 | + .amount(testAmount) |
| 142 | + .sender({ address: senderAddress }) |
| 143 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 144 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 145 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 100 }); |
| 146 | + |
| 147 | + // Set up material |
| 148 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 149 | + originalBuilder.material(material); |
| 150 | + |
| 151 | + // Build the transaction and get the serialized hex |
| 152 | + const tx = await originalBuilder.build(); |
| 153 | + const rawTxHex = tx.toBroadcastFormat(); |
| 154 | + |
| 155 | + // Create a new builder and reconstruct from the transaction hex |
| 156 | + const newBuilder = factory.getUnbondBuilder(); |
| 157 | + newBuilder.material(material); |
| 158 | + newBuilder.from(rawTxHex); |
| 159 | + |
| 160 | + // Verify the reconstructed builder has the same parameters |
| 161 | + should.equal(newBuilder.getAmount(), testAmount); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments