From 2b67f63c2d788021885fc933ed4a75fc31080ac4 Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Tue, 24 Sep 2024 09:06:20 -0400 Subject: [PATCH] Adds basic peginv1 test --- lib/tests/2wp.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 5eaf9849..3a101b69 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -15,6 +15,7 @@ const { sendPegin, const { ensure0x } = require('../utils'); const { getBtcAddressBalanceInSatoshis, waitForBitcoinMempoolToGetTxs } = require('../btc-utils'); const bitcoinJsLib = require('bitcoinjs-lib'); +const { createPeginV1TxData } = require('pegin-address-verificator'); const DONATION_AMOUNT = 250; @@ -329,6 +330,54 @@ const execute = (description, getRskHost) => { }); + it('should do a basic pegin v1 with the exact minimum value', async () => { + + // Arrange + + const initialFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); + 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, satoshisToBtc(peginValueInSatoshis), peginV1Data); + await ensurePeginIsRegistered(rskTxHelper, btcPeginTxHash); + + // Assert + + // The btc pegin tx is already marked as processed by the bridge + const isBtcTxHashAlreadyProcessed = await bridge.methods.isBtcTxHashAlreadyProcessed(btcPeginTxHash).call(); + expect(isBtcTxHashAlreadyProcessed).to.be.true; + + // The pegin_btc event is emitted with the expected values + const recipientRskAddressChecksumed = rskTxHelper.getClient().utils.toChecksumAddress(peginV1RskRecipientAddress); + const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipientRskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis, '1'); + const btcTxHashProcessedHeight = Number(await bridge.methods.getBtcTxHashProcessedHeight(btcPeginTxHash).call()); + const peginBtcEvent = await findEventInBlock(rskTxHelper, expectedEvent.name, btcTxHashProcessedHeight); + expect(peginBtcEvent).to.be.deep.equal(expectedEvent); + + // The federation balance is increased by the pegin value + const finalFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress); + expect(finalFederationAddressBalanceInSatoshis).to.be.equal(initialFederationAddressBalanceInSatoshis + 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 finalSenderDerivedRskAddressBalance = Number(await rskTxHelper.getBalance(senderRecipientInfo.rskRecipientRskAddressInfo.address)); + expect(finalSenderDerivedRskAddressBalance).to.be.equal(0); + + // The pegin v1 rsk recipient address has the funds + const finalRskRecipientBalance = Number(await rskTxHelper.getBalance(peginV1RskRecipientAddress)); + expect(finalRskRecipientBalance).to.be.equal(Number(satoshisToWeis(peginValueInSatoshis))); + + }); + }); }