|
| 1 | +import { TransactionBuilderFactory } from '../../src'; |
| 2 | +import { coins } from '@bitgo/statics'; |
| 3 | +import * as testData from '../resources/vet'; |
| 4 | +import should from 'should'; |
| 5 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 6 | + |
| 7 | +import { FlushTokenTransaction } from '../../src/lib/transaction/flushTokenTransaction'; |
| 8 | + |
| 9 | +describe('Flush Token Transaction', () => { |
| 10 | + const factory = new TransactionBuilderFactory(coins.get('tvet')); |
| 11 | + |
| 12 | + describe('Succeed', () => { |
| 13 | + it('should build a flush token transaction', async function () { |
| 14 | + const transaction = new FlushTokenTransaction(coins.get('tvet')); |
| 15 | + const txBuilder = factory.getFlushTokenTransactionBuilder(transaction); |
| 16 | + txBuilder.gas(21000); |
| 17 | + txBuilder.nonce(64248); |
| 18 | + txBuilder.blockRef('0x014ead140e77bbc1'); |
| 19 | + txBuilder.expiration(64); |
| 20 | + txBuilder.gasPriceCoef(128); |
| 21 | + txBuilder.contract(testData.FORWARDER_ADDRESS); |
| 22 | + txBuilder.tokenAddress(testData.TOKEN_ADDRESS); |
| 23 | + txBuilder.forwarderVersion(4); |
| 24 | + const tx = (await txBuilder.build()) as FlushTokenTransaction; |
| 25 | + should.equal(tx.gas, 21000); |
| 26 | + should.equal(tx.nonce, 64248); |
| 27 | + should.equal(tx.expiration, 64); |
| 28 | + should.equal(tx.type, TransactionType.FlushTokens); |
| 29 | + should.equal(tx.blockRef, '0x014ead140e77bbc1'); |
| 30 | + should.equal(tx.clauses.length, 1); |
| 31 | + should.equal(tx.clauses[0].to, testData.FORWARDER_ADDRESS); |
| 32 | + should.equal(tx.clauses[0].data, testData.FLUSH_TOKEN_DATA); |
| 33 | + should.equal(tx.clauses[0].value, '0x0'); |
| 34 | + const rawTx = tx.toBroadcastFormat(); |
| 35 | + should.equal(txBuilder.isValidRawTransaction(rawTx), true); |
| 36 | + rawTx.should.equal(testData.FLUSH_TOKEN_TRANSACTION); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should succeed to validate a valid signablePayload', async function () { |
| 40 | + const transaction = new FlushTokenTransaction(coins.get('tvet')); |
| 41 | + const txBuilder = factory.getFlushTokenTransactionBuilder(transaction); |
| 42 | + txBuilder.gas(21000); |
| 43 | + txBuilder.nonce(64248); |
| 44 | + txBuilder.blockRef('0x014ead140e77bbc1'); |
| 45 | + txBuilder.expiration(64); |
| 46 | + txBuilder.gasPriceCoef(128); |
| 47 | + txBuilder.contract(testData.FORWARDER_ADDRESS); |
| 48 | + txBuilder.tokenAddress(testData.TOKEN_ADDRESS); |
| 49 | + txBuilder.forwarderVersion(4); |
| 50 | + const tx = (await txBuilder.build()) as FlushTokenTransaction; |
| 51 | + const signablePayload = tx.signablePayload; |
| 52 | + should.equal(signablePayload.toString('hex'), testData.FLUSH_TOKEN_SIGNABLE_PAYLOAD); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should build a unsigned tx and validate its toJson', async function () { |
| 56 | + const transaction = new FlushTokenTransaction(coins.get('tvet')); |
| 57 | + const txBuilder = factory.getFlushTokenTransactionBuilder(transaction); |
| 58 | + txBuilder.gas(21000); |
| 59 | + txBuilder.nonce(64248); |
| 60 | + txBuilder.blockRef('0x014ead140e77bbc1'); |
| 61 | + txBuilder.expiration(64); |
| 62 | + txBuilder.gasPriceCoef(128); |
| 63 | + txBuilder.contract(testData.FORWARDER_ADDRESS); |
| 64 | + txBuilder.tokenAddress(testData.TOKEN_ADDRESS); |
| 65 | + txBuilder.forwarderVersion(4); |
| 66 | + const tx = (await txBuilder.build()) as FlushTokenTransaction; |
| 67 | + |
| 68 | + const toJson = tx.toJson(); |
| 69 | + should.equal(toJson.nonce, 64248); |
| 70 | + should.equal(toJson.gas, 21000); |
| 71 | + should.equal(toJson.gasPriceCoef, 128); |
| 72 | + should.equal(toJson.expiration, 64); |
| 73 | + should.equal(toJson.data, testData.FLUSH_TOKEN_DATA); |
| 74 | + should.equal(toJson.to, testData.FORWARDER_ADDRESS); |
| 75 | + should.equal(toJson.tokenAddress, testData.TOKEN_ADDRESS); |
| 76 | + should.equal(toJson.value, '0'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('Fail', () => { |
| 81 | + it('should fail if invalid params are used to build a tx', async function () { |
| 82 | + const transaction = new FlushTokenTransaction(coins.get('tvet')); |
| 83 | + const txBuilder = factory.getFlushTokenTransactionBuilder(transaction); |
| 84 | + |
| 85 | + should(() => txBuilder.tokenAddress('randomString')).throwError('Invalid address randomString'); |
| 86 | + should(() => txBuilder.contract('randomString')).throwError('Invalid address randomString'); |
| 87 | + should(() => txBuilder.forwarderVersion(3)).throwError('Invalid forwarder version: 3'); |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments