|
| 1 | +import should from 'should'; |
| 2 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 3 | +import { TransactionBuilderFactory } from '../../src'; // Adjust path as needed |
| 4 | +import { coins } from '@bitgo/statics'; |
| 5 | +import * as testData from '../resources/ton'; |
| 6 | +import { TON_WHALES_DEPOSIT_OPCODE } from '../../src/lib/constants'; |
| 7 | + |
| 8 | +describe('Ton Whales Deposit Builder', () => { |
| 9 | + const factory = new TransactionBuilderFactory(coins.get('tton')); |
| 10 | + const fixture = testData.signedTonWhalesDepositTransaction; |
| 11 | + |
| 12 | + it('should parse a raw transaction and extract correct parameters', async function () { |
| 13 | + const txBuilder = factory.from(fixture.tx); |
| 14 | + const builtTx = await txBuilder.build(); |
| 15 | + const jsonTx = builtTx.toJson(); |
| 16 | + |
| 17 | + // Verify Business Logic Fields |
| 18 | + should.equal(builtTx.type, TransactionType.TonWhalesDeposit); |
| 19 | + should.equal(jsonTx.amount, fixture.recipient.amount); |
| 20 | + should.equal(jsonTx.destination, fixture.recipient.address); |
| 21 | + should.equal(jsonTx.sender, fixture.sender); |
| 22 | + |
| 23 | + // Verify Network Constraints |
| 24 | + should.equal(jsonTx.seqno, fixture.seqno); |
| 25 | + should.equal(jsonTx.expirationTime, fixture.expireTime); |
| 26 | + should.equal(jsonTx.bounceable, fixture.bounceable); |
| 27 | + |
| 28 | + // Verify Payload Structure (OpCode Check) |
| 29 | + const msg = builtTx['message'] || ''; |
| 30 | + should.equal(msg.startsWith(TON_WHALES_DEPOSIT_OPCODE), true); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should parse and rebuild the transaction resulting in the same hex', async function () { |
| 34 | + const txBuilder = factory.from(fixture.tx); |
| 35 | + const builtTx = await txBuilder.build(); |
| 36 | + |
| 37 | + // Verify the parser extracted the signature |
| 38 | + const signature = builtTx.signature[0]; |
| 39 | + should.exist(signature); |
| 40 | + signature.should.not.be.empty(); |
| 41 | + |
| 42 | + // Rebuild from the parsed object |
| 43 | + const builder2 = factory.from(builtTx.toBroadcastFormat()); |
| 44 | + const builtTx2 = await builder2.build(); |
| 45 | + |
| 46 | + // The output of the second build should match the original raw transaction |
| 47 | + should.equal(builtTx2.toBroadcastFormat(), fixture.tx); |
| 48 | + should.equal(builtTx2.type, TransactionType.TonWhalesDeposit); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should build a transaction from scratch that byte-for-byte matches the raw fixture', async function () { |
| 52 | + const builder = factory.getTonWhalesDepositBuilder(); |
| 53 | + |
| 54 | + // Set Header Info from Fixture |
| 55 | + builder.sender(fixture.sender); |
| 56 | + builder.publicKey(fixture.publicKey); |
| 57 | + builder.sequenceNumber(fixture.seqno); |
| 58 | + builder.expireTime(fixture.expireTime); |
| 59 | + builder.bounceable(fixture.bounceable); |
| 60 | + |
| 61 | + // Set Staking Info from Fixture |
| 62 | + builder.send({ |
| 63 | + address: fixture.recipient.address, |
| 64 | + amount: fixture.recipient.amount, |
| 65 | + }); |
| 66 | + builder.setDepositAmount(fixture.recipient.amount); |
| 67 | + |
| 68 | + // Set the specific QueryID from Fixture so binary hash matches |
| 69 | + builder.setDepositMessage(fixture.queryId); |
| 70 | + |
| 71 | + // Attach Signature from Fixture (Mocking the HSM signing process) |
| 72 | + if (fixture.signature) { |
| 73 | + builder.addSignature({ pub: fixture.publicKey }, Buffer.from(fixture.signature, 'hex')); |
| 74 | + } |
| 75 | + |
| 76 | + // Build Signed Transaction |
| 77 | + const signedBuiltTx = await builder.build(); |
| 78 | + |
| 79 | + // Final Assertion: Byte-for-byte equality with the Sandbox output |
| 80 | + should.equal(signedBuiltTx.toBroadcastFormat(), fixture.tx); |
| 81 | + should.equal(signedBuiltTx.type, TransactionType.TonWhalesDeposit); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should parse the bounceable flag correctly', async function () { |
| 85 | + const txBuilder = factory.from(fixture.tx); |
| 86 | + const tx = await txBuilder.build(); |
| 87 | + |
| 88 | + // The fixture is set to true, so the parser must reflect that |
| 89 | + const isBounceable = tx.toJson().bounceable; |
| 90 | + should.equal(isBounceable, fixture.bounceable); |
| 91 | + should.equal(typeof isBounceable, 'boolean'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments