@@ -5,9 +5,11 @@ import assertRevert from '@synthetixio/core-utils/utils/assertions/assert-revert
5
5
import assertEvent from '@synthetixio/core-utils/utils/assertions/assert-event' ;
6
6
import assertBn from '@synthetixio/core-utils/utils/assertions/assert-bignumber' ;
7
7
import { createRewardsDistributor } from '../bootstrap' ;
8
+ import { snapshotCheckpoint } from '@synthetixio/core-utils/utils/mocha/snapshot' ;
8
9
9
10
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 ( {
11
13
synthMarkets : [
12
14
{
13
15
name : 'Bitcoin' ,
@@ -17,7 +19,7 @@ describe('PerpsMarket: Reward Distributor configuration test', () => {
17
19
} ,
18
20
] ,
19
21
perpsMarkets : [ ] , // don't create a market in bootstrap
20
- traderAccountIds : [ 2 , 3 ] ,
22
+ traderAccountIds : [ accountId ] ,
21
23
collateralLiquidateRewardRatio : bn ( 0.42 ) ,
22
24
skipRegisterDistributors : true ,
23
25
} ) ;
@@ -64,6 +66,8 @@ describe('PerpsMarket: Reward Distributor configuration test', () => {
64
66
} ;
65
67
} ) ;
66
68
69
+ const restoreToSetup = snapshotCheckpoint ( provider ) ;
70
+
67
71
describe ( 'initial configuration' , ( ) => {
68
72
it ( 'collateral liquidate reward ratio' , async ( ) => {
69
73
assertBn . equal ( await systems ( ) . PerpsMarket . getCollateralLiquidateRewardRatio ( ) , bn ( 0.42 ) ) ;
@@ -253,4 +257,109 @@ describe('PerpsMarket: Reward Distributor configuration test', () => {
253
257
} ) ;
254
258
} ) ;
255
259
} ) ;
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
+ } ) ;
256
365
} ) ;
0 commit comments