|
| 1 | +import should from 'should'; |
| 2 | +import * as testData from '../../resources/near'; |
| 3 | +import { getBuilderFactory } from '../getBuilderFactory'; |
| 4 | +import { TransactionType } from '@bitgo/sdk-core'; |
| 5 | +import { metaPoolContractAddress } from '../../resources/near'; |
| 6 | + |
| 7 | +describe('Near Meta Pool Withdraw Builder', () => { |
| 8 | + const factory = getBuilderFactory('tnear'); |
| 9 | + const gas = '125000000000000'; |
| 10 | + |
| 11 | + describe('Succeed', () => { |
| 12 | + it('build a meta pool withdraw_all signed tx', async () => { |
| 13 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 14 | + txBuilder |
| 15 | + .gas(gas) |
| 16 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 17 | + .receiverId(metaPoolContractAddress) |
| 18 | + .recentBlockHash(testData.blockHash.block1) |
| 19 | + .nonce(BigInt(1)); |
| 20 | + txBuilder.sign({ key: testData.accounts.account1.secretKey }); |
| 21 | + const tx = await txBuilder.build(); |
| 22 | + tx.inputs.length.should.equal(0); |
| 23 | + tx.outputs.length.should.equal(0); |
| 24 | + should.equal(tx.type, TransactionType.StakingWithdraw); |
| 25 | + const txJson = tx.toJson(); |
| 26 | + txJson.should.have.properties(['id', 'signerId', 'publicKey', 'nonce', 'actions', 'signature']); |
| 27 | + txJson.signerId.should.equal(testData.accounts.account1.address); |
| 28 | + txJson.publicKey.should.equal(testData.accounts.account1.publicKeyBase58); |
| 29 | + txJson.nonce.should.equal(BigInt(1)); |
| 30 | + txJson.receiverId.should.equal(metaPoolContractAddress); |
| 31 | + txJson.actions.should.deepEqual([ |
| 32 | + { |
| 33 | + functionCall: { |
| 34 | + methodName: 'withdraw_all', |
| 35 | + args: {}, |
| 36 | + gas: '125000000000000', |
| 37 | + deposit: '0', |
| 38 | + }, |
| 39 | + }, |
| 40 | + ]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('build a meta pool withdraw_all unsigned tx', async () => { |
| 44 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 45 | + txBuilder |
| 46 | + .gas(gas) |
| 47 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 48 | + .receiverId(metaPoolContractAddress) |
| 49 | + .recentBlockHash(testData.blockHash.block1) |
| 50 | + .nonce(BigInt(1)); |
| 51 | + const tx = await txBuilder.build(); |
| 52 | + tx.inputs.length.should.equal(0); |
| 53 | + tx.outputs.length.should.equal(0); |
| 54 | + should.equal(tx.type, TransactionType.StakingWithdraw); |
| 55 | + const explainTx = tx.explainTransaction(); |
| 56 | + explainTx.outputAmount.should.equal('0'); |
| 57 | + explainTx.outputs[0].amount.should.equal('0'); |
| 58 | + explainTx.outputs[0].address.should.equal(testData.accounts.account1.address); |
| 59 | + }); |
| 60 | + |
| 61 | + it('build from a raw unsigned meta pool withdraw_all tx (round-trip)', async () => { |
| 62 | + // Build the original transaction |
| 63 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 64 | + txBuilder |
| 65 | + .gas(gas) |
| 66 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 67 | + .receiverId(metaPoolContractAddress) |
| 68 | + .recentBlockHash(testData.blockHash.block1) |
| 69 | + .nonce(BigInt(1)); |
| 70 | + const originalTx = await txBuilder.build(); |
| 71 | + const rawTx = originalTx.toBroadcastFormat(); |
| 72 | + |
| 73 | + // Reconstruct from raw |
| 74 | + const rebuiltBuilder = factory.from(rawTx); |
| 75 | + const rebuiltTx = await rebuiltBuilder.build(); |
| 76 | + const rebuiltJson = rebuiltTx.toJson(); |
| 77 | + |
| 78 | + rebuiltJson.signerId.should.equal(testData.accounts.account1.address); |
| 79 | + rebuiltJson.receiverId.should.equal(metaPoolContractAddress); |
| 80 | + rebuiltJson.actions.should.deepEqual([ |
| 81 | + { |
| 82 | + functionCall: { |
| 83 | + methodName: 'withdraw_all', |
| 84 | + args: {}, |
| 85 | + gas: '125000000000000', |
| 86 | + deposit: '0', |
| 87 | + }, |
| 88 | + }, |
| 89 | + ]); |
| 90 | + should.equal(rebuiltTx.type, TransactionType.StakingWithdraw); |
| 91 | + rebuiltTx.id.should.equal(originalTx.id); |
| 92 | + }); |
| 93 | + |
| 94 | + it('build from a raw signed meta pool withdraw_all tx (round-trip)', async () => { |
| 95 | + // Build the original signed transaction |
| 96 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 97 | + txBuilder |
| 98 | + .gas(gas) |
| 99 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 100 | + .receiverId(metaPoolContractAddress) |
| 101 | + .recentBlockHash(testData.blockHash.block1) |
| 102 | + .nonce(BigInt(1)); |
| 103 | + txBuilder.sign({ key: testData.accounts.account1.secretKey }); |
| 104 | + const originalTx = await txBuilder.build(); |
| 105 | + const rawTx = originalTx.toBroadcastFormat(); |
| 106 | + |
| 107 | + // Reconstruct from raw |
| 108 | + const rebuiltBuilder = factory.from(rawTx); |
| 109 | + const rebuiltTx = await rebuiltBuilder.build(); |
| 110 | + const rebuiltJson = rebuiltTx.toJson(); |
| 111 | + |
| 112 | + rebuiltJson.signerId.should.equal(testData.accounts.account1.address); |
| 113 | + rebuiltJson.receiverId.should.equal(metaPoolContractAddress); |
| 114 | + rebuiltJson.actions.should.deepEqual([ |
| 115 | + { |
| 116 | + functionCall: { |
| 117 | + methodName: 'withdraw_all', |
| 118 | + args: {}, |
| 119 | + gas: '125000000000000', |
| 120 | + deposit: '0', |
| 121 | + }, |
| 122 | + }, |
| 123 | + ]); |
| 124 | + should.equal(rebuiltTx.type, TransactionType.StakingWithdraw); |
| 125 | + rebuiltTx.id.should.equal(originalTx.id); |
| 126 | + rebuiltJson.should.have.property('signature'); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('Fail', () => { |
| 131 | + it('meta pool withdraw with missing gas', async () => { |
| 132 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 133 | + txBuilder |
| 134 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 135 | + .receiverId(metaPoolContractAddress) |
| 136 | + .recentBlockHash(testData.blockHash.block1) |
| 137 | + .nonce(BigInt(1)); |
| 138 | + await txBuilder.build().should.be.rejectedWith('gas is required before building staking withdraw'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('meta pool withdraw rejects amount()', () => { |
| 142 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 143 | + should(() => txBuilder.amount('1000000')).throw('amount is not applicable for withdraw_all'); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('Routing', () => { |
| 148 | + it('factory.from routes withdraw to StakingWithdrawBuilder', async () => { |
| 149 | + // Build a native withdraw tx |
| 150 | + const txBuilder = factory.getStakingWithdrawBuilder(); |
| 151 | + txBuilder |
| 152 | + .amount('1000000') |
| 153 | + .gas(gas) |
| 154 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 155 | + .receiverId(testData.validatorContractAddress) |
| 156 | + .recentBlockHash(testData.blockHash.block1) |
| 157 | + .nonce(BigInt(1)); |
| 158 | + const tx = await txBuilder.build(); |
| 159 | + const rawTx = tx.toBroadcastFormat(); |
| 160 | + |
| 161 | + // Verify from() routes to StakingWithdrawBuilder (not MetaPoolWithdrawBuilder) |
| 162 | + const rebuiltBuilder = factory.from(rawTx); |
| 163 | + const rebuiltTx = await rebuiltBuilder.build(); |
| 164 | + const rebuiltJson = rebuiltTx.toJson(); |
| 165 | + rebuiltJson.actions[0].functionCall!.methodName.should.equal('withdraw'); |
| 166 | + should.equal(rebuiltTx.type, TransactionType.StakingWithdraw); |
| 167 | + }); |
| 168 | + |
| 169 | + it('factory.from routes withdraw_all to MetaPoolWithdrawBuilder', async () => { |
| 170 | + // Build a meta pool withdraw_all tx |
| 171 | + const txBuilder = factory.getMetaPoolWithdrawBuilder(); |
| 172 | + txBuilder |
| 173 | + .gas(gas) |
| 174 | + .sender(testData.accounts.account1.address, testData.accounts.account1.publicKey) |
| 175 | + .receiverId(metaPoolContractAddress) |
| 176 | + .recentBlockHash(testData.blockHash.block1) |
| 177 | + .nonce(BigInt(1)); |
| 178 | + const tx = await txBuilder.build(); |
| 179 | + const rawTx = tx.toBroadcastFormat(); |
| 180 | + |
| 181 | + // Verify from() routes to MetaPoolWithdrawBuilder |
| 182 | + const rebuiltBuilder = factory.from(rawTx); |
| 183 | + const rebuiltTx = await rebuiltBuilder.build(); |
| 184 | + const rebuiltJson = rebuiltTx.toJson(); |
| 185 | + rebuiltJson.actions[0].functionCall!.methodName.should.equal('withdraw_all'); |
| 186 | + should.equal(rebuiltTx.type, TransactionType.StakingWithdraw); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments