|
| 1 | +import { getBuilderFactory } from '../getBuilderFactory'; |
| 2 | +import { coins } from '@bitgo/statics'; |
| 3 | +import * as testData from '../../resources/apt'; |
| 4 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 5 | +import should from 'should'; |
| 6 | +import { DelegationPoolWithdrawTransaction } from '../../../src/lib/transaction/delegationPoolWithdrawTransaction'; |
| 7 | + |
| 8 | +describe('Apt Delegation Pool Withdraw Builder', () => { |
| 9 | + const factory = getBuilderFactory('tapt'); |
| 10 | + |
| 11 | + describe('Succeed', () => { |
| 12 | + it('should build a staking withdraw transaction', async function () { |
| 13 | + const transaction = new DelegationPoolWithdrawTransaction(coins.get('tapt')); |
| 14 | + const txBuilder = factory.getDelegationPoolWithdrawTransactionBuilder(transaction); |
| 15 | + txBuilder.sender(testData.sender.address); |
| 16 | + txBuilder.validator(testData.delegationPoolData.validatorAddress, testData.delegationPoolData.amount); |
| 17 | + txBuilder.gasData({ |
| 18 | + maxGasAmount: 200000, |
| 19 | + gasUnitPrice: 100, |
| 20 | + }); |
| 21 | + txBuilder.sequenceNumber(14); |
| 22 | + txBuilder.expirationTime(1736246155); |
| 23 | + txBuilder.addFeePayerAddress(testData.feePayer.address); |
| 24 | + const tx = (await txBuilder.build()) as DelegationPoolWithdrawTransaction; |
| 25 | + should.equal(tx.sender, testData.sender.address); |
| 26 | + should.deepEqual(tx.recipients, []); |
| 27 | + should.equal(tx.validatorAddress, testData.delegationPoolData.validatorAddress); |
| 28 | + should.equal(tx.amount, testData.delegationPoolData.amount); |
| 29 | + should.equal(tx.maxGasAmount, 200000); |
| 30 | + should.equal(tx.gasUnitPrice, 100); |
| 31 | + should.equal(tx.sequenceNumber, 14); |
| 32 | + should.equal(tx.expirationTime, 1736246155); |
| 33 | + should.equal(tx.type, TransactionType.StakingWithdraw); |
| 34 | + should.deepEqual(tx.inputs, [ |
| 35 | + { |
| 36 | + address: testData.delegationPoolData.validatorAddress, |
| 37 | + value: testData.delegationPoolData.amount, |
| 38 | + coin: 'tapt', |
| 39 | + }, |
| 40 | + ]); |
| 41 | + should.deepEqual(tx.outputs, [ |
| 42 | + { |
| 43 | + address: testData.sender.address, |
| 44 | + value: testData.delegationPoolData.amount, |
| 45 | + coin: 'tapt', |
| 46 | + }, |
| 47 | + ]); |
| 48 | + const rawTx = tx.toBroadcastFormat(); |
| 49 | + should.equal(txBuilder.isValidRawTransaction(rawTx), true); |
| 50 | + rawTx.should.equal(testData.DELEGATION_POOL_WITHDRAW_TX_HEX); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should build and send a signed tx', async function () { |
| 54 | + const txBuilder = factory.from(testData.DELEGATION_POOL_WITHDRAW_TX_HEX); |
| 55 | + const tx = (await txBuilder.build()) as DelegationPoolWithdrawTransaction; |
| 56 | + tx.inputs.should.deepEqual([ |
| 57 | + { |
| 58 | + address: testData.delegationPoolData.validatorAddress, |
| 59 | + value: testData.delegationPoolData.amount, |
| 60 | + coin: 'tapt', |
| 61 | + }, |
| 62 | + ]); |
| 63 | + tx.outputs.should.deepEqual([ |
| 64 | + { |
| 65 | + address: testData.sender.address, |
| 66 | + value: testData.delegationPoolData.amount, |
| 67 | + coin: 'tapt', |
| 68 | + }, |
| 69 | + ]); |
| 70 | + should.equal(tx.id, '0xd795391e85ffd5e37b844db4206c5bd99ba28a42430df996969ee9b7f16a5f21'); |
| 71 | + should.equal(tx.maxGasAmount, 200000); |
| 72 | + should.equal(tx.gasUnitPrice, 100); |
| 73 | + should.equal(tx.sequenceNumber, 14); |
| 74 | + should.equal(tx.expirationTime, 1736246155); |
| 75 | + should.equal(tx.type, TransactionType.StakingWithdraw); |
| 76 | + const rawTx = tx.toBroadcastFormat(); |
| 77 | + should.equal(txBuilder.isValidRawTransaction(rawTx), true); |
| 78 | + should.equal(rawTx, testData.DELEGATION_POOL_WITHDRAW_TX_HEX); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should succeed to validate a valid signablePayload', async function () { |
| 82 | + const transaction = new DelegationPoolWithdrawTransaction(coins.get('tapt')); |
| 83 | + const txBuilder = factory.getDelegationPoolWithdrawTransactionBuilder(transaction); |
| 84 | + txBuilder.sender(testData.sender.address); |
| 85 | + txBuilder.validator(testData.delegationPoolData.validatorAddress, testData.delegationPoolData.amount); |
| 86 | + txBuilder.gasData({ |
| 87 | + maxGasAmount: 200000, |
| 88 | + gasUnitPrice: 100, |
| 89 | + }); |
| 90 | + txBuilder.sequenceNumber(14); |
| 91 | + txBuilder.expirationTime(1736246155); |
| 92 | + txBuilder.addFeePayerAddress(testData.feePayer.address); |
| 93 | + const tx = (await txBuilder.build()) as DelegationPoolWithdrawTransaction; |
| 94 | + const signablePayload = tx.signablePayload; |
| 95 | + should.equal(signablePayload.toString('hex'), testData.DELEGATION_POOL_WITHDRAW_TX_HEX_SIGNABLE_PAYLOAD); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should build a unsigned tx and validate its toJson', async function () { |
| 99 | + const transaction = new DelegationPoolWithdrawTransaction(coins.get('tapt')); |
| 100 | + const txBuilder = factory.getDelegationPoolWithdrawTransactionBuilder(transaction); |
| 101 | + txBuilder.sender(testData.sender.address); |
| 102 | + txBuilder.validator(testData.delegationPoolData.validatorAddress, testData.delegationPoolData.amount); |
| 103 | + txBuilder.gasData({ |
| 104 | + maxGasAmount: 200000, |
| 105 | + gasUnitPrice: 100, |
| 106 | + }); |
| 107 | + txBuilder.sequenceNumber(14); |
| 108 | + txBuilder.expirationTime(1736246155); |
| 109 | + txBuilder.assetId(testData.fungibleTokenAddress.usdt); |
| 110 | + txBuilder.addFeePayerAddress(testData.feePayer.address); |
| 111 | + const tx = (await txBuilder.build()) as DelegationPoolWithdrawTransaction; |
| 112 | + const toJson = tx.toJson(); |
| 113 | + should.equal(toJson.sender, testData.sender.address); |
| 114 | + should.deepEqual(toJson.recipients, []); |
| 115 | + should.equal(toJson.validatorAddress, testData.delegationPoolData.validatorAddress); |
| 116 | + should.equal(toJson.amount, testData.delegationPoolData.amount); |
| 117 | + should.equal(toJson.sequenceNumber, 14); |
| 118 | + should.equal(toJson.maxGasAmount, 200000); |
| 119 | + should.equal(toJson.gasUnitPrice, 100); |
| 120 | + should.equal(toJson.expirationTime, 1736246155); |
| 121 | + should.equal(toJson.feePayer, testData.feePayer.address); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should build a signed tx and validate its toJson', async function () { |
| 125 | + const txBuilder = factory.from(testData.DELEGATION_POOL_WITHDRAW_TX_HEX); |
| 126 | + const tx = (await txBuilder.build()) as DelegationPoolWithdrawTransaction; |
| 127 | + const toJson = tx.toJson(); |
| 128 | + should.equal(toJson.id, '0xd795391e85ffd5e37b844db4206c5bd99ba28a42430df996969ee9b7f16a5f21'); |
| 129 | + should.equal(toJson.sender, testData.sender.address); |
| 130 | + should.deepEqual(toJson.recipients, []); |
| 131 | + should.equal(toJson.validatorAddress, testData.delegationPoolData.validatorAddress); |
| 132 | + should.equal(toJson.amount, testData.delegationPoolData.amount); |
| 133 | + should.equal(toJson.maxGasAmount, 200000); |
| 134 | + should.equal(toJson.gasUnitPrice, 100); |
| 135 | + should.equal(toJson.sequenceNumber, 14); |
| 136 | + should.equal(toJson.expirationTime, 1736246155); |
| 137 | + }); |
| 138 | + }); |
| 139 | +}); |
0 commit comments