From 113fe2cb5db966bec65104e49abb6524d728673b Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Wed, 2 Oct 2024 14:07:30 -0400 Subject: [PATCH 1/2] Adds simple peginv1 test --- lib/tests/2wp.js | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index cf4f7cc9..02203fbb 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; const BN = require('bn.js'); +const { createPeginV1TxData } = require('pegin-address-verificator'); const { getBridge } = require('../precompiled-abi-forks-util'); const { getBtcClient } = require('../btc-client-provider'); const { getRskTransactionHelper } = require('../rsk-tx-helper-provider'); @@ -34,7 +35,7 @@ const execute = (description, getRskHost) => { federationAddress = await bridge.methods.getFederationAddress().call(); minimumPeginValueInSatoshis = Number(await bridge.methods.getMinimumLockTxValue().call()); - btcFeeInSatoshis = btcToSatoshis(await btcTxHelper.getFee()); + btcFeeInSatoshis = Number(btcToSatoshis(await btcTxHelper.getFee())); await btcTxHelper.importAddress(federationAddress, 'federation'); @@ -105,13 +106,54 @@ const execute = (description, getRskHost) => { }); + it('should do a basic pegin v1 with the exact minimum value', async () => { + + // Arrange + + const initial2wpBalances = await get2wpBalances(rskTxHelper, btcTxHelper); + const senderRecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper); + const initialSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); + const peginValueInSatoshis = minimumPeginValueInSatoshis; + const peginV1RskRecipientAddress = await rskTxHelper.newAccountWithSeed(''); + + // Act + + const peginV1Data = [Buffer.from(createPeginV1TxData(peginV1RskRecipientAddress), 'hex')]; + + const btcPeginTxHash = await sendPegin(rskTxHelper, btcTxHelper, senderRecipientInfo.btcSenderAddressInfo, Number(satoshisToBtc(peginValueInSatoshis)), peginV1Data); + + // Assert + + const expectedCountOfThisPeginUtxosInTheBridge = 1; + await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash, expectedCountOfThisPeginUtxosInTheBridge); + + const expectedPeginProtocolVersion = '1'; + await assertExpectedPeginBtcEventIsEmitted(btcPeginTxHash, peginV1RskRecipientAddress, peginValueInSatoshis, expectedPeginProtocolVersion); + + await assert2wpBalancesAfterSuccessfulPegin(initial2wpBalances, peginValueInSatoshis); + + // The sender address balance is decreased by the pegin value and the btc fee + const finalSenderAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, senderRecipientInfo.btcSenderAddressInfo.address); + expect(finalSenderAddressBalanceInSatoshis).to.be.equal(initialSenderAddressBalanceInSatoshis - peginValueInSatoshis - btcFeeInSatoshis); + + // The sender derived rsk address rsk address balance is unchanged + const finalSenderDerivedRskAddressBalanceInWeisBN = await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address); + expect(finalSenderDerivedRskAddressBalanceInWeisBN.eq(new BN('0'))).to.be.true; + + // The pegin v1 rsk recipient address has the funds + const finalRskRecipientBalanceInWeisBN = await rskTxHelper.getBalance(peginV1RskRecipientAddress); + const expectedFinalRskRecipientBalanceInWeisBN = new BN(satoshisToWeis(peginValueInSatoshis)); + expect(finalRskRecipientBalanceInWeisBN.eq(expectedFinalRskRecipientBalanceInWeisBN)).to.be.true; + + }); + }); } -const assertExpectedPeginBtcEventIsEmitted = async (btcPeginTxHash, rskRecipientAddress, peginValueInSatoshis) => { +const assertExpectedPeginBtcEventIsEmitted = async (btcPeginTxHash, rskRecipientAddress, peginValueInSatoshis, expectedPeginProtocolVersion) => { const recipient1RskAddressChecksumed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(rskRecipientAddress)); - const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipient1RskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis); + const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipient1RskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis, expectedPeginProtocolVersion); const btcTxHashProcessedHeight = Number(await bridge.methods.getBtcTxHashProcessedHeight(btcPeginTxHash).call()); const peginBtcEvent = await findEventInBlock(rskTxHelper, expectedEvent.name, btcTxHashProcessedHeight); expect(peginBtcEvent).to.be.deep.equal(expectedEvent); From b100a3350503ead52c9e049d89958e0dc9a8046f Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Wed, 2 Oct 2024 14:58:31 -0400 Subject: [PATCH 2/2] Removes unnecesary parameter --- lib/tests/2wp.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 02203fbb..99cbaafd 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -124,8 +124,7 @@ const execute = (description, getRskHost) => { // Assert - const expectedCountOfThisPeginUtxosInTheBridge = 1; - await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash, expectedCountOfThisPeginUtxosInTheBridge); + await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash); const expectedPeginProtocolVersion = '1'; await assertExpectedPeginBtcEventIsEmitted(btcPeginTxHash, peginV1RskRecipientAddress, peginValueInSatoshis, expectedPeginProtocolVersion); @@ -151,7 +150,7 @@ const execute = (description, getRskHost) => { } -const assertExpectedPeginBtcEventIsEmitted = async (btcPeginTxHash, rskRecipientAddress, peginValueInSatoshis, expectedPeginProtocolVersion) => { +const assertExpectedPeginBtcEventIsEmitted = async (btcPeginTxHash, rskRecipientAddress, peginValueInSatoshis, expectedPeginProtocolVersion = '0') => { const recipient1RskAddressChecksumed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(rskRecipientAddress)); const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipient1RskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis, expectedPeginProtocolVersion); const btcTxHashProcessedHeight = Number(await bridge.methods.getBtcTxHashProcessedHeight(btcPeginTxHash).call());