|
| 1 | +import should from 'should'; |
| 2 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 3 | +import { TransactionBuilderFactory } from '../../src/lib/transactionBuilderFactory'; |
| 4 | +import { coins } from '@bitgo/statics'; |
| 5 | +import * as testData from '../resources/ton'; |
| 6 | +import { KeyPair } from '../../src/lib/keyPair'; |
| 7 | +import * as utils from '../../src/lib/utils'; |
| 8 | +import { TON_WHALES_DEPOSIT_OPCODE } from '../../src/lib/constants'; |
| 9 | + |
| 10 | +describe('Ton Whales Deposit Builder', () => { |
| 11 | + const factory = new TransactionBuilderFactory(coins.get('tton')); |
| 12 | + |
| 13 | + // Use a valid pool address (TonWhales Testnet Pool) |
| 14 | + const poolAddress = 'kQDr9Sq482A6ikIUh5mUUjJaBUUJBrye13CJiDB-R31_l7mg'; |
| 15 | + const depositAmount = '10000000000'; // 10 TON |
| 16 | + |
| 17 | + it('should build a unsigned deposit tx', async function () { |
| 18 | + const txBuilder = factory.getTonWhalesDepositBuilder(); |
| 19 | + |
| 20 | + // 1. Setup Sender using fixture data |
| 21 | + txBuilder.sender(testData.sender.address); |
| 22 | + txBuilder.sequenceNumber(0); |
| 23 | + txBuilder.publicKey(testData.sender.publicKey); |
| 24 | + txBuilder.expireTime(1234567890); |
| 25 | + |
| 26 | + // 2. Setup Deposit |
| 27 | + txBuilder.send({ address: poolAddress, amount: depositAmount }); |
| 28 | + txBuilder.setDepositMessage(); // Sets the OpCode |
| 29 | + |
| 30 | + const tx = await txBuilder.build(); |
| 31 | + const json = tx.toJson(); |
| 32 | + |
| 33 | + // 3. Verify Type and Fields |
| 34 | + should.equal(tx.type, TransactionType.TonWhalesDeposit); |
| 35 | + |
| 36 | + // Check Outputs |
| 37 | + tx.outputs.length.should.equal(1); |
| 38 | + tx.outputs[0].should.deepEqual({ |
| 39 | + address: poolAddress, |
| 40 | + value: depositAmount, |
| 41 | + coin: 'tton', |
| 42 | + }); |
| 43 | + |
| 44 | + // Check Payload contains OpCode + QueryID (0x7BCD136F + 0...0) |
| 45 | + const expectedPayload = TON_WHALES_DEPOSIT_OPCODE + '0000000000000000'; |
| 46 | + should.equal(tx['message'], expectedPayload); |
| 47 | + |
| 48 | + // Check JSON |
| 49 | + should.equal(json.bounceable, false); |
| 50 | + should.equal(json.amount, depositAmount); |
| 51 | + should.equal(json.destination, poolAddress); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should parse a raw deposit transaction (Round Trip)', async function () { |
| 55 | + // 1. Build the transaction |
| 56 | + const buildStep = factory.getTonWhalesDepositBuilder(); |
| 57 | + buildStep.sender(testData.sender.address); |
| 58 | + buildStep.sequenceNumber(123); |
| 59 | + buildStep.publicKey(testData.sender.publicKey); |
| 60 | + buildStep.expireTime(1700000000); |
| 61 | + buildStep.send({ address: poolAddress, amount: depositAmount }); |
| 62 | + buildStep.setDepositMessage(); |
| 63 | + |
| 64 | + const builtTx = await buildStep.build(); |
| 65 | + const rawBoc = builtTx.toBroadcastFormat(); |
| 66 | + |
| 67 | + // 2. Parse it back (Simulate receiving from chain/API) |
| 68 | + const parseStep = factory.from(rawBoc); |
| 69 | + const parsedTx = await parseStep.build(); |
| 70 | + const parsedJson = parsedTx.toJson(); |
| 71 | + |
| 72 | + // 3. Verify Parsing Logic correctly identified it as TonWhalesDeposit |
| 73 | + should.equal(parsedTx.type, TransactionType.TonWhalesDeposit); |
| 74 | + |
| 75 | + // Verify fields persisted |
| 76 | + parsedJson.seqno.should.equal(123); |
| 77 | + parsedJson.expirationTime.should.equal(1700000000); |
| 78 | + parsedJson.amount.should.equal(depositAmount); |
| 79 | + parsedJson.destination.should.equal(poolAddress); |
| 80 | + parsedJson.sender.should.equal(testData.sender.address); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should build a signed deposit tx using add signature', async function () { |
| 84 | + // Use private key from fixtures |
| 85 | + const keyPair = new KeyPair({ prv: testData.privateKeys.prvKey1 }); |
| 86 | + const publicKey = keyPair.getKeys().pub; |
| 87 | + const address = await utils.default.getAddressFromPublicKey(publicKey); |
| 88 | + |
| 89 | + const txBuilder = factory.getTonWhalesDepositBuilder(); |
| 90 | + txBuilder.sender(address); |
| 91 | + txBuilder.sequenceNumber(0); |
| 92 | + txBuilder.publicKey(publicKey); |
| 93 | + const expireAt = Math.floor(Date.now() / 1e3) + 60 * 60 * 24 * 7; |
| 94 | + txBuilder.expireTime(expireAt); |
| 95 | + |
| 96 | + txBuilder.send({ address: poolAddress, amount: depositAmount }); |
| 97 | + txBuilder.setDepositMessage(); |
| 98 | + |
| 99 | + const tx = await txBuilder.build(); |
| 100 | + |
| 101 | + // Sign |
| 102 | + const signable = tx.signablePayload; |
| 103 | + const signature = keyPair.signMessageinUint8Array(signable); |
| 104 | + |
| 105 | + // Add Signature |
| 106 | + txBuilder.addSignature(keyPair.getKeys(), Buffer.from(signature)); |
| 107 | + |
| 108 | + const signedTx = await txBuilder.build(); |
| 109 | + |
| 110 | + // Re-parse to verify signature attachment |
| 111 | + const builder2 = factory.from(signedTx.toBroadcastFormat()); |
| 112 | + const tx2 = await builder2.build(); |
| 113 | + |
| 114 | + should.equal(tx.toBroadcastFormat(), tx2.toBroadcastFormat()); |
| 115 | + should.equal(tx2.type, TransactionType.TonWhalesDeposit); |
| 116 | + should.exist(tx2.toJson().signature); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should build deposit tx with bounceable flag', async function () { |
| 120 | + const txBuilder = factory.getTonWhalesDepositBuilder(); |
| 121 | + txBuilder.sender(testData.sender.address); |
| 122 | + txBuilder.sequenceNumber(0); |
| 123 | + txBuilder.publicKey(testData.sender.publicKey); |
| 124 | + txBuilder.expireTime(1234567890); |
| 125 | + |
| 126 | + txBuilder.send({ address: poolAddress, amount: depositAmount }); |
| 127 | + txBuilder.setDepositMessage(); |
| 128 | + |
| 129 | + // Set Bounceable |
| 130 | + txBuilder.bounceable(true); |
| 131 | + |
| 132 | + const tx = await txBuilder.build(); |
| 133 | + should.equal(tx.type, TransactionType.TonWhalesDeposit); |
| 134 | + should.equal(tx.toJson().bounceable, true); |
| 135 | + |
| 136 | + // Round trip verification |
| 137 | + const builder2 = factory.from(tx.toBroadcastFormat()); |
| 138 | + const tx2 = await builder2.build(); |
| 139 | + should.equal(tx2.toJson().bounceable, true); |
| 140 | + }); |
| 141 | +}); |
0 commit comments