Skip to content

Commit 30c16f7

Browse files
committed
merge both getEstimatedFees methods in one file
1 parent 036cad4 commit 30c16f7

4 files changed

Lines changed: 173 additions & 237 deletions
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
const expect = require('chai').expect;
2+
const BN = require('bn.js');
3+
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
4+
const { btcToWeis, satoshisToBtc, satoshisToWeis } = require('@rsksmart/btc-eth-unit-converter');
5+
const { assertContractCallFails } = require('../assertions/contractMethods');
6+
const { getBridge } = require('../bridge-provider');
7+
const CustomError = require('../CustomError');
8+
const {
9+
createSenderRecipientInfo,
10+
ensurePeginIsRegistered,
11+
sendPeginToActiveFederation,
12+
sendTxToBridge,
13+
} = require('../2wp-utils');
14+
const { getBtcClient } = require('../btc-client-provider');
15+
const { MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS } = require('../constants/pegout-constants');
16+
const { getRskTransactionHelper, getRskTransactionHelpers } = require('../rsk-tx-helper-provider');
17+
const { sendFromCow, triggerRelease } = require('../rsk-utils');
18+
19+
const FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS = 500_000;
20+
const FEDERATION_PEGIN_AMOUNT_IN_BTC = Number(satoshisToBtc(FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS));
21+
const BTC_FUNDING_AMOUNT_IN_BTC = 2;
22+
const PEGOUT_SENDER_FUNDING_IN_BTC = 2;
23+
24+
/**
25+
* @param {string} [description]
26+
*/
27+
const execute = (description) => {
28+
let bridge;
29+
let rskTxHelper;
30+
let rskTxHelpers;
31+
let btcTxHelper;
32+
let senderInfo;
33+
34+
const getQueuedPegoutsCount = async () =>
35+
Number(await bridge.methods.getQueuedPegoutsCount().call());
36+
37+
const getEstimatedFeesForNextPegoutEvent = async () =>
38+
Number(await bridge.methods.getEstimatedFeesForNextPegOutEvent().call());
39+
40+
const getEstimatedFeesForPegOutAmount = async (amountInWeis) =>
41+
Number(await bridge.methods.getEstimatedFeesForPegOutAmount(amountInWeis).call());
42+
43+
const setupFedUtxosAndCreateAPegoutRequest = async () => {
44+
senderInfo = await createSenderRecipientInfo(
45+
rskTxHelper,
46+
btcTxHelper,
47+
'legacy',
48+
BTC_FUNDING_AMOUNT_IN_BTC
49+
);
50+
51+
const peginBtcTxHash = await sendPeginToActiveFederation(
52+
rskTxHelper,
53+
btcTxHelper,
54+
senderInfo.btcSenderAddressInfo,
55+
FEDERATION_PEGIN_AMOUNT_IN_BTC
56+
);
57+
await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash);
58+
59+
const bridgeStateAfterPegin = await getBridgeState(rskTxHelper.getClient());
60+
const peginUtxoInBridgeState = bridgeStateAfterPegin.activeFederationUtxos.find(
61+
(utxo) =>
62+
utxo.btcTxHash === peginBtcTxHash &&
63+
utxo.valueInSatoshis === FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS
64+
);
65+
expect(peginUtxoInBridgeState).to.not.be.undefined;
66+
67+
await sendFromCow(
68+
rskTxHelper,
69+
senderInfo.rskRecipientRskAddressInfo.address,
70+
Number(btcToWeis(PEGOUT_SENDER_FUNDING_IN_BTC))
71+
);
72+
73+
await sendTxToBridge(
74+
rskTxHelper,
75+
new BN(satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)),
76+
senderInfo.rskRecipientRskAddressInfo.address
77+
);
78+
};
79+
80+
const assertReleaseRequestsAreProcessed = async () => {
81+
const countBeforeRelease = await getQueuedPegoutsCount();
82+
const expectedPegoutCount = 2;
83+
expect(countBeforeRelease).to.equal(expectedPegoutCount);
84+
85+
await triggerRelease(rskTxHelpers, btcTxHelper);
86+
87+
const countAfterRelease = await getQueuedPegoutsCount();
88+
expect(countAfterRelease).to.equal(0);
89+
};
90+
91+
describe(description, () => {
92+
before(async () => {
93+
rskTxHelper = getRskTransactionHelper();
94+
rskTxHelpers = getRskTransactionHelpers();
95+
btcTxHelper = getBtcClient();
96+
bridge = await getBridge(rskTxHelper.getClient());
97+
});
98+
99+
beforeEach(async () => {
100+
await triggerRelease(rskTxHelpers, btcTxHelper);
101+
await setupFedUtxosAndCreateAPegoutRequest();
102+
});
103+
104+
afterEach(async () => {
105+
await triggerRelease(rskTxHelpers, btcTxHelper);
106+
});
107+
108+
describe('getEstimatedFeesForNextPegOutEvent', () => {
109+
it('should allow constructing a second pegout request using the fees from getEstimatedFeesForNextPegoutEvent and process both', async () => {
110+
try {
111+
const estimatedFeesForNextPegout = await getEstimatedFeesForNextPegoutEvent();
112+
expect(estimatedFeesForNextPegout).to.be.greaterThan(0);
113+
114+
const secondPegoutAmountInSatoshis =
115+
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS + estimatedFeesForNextPegout;
116+
await sendTxToBridge(
117+
rskTxHelper,
118+
new BN(satoshisToWeis(secondPegoutAmountInSatoshis)),
119+
senderInfo.rskRecipientRskAddressInfo.address
120+
);
121+
122+
await assertReleaseRequestsAreProcessed();
123+
} catch (err) {
124+
throw new CustomError(
125+
'Error validating getEstimatedFeesForNextPegOutEvent with two pegouts',
126+
err
127+
);
128+
}
129+
});
130+
});
131+
132+
describe('getEstimatedFeesForPegOutAmount', () => {
133+
it('should allow constructing a second pegout request using the estimated fees from getEstimatedFeesForPegOutAmount and process both', async () => {
134+
try {
135+
const estimatedFeesForPegout = await getEstimatedFeesForPegOutAmount(
136+
satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)
137+
);
138+
expect(estimatedFeesForPegout).to.be.greaterThan(0);
139+
140+
const secondPegoutAmountInSatoshis =
141+
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS + estimatedFeesForPegout;
142+
await sendTxToBridge(
143+
rskTxHelper,
144+
new BN(satoshisToWeis(secondPegoutAmountInSatoshis)),
145+
senderInfo.rskRecipientRskAddressInfo.address
146+
);
147+
148+
await assertReleaseRequestsAreProcessed();
149+
} catch (err) {
150+
throw new CustomError(
151+
'Error validating getEstimatedFeesForPegOutAmount with two pegouts',
152+
err
153+
);
154+
}
155+
});
156+
157+
it('should revert when peg-out amount in weis is below minimum peg-out', async () => {
158+
await assertContractCallFails(
159+
getEstimatedFeesForPegOutAmount(
160+
satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS - 1)
161+
)
162+
);
163+
});
164+
});
165+
});
166+
};
167+
168+
module.exports = {
169+
execute,
170+
};

tests/01_07_01-get_estimated_fees_for_next_pegout_event.js

Lines changed: 0 additions & 113 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const getEstimatedFeesMethodsTests = require('../lib/tests/get_estimated_fees_methods');
2+
3+
getEstimatedFeesMethodsTests.execute('Bridge peg-out fee estimation methods');

0 commit comments

Comments
 (0)