|
| 1 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 2 | +import should from 'should'; |
| 3 | +import { decodeTransferData } from '@bitgo/abstract-eth'; |
| 4 | +import * as testData from '../../resources'; |
| 5 | +import { getBuilder } from '../../getBuilder'; |
| 6 | +import { TransactionBuilder } from '../../../src'; |
| 7 | + |
| 8 | +describe('ZKeth transaction builder send', () => { |
| 9 | + describe('should sign and build', () => { |
| 10 | + let txBuilder; |
| 11 | + let key; |
| 12 | + let contractAddress; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + contractAddress = '0x8f977e912ef500548a0c3be6ddde9899f1199b81'; |
| 16 | + txBuilder = getBuilder('tzketh') as TransactionBuilder; |
| 17 | + key = testData.KEYPAIR_PRV.getKeys().prv as string; |
| 18 | + txBuilder.fee({ |
| 19 | + fee: '1000000000', |
| 20 | + gasLimit: '12100000', |
| 21 | + }); |
| 22 | + txBuilder.counter(2); |
| 23 | + txBuilder.type(TransactionType.Send); |
| 24 | + txBuilder.contract(contractAddress); |
| 25 | + }); |
| 26 | + |
| 27 | + it('a send funds transaction', async () => { |
| 28 | + const recipient = '0x19645032c7f1533395d44a629462e751084d3e4c'; |
| 29 | + const amount = '1000000000'; |
| 30 | + const expireTime = 1590066728; |
| 31 | + const sequenceId = 5; |
| 32 | + txBuilder |
| 33 | + .transfer() |
| 34 | + .amount(amount) |
| 35 | + .to(recipient) |
| 36 | + .expirationTime(expireTime) |
| 37 | + .contractSequenceId(sequenceId) |
| 38 | + .key(key); |
| 39 | + txBuilder.sign({ key: testData.PRIVATE_KEY_1 }); |
| 40 | + const tx = await txBuilder.build(); |
| 41 | + |
| 42 | + should.equal(tx.toJson().chainId, '0x12c'); |
| 43 | + should.equal(tx.toBroadcastFormat(), testData.SEND_TX_BROADCAST_LEGACY); |
| 44 | + should.equal(tx.signature.length, 2); |
| 45 | + should.equal(tx.inputs.length, 1); |
| 46 | + should.equal(tx.inputs[0].address, contractAddress); |
| 47 | + should.equal(tx.inputs[0].value, amount); |
| 48 | + |
| 49 | + should.equal(tx.outputs.length, 1); |
| 50 | + should.equal(tx.outputs[0].address, recipient); |
| 51 | + should.equal(tx.outputs[0].value, amount); |
| 52 | + |
| 53 | + const data = tx.toJson().data; |
| 54 | + const { |
| 55 | + to, |
| 56 | + amount: parsedAmount, |
| 57 | + expireTime: parsedExpireTime, |
| 58 | + sequenceId: parsedSequenceId, |
| 59 | + } = decodeTransferData(data); |
| 60 | + should.equal(to, recipient); |
| 61 | + should.equal(parsedAmount, amount); |
| 62 | + should.equal(parsedExpireTime, expireTime); |
| 63 | + should.equal(parsedSequenceId, sequenceId); |
| 64 | + }); |
| 65 | + |
| 66 | + it('a send funds with amount 0 transaction', async () => { |
| 67 | + txBuilder |
| 68 | + .transfer() |
| 69 | + .amount('0') |
| 70 | + .to('0x19645032c7f1533395d44a629462e751084d3e4c') |
| 71 | + .expirationTime(1590066728) |
| 72 | + .contractSequenceId(5) |
| 73 | + .key(key); |
| 74 | + txBuilder.sign({ key: testData.PRIVATE_KEY_1 }); |
| 75 | + const tx = await txBuilder.build(); |
| 76 | + should.equal(tx.toBroadcastFormat(), testData.SEND_TX_AMOUNT_ZERO_BROADCAST); |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments