Skip to content

Commit 1381036

Browse files
authored
Fixed minor issue with spot manager (#219)
* fixed spot oracle price bug * deployed spot-usdc mananger
1 parent 654ffc5 commit 1381036

File tree

6 files changed

+110
-34
lines changed

6 files changed

+110
-34
lines changed

spot-vaults/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The official mainnet addresses are:
77
- Bill Broker (SPOT-USDC): [0xA088Aef966CAD7fE0B38e28c2E07590127Ab4ccB](https://etherscan.io/address/0xA088Aef966CAD7fE0B38e28c2E07590127Ab4ccB)
88
- SpotAppraiser: [0x965FBFebDA76d9AA11642C1d0074CdF02e546F3c](https://etherscan.io/address/0x965FBFebDA76d9AA11642C1d0074CdF02e546F3c)
99
- WethWamplManager: [0x6785fa26191eb531c54fd093931f395c4b01b583](https://etherscan.io/address/0x6785fa26191eb531c54fd093931f395c4b01b583)
10+
- UsdcSpotManager: [0x780eB92040bf24cd9BF993505390e88E8ED59935](https://etherscan.io/address/0x780eB92040bf24cd9BF993505390e88E8ED59935)
1011

1112
The official testnet addresses are:
1213

spot-vaults/contracts/UsdcSpotManager.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ contract UsdcSpotManager {
174174
function getSpotUSDPrice() public view returns (uint256) {
175175
uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(VAULT.getTwap());
176176
uint256 ratioX192 = uint256(sqrtPriceX96) * sqrtPriceX96;
177-
uint256 spotPerUsdc = FullMath.mulDiv(ONE, ratioX192, (1 << 192));
178-
return FullMath.mulDiv(spotPerUsdc, ONE_USDC, ONE_SPOT);
177+
uint256 usdcPerSpot = FullMath.mulDiv(ONE, (1 << 192), ratioX192);
178+
return FullMath.mulDiv(usdcPerSpot, ONE_SPOT, ONE_USDC);
179179
}
180180

181181
/// @notice Checks the vault is overweight SPOT, and looking to sell the extra SPOT for USDC.

spot-vaults/tasks/deploy.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,40 @@ task("deploy:WethWamplManager")
123123
console.log("Skipping verification");
124124
}
125125
});
126+
127+
task("deploy:UsdcSpotManager")
128+
.addParam(
129+
"vault",
130+
"the address of the usdc-spot charm vault",
131+
undefined,
132+
types.string,
133+
false,
134+
)
135+
.addParam(
136+
"spotAppraiser",
137+
"the address of the spot appraiser",
138+
undefined,
139+
types.string,
140+
false,
141+
)
142+
.addParam("verify", "flag to set false for local deployments", true, types.boolean)
143+
.setAction(async function (args: TaskArguments, hre) {
144+
const deployer = (await hre.ethers.getSigners())[0];
145+
console.log("Signer", await deployer.getAddress());
146+
147+
const { vault, spotAppraiser } = args;
148+
149+
const UsdcSpotManager = await hre.ethers.getContractFactory("UsdcSpotManager");
150+
const manager = await UsdcSpotManager.deploy(vault, spotAppraiser);
151+
console.log("usdcSpotManager", manager.target);
152+
153+
if (args.verify) {
154+
await sleep(30);
155+
await hre.run("verify:contract", {
156+
address: manager.target,
157+
constructorArguments: [vault, spotAppraiser],
158+
});
159+
} else {
160+
console.log("Skipping verification");
161+
}
162+
});

spot-vaults/tasks/info.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ task("info:WethWamplManager")
165165
console.log("dataValid:", r[1]);
166166
console.log("isOverweightWampl:", await manager.isOverweightWampl());
167167
console.log("prevDeviation:", pp(await manager.prevDeviation(), managerDecimals));
168-
console.log("amplDeviation:", pp(deviation, managerDecimals));
168+
console.log("deviation:", pp(deviation, managerDecimals));
169169
console.log(
170170
"activeLiqPerc:",
171171
pp(await manager.computeActiveLiqPerc(deviation), managerDecimals),
@@ -180,3 +180,42 @@ task("info:WethWamplManager")
180180
console.log("rebalanceActive:", rebalanceActive);
181181
console.log("---------------------------------------------------------------");
182182
});
183+
184+
task("info:UsdcSpotManager")
185+
.addPositionalParam(
186+
"address",
187+
"the address of the usdc-spot mananger contract",
188+
undefined,
189+
types.string,
190+
false,
191+
)
192+
.setAction(async function (args: TaskArguments, hre) {
193+
const { address } = args;
194+
195+
const manager = await hre.ethers.getContractAt("UsdcSpotManager", address);
196+
const managerDecimals = await manager.decimals();
197+
console.log("---------------------------------------------------------------");
198+
console.log("UsdcSpotManager:", manager.target);
199+
console.log("owner:", await manager.owner());
200+
console.log("spotAppraiser:", await manager.spotAppraiser());
201+
202+
console.log("---------------------------------------------------------------");
203+
const spotPrice = await manager.getSpotUSDPrice();
204+
console.log("spotPrice:", pp(spotPrice, managerDecimals));
205+
206+
const r = await manager.computeDeviationFactor.staticCall();
207+
const deviation = r[0];
208+
console.log("dataValid:", r[1]);
209+
console.log("isOverweightSpot:", await manager.isOverweightSpot());
210+
console.log("prevDeviation:", pp(await manager.prevDeviation(), managerDecimals));
211+
console.log("deviation:", pp(deviation, managerDecimals));
212+
213+
let rebalanceActive = true;
214+
try {
215+
await manager.rebalance.staticCall();
216+
} catch (e) {
217+
rebalanceActive = false;
218+
}
219+
console.log("rebalanceActive:", rebalanceActive);
220+
console.log("---------------------------------------------------------------");
221+
});

spot-vaults/tasks/tools.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { task, types } from "hardhat/config";
22
import { TaskArguments } from "hardhat/types";
33

4-
export async function sleep(sleepSec: number) {
5-
await new Promise(resolve => setTimeout(resolve, sleepSec));
6-
}
4+
export const sleep = seconds =>
5+
new Promise(resolve => setTimeout(resolve, seconds * 1000));
76

87
task("accounts", "Prints the list of accounts", async (_taskArgs, hre) => {
98
const accounts = await hre.ethers.getSigners();

spot-vaults/test/UsdcSpotManager.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe("UsdcSpotManager", function () {
2121
await mockVault.mockMethod("fullUpper()", [800000]);
2222
await mockVault.mockMethod("baseLower()", [45000]);
2323
await mockVault.mockMethod("baseUpper()", [55000]);
24-
await mockVault.mockMethod("getTwap()", [71000]);
24+
await mockVault.mockMethod("getTwap()", [67200]);
2525
await mockVault.mockMethod("limitThreshold()", [800000]);
2626

2727
const mockPool = new DMock("IUniswapV3Pool");
@@ -176,7 +176,7 @@ describe("UsdcSpotManager", function () {
176176
const { manager, mockAppraiser } = await loadFixture(setupContracts);
177177
await mockAppraiser.mockMethod("perpPrice()", [priceFP("1.2"), false]);
178178
const r = await manager.computeDeviationFactor.staticCall();
179-
expect(r[0]).to.eq(percFP("1.009614109343384160"));
179+
expect(r[0]).to.eq(percFP("1.0057863765655975"));
180180
expect(r[1]).to.eq(false);
181181
});
182182
});
@@ -186,47 +186,47 @@ describe("UsdcSpotManager", function () {
186186
const { manager, mockAppraiser } = await loadFixture(setupContracts);
187187
await mockAppraiser.mockMethod("usdPrice()", [priceFP("0.8"), false]);
188188
const r = await manager.computeDeviationFactor.staticCall();
189-
expect(r[0]).to.eq(percFP("1.009614109343384160"));
189+
expect(r[0]).to.eq(percFP("1.0057863765655975"));
190190
expect(r[1]).to.eq(false);
191191
});
192192
});
193193

194194
it("should return deviation factor", async function () {
195195
const { manager } = await loadFixture(setupContracts);
196196
const r = await manager.computeDeviationFactor.staticCall();
197-
expect(r[0]).to.eq(percFP("1.009614109343384160"));
197+
expect(r[0]).to.eq(percFP("1.0057863765655975"));
198198
expect(r[1]).to.eq(true);
199199
});
200200

201201
it("should return deviation factor", async function () {
202202
const { manager, mockVault } = await loadFixture(setupContracts);
203-
await mockVault.mockMethod("getTwap()", [72500]);
203+
await mockVault.mockMethod("getTwap()", [65800]);
204204
const r = await manager.computeDeviationFactor.staticCall();
205-
expect(r[0]).to.eq(percFP("1.172995447264373845"));
205+
expect(r[0]).to.eq(percFP("1.1569216182711425"));
206206
expect(r[1]).to.eq(true);
207207
});
208208

209209
it("should return deviation factor", async function () {
210210
const { manager, mockVault } = await loadFixture(setupContracts);
211-
await mockVault.mockMethod("getTwap()", [70500]);
211+
await mockVault.mockMethod("getTwap()", [67800]);
212212
const r = await manager.computeDeviationFactor.staticCall();
213-
expect(r[0]).to.eq(percFP("0.960377048978079093"));
213+
expect(r[0]).to.eq(percFP("0.947216779268338333"));
214214
expect(r[1]).to.eq(true);
215215
});
216216

217217
it("should return deviation factor", async function () {
218218
const { manager, mockAppraiser } = await loadFixture(setupContracts);
219219
await mockAppraiser.mockMethod("perpPrice()", [priceFP("1.5"), true]);
220220
const r = await manager.computeDeviationFactor.staticCall();
221-
expect(r[0]).to.eq(percFP("0.807691287474707328"));
221+
expect(r[0]).to.eq(percFP("0.804629101252478"));
222222
expect(r[1]).to.eq(true);
223223
});
224224

225225
it("should return deviation factor", async function () {
226226
const { manager, mockAppraiser } = await loadFixture(setupContracts);
227227
await mockAppraiser.mockMethod("perpPrice()", [priceFP("1"), true]);
228228
const r = await manager.computeDeviationFactor.staticCall();
229-
expect(r[0]).to.eq(percFP("1.211536931212060992"));
229+
expect(r[0]).to.eq(percFP("1.206943651878717"));
230230
expect(r[1]).to.eq(true);
231231
});
232232

@@ -267,7 +267,7 @@ describe("UsdcSpotManager", function () {
267267
it("should keep limit range", async function () {
268268
const { manager, mockVault } = await loadFixture(setupContracts);
269269

270-
await mockVault.mockMethod("getTwap()", [72000]);
270+
await mockVault.mockMethod("getTwap()", [66200]);
271271
await mockVault.mockMethod("limitLower()", [40000]);
272272
await mockVault.mockMethod("limitUpper()", [45000]);
273273

@@ -279,15 +279,15 @@ describe("UsdcSpotManager", function () {
279279
expect(await manager.prevDeviation()).to.eq("0");
280280
expect(await manager.isOverweightSpot()).to.eq(true);
281281
await expect(manager.rebalance()).not.to.be.reverted;
282-
expect(await manager.prevDeviation()).to.eq(percFP("1.11579057353024426"));
282+
expect(await manager.prevDeviation()).to.eq(percFP("1.111560295732100833"));
283283
});
284284
});
285285

286286
describe("when overweight usdc", function () {
287287
it("should remove limit range", async function () {
288288
const { manager, mockVault, mockPool } = await loadFixture(setupContracts);
289289

290-
await mockVault.mockMethod("getTwap()", [72000]);
290+
await mockVault.mockMethod("getTwap()", [66200]);
291291
await mockVault.mockMethod("limitLower()", [73000]);
292292
await mockVault.mockMethod("limitUpper()", [75000]);
293293
await mockPool.mockCall(
@@ -309,7 +309,7 @@ describe("UsdcSpotManager", function () {
309309
expect(await manager.prevDeviation()).to.eq("0");
310310
expect(await manager.isOverweightSpot()).to.eq(false);
311311
await expect(manager.rebalance()).not.to.be.reverted;
312-
expect(await manager.prevDeviation()).to.eq(percFP("1.115790573530244260"));
312+
expect(await manager.prevDeviation()).to.eq(percFP("1.111560295732100833"));
313313
});
314314
});
315315
});
@@ -319,7 +319,7 @@ describe("UsdcSpotManager", function () {
319319
it("should remove limit range", async function () {
320320
const { manager, mockVault, mockPool } = await loadFixture(setupContracts);
321321

322-
await mockVault.mockMethod("getTwap()", [72000]);
322+
await mockVault.mockMethod("getTwap()", [66200]);
323323
await mockVault.mockMethod("limitLower()", [40000]);
324324
await mockVault.mockMethod("limitUpper()", [45000]);
325325
await mockVault.mockMethod("period()", [86400]);
@@ -328,7 +328,7 @@ describe("UsdcSpotManager", function () {
328328
await mockVault.mockMethod("rebalance()", []);
329329
await manager.rebalance();
330330

331-
await mockVault.mockMethod("getTwap()", [70000]);
331+
await mockVault.mockMethod("getTwap()", [67800]);
332332
await mockVault.mockMethod("limitLower()", [60000]);
333333
await mockVault.mockMethod("limitUpper()", [65000]);
334334
await mockPool.mockCall(
@@ -343,18 +343,18 @@ describe("UsdcSpotManager", function () {
343343
[],
344344
);
345345

346-
expect(await manager.prevDeviation()).to.eq(percFP("1.115790573530244260"));
346+
expect(await manager.prevDeviation()).to.eq(percFP("1.111560295732100833"));
347347
expect(await manager.isOverweightSpot()).to.eq(true);
348348
await expect(manager.rebalance()).not.to.be.reverted;
349-
expect(await manager.prevDeviation()).to.eq(percFP("0.913541191300990579"));
349+
expect(await manager.prevDeviation()).to.eq(percFP("0.947216779268338333"));
350350
});
351351
});
352352

353353
describe("when overweight usdc", function () {
354354
it("should keep limit range", async function () {
355355
const { manager, mockVault } = await loadFixture(setupContracts);
356356

357-
await mockVault.mockMethod("getTwap()", [72000]);
357+
await mockVault.mockMethod("getTwap()", [66200]);
358358
await mockVault.mockMethod("limitLower()", [40000]);
359359
await mockVault.mockMethod("limitUpper()", [45000]);
360360
await mockVault.mockMethod("period()", [86400]);
@@ -363,15 +363,15 @@ describe("UsdcSpotManager", function () {
363363
await mockVault.mockMethod("rebalance()", []);
364364
await manager.rebalance();
365365

366-
await mockVault.mockMethod("getTwap()", [70000]);
366+
await mockVault.mockMethod("getTwap()", [67800]);
367367
await mockVault.mockMethod("limitLower()", [75000]);
368368
await mockVault.mockMethod("limitUpper()", [80000]);
369369
await mockVault.clearMockMethod("emergencyBurn(int24,int24,uint128)");
370370

371-
expect(await manager.prevDeviation()).to.eq(percFP("1.115790573530244260"));
371+
expect(await manager.prevDeviation()).to.eq(percFP("1.111560295732100833"));
372372
expect(await manager.isOverweightSpot()).to.eq(false);
373373
await expect(manager.rebalance()).not.to.be.reverted;
374-
expect(await manager.prevDeviation()).to.eq(percFP("0.913541191300990579"));
374+
expect(await manager.prevDeviation()).to.eq(percFP("0.947216779268338333"));
375375
});
376376
});
377377
});
@@ -381,7 +381,7 @@ describe("UsdcSpotManager", function () {
381381
it("should not force rebalance", async function () {
382382
const { manager, mockVault, mockPool } = await loadFixture(setupContracts);
383383

384-
await mockVault.mockMethod("getTwap()", [70500]);
384+
await mockVault.mockMethod("getTwap()", [67800]);
385385
await mockVault.mockMethod("limitLower()", [40000]);
386386
await mockVault.mockMethod("limitUpper()", [45000]);
387387
await mockVault.mockMethod("rebalance()", []);
@@ -399,7 +399,7 @@ describe("UsdcSpotManager", function () {
399399
expect(await manager.prevDeviation()).to.eq("0");
400400
expect(await manager.isOverweightSpot()).to.eq(true);
401401
await expect(manager.rebalance()).not.to.be.reverted;
402-
expect(await manager.prevDeviation()).to.eq(percFP("0.960377048978079093"));
402+
expect(await manager.prevDeviation()).to.eq(percFP("0.947216779268338333"));
403403
});
404404
});
405405
});
@@ -409,7 +409,7 @@ describe("UsdcSpotManager", function () {
409409
it("should not force rebalance", async function () {
410410
const { manager, mockVault, mockPool } = await loadFixture(setupContracts);
411411

412-
await mockVault.mockMethod("getTwap()", [72000]);
412+
await mockVault.mockMethod("getTwap()", [66200]);
413413
await mockVault.mockMethod("limitLower()", [40000]);
414414
await mockVault.mockMethod("limitUpper()", [45000]);
415415
await mockVault.mockMethod("period()", [86400]);
@@ -423,7 +423,7 @@ describe("UsdcSpotManager", function () {
423423
await mockVault.clearMockCall("period()", []);
424424
await mockVault.clearMockMethod("emergencyBurn(int24,int24,uint128)");
425425

426-
await mockVault.mockMethod("getTwap()", [71500]);
426+
await mockVault.mockMethod("getTwap()", [66800]);
427427
await mockVault.mockMethod("limitLower()", [75000]);
428428
await mockVault.mockMethod("limitUpper()", [80000]);
429429
await mockPool.mockCall(
@@ -433,10 +433,10 @@ describe("UsdcSpotManager", function () {
433433
);
434434
await mockVault.mockMethod("emergencyBurn(int24,int24,uint128)", []);
435435

436-
expect(await manager.prevDeviation()).to.eq(percFP("1.115790573530244260"));
436+
expect(await manager.prevDeviation()).to.eq(percFP("1.111560295732100833"));
437437
expect(await manager.isOverweightSpot()).to.eq(false);
438438
await expect(manager.rebalance()).not.to.be.reverted;
439-
expect(await manager.prevDeviation()).to.eq(percFP("1.061375478380992817"));
439+
expect(await manager.prevDeviation()).to.eq(percFP("1.0468312037404625"));
440440
});
441441
});
442442
});

0 commit comments

Comments
 (0)