Skip to content

Commit b259324

Browse files
committed
fix: add PriceFeedRegister.loadUpdatablePriceFeeds
1 parent 4e894cd commit b259324

20 files changed

+233
-65
lines changed

src/sdk/abi/compressors.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,11 @@ export const iMarketCompressorAbi = [
728728
type: "address",
729729
internalType: "address",
730730
},
731+
{
732+
name: "acl",
733+
type: "address",
734+
internalType: "address",
735+
},
731736
{
732737
name: "name",
733738
type: "string",
@@ -1691,6 +1696,11 @@ export const iMarketCompressorAbi = [
16911696
type: "address",
16921697
internalType: "address",
16931698
},
1699+
{
1700+
name: "acl",
1701+
type: "address",
1702+
internalType: "address",
1703+
},
16941704
{
16951705
name: "name",
16961706
type: "string",
@@ -2589,6 +2599,64 @@ export const iMarketCompressorAbi = [
25892599
],
25902600
stateMutability: "view",
25912601
},
2602+
{
2603+
type: "function",
2604+
name: "getUpdatablePriceFeeds",
2605+
inputs: [
2606+
{
2607+
name: "filter",
2608+
type: "tuple",
2609+
internalType: "struct MarketFilter",
2610+
components: [
2611+
{
2612+
name: "curators",
2613+
type: "address[]",
2614+
internalType: "address[]",
2615+
},
2616+
{
2617+
name: "pools",
2618+
type: "address[]",
2619+
internalType: "address[]",
2620+
},
2621+
{
2622+
name: "underlying",
2623+
type: "address",
2624+
internalType: "address",
2625+
},
2626+
],
2627+
},
2628+
],
2629+
outputs: [
2630+
{
2631+
name: "updatablePriceFeeds",
2632+
type: "tuple[]",
2633+
internalType: "struct BaseParams[]",
2634+
components: [
2635+
{
2636+
name: "addr",
2637+
type: "address",
2638+
internalType: "address",
2639+
},
2640+
{
2641+
name: "version",
2642+
type: "uint256",
2643+
internalType: "uint256",
2644+
},
2645+
{
2646+
name: "contractType",
2647+
type: "bytes32",
2648+
internalType: "bytes32",
2649+
},
2650+
{
2651+
name: "serializedParams",
2652+
type: "bytes",
2653+
internalType: "bytes",
2654+
},
2655+
],
2656+
},
2657+
],
2658+
stateMutability: "view",
2659+
},
25922660
{
25932661
type: "function",
25942662
name: "version",

src/sdk/market/MarketFactory.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { PoolFactory } from "./PoolFactory";
88
import { PriceOracleContract } from "./PriceOracleContract";
99

1010
export class MarketFactory extends SDKConstruct {
11-
public readonly riskCurator!: Address;
12-
public readonly poolFactory!: PoolFactory;
13-
public readonly priceOracle!: PriceOracleContract;
11+
public readonly acl: Address;
12+
public readonly riskCurator: Address;
13+
public readonly poolFactory: PoolFactory;
14+
public readonly priceOracle: PriceOracleContract;
1415
public readonly creditManagers: CreditFactory[] = [];
1516
/**
1617
* Original data received from compressor
@@ -21,6 +22,7 @@ export class MarketFactory extends SDKConstruct {
2122
super(sdk);
2223
this.state = marketData;
2324
this.riskCurator = marketData.owner;
25+
this.acl = marketData.acl;
2426

2527
for (const t of marketData.tokens) {
2628
sdk.tokensMeta.upsert(t.addr, t);

src/sdk/market/MarketRegister.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,34 @@ export class MarketRegister extends SDKConstruct {
6767
AP_MARKET_COMPRESSOR,
6868
3_10,
6969
);
70-
const markets = await this.sdk.provider.publicClient.readContract({
71-
address: marketCompressorAddress,
72-
abi: iMarketCompressorAbi,
73-
functionName: "getMarkets",
74-
args: [
70+
// to have correct prices we must first get all updatable price feeds
71+
await this.sdk.priceFeeds.loadUpdatablePriceFeeds(curators, pools);
72+
// the generate updates
73+
const { txs } = await this.sdk.priceFeeds.generatePriceFeedsUpdateTxs();
74+
// ...and push them using multicall before getting answers
75+
const resp = await simulateMulticall(this.provider.publicClient, {
76+
contracts: [
77+
...txs.map(rawTxToMulticallPriceUpdate),
7578
{
76-
curators,
77-
pools,
78-
underlying: ADDRESS_0X0,
79+
abi: iMarketCompressorAbi,
80+
address: marketCompressorAddress,
81+
functionName: "getMarkets",
82+
args: [
83+
{
84+
curators,
85+
pools,
86+
underlying: ADDRESS_0X0,
87+
},
88+
],
7989
},
8090
],
81-
// It's passed as ...rest in viem readContract action, but this might change
82-
// @ts-ignore
83-
gas: 500_000_000n,
91+
allowFailure: false,
92+
gas: 550_000_000n,
93+
batchSize: 0, // we cannot have price updates and compressor request in different batches
8494
});
8595

96+
const markets = resp.pop() as MarketData[];
97+
8698
for (const data of markets) {
8799
this.#markets.upsert(
88100
data.pool.baseParams.addr,

src/sdk/market/pricefeeds/AbstractPriceFeed.ts

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Abi, UnionOmit } from "viem";
1+
import type { Abi, RequiredBy, UnionOmit } from "viem";
22

33
import { ilpPriceFeedAbi } from "../../abi";
44
import type { PriceFeedTreeNode } from "../../base";
@@ -8,8 +8,13 @@ import type { PriceFeedStateHuman } from "../../types";
88
import { PriceFeedRef } from "./PriceFeedRef";
99
import type { IPriceFeedContract, PriceFeedContractType } from "./types";
1010

11+
export type PartialPriceFeedTreeNode = RequiredBy<
12+
Partial<PriceFeedTreeNode>,
13+
"baseParams"
14+
>;
15+
1116
export type PriceFeedConstructorArgs<abi extends Abi | readonly unknown[]> =
12-
PriceFeedTreeNode & {
17+
PartialPriceFeedTreeNode & {
1318
abi: abi;
1419
name: string;
1520
};
@@ -23,10 +28,11 @@ export abstract class AbstractPriceFeedContract<
2328
/**
2429
* True if the contract deployed at this address implements IUpdatablePriceFeed interface
2530
*/
26-
public readonly updatable: boolean;
27-
public readonly decimals: number;
28-
public readonly underlyingPriceFeeds: PriceFeedRef[];
29-
public readonly skipCheck: boolean;
31+
#updatable?: boolean;
32+
#decimals?: number;
33+
#underlyingPriceFeeds?: readonly PriceFeedRef[];
34+
#skipCheck?: boolean;
35+
3036
public hasLowerBoundCap = false;
3137

3238
constructor(sdk: GearboxSDK, args: PriceFeedConstructorArgs<abi>) {
@@ -37,13 +43,52 @@ export abstract class AbstractPriceFeedContract<
3743
contractType: args.baseParams.contractType,
3844
version: args.baseParams.version,
3945
});
40-
this.decimals = args.decimals;
41-
this.updatable = args.updatable;
42-
this.skipCheck = args.skipCheck;
43-
this.underlyingPriceFeeds = args.underlyingFeeds.map(
44-
(address, i) =>
45-
new PriceFeedRef(this.sdk, address, args.underlyingStalenessPeriods[i]),
46-
);
46+
this.#decimals = args.decimals;
47+
this.#updatable = args.updatable;
48+
this.#skipCheck = args.skipCheck;
49+
50+
if (args.underlyingFeeds && args.underlyingStalenessPeriods) {
51+
this.#underlyingPriceFeeds = args.underlyingFeeds.map(
52+
(address, i) =>
53+
new PriceFeedRef(
54+
this.sdk,
55+
address,
56+
args.underlyingStalenessPeriods![i],
57+
),
58+
);
59+
}
60+
}
61+
62+
public get loaded(): boolean {
63+
return this.#decimals !== undefined;
64+
}
65+
66+
public get decimals(): number {
67+
if (this.#decimals === undefined) {
68+
throw new Error("price feed has been initialized with BaseParams only");
69+
}
70+
return this.#decimals;
71+
}
72+
73+
public get updatable(): boolean {
74+
if (this.#updatable === undefined) {
75+
throw new Error("price feed has been initialized with BaseParams only");
76+
}
77+
return this.#updatable;
78+
}
79+
80+
public get skipCheck(): boolean {
81+
if (this.#skipCheck === undefined) {
82+
throw new Error("price feed has been initialized with BaseParams only");
83+
}
84+
return this.#skipCheck;
85+
}
86+
87+
public get underlyingPriceFeeds(): readonly PriceFeedRef[] {
88+
if (!this.#underlyingPriceFeeds) {
89+
throw new Error("price feed has been initialized with BaseParams only");
90+
}
91+
return this.#underlyingPriceFeeds;
4792
}
4893

4994
public get priceFeedType(): PriceFeedContractType {

src/sdk/market/pricefeeds/BalancerStablePriceFeed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { bptStablePriceFeedAbi, iBalancerStablePoolAbi } from "../../abi";
2-
import type { PriceFeedTreeNode } from "../../base";
32
import type { GearboxSDK } from "../../GearboxSDK";
43
import { AbstractLPPriceFeedContract } from "./AbstractLPPriceFeed";
4+
import type { PartialPriceFeedTreeNode } from "./AbstractPriceFeed";
55

66
type abi = typeof bptStablePriceFeedAbi;
77

88
export class BalancerStablePriceFeedContract extends AbstractLPPriceFeedContract<abi> {
9-
constructor(sdk: GearboxSDK, args: PriceFeedTreeNode) {
9+
constructor(sdk: GearboxSDK, args: PartialPriceFeedTreeNode) {
1010
super(sdk, {
1111
...args,
1212
name: "BalancerStablePriceFeed",

src/sdk/market/pricefeeds/BalancerWeightedPriceFeed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { bptWeightedPriceFeedAbi, iBalancerWeightedPoolAbi } from "../../abi";
2-
import type { PriceFeedTreeNode } from "../../base";
32
import type { GearboxSDK } from "../../GearboxSDK";
43
import { AbstractLPPriceFeedContract } from "./AbstractLPPriceFeed";
4+
import type { PartialPriceFeedTreeNode } from "./AbstractPriceFeed";
55

66
type abi = typeof bptWeightedPriceFeedAbi;
77

88
export class BalancerWeightedPriceFeedContract extends AbstractLPPriceFeedContract<abi> {
9-
constructor(sdk: GearboxSDK, args: PriceFeedTreeNode) {
9+
constructor(sdk: GearboxSDK, args: PartialPriceFeedTreeNode) {
1010
super(sdk, {
1111
...args,
1212
name: "BalancerWeighedPriceFeed",

src/sdk/market/pricefeeds/BoundedPriceFeed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { boundedPriceFeedAbi } from "../../abi";
2-
import type { PriceFeedTreeNode } from "../../base";
32
import type { GearboxSDK } from "../../GearboxSDK";
43
import type { BoundedOracleStateHuman } from "../../types";
4+
import type { PartialPriceFeedTreeNode } from "./AbstractPriceFeed";
55
import { AbstractPriceFeedContract } from "./AbstractPriceFeed";
66

77
type abi = typeof boundedPriceFeedAbi;
88

99
export class BoundedPriceFeedContract extends AbstractPriceFeedContract<abi> {
1010
upperBound = 0n;
1111

12-
constructor(sdk: GearboxSDK, args: PriceFeedTreeNode) {
12+
constructor(sdk: GearboxSDK, args: PartialPriceFeedTreeNode) {
1313
super(sdk, { ...args, name: "BoundedPriceFeed", abi: boundedPriceFeedAbi });
1414
}
1515

src/sdk/market/pricefeeds/ChainlinkPriceFeed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { chainlinkReadableAggregatorAbi } from "../../abi";
2-
import type { PriceFeedTreeNode } from "../../base";
32
import type { GearboxSDK } from "../../GearboxSDK";
3+
import type { PartialPriceFeedTreeNode } from "./AbstractPriceFeed";
44
import { AbstractPriceFeedContract } from "./AbstractPriceFeed";
55

66
type abi = typeof chainlinkReadableAggregatorAbi;
77

88
export class ChainlinkPriceFeedContract extends AbstractPriceFeedContract<abi> {
9-
constructor(sdk: GearboxSDK, args: PriceFeedTreeNode) {
9+
constructor(sdk: GearboxSDK, args: PartialPriceFeedTreeNode) {
1010
super(sdk, {
1111
...args,
1212
name: "ChainlinkPriceFeed",

src/sdk/market/pricefeeds/CompositePriceFeed.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import { compositePriceFeedAbi } from "../../abi";
2-
import type { PriceFeedTreeNode } from "../../base";
32
import type { GearboxSDK } from "../../GearboxSDK";
3+
import type { PartialPriceFeedTreeNode } from "./AbstractPriceFeed";
44
import { AbstractPriceFeedContract } from "./AbstractPriceFeed";
55
import type { PriceFeedRef } from "./PriceFeedRef";
66

77
type abi = typeof compositePriceFeedAbi;
88

99
export class CompositePriceFeedContract extends AbstractPriceFeedContract<abi> {
10-
constructor(sdk: GearboxSDK, args: PriceFeedTreeNode) {
10+
constructor(sdk: GearboxSDK, args: PartialPriceFeedTreeNode) {
1111
super(sdk, {
1212
...args,
1313
name: "CompositePriceFeed",
1414
abi: compositePriceFeedAbi,
15-
decimals: 8,
1615
});
1716
}
1817

src/sdk/market/pricefeeds/CurveCryptoPriceFeed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { curveCryptoLpPriceFeedAbi, iCurvePoolAbi } from "../../abi";
2-
import type { PriceFeedTreeNode } from "../../base";
32
import type { GearboxSDK } from "../../GearboxSDK";
43
import { AbstractLPPriceFeedContract } from "./AbstractLPPriceFeed";
4+
import type { PartialPriceFeedTreeNode } from "./AbstractPriceFeed";
55

66
type abi = typeof curveCryptoLpPriceFeedAbi;
77

88
export class CurveCryptoPriceFeedContract extends AbstractLPPriceFeedContract<abi> {
9-
constructor(sdk: GearboxSDK, args: PriceFeedTreeNode) {
9+
constructor(sdk: GearboxSDK, args: PartialPriceFeedTreeNode) {
1010
super(sdk, {
1111
...args,
1212
name: "CurveCryptoPriceFeed",

0 commit comments

Comments
 (0)