diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 78336846..cf4f7cc9 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -11,11 +11,9 @@ const { sendPegin, createSenderRecipientInfo, createExpectedPeginBtcEvent, get2wpBalances, - mineForPeginRegistration, } = require('../2wp-utils'); -const { getBtcAddressBalanceInSatoshis, waitForBitcoinMempoolToGetTxs } = require('../btc-utils'); +const { getBtcAddressBalanceInSatoshis } = require('../btc-utils'); const { ensure0x } = require('../utils'); -const bitcoinJsLib = require('bitcoinjs-lib'); let btcTxHelper; let rskTxHelper; @@ -107,74 +105,6 @@ const execute = (description, getRskHost) => { }); - it('should do legacy pegin with multiple inputs from different accounts and one output to the federation with value exactly minimum', async () => { - - // Arrange - - const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); - const senderRecipientInfo1 = await createSenderRecipientInfo(rskTxHelper, btcTxHelper); - const senderRecipientInfo2 = await createSenderRecipientInfo(rskTxHelper, btcTxHelper); - const initialSender1AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo1.btcSenderAddressInfo.address); - const initialSender2AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo2.btcSenderAddressInfo.address); - - const sender1PeginValueInSatoshis = minimumPeginValueInSatoshis; - const sender2PeginValueInSatoshis = minimumPeginValueInSatoshis; - const peginValueInSatoshis = sender1PeginValueInSatoshis + sender2PeginValueInSatoshis; - - const sender1UtxosInfo = await btcTxHelper.selectSpendableUTXOsFromAddress(senderRecipientInfo1.btcSenderAddressInfo.address, satoshisToBtc(sender1PeginValueInSatoshis)); - const sender2UtxosInfo = await btcTxHelper.selectSpendableUTXOsFromAddress(senderRecipientInfo2.btcSenderAddressInfo.address, satoshisToBtc(sender2PeginValueInSatoshis)); - - const sender1ChangeInSatoshis = btcToSatoshis(sender1UtxosInfo.change); - const sender2ChangeInSatoshis = btcToSatoshis(sender2UtxosInfo.change); - - const tx = new bitcoinJsLib.Transaction(); - - // Adding inputs - addInputs(tx, sender1UtxosInfo.utxos); - addInputs(tx, sender2UtxosInfo.utxos); - - // Adding output to federation - addOutput(tx, federationAddress, peginValueInSatoshis); - - // Adding change outputs - addOutput(tx, senderRecipientInfo1.btcSenderAddressInfo.address, sender1ChangeInSatoshis - btcFeeInSatoshis); - addOutput(tx, senderRecipientInfo2.btcSenderAddressInfo.address, sender2ChangeInSatoshis - btcFeeInSatoshis); - - const sendersPrivateKeys = [senderRecipientInfo1.btcSenderAddressInfo.privateKey, senderRecipientInfo2.btcSenderAddressInfo.privateKey] - const signedTx = await btcTxHelper.nodeClient.signTransaction(tx.toHex(), [], sendersPrivateKeys); - - // Act - - // Sending the pegin and ensuring the pegin is registered - const btcPeginTxHash = await btcTxHelper.nodeClient.sendTransaction(signedTx); - - // Assert - - // Since we are not using `sendPegin` here, we need to do some extra steps before ensuring the pegin is registered. - await ensurePeginIsPushed(btcPeginTxHash); - - await assertExpectedPeginBtcEventIsEmitted(btcPeginTxHash, senderRecipientInfo1.rskRecipientRskAddressInfo.address, peginValueInSatoshis); - - await assert2wpBalancesAfterSuccessfulPegin(initial2wpBalances, peginValueInSatoshis); - - // The senders should have their balances reduced by the amount sent to the federation and the fee. - const finalSender1AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo1.btcSenderAddressInfo.address); - expect(finalSender1AddressBalanceInSatoshis).to.be.equal(initialSender1AddressBalanceInSatoshis - sender1PeginValueInSatoshis - btcFeeInSatoshis); - - const finalSender2AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo2.btcSenderAddressInfo.address); - expect(finalSender2AddressBalanceInSatoshis).to.be.equal(initialSender2AddressBalanceInSatoshis - sender2PeginValueInSatoshis - btcFeeInSatoshis); - - // Only the first sender should have the total amount in rsk since in legacy pegins the rsk address is derived from the first input. - const finalRskRecipient1BalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo1.rskRecipientRskAddressInfo.address); - const expectedRskRecipient1BalanceInWeisBN = rskTxHelper.getClient().utils.BN(satoshisToWeis(peginValueInSatoshis)); - expect(finalRskRecipient1BalanceInWeisBN.eq(expectedRskRecipient1BalanceInWeisBN)).to.be.true; - - // The second sender should have 0 balance in rsk. - const finalRskRecipient2BalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo2.rskRecipientRskAddressInfo.address); - expect(finalRskRecipient2BalanceInWeisBN.eq(new BN('0'))).to.be.true; - - }); - }); } @@ -207,27 +137,6 @@ const assert2wpBalancesAfterSuccessfulPegin = async (initial2wpBalances, peginVa }; -const addInputs = (tx, utxos) => { - utxos.forEach(utxo => { - tx.addInput(Buffer.from(utxo.txid, 'hex').reverse(), utxo.vout); - }); -}; - -const ensurePeginIsPushed = async (btcPeginTxHash, expectedUtxosCount = 1) => { - await waitForBitcoinMempoolToGetTxs(btcTxHelper, btcPeginTxHash); - await mineForPeginRegistration(rskTxHelper, btcTxHelper); - await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash, expectedUtxosCount); -}; - -const addOutput = (tx, address, outputValueInSatoshis) => { - if(outputValueInSatoshis > 0) { - tx.addOutput( - bitcoinJsLib.address.toOutputScript(address, btcTxHelper.btcConfig.network), - outputValueInSatoshis - ); - } -}; - module.exports = { execute, };