|
| 1 | +import sinon from 'sinon'; |
| 2 | +import 'should'; |
| 3 | +import { BuildTokenApprovalResponse, Wallet } from '../../../../src'; |
| 4 | + |
| 5 | +describe('Wallet - Token Approval', function () { |
| 6 | + let wallet: Wallet; |
| 7 | + let mockBitGo: any; |
| 8 | + let mockBaseCoin: any; |
| 9 | + let mockWalletData: any; |
| 10 | + |
| 11 | + beforeEach(function () { |
| 12 | + mockBitGo = { |
| 13 | + post: sinon.stub(), |
| 14 | + get: sinon.stub(), |
| 15 | + setRequestTracer: sinon.stub(), |
| 16 | + }; |
| 17 | + |
| 18 | + mockBaseCoin = { |
| 19 | + getFamily: sinon.stub().returns('eth'), |
| 20 | + url: sinon.stub(), |
| 21 | + keychains: sinon.stub(), |
| 22 | + supportsTss: sinon.stub().returns(false), |
| 23 | + getMPCAlgorithm: sinon.stub(), |
| 24 | + }; |
| 25 | + |
| 26 | + mockWalletData = { |
| 27 | + id: 'test-wallet-id', |
| 28 | + coin: 'teth', |
| 29 | + keys: ['user-key', 'backup-key', 'bitgo-key'], |
| 30 | + }; |
| 31 | + |
| 32 | + wallet = new Wallet(mockBitGo, mockBaseCoin, mockWalletData); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(function () { |
| 36 | + sinon.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('buildErc20TokenApproval', function () { |
| 40 | + const mockTokenApprovalBuild: BuildTokenApprovalResponse = { |
| 41 | + txHex: '0x123456', |
| 42 | + txInfo: { |
| 43 | + amount: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', |
| 44 | + contractAddress: '0x1234567890123456789012345678901234567890', |
| 45 | + spender: '0x0987654321098765432109876543210987654321', |
| 46 | + }, |
| 47 | + recipients: [ |
| 48 | + { |
| 49 | + address: '0x0987654321098765432109876543210987654321', |
| 50 | + amount: '0', |
| 51 | + data: '0x095ea7b30000000000000000000000000987654321098765432109876543210987654321ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', |
| 52 | + }, |
| 53 | + ], |
| 54 | + eip1559: { |
| 55 | + maxFeePerGas: '0x3b9aca00', |
| 56 | + maxPriorityFeePerGas: '0x3b9aca00', |
| 57 | + }, |
| 58 | + nextContractSequenceId: 0, |
| 59 | + coin: 'teth', |
| 60 | + walletId: 'test-wallet-id', |
| 61 | + }; |
| 62 | + |
| 63 | + it('should build token approval transaction without signing', async function () { |
| 64 | + mockBaseCoin.url.returns('/test/wallet/token/approval/build'); |
| 65 | + mockBitGo.post.returns({ |
| 66 | + send: sinon.stub().returns({ |
| 67 | + result: sinon.stub().resolves(mockTokenApprovalBuild), |
| 68 | + }), |
| 69 | + }); |
| 70 | + |
| 71 | + const result = await wallet.buildErc20TokenApproval('USDC'); |
| 72 | + |
| 73 | + result.should.eql(mockTokenApprovalBuild); |
| 74 | + sinon.assert.calledWith(mockBaseCoin.url, '/wallet/test-wallet-id/token/approval/build'); |
| 75 | + sinon.assert.calledOnce(mockBitGo.post); |
| 76 | + sinon.assert.calledOnce(mockBitGo.setRequestTracer); |
| 77 | + const postRequest = mockBitGo.post.getCall(0); |
| 78 | + const sendCall = postRequest.returnValue.send.getCall(0); |
| 79 | + sendCall.args[0].should.eql({ tokenName: 'USDC' }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should throw error if token build request fails', async function () { |
| 83 | + mockBaseCoin.url.returns('/test/wallet/token/approval/build'); |
| 84 | + mockBitGo.post.returns({ |
| 85 | + send: sinon.stub().returns({ |
| 86 | + result: sinon.stub().rejects(new Error('token not supported')), |
| 87 | + }), |
| 88 | + }); |
| 89 | + |
| 90 | + await wallet |
| 91 | + .buildErc20TokenApproval('INVALID_TOKEN') |
| 92 | + .should.be.rejectedWith(/error building erc20 token approval tx: Error: token not supported/); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should build, sign, and send token approval transaction when passphrase is provided', async function () { |
| 96 | + mockBaseCoin.url.returns('/test/wallet/token/approval/build'); |
| 97 | + mockBitGo.post.returns({ |
| 98 | + send: sinon.stub().returns({ |
| 99 | + result: sinon.stub().resolves(mockTokenApprovalBuild), |
| 100 | + }), |
| 101 | + }); |
| 102 | + |
| 103 | + const mockKeychain = { id: 'user-key', pub: 'pub-key', encryptedPrv: 'encrypted-prv' }; |
| 104 | + mockBaseCoin.keychains.returns({ |
| 105 | + get: sinon.stub().resolves(mockKeychain), |
| 106 | + }); |
| 107 | + |
| 108 | + const signTransactionStub = sinon.stub(wallet, 'signTransaction' as keyof Wallet).resolves({ txHex: '0xsigned' }); |
| 109 | + const sendTransactionStub = sinon.stub(wallet, 'sendTransaction' as keyof Wallet).resolves({ txid: '0xtxid' }); |
| 110 | + const getKeychainsStub = sinon.stub(wallet as any, 'getKeychainsAndValidatePassphrase').resolves([mockKeychain]); |
| 111 | + |
| 112 | + const result = await wallet.buildErc20TokenApproval('USDC', 'passphrase123'); |
| 113 | + |
| 114 | + result.should.have.property('txid', '0xtxid'); |
| 115 | + |
| 116 | + sinon.assert.calledOnce(getKeychainsStub); |
| 117 | + getKeychainsStub.getCall(0).args[0].should.have.property('walletPassphrase', 'passphrase123'); |
| 118 | + |
| 119 | + sinon.assert.calledOnce(signTransactionStub); |
| 120 | + const signCall = signTransactionStub.getCall(0); |
| 121 | + if (signCall && signCall.args[0]) { |
| 122 | + signCall.args[0].should.have.property('txPrebuild', mockTokenApprovalBuild); |
| 123 | + signCall.args[0].should.have.property('keychain', mockKeychain); |
| 124 | + signCall.args[0].should.have.property('walletPassphrase', 'passphrase123'); |
| 125 | + } |
| 126 | + |
| 127 | + sinon.assert.calledOnce(sendTransactionStub); |
| 128 | + const sendCall = sendTransactionStub.getCall(0); |
| 129 | + if (sendCall && sendCall.args[0]) { |
| 130 | + sendCall.args[0].should.have.property('txHex', '0xsigned'); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle signing errors', async function () { |
| 135 | + mockBaseCoin.url.returns('/test/wallet/token/approval/build'); |
| 136 | + mockBitGo.post.returns({ |
| 137 | + send: sinon.stub().returns({ |
| 138 | + result: sinon.stub().resolves(mockTokenApprovalBuild), |
| 139 | + }), |
| 140 | + }); |
| 141 | + |
| 142 | + const mockKeychain = { id: 'user-key', pub: 'pub-key', encryptedPrv: 'encrypted-prv' }; |
| 143 | + mockBaseCoin.keychains.returns({ |
| 144 | + get: sinon.stub().resolves(mockKeychain), |
| 145 | + }); |
| 146 | + |
| 147 | + sinon.stub(wallet as any, 'getKeychainsAndValidatePassphrase').resolves([mockKeychain]); |
| 148 | + sinon.stub(wallet, 'signTransaction' as keyof Wallet).rejects(new Error('signing error')); |
| 149 | + |
| 150 | + await wallet.buildErc20TokenApproval('USDC', 'passphrase123').should.be.rejectedWith('signing error'); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments