Skip to content

Commit 68d763a

Browse files
committed
test modifyCollateral with unregistered rd
1 parent 156ebeb commit 68d763a

File tree

1 file changed

+111
-2
lines changed

1 file changed

+111
-2
lines changed

markets/perps-market/test/integration/Market/Market.RewardDistributor.test.ts

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import assertRevert from '@synthetixio/core-utils/utils/assertions/assert-revert
55
import assertEvent from '@synthetixio/core-utils/utils/assertions/assert-event';
66
import assertBn from '@synthetixio/core-utils/utils/assertions/assert-bignumber';
77
import { createRewardsDistributor } from '../bootstrap';
8+
import { snapshotCheckpoint } from '@synthetixio/core-utils/utils/mocha/snapshot';
89

910
describe('PerpsMarket: Reward Distributor configuration test', () => {
10-
const { systems, signers, owner, synthMarkets } = bootstrapMarkets({
11+
const accountId = 4;
12+
const { systems, signers, trader1, owner, synthMarkets, provider } = bootstrapMarkets({
1113
synthMarkets: [
1214
{
1315
name: 'Bitcoin',
@@ -17,7 +19,7 @@ describe('PerpsMarket: Reward Distributor configuration test', () => {
1719
},
1820
],
1921
perpsMarkets: [], // don't create a market in bootstrap
20-
traderAccountIds: [2, 3],
22+
traderAccountIds: [accountId],
2123
collateralLiquidateRewardRatio: bn(0.42),
2224
skipRegisterDistributors: true,
2325
});
@@ -64,6 +66,8 @@ describe('PerpsMarket: Reward Distributor configuration test', () => {
6466
};
6567
});
6668

69+
const restoreToSetup = snapshotCheckpoint(provider);
70+
6771
describe('initial configuration', () => {
6872
it('collateral liquidate reward ratio', async () => {
6973
assertBn.equal(await systems().PerpsMarket.getCollateralLiquidateRewardRatio(), bn(0.42));
@@ -253,4 +257,109 @@ describe('PerpsMarket: Reward Distributor configuration test', () => {
253257
});
254258
});
255259
});
260+
261+
describe('delegate collateral', () => {
262+
let tx: ethers.ContractTransaction;
263+
const delegatedCollateral = bn(1);
264+
265+
describe('can delegate snxUSD (collateralId = 0)', () => {
266+
before('delegate collateral', async () => {
267+
tx = await systems()
268+
.PerpsMarket.connect(trader1())
269+
.modifyCollateral(accountId, 0, delegatedCollateral);
270+
});
271+
272+
it('emits event', async () => {
273+
await assertEvent(
274+
tx,
275+
`CollateralModified(${accountId.toString()}, ${bn(0).toString()}, ${delegatedCollateral.toString()}, "${await trader1().getAddress()}")`,
276+
systems().PerpsMarket
277+
);
278+
});
279+
280+
it('has correct available margin', async () => {
281+
assertBn.equal(
282+
await systems().PerpsMarket.getAvailableMargin(accountId),
283+
delegatedCollateral
284+
);
285+
});
286+
287+
it('has correct withdrawable margin', async () => {
288+
assertBn.equal(
289+
await systems().PerpsMarket.getWithdrawableMargin(accountId),
290+
delegatedCollateral
291+
);
292+
});
293+
});
294+
295+
describe('fails to delegate other collateral without a registered distributor', () => {
296+
before(restoreToSetup);
297+
298+
it('reverts delegate snxBTC (collateralId = 2)', async () => {
299+
await assertRevert(
300+
systems()
301+
.PerpsMarket.connect(trader1())
302+
.modifyCollateral(accountId, synthBTCMarketId, delegatedCollateral),
303+
`InvalidId("${synthBTCMarketId}")`
304+
);
305+
});
306+
});
307+
308+
describe('can delegate other collateral when the distributor is registered', () => {
309+
const delegatedCollateralValue = delegatedCollateral.mul(10_000);
310+
before(restoreToSetup);
311+
312+
before('register distributor', async () => {
313+
await systems()
314+
.PerpsMarket.connect(owner())
315+
.registerDistributor(
316+
distributorData.tokenAddress,
317+
distributorData.distributorAddress,
318+
synthBTCMarketId,
319+
distributorData.poolDelegatedCollateralTypes
320+
);
321+
});
322+
323+
before('get and approve synth collateral', async () => {
324+
// trade snxUSD for synth
325+
await systems()
326+
.SpotMarket.connect(trader1())
327+
.buy(synthBTCMarketId, bn(10_000 * 1000), 0, ethers.constants.AddressZero);
328+
329+
// approve amount of collateral to be transfered to the market
330+
await synthMarkets()[0]
331+
.synth()
332+
.connect(trader1())
333+
.approve(systems().PerpsMarket.address, delegatedCollateral);
334+
});
335+
336+
before('delegate collateral', async () => {
337+
tx = await systems()
338+
.PerpsMarket.connect(trader1())
339+
.modifyCollateral(accountId, synthBTCMarketId, delegatedCollateral);
340+
});
341+
342+
it('emits event', async () => {
343+
await assertEvent(
344+
tx,
345+
`CollateralModified(${accountId.toString()}, ${synthBTCMarketId.toString()}, ${delegatedCollateral.toString()}, "${await trader1().getAddress()}")`,
346+
systems().PerpsMarket
347+
);
348+
});
349+
350+
it('has correct available margin', async () => {
351+
assertBn.equal(
352+
await systems().PerpsMarket.getAvailableMargin(accountId),
353+
delegatedCollateralValue
354+
);
355+
});
356+
357+
it('has correct withdrawable margin', async () => {
358+
assertBn.equal(
359+
await systems().PerpsMarket.getWithdrawableMargin(accountId),
360+
delegatedCollateralValue
361+
);
362+
});
363+
});
364+
});
256365
});

0 commit comments

Comments
 (0)