Skip to content

Commit 29db4a3

Browse files
author
Sachin
authored
fix(BasisTradingStrategy): Increase reinvest threshold amount (#32)
* Increase reinvest amount requirement * Fix typo * Add minReinvestUnits methodology parameter * Update event; Add minReinvestUnits > 0 check * Update javadocs; package to 0.0.9-basis.0 * Update package to 0.0.10
1 parent da2424a commit 29db4a3

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

contracts/extensions/DeltaNeutralBasisTradingStrategyExtension.sol

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {
127127
uint256 recenteringSpeed; // % at which to rebalance back to target leverage in precise units (10e18). Always a positive number
128128
uint256 rebalanceInterval; // Period of time required since last rebalance timestamp in seconds
129129
uint256 reinvestInterval; // Period of time required since last reinvestment timestamp in seconds
130+
uint256 minReinvestUnits; // Minimum reinvestment amount per Set at which `shouldRebalance` returns 4 (`ShouldRebalance.REINVEST`)
131+
// provided the reinvest interval has elapsed. This threshold helps to avoid weird rounding errors that
132+
// can happen while reinvesting very small amounts and helps the methodologist to not waste gas on reinvesting
133+
// very small amounts. In collateral decimals (10e6 for USDC).
134+
130135
}
131136

132137
struct ExecutionSettings {
@@ -204,7 +209,8 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {
204209
int256 _maxLeverageRatio,
205210
uint256 _recenteringSpeed,
206211
uint256 _rebalanceInterval,
207-
uint256 _reinvestInterval
212+
uint256 _reinvestInterval,
213+
uint256 _minReinvestUnits
208214
);
209215
event ExecutionSettingsUpdated(
210216
uint256 _twapCooldownPeriod,
@@ -529,7 +535,8 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {
529535
methodology.maxLeverageRatio,
530536
methodology.recenteringSpeed,
531537
methodology.rebalanceInterval,
532-
methodology.reinvestInterval
538+
methodology.reinvestInterval,
539+
methodology.minReinvestUnits
533540
);
534541
}
535542

@@ -1386,9 +1393,10 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {
13861393
// `reinvest()` transaction is mined.
13871394
if (block.timestamp.sub(lastReinvestTimestamp) > methodology.reinvestInterval) {
13881395
uint256 reinvestmentNotional = strategy.basisTradingModule.getUpdatedSettledFunding(strategy.setToken);
1396+
uint256 setTotalSupply = strategy.setToken.totalSupply();
1397+
uint256 reinvestUnits = reinvestmentNotional.fromPreciseUnitToDecimals(collateralDecimals).preciseDiv(setTotalSupply);
13891398

1390-
// Reinvest only if reinvestment amount is greater than 1 wei worth of USDC (to account for rounding errors)
1391-
if (reinvestmentNotional.fromPreciseUnitToDecimals(collateralDecimals) > 1) {
1399+
if (reinvestUnits >= methodology.minReinvestUnits) {
13921400
return ShouldRebalance.REINVEST;
13931401
}
13941402
}
@@ -1427,6 +1435,7 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {
14271435
_methodology.recenteringSpeed <= PreciseUnitMath.preciseUnit() && _methodology.recenteringSpeed > 0,
14281436
"Must be valid recentering speed"
14291437
);
1438+
require(_methodology.minReinvestUnits > 0, "Must be valid min reinvest units");
14301439
require (
14311440
_execution.slippageTolerance <= PreciseUnitMath.preciseUnit(),
14321441
"Slippage tolerance must be <100%"

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@setprotocol/set-v2-strategies",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "",
55
"main": "dist",
66
"types": "dist/types",

test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
250250
const recenteringSpeed = ether(0.05);
251251
const rebalanceInterval = ONE_DAY_IN_SECONDS;
252252
const reinvestInterval = ONE_DAY_IN_SECONDS.mul(7);
253+
const minReinvestUnits = TWO;
253254

254255
const exchangeName = "UniswapV3ExchangeAdapterV2";
255256
const buyExactSpotTradeData = await uniswapV3ExchangeAdapter.generateDataParam(
@@ -295,7 +296,8 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
295296
maxLeverageRatio: maxLeverageRatio,
296297
recenteringSpeed: recenteringSpeed,
297298
rebalanceInterval: rebalanceInterval,
298-
reinvestInterval: reinvestInterval
299+
reinvestInterval: reinvestInterval,
300+
minReinvestUnits: minReinvestUnits
299301
};
300302
execution = {
301303
twapCooldownPeriod: twapCooldownPeriod,
@@ -365,7 +367,8 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
365367
maxLeverageRatio: ether(-1.3),
366368
recenteringSpeed: ether(0.05),
367369
rebalanceInterval: BigNumber.from(86400),
368-
reinvestInterval: ONE_DAY_IN_SECONDS.mul(7)
370+
reinvestInterval: ONE_DAY_IN_SECONDS.mul(7),
371+
minReinvestUnits: TWO
369372
};
370373
subjectExecutionSettings = {
371374
twapCooldownPeriod: BigNumber.from(120),
@@ -2617,7 +2620,8 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
26172620
maxLeverageRatio: ether(-1.1),
26182621
recenteringSpeed: ether(0.1),
26192622
rebalanceInterval: BigNumber.from(43200),
2620-
reinvestInterval: ONE_DAY_IN_SECONDS.mul(7)
2623+
reinvestInterval: ONE_DAY_IN_SECONDS.mul(7),
2624+
minReinvestUnits: TWO
26212625
};
26222626
subjectCaller = owner;
26232627
};
@@ -2650,10 +2654,24 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
26502654
subjectMethodologySettings.maxLeverageRatio,
26512655
subjectMethodologySettings.recenteringSpeed,
26522656
subjectMethodologySettings.rebalanceInterval,
2653-
subjectMethodologySettings.reinvestInterval
2657+
subjectMethodologySettings.reinvestInterval,
2658+
subjectMethodologySettings.minReinvestUnits
26542659
);
26552660
});
26562661

2662+
describe("when min reinvest units is zero", async () => {
2663+
beforeEach(async () => {
2664+
subjectMethodologySettings = {
2665+
...subjectMethodologySettings,
2666+
minReinvestUnits: ZERO
2667+
};
2668+
});
2669+
2670+
it("should revert", async () => {
2671+
await expect(subject()).to.be.revertedWith("Must be valid min reinvest units");
2672+
});
2673+
});
2674+
26572675
describe("when the caller is not the operator", async () => {
26582676
beforeEach(async () => {
26592677
subjectCaller = await getRandomAccount();
@@ -3513,7 +3531,7 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
35133531
});
35143532
});
35153533

3516-
describe("when reinvestment amount is zero", async () => {
3534+
describe("when reinvestment amount is below threshold", async () => {
35173535
beforeEach(async () => {
35183536
// Set oracle price above mark price to accrue negative funding
35193537
await perpV2Setup.setBaseTokenOraclePrice(perpV2Setup.vETH, usdc(1020));
@@ -3828,7 +3846,7 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
38283846
});
38293847
});
38303848

3831-
describe("when reinvestment amount is zero", async () => {
3849+
describe("when reinvestment amount is below threshold", async () => {
38323850
beforeEach(async () => {
38333851
// Set oracle price above mark price to accrue negative funding
38343852
await perpV2Setup.setBaseTokenOraclePrice(perpV2Setup.vETH, usdc(1020));

utils/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export interface PerpV2BasisMethodologySettings {
9090
recenteringSpeed: BigNumber;
9191
rebalanceInterval: BigNumber;
9292
reinvestInterval: BigNumber;
93+
minReinvestUnits: BigNumber;
9394
}
9495

9596
export interface PerpV2BasisExecutionSettings {

0 commit comments

Comments
 (0)