Skip to content

Commit 4dc87a1

Browse files
committed
merge both getEstimatedFees methods in one file
1 parent aaad648 commit 4dc87a1

4 files changed

Lines changed: 240 additions & 237 deletions
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
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+
33+
const getQueuedPegoutsCount = async () =>
34+
Number(await bridge.methods.getQueuedPegoutsCount().call());
35+
36+
const getEstimatedFeesForNextPegoutEvent = async () =>
37+
Number(await bridge.methods.getEstimatedFeesForNextPegOutEvent().call());
38+
39+
const getEstimatedFeesForPegOutAmount = async (amountInWeis) =>
40+
Number(await bridge.methods.getEstimatedFeesForPegOutAmount(amountInWeis).call());
41+
42+
describe(description, () => {
43+
before(async () => {
44+
rskTxHelper = getRskTransactionHelper();
45+
rskTxHelpers = getRskTransactionHelpers();
46+
btcTxHelper = getBtcClient();
47+
bridge = await getBridge(rskTxHelper.getClient());
48+
});
49+
50+
beforeEach(async () => {
51+
await triggerRelease(rskTxHelpers, btcTxHelper);
52+
});
53+
54+
afterEach(async () => {
55+
await triggerRelease(rskTxHelpers, btcTxHelper);
56+
});
57+
58+
describe('getEstimatedFeesForNextPegOutEvent', () => {
59+
it('should allow constructing a second pegout request using the fees from getEstimatedFeesForNextPegoutEvent and process both', async () => {
60+
try {
61+
const senderInfo = await createSenderRecipientInfo(
62+
rskTxHelper,
63+
btcTxHelper,
64+
'legacy',
65+
BTC_FUNDING_AMOUNT_IN_BTC
66+
);
67+
68+
const peginBtcTxHash = await sendPeginToActiveFederation(
69+
rskTxHelper,
70+
btcTxHelper,
71+
senderInfo.btcSenderAddressInfo,
72+
FEDERATION_PEGIN_AMOUNT_IN_BTC
73+
);
74+
await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash);
75+
76+
const bridgeStateAfterPegin = await getBridgeState(rskTxHelper.getClient());
77+
const peginUtxoInBridgeState = bridgeStateAfterPegin.activeFederationUtxos.find(
78+
(utxo) =>
79+
utxo.btcTxHash === peginBtcTxHash &&
80+
utxo.valueInSatoshis === FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS
81+
);
82+
expect(peginUtxoInBridgeState).to.not.be.undefined;
83+
84+
await sendFromCow(
85+
rskTxHelper,
86+
senderInfo.rskRecipientRskAddressInfo.address,
87+
Number(btcToWeis(PEGOUT_SENDER_FUNDING_IN_BTC))
88+
);
89+
90+
await sendTxToBridge(
91+
rskTxHelper,
92+
new BN(satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)),
93+
senderInfo.rskRecipientRskAddressInfo.address
94+
);
95+
96+
const estimatedFeesForNextPegout = await getEstimatedFeesForNextPegoutEvent();
97+
expect(estimatedFeesForNextPegout).to.be.greaterThan(0);
98+
99+
const secondPegoutAmountInSatoshis =
100+
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS + estimatedFeesForNextPegout;
101+
await sendTxToBridge(
102+
rskTxHelper,
103+
new BN(satoshisToWeis(secondPegoutAmountInSatoshis)),
104+
senderInfo.rskRecipientRskAddressInfo.address
105+
);
106+
107+
const countBeforeRelease = await getQueuedPegoutsCount();
108+
expect(countBeforeRelease).to.equal(2);
109+
110+
await triggerRelease(rskTxHelpers, btcTxHelper);
111+
112+
const countAfterRelease = await getQueuedPegoutsCount();
113+
expect(countAfterRelease).to.equal(0);
114+
} catch (err) {
115+
throw new CustomError(
116+
'Error validating getEstimatedFeesForNextPegOutEvent with two pegouts',
117+
err
118+
);
119+
}
120+
});
121+
});
122+
123+
describe('getEstimatedFeesForPegOutAmount', () => {
124+
it('should allow constructing a second pegout request using the estimated fees from getEstimatedFeesForPegOutAmount and process both', async () => {
125+
try {
126+
const senderInfo = await createSenderRecipientInfo(
127+
rskTxHelper,
128+
btcTxHelper,
129+
'legacy',
130+
BTC_FUNDING_AMOUNT_IN_BTC
131+
);
132+
133+
const peginBtcTxHash = await sendPeginToActiveFederation(
134+
rskTxHelper,
135+
btcTxHelper,
136+
senderInfo.btcSenderAddressInfo,
137+
FEDERATION_PEGIN_AMOUNT_IN_BTC
138+
);
139+
await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash);
140+
141+
const bridgeStateAfterPegin = await getBridgeState(rskTxHelper.getClient());
142+
const peginUtxoInBridgeState = bridgeStateAfterPegin.activeFederationUtxos.find(
143+
(utxo) =>
144+
utxo.btcTxHash === peginBtcTxHash &&
145+
utxo.valueInSatoshis === FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS
146+
);
147+
expect(peginUtxoInBridgeState).to.not.be.undefined;
148+
149+
await sendFromCow(
150+
rskTxHelper,
151+
senderInfo.rskRecipientRskAddressInfo.address,
152+
Number(btcToWeis(PEGOUT_SENDER_FUNDING_IN_BTC))
153+
);
154+
155+
await sendTxToBridge(
156+
rskTxHelper,
157+
new BN(satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)),
158+
senderInfo.rskRecipientRskAddressInfo.address
159+
);
160+
161+
const estimatedFeesForPegout = await getEstimatedFeesForPegOutAmount(
162+
satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)
163+
);
164+
expect(estimatedFeesForPegout).to.be.greaterThan(0);
165+
166+
const secondPegoutAmountInSatoshis =
167+
MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS + estimatedFeesForPegout;
168+
await sendTxToBridge(
169+
rskTxHelper,
170+
new BN(satoshisToWeis(secondPegoutAmountInSatoshis)),
171+
senderInfo.rskRecipientRskAddressInfo.address
172+
);
173+
174+
const countBeforeRelease = await getQueuedPegoutsCount();
175+
expect(countBeforeRelease).to.equal(2);
176+
177+
await triggerRelease(rskTxHelpers, btcTxHelper);
178+
179+
const countAfterRelease = await getQueuedPegoutsCount();
180+
expect(countAfterRelease).to.equal(0);
181+
} catch (err) {
182+
throw new CustomError(
183+
'Error validating getEstimatedFeesForPegOutAmount with two pegouts',
184+
err
185+
);
186+
}
187+
});
188+
189+
it('should revert when peg-out amount in weis is below minimum peg-out', async () => {
190+
const senderInfo = await createSenderRecipientInfo(
191+
rskTxHelper,
192+
btcTxHelper,
193+
'legacy',
194+
BTC_FUNDING_AMOUNT_IN_BTC
195+
);
196+
197+
const peginBtcTxHash = await sendPeginToActiveFederation(
198+
rskTxHelper,
199+
btcTxHelper,
200+
senderInfo.btcSenderAddressInfo,
201+
FEDERATION_PEGIN_AMOUNT_IN_BTC
202+
);
203+
await ensurePeginIsRegistered(rskTxHelper, peginBtcTxHash);
204+
205+
const bridgeStateAfterPegin = await getBridgeState(rskTxHelper.getClient());
206+
const peginUtxoInBridgeState = bridgeStateAfterPegin.activeFederationUtxos.find(
207+
(utxo) =>
208+
utxo.btcTxHash === peginBtcTxHash &&
209+
utxo.valueInSatoshis === FEDERATION_PEGIN_AMOUNT_IN_SATOSHIS
210+
);
211+
expect(peginUtxoInBridgeState).to.not.be.undefined;
212+
213+
await sendFromCow(
214+
rskTxHelper,
215+
senderInfo.rskRecipientRskAddressInfo.address,
216+
Number(btcToWeis(PEGOUT_SENDER_FUNDING_IN_BTC))
217+
);
218+
219+
await sendTxToBridge(
220+
rskTxHelper,
221+
new BN(satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS)),
222+
senderInfo.rskRecipientRskAddressInfo.address
223+
);
224+
225+
await assertContractCallFails(
226+
bridge.methods.getEstimatedFeesForPegOutAmount(
227+
satoshisToWeis(MINIMUM_PEGOUT_AMOUNT_IN_SATOSHIS - 1)
228+
)
229+
);
230+
});
231+
});
232+
});
233+
};
234+
235+
module.exports = {
236+
execute,
237+
};

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)