|
| 1 | +import { decode } from '@substrate/txwrapper-polkadot'; |
| 2 | +import { coins } from '@bitgo/statics'; |
| 3 | +import should from 'should'; |
| 4 | +import { TransactionBuilderFactory, NominateBuilder, BatchBuilder } from '../../../src/lib'; |
| 5 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 6 | +import utils from '../../../src/lib/utils'; |
| 7 | +import { accounts, nominateTx, nominateValidators, stakingTx } from '../../resources'; |
| 8 | + |
| 9 | +describe('Polyx Nominate Builder', function () { |
| 10 | + let builder: NominateBuilder; |
| 11 | + const factory = new TransactionBuilderFactory(coins.get('tpolyx')); |
| 12 | + |
| 13 | + const senderAddress = accounts.account1.address; |
| 14 | + const validatorAddress = nominateValidators[0]; |
| 15 | + const validatorAddress2 = nominateValidators[1]; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + builder = factory.getNominateBuilder(); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('transaction type', () => { |
| 22 | + it('should return StakingVote transaction type', () => { |
| 23 | + should.equal(builder['transactionType'], TransactionType.StakingVote); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('validators setter validation', () => { |
| 28 | + it('should reject empty validators array', () => { |
| 29 | + should.throws(() => builder.validators([]), /at least 1/); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should reject more than 16 validators', () => { |
| 33 | + const tooMany = Array(17).fill(validatorAddress); |
| 34 | + should.throws(() => builder.validators(tooMany), /at most 16/); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should reject malformed validator addresses', () => { |
| 38 | + should.throws(() => builder.validators(['not-a-valid-address']), /is not a well-formed/); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should accept valid single validator', () => { |
| 42 | + should.doesNotThrow(() => builder.validators([validatorAddress])); |
| 43 | + should.deepEqual(builder.getValidators(), [validatorAddress]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should accept multiple valid validators', () => { |
| 47 | + should.doesNotThrow(() => builder.validators([validatorAddress, validatorAddress2])); |
| 48 | + should.deepEqual(builder.getValidators(), [validatorAddress, validatorAddress2]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should accept exactly 16 validators', () => { |
| 52 | + const exactly16 = Array(16).fill(validatorAddress); |
| 53 | + should.doesNotThrow(() => builder.validators(exactly16)); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('build unsigned nominate transaction', () => { |
| 58 | + it('should build an unsigned nominate transaction', async () => { |
| 59 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 60 | + builder |
| 61 | + .validators([validatorAddress, validatorAddress2]) |
| 62 | + .sender({ address: senderAddress }) |
| 63 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 64 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 65 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 15 }) |
| 66 | + .fee({ amount: 0, type: 'tip' }) |
| 67 | + .material(material); |
| 68 | + |
| 69 | + const tx = await builder.build(); |
| 70 | + should.exist(tx); |
| 71 | + should.equal(tx.type, TransactionType.StakingVote); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('build signed nominate transaction', () => { |
| 76 | + it('should parse a signed nominate transaction from hex', () => { |
| 77 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 78 | + const signedBuilder = factory.getNominateBuilder(); |
| 79 | + signedBuilder.material(material); |
| 80 | + signedBuilder.from(nominateTx.signed); |
| 81 | + |
| 82 | + const validators = signedBuilder.getValidators(); |
| 83 | + should.exist(validators); |
| 84 | + validators.length.should.be.greaterThan(0); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('validateDecodedTransaction', () => { |
| 89 | + it('should reject non-nominate method name', () => { |
| 90 | + const mockDecoded = { |
| 91 | + method: { name: 'bond', pallet: 'staking', args: {} }, |
| 92 | + }; |
| 93 | + should.throws(() => builder.validateDecodedTransaction(mockDecoded as never), /Invalid transaction type/); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should validate a real built nominate transaction', () => { |
| 97 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 98 | + builder |
| 99 | + .validators([validatorAddress, validatorAddress2]) |
| 100 | + .sender({ address: senderAddress }) |
| 101 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 102 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 103 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 15 }) |
| 104 | + .fee({ amount: 0, type: 'tip' }) |
| 105 | + .material(material); |
| 106 | + |
| 107 | + const unsignedTx = builder['buildTransaction'](); |
| 108 | + const decodedTx = decode(unsignedTx, { |
| 109 | + metadataRpc: material.metadata, |
| 110 | + registry: builder['_registry'], |
| 111 | + }); |
| 112 | + |
| 113 | + should.equal(decodedTx.method.name, 'nominate'); |
| 114 | + should.doesNotThrow(() => builder.validateDecodedTransaction(decodedTx)); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('parse from raw signed transaction', () => { |
| 119 | + it('should parse signed nominate tx and recover validators', () => { |
| 120 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 121 | + const newBuilder = factory.getNominateBuilder(); |
| 122 | + newBuilder.material(material); |
| 123 | + newBuilder.from(nominateTx.signed); |
| 124 | + |
| 125 | + const validators = newBuilder.getValidators(); |
| 126 | + should.exist(validators); |
| 127 | + validators.length.should.be.greaterThan(0); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('parse from raw unsigned transaction', () => { |
| 132 | + it('should parse unsigned nominate tx and recover validators', async () => { |
| 133 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 134 | + // Build a real unsigned tx first, then parse it back |
| 135 | + const originalBuilder = factory.getNominateBuilder(); |
| 136 | + originalBuilder |
| 137 | + .validators([validatorAddress, validatorAddress2]) |
| 138 | + .sender({ address: senderAddress }) |
| 139 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 140 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 141 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 15 }) |
| 142 | + .fee({ amount: 0, type: 'tip' }) |
| 143 | + .material(material); |
| 144 | + |
| 145 | + const tx = await originalBuilder.build(); |
| 146 | + const rawHex = tx.toBroadcastFormat(); |
| 147 | + |
| 148 | + const newBuilder = factory.getNominateBuilder(); |
| 149 | + newBuilder.material(material); |
| 150 | + newBuilder.from(rawHex); |
| 151 | + |
| 152 | + const validators = newBuilder.getValidators(); |
| 153 | + should.exist(validators); |
| 154 | + should.deepEqual(validators, [validatorAddress, validatorAddress2]); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('round-trip', () => { |
| 159 | + it('should rebuild from serialized unsigned transaction and preserve validators', async () => { |
| 160 | + const material = utils.getMaterial(coins.get('tpolyx').network.type); |
| 161 | + const originalBuilder = factory.getNominateBuilder(); |
| 162 | + originalBuilder |
| 163 | + .validators([validatorAddress, validatorAddress2]) |
| 164 | + .sender({ address: senderAddress }) |
| 165 | + .validity({ firstValid: 3933, maxDuration: 64 }) |
| 166 | + .referenceBlock('0x149799bc9602cb5cf201f3425fb8d253b2d4e61fc119dcab3249f307f594754d') |
| 167 | + .sequenceId({ name: 'Nonce', keyword: 'nonce', value: 15 }) |
| 168 | + .fee({ amount: 0, type: 'tip' }) |
| 169 | + .material(material); |
| 170 | + |
| 171 | + const tx = await originalBuilder.build(); |
| 172 | + const rawHex = tx.toBroadcastFormat(); |
| 173 | + |
| 174 | + const rebuiltBuilder = factory.getNominateBuilder(); |
| 175 | + rebuiltBuilder.material(material); |
| 176 | + rebuiltBuilder.from(rawHex); |
| 177 | + |
| 178 | + should.deepEqual(rebuiltBuilder.getValidators(), [validatorAddress, validatorAddress2]); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe('factory routing', () => { |
| 183 | + it('should route raw nominate extrinsic to NominateBuilder', () => { |
| 184 | + const resolvedBuilder = factory.from(nominateTx.signed); |
| 185 | + should.ok(resolvedBuilder instanceof NominateBuilder, 'expected NominateBuilder'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should route batchAll bond+nominate to BatchBuilder', () => { |
| 189 | + const resolvedBuilder = factory.from(stakingTx.batch.bondAndNominate.signed); |
| 190 | + should.ok(resolvedBuilder instanceof BatchBuilder, 'expected BatchBuilder'); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments