-
Notifications
You must be signed in to change notification settings - Fork 6
added a test for the getEstimatedFeesForNextPegoutEvent #385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 18 commits
46a0a0f
24679f8
f93c36d
7b6cb5b
036cad4
30c16f7
d3ede49
5356a97
3192676
18b783f
6b657f4
1111867
16a933f
0a35438
6335839
7488540
3b7148f
d8a1d24
856bf83
1f2080e
98fd3d2
e937fc3
313572e
1254091
6ea1829
d99c752
7855484
15cf620
44cefb7
9bf91f0
ea8b9bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,148 @@ | ||||||
| const expect = require('chai').expect; | ||||||
| const BN = require('bn.js'); | ||||||
| const { getBridgeState } = require('@rsksmart/bridge-state-data-parser'); | ||||||
| const { btcToWeis, satoshisToBtc, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter'); | ||||||
| const { assertContractCallFails } = require('../assertions/contractMethods'); | ||||||
| const { getBridge } = require('../bridge-provider'); | ||||||
| const { | ||||||
| createSenderRecipientInfo, | ||||||
| ensurePeginIsRegistered, | ||||||
| sendPeginToActiveFederation, | ||||||
| sendTxToBridge, | ||||||
| } = require('../2wp-utils'); | ||||||
| const { getBtcClient } = require('../btc-client-provider'); | ||||||
| const { MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS } = require('../constants/pegout-constants'); | ||||||
| const { getRskTransactionHelpers } = require('../rsk-tx-helper-provider'); | ||||||
| const { sendFromCow, triggerRelease } = require('../rsk-utils'); | ||||||
|
|
||||||
| const FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS = 500_000; | ||||||
| const FEDERATION_PEGIN_AMOUNT_IN_BTC = Number(satoshisToBtc(FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS)); | ||||||
| const BTC_FUNDING_AMOUNT_IN_BTC = 2; | ||||||
| const PEGOUT_SENDER_FUNDING_IN_BTC = 2; | ||||||
|
|
||||||
| /** | ||||||
| * @param {string} [description] | ||||||
| */ | ||||||
| const execute = (description) => { | ||||||
| let bridge; | ||||||
| let rskTxHelper; | ||||||
| let rskTxHelpers; | ||||||
| let btcTxHelper; | ||||||
| let senderInfo; | ||||||
|
|
||||||
| const getQueuedPegoutsCount = async () => | ||||||
| Number(await bridge.methods.getQueuedPegoutsCount().call()); | ||||||
|
|
||||||
| const getEstimatedFeesForNextPegOutEvent = async () => | ||||||
| Number(await bridge.methods.getEstimatedFeesForNextPegOutEvent().call()); | ||||||
|
|
||||||
| const getEstimatedFeesForPegOutAmount = async (satoshis) => await bridge.methods | ||||||
| .getEstimatedFeesForPegOutAmount(satoshisToWeis(satoshis)) | ||||||
|
|
||||||
| const setupFedUtxosAndAddAPegoutRequestToTheBridge = async () => { | ||||||
| senderInfo = await createSenderRecipientInfo( | ||||||
| rskTxHelper, | ||||||
| btcTxHelper, | ||||||
| 'legacy', | ||||||
| BTC_FUNDING_AMOUNT_IN_BTC | ||||||
| ); | ||||||
|
|
||||||
| const peginBtcTxHash = await sendPeginToActiveFederation( | ||||||
| rskTxHelper, | ||||||
| btcTxHelper, | ||||||
| senderInfo.btcSenderAddressInfo, | ||||||
| FEDERATION_PEGIN_AMOUNT_IN_BTC | ||||||
| ); | ||||||
| await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash); | ||||||
|
|
||||||
| const bridgeStateAfterPegin = await getBridgeState(rskTxHelper.getClient()); | ||||||
| const peginUtxoInBridgeState = bridgeStateAfterPegin.activeFederationUtxos.find( | ||||||
| (utxo) => | ||||||
| utxo.btcTxHash === peginBtcTxHash && | ||||||
| utxo.valueInSatoshis === FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS | ||||||
| ); | ||||||
| expect(peginUtxoInBridgeState).to.not.be.undefined; | ||||||
|
|
||||||
| await sendFromCow( | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to send funds from the cow address? Weren't those funds already received from the peg-in?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done 44cefb7 |
||||||
| rskTxHelper, | ||||||
| senderInfo.rskRecipientRskAddressInfo.address, | ||||||
| Number(btcToWeis(PEGOUT_SENDER_FUNDING_IN_BTC)) | ||||||
| ); | ||||||
|
|
||||||
| await sendTxToBridge( | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to get the bridge state after this and assert there is a pegout request?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done 9bf91f0 |
||||||
| rskTxHelper, | ||||||
| new BN(satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)), | ||||||
| senderInfo.rskRecipientRskAddressInfo.address | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| const processReleaseRequestsAndAssertQueueCleared = async () => { | ||||||
|
julia-zack marked this conversation as resolved.
Outdated
|
||||||
| const countBeforeRelease = await getQueuedPegoutsCount(); | ||||||
| const expectedPegoutCount = 2; | ||||||
| expect(countBeforeRelease).to.equal(expectedPegoutCount); | ||||||
|
|
||||||
| await triggerRelease(rskTxHelpers, btcTxHelper); | ||||||
|
|
||||||
| const countAfterRelease = await getQueuedPegoutsCount(); | ||||||
| expect(countAfterRelease).to.equal(0); | ||||||
| }; | ||||||
|
|
||||||
| const sendPegoutWithEstimatedFees = async (estimatedFees) => { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Adding the unit helps, it can get confusing vey fast
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done ea8b9bd |
||||||
| const secondPegoutAmountInSatoshis = | ||||||
| MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS + estimatedFees; | ||||||
| await sendTxToBridge( | ||||||
| rskTxHelper, | ||||||
| new BN(satoshisToWeis(secondPegoutAmountInSatoshis)), | ||||||
| senderInfo.rskRecipientRskAddressInfo.address | ||||||
| ); | ||||||
| }; | ||||||
|
|
||||||
| describe(description, () => { | ||||||
| before(async () => { | ||||||
| rskTxHelpers = getRskTransactionHelpers(); | ||||||
| rskTxHelper = rskTxHelpers[0]; | ||||||
| btcTxHelper = getBtcClient(); | ||||||
| bridge = await getBridge(rskTxHelper.getClient()); | ||||||
| }); | ||||||
|
|
||||||
| beforeEach(async () => { | ||||||
| await setupFedUtxosAndAddAPegoutRequestToTheBridge(); | ||||||
| }); | ||||||
|
|
||||||
| // Ensuring there's no pegout pending in the Bridge contract | ||||||
| // after each test to avoid interference between tests | ||||||
| afterEach(async () => { | ||||||
| await triggerRelease(rskTxHelpers, btcTxHelper); | ||||||
|
jeremy-then marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get the bridge state and assert there are no pending pegout requests after this? |
||||||
| }); | ||||||
|
|
||||||
| describe('getEstimatedFeesForNextPegOutEvent', () => { | ||||||
| it('should allow constructing a second pegout request using the fees from getEstimatedFeesForNextPegOutEvent and process both', async () => { | ||||||
|
julia-zack marked this conversation as resolved.
Outdated
|
||||||
| const estimatedFeesForNextPegOut = await getEstimatedFeesForNextPegOutEvent(); | ||||||
| expect(estimatedFeesForNextPegOut).to.be.greaterThan(0); | ||||||
|
|
||||||
| await sendPegoutWithEstimatedFees(estimatedFeesForNextPegOut); | ||||||
| await processReleaseRequestsAndAssertQueueCleared(); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| describe('getEstimatedFeesForPegOutAmount', () => { | ||||||
| it('should allow constructing a second pegout request using the estimated fees from getEstimatedFeesForPegOutAmount and process both', async () => { | ||||||
| const estimatedFeesForPegout = Number(getEstimatedFeesForPegOutAmount(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS).call()); | ||||||
| expect(estimatedFeesForPegout).to.be.greaterThan(0); | ||||||
|
|
||||||
| await sendPegoutWithEstimatedFees(estimatedFeesForPegout); | ||||||
| await processReleaseRequestsAndAssertQueueCleared(); | ||||||
| }); | ||||||
|
|
||||||
| it('should revert when peg-out amount in weis is below minimum peg-out', async () => { | ||||||
| await assertContractCallFails( | ||||||
| getEstimatedFeesForPegOutAmount(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS - 1) | ||||||
| ); | ||||||
| }); | ||||||
| }); | ||||||
| }); | ||||||
| }; | ||||||
|
|
||||||
| module.exports = { | ||||||
| execute, | ||||||
| }; | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| const getEstimatedFeesMethodsTests = require('../lib/tests/get_estimated_fees_methods'); | ||
|
|
||
| getEstimatedFeesMethodsTests.execute('Bridge peg-out fee estimation methods'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing a bunch of test cases: 0 requests, multiple requests, different pegout amounts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this wasn't the idea, it is an integration test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
98fd3d2
added a second part of each test as discussed checking getEstimatedFees with no pegout requests