Skip to content

Commit b6a7f07

Browse files
committed
fix: move update prices from oracle to market register
1 parent 124cee5 commit b6a7f07

File tree

2 files changed

+64
-46
lines changed

2 files changed

+64
-46
lines changed

src/sdk/market/MarketRegister.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import type { Address } from "viem";
22

3-
import { iMarketCompressorAbi } from "../abi";
4-
import type { MarketData } from "../base";
3+
import { iMarketCompressorAbi, iPriceFeedCompressorAbi } from "../abi";
4+
import type { MarketData, PriceFeedMapEntry, PriceFeedTreeNode } from "../base";
55
import { SDKConstruct } from "../base";
66
import {
77
ADDRESS_0X0,
88
AP_MARKET_COMPRESSOR,
99
AP_MARKET_CONFIGURATOR,
10+
AP_PRICE_FEED_COMPRESSOR,
1011
} from "../constants";
1112
import type { GearboxSDK } from "../GearboxSDK";
1213
import type { ILogger, MarketStateHuman, TVL } from "../types";
1314
import { AddressMap, childLogger } from "../utils";
15+
import { simulateMulticall } from "../utils/viem";
1416
import type { CreditFactory } from "./CreditFactory";
1517
import { MarketConfiguratorContract } from "./MarketConfiguratorContract";
1618
import { MarketFactory } from "./MarketFactory";
1719
import type { PoolFactory } from "./PoolFactory";
20+
import { rawTxToMulticallPriceUpdate } from "./pricefeeds";
21+
import type { PriceOracleContract } from "./PriceOracleContract";
1822

1923
export class MarketRegister extends SDKConstruct {
2024
#logger?: ILogger;
@@ -91,6 +95,51 @@ export class MarketRegister extends SDKConstruct {
9195
this.#logger?.info(`loaded ${markets.length} markets`);
9296
}
9397

98+
/**
99+
* Loads new prices for given oracles from PriceFeedCompressor, defaults to all oracles
100+
* Does not update price feeds, only updates prices
101+
*/
102+
public async updatePrices(oracles?: Address[]): Promise<void> {
103+
const oraclez = oracles ?? this.markets.map(m => m.priceOracle.address);
104+
if (!oraclez.length) {
105+
return;
106+
}
107+
const { txs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs();
108+
const resp = await simulateMulticall(this.provider.publicClient, {
109+
contracts: [
110+
...txs.map(rawTxToMulticallPriceUpdate),
111+
...oraclez.map(o => ({
112+
abi: iPriceFeedCompressorAbi,
113+
address: this.sdk.addressProvider.getLatestVersion(
114+
AP_PRICE_FEED_COMPRESSOR,
115+
),
116+
functionName: "getPriceFeeds",
117+
args: [o],
118+
})),
119+
],
120+
allowFailure: false,
121+
gas: 550_000_000n,
122+
batchSize: 0, // we cannot have price updates and compressor request in different batches
123+
});
124+
const oraclesStates = resp.slice(txs.length) as any as Array<
125+
[PriceFeedMapEntry[], PriceFeedTreeNode[]]
126+
>;
127+
for (let i = 0; i < oraclez.length; i++) {
128+
const oracleAddr = oraclez[i];
129+
const oracle = this.findPriceOracle(oracleAddr);
130+
const [entries, tree] = oraclesStates[i];
131+
for (const { token, priceFeed, reserve } of entries) {
132+
const price = tree.find(n => n.baseParams.addr === priceFeed)?.answer
133+
?.price;
134+
if (reserve && price) {
135+
oracle.reservePrices.upsert(token, price);
136+
} else if (price) {
137+
oracle.mainPrices.upsert(token, price);
138+
}
139+
}
140+
}
141+
}
142+
94143
public get state(): MarketData[] {
95144
return this.markets.map(market => market.state);
96145
}
@@ -118,6 +167,15 @@ export class MarketRegister extends SDKConstruct {
118167
throw new Error(`cannot find credit manager ${creditManager}`);
119168
}
120169

170+
public findPriceOracle(address: Address): PriceOracleContract {
171+
for (const market of this.markets) {
172+
if (market.priceOracle.address.toLowerCase() === address.toLowerCase()) {
173+
return market.priceOracle;
174+
}
175+
}
176+
throw new Error(`cannot find price oracle ${address}`);
177+
}
178+
121179
public findByCreditManager(creditManager: Address): MarketFactory {
122180
const market = this.markets.find(m =>
123181
m.creditManagers.some(

src/sdk/market/PriceOracleContract.ts

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import type { Address, ContractEventName, Hex, Log } from "viem";
22
import { decodeFunctionData } from "viem";
33

4-
import {
5-
iPriceFeedCompressorAbi,
6-
iUpdatablePriceFeedAbi,
7-
priceOracleV3Abi,
8-
} from "../abi";
9-
import type {
10-
PriceFeedMapEntry,
11-
PriceFeedTreeNode,
12-
PriceOracleData,
13-
} from "../base";
4+
import { iUpdatablePriceFeedAbi, priceOracleV3Abi } from "../abi";
5+
import type { PriceFeedTreeNode, PriceOracleData } from "../base";
146
import { BaseContract } from "../base";
157
import type { NetworkType } from "../chain";
16-
import { AP_PRICE_FEED_COMPRESSOR } from "../constants";
178
import type { GearboxSDK } from "../GearboxSDK";
189
import type { PriceOracleV3StateHuman } from "../types";
1910
import { AddressMap } from "../utils";
20-
import { simulateMulticall } from "../utils/viem";
2111
import type {
2212
IPriceFeedContract,
2313
PriceFeedUsageType,
2414
UpdatePriceFeedsResult,
2515
} from "./pricefeeds";
26-
import { PriceFeedRef, rawTxToMulticallPriceUpdate } from "./pricefeeds";
16+
import { PriceFeedRef } from "./pricefeeds";
2717

2818
type abi = typeof priceOracleV3Abi;
2919

@@ -253,37 +243,7 @@ export class PriceOracleContract extends BaseContract<abi> {
253243
* Does not update price feeds, only updates prices
254244
*/
255245
public async updatePrices(): Promise<void> {
256-
const { txs } = await this.updatePriceFeeds();
257-
const resp = await simulateMulticall(this.provider.publicClient, {
258-
contracts: [
259-
...txs.map(rawTxToMulticallPriceUpdate),
260-
{
261-
abi: iPriceFeedCompressorAbi,
262-
address: this.sdk.addressProvider.getLatestVersion(
263-
AP_PRICE_FEED_COMPRESSOR,
264-
),
265-
functionName: "getPriceFeeds",
266-
args: [this.address],
267-
},
268-
],
269-
allowFailure: false,
270-
gas: 550_000_000n,
271-
batchSize: 0, // we cannot have price updates and compressor request in different batches
272-
});
273-
const [entries, tree] = resp.pop() as [
274-
PriceFeedMapEntry[],
275-
PriceFeedTreeNode[],
276-
];
277-
278-
entries.forEach(({ token, priceFeed, reserve }) => {
279-
const price = tree.find(n => n.baseParams.addr === priceFeed)?.answer
280-
?.price;
281-
if (reserve && price) {
282-
this.reservePrices.upsert(token, price);
283-
} else if (price) {
284-
this.mainPrices.upsert(token, price);
285-
}
286-
});
246+
await this.sdk.marketRegister.updatePrices([this.address]);
287247
}
288248

289249
#labelPriceFeed(

0 commit comments

Comments
 (0)