Skip to content

Commit

Permalink
Adds legacy pegin test with multiple inputs from multiple accounts.
Browse files Browse the repository at this point in the history
Rebases
  • Loading branch information
jeremy-then committed Oct 1, 2024
1 parent 87238e9 commit e592cc0
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions lib/tests/2wp.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,84 @@ 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 initialFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress);
const sender1RecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper);
const sender2RecipientInfo = await createSenderRecipientInfo(rskTxHelper, btcTxHelper);
const initialSender1AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, sender1RecipientInfo.btcSenderAddressInfo.address);
const initialSender2AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, sender2RecipientInfo.btcSenderAddressInfo.address);

const sender1PeginValueInSatoshis = minimumPeginValueInSatoshis;
const sender2PeginValueInSatoshis = minimumPeginValueInSatoshis;
const peginValueInSatoshis = sender1PeginValueInSatoshis + sender2PeginValueInSatoshis;

const sender1UtxosInfo = await getSenderUtxosInfo(sender1RecipientInfo, satoshisToBtc(sender1PeginValueInSatoshis));
const sender2UtxosInfo = await getSenderUtxosInfo(sender2RecipientInfo, satoshisToBtc(sender2PeginValueInSatoshis));

const sender1ChangeInSatoshis = btcToSatoshis(sender1UtxosInfo.change);
const sender2ChangeInSatoshis = btcToSatoshis(sender2UtxosInfo.change);

const tx = new bitcoinJsLib.Transaction();

// Adding inputs
addInputs(tx, sender1UtxosInfo);
addInputs(tx, sender2UtxosInfo);

// Adding output to federation
addOutputToFed(tx, peginValueInSatoshis);

// Adding change outputs
addChangeOutputs(tx, sender1RecipientInfo.btcSenderAddressInfo.address, sender1ChangeInSatoshis);
addChangeOutputs(tx, sender2RecipientInfo.btcSenderAddressInfo.address, sender2ChangeInSatoshis);

// Signing the transaction
const sender1PrivateKey = sender1RecipientInfo.btcSenderAddressInfo.privateKey;
const sender2PrivateKey = sender2RecipientInfo.btcSenderAddressInfo.privateKey;
const sendersPrivateKeys = [sender1PrivateKey, sender2PrivateKey];
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);
await pushPegin(btcPeginTxHash);

// Assert

const isBtcTxHashAlreadyProcessed = await bridge.methods.isBtcTxHashAlreadyProcessed(btcPeginTxHash).call();
expect(isBtcTxHashAlreadyProcessed).to.be.true;

// The expected pegin_btc event should be emitted with the expected values
const recipient1RskAddressChecksumed = rskTxHelper.getClient().utils.toChecksumAddress(ensure0x(sender1RecipientInfo.rskRecipientRskAddressInfo.address));
const expectedEvent = createExpectedPeginBtcEvent(PEGIN_EVENTS.PEGIN_BTC, recipient1RskAddressChecksumed, btcPeginTxHash, peginValueInSatoshis);
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 address should have received the total amount sent by the senders
const finalFederationAddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, federationAddress);
expect(finalFederationAddressBalanceInSatoshis).to.be.equal(initialFederationAddressBalanceInSatoshis + peginValueInSatoshis);

// The senders should have their balances reduced by the amount sent to the federation and the fee
const finalSender1AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, sender1RecipientInfo.btcSenderAddressInfo.address);
expect(finalSender1AddressBalanceInSatoshis).to.be.equal(initialSender1AddressBalanceInSatoshis - sender1PeginValueInSatoshis - btcFeeInSatoshis);

const finalSender2AddressBalanceInSatoshis = await getBtcAddressBalanceInSatoshis(btcTxHelper, sender2RecipientInfo.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 finalRskRecipient1Balance = Number(await rskTxHelper.getBalance(sender1RecipientInfo.rskRecipientRskAddressInfo.address));
expect(finalRskRecipient1Balance).to.be.equal(Number(satoshisToWeis(peginValueInSatoshis)));

// Other senders should have 0 balance in rsk.
const finalRskRecipient2Balance = Number(await rskTxHelper.getBalance(sender2RecipientInfo.rskRecipientRskAddressInfo.address));
expect(finalRskRecipient2Balance).to.be.equal(0);

});

});

}
Expand Down

0 comments on commit e592cc0

Please sign in to comment.