Skip to content

Commit

Permalink
Adds basic peginv1 test
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Sep 24, 2024
1 parent fa87eee commit 2b67f63
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)));

});

});

}
Expand Down

0 comments on commit 2b67f63

Please sign in to comment.