Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#patch); pricelib; fix UniswapForksRouter.getLpTokenLiquidityUsdc decimals; add DPR override #2348

Merged
merged 5 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions deployment/deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@
"status": "prod",
"versions": {
"schema": "1.2.0",
"subgraph": "1.1.1",
"subgraph": "1.1.2",
"methodology": "1.0.0"
},
"files": {
Expand All @@ -1374,7 +1374,7 @@
"status": "prod",
"versions": {
"schema": "1.2.0",
"subgraph": "1.1.1",
"subgraph": "1.1.2",
"methodology": "1.0.0"
},
"files": {
Expand Down Expand Up @@ -2618,7 +2618,7 @@
"status": "prod",
"versions": {
"schema": "1.4.0",
"subgraph": "1.0.6",
"subgraph": "1.0.7",
"methodology": "1.0.0"
},
"files": {
Expand Down Expand Up @@ -8164,7 +8164,7 @@
"status": "prod",
"versions": {
"schema": "1.2.0",
"subgraph": "1.1.1",
"subgraph": "1.1.2",
"methodology": "1.0.0"
},
"services": {
Expand All @@ -8185,7 +8185,7 @@
"status": "prod",
"versions": {
"schema": "1.2.0",
"subgraph": "1.1.1",
"subgraph": "1.1.2",
"methodology": "1.0.0"
},
"services": {
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/_reference_/src/prices/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const CHAIN_LINK_USD_ADDRESS = Address.fromString(
"0x0000000000000000000000000000000000000348"
);

export const PRICE_LIB_VERSION = "1.3.1";
export const PRICE_LIB_VERSION = "1.3.2";

export const INT_ZERO = 0 as i32;
export const INT_ONE = 1 as i32;
Expand Down
9 changes: 9 additions & 0 deletions subgraphs/_reference_/src/prices/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export function isNullAddress(tokenAddr: Address): boolean {
return tokenAddr.equals(constants.NULL.TYPE_ADDRESS);
}

export function bigIntToBigDecimal(
quantity: BigInt,
decimals: i32 = constants.DEFAULT_DECIMALS.toI32()
): BigDecimal {
return quantity.divDecimal(
constants.BIGINT_TEN.pow(decimals as u8).toBigDecimal()
);
}

export function readValue<T>(
callResult: ethereum.CallResult<T>,
defaultValue: T
Expand Down
37 changes: 31 additions & 6 deletions subgraphs/_reference_/src/prices/config/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const HARDCODED_STABLES: Address[] = [
///////////////////////////////////////////////////////////////////////////

// https://github.com/messari/subgraphs/issues/2090
class spellOverride implements OracleConfig {
class SpellOverride implements OracleConfig {
oracleCount(): number {
return constants.INT_ONE;
}
Expand All @@ -119,7 +119,7 @@ class spellOverride implements OracleConfig {
}

// https://github.com/messari/subgraphs/issues/726
class stETHOverride implements OracleConfig {
class StETHOverride implements OracleConfig {
oracleCount(): number {
return constants.INT_ONE;
}
Expand All @@ -136,7 +136,24 @@ class stETHOverride implements OracleConfig {
}

// https://github.com/messari/subgraphs/issues/2097
class baxaOverride implements OracleConfig {
class BaxaOverride implements OracleConfig {
oracleCount(): number {
return constants.INT_ONE;
}
oracleOrder(): string[] {
return [
constants.OracleType.UNISWAP_FORKS_ROUTER,
constants.OracleType.YEARN_LENS_ORACLE,
constants.OracleType.CHAINLINK_FEED,
constants.OracleType.CURVE_CALCULATIONS,
constants.OracleType.CURVE_ROUTER,
constants.OracleType.SUSHI_CALCULATIONS,
];
}
}

// https://github.com/messari/subgraphs/issues/2329
class DelperOverride implements OracleConfig {
oracleCount(): number {
return constants.INT_ONE;
}
Expand Down Expand Up @@ -240,7 +257,7 @@ export class config implements Configurations {
Address.fromString("0x090185f2135308bad17527004364ebcc2d37e5f6"), // SPELL
].includes(tokenAddr)
) {
return new spellOverride();
return new SpellOverride();
}
if (
tokenAddr &&
Expand All @@ -251,15 +268,23 @@ export class config implements Configurations {
block.number.gt(BigInt.fromString("14019699")) &&
block.number.lt(BigInt.fromString("14941265"))
) {
return new stETHOverride();
return new StETHOverride();
}
if (
tokenAddr &&
[
Address.fromString("0x91b08f4a7c1251dfccf5440f8894f8daa10c8de5"), // BAXA
].includes(tokenAddr)
) {
return new baxaOverride();
return new BaxaOverride();
}
if (
tokenAddr &&
[
Address.fromString("0x077416cc6242b3a7d8e42652b8a6a2599fda4a92"), // DPR
].includes(tokenAddr)
) {
return new DelperOverride();
}
}

Expand Down
4 changes: 2 additions & 2 deletions subgraphs/_reference_/src/prices/config/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const HARDCODED_STABLES: Address[] = [];
///////////////////////// ORACLE CONFIG OVERRIDES /////////////////////////
///////////////////////////////////////////////////////////////////////////

class someOverride implements OracleConfig {
class SomeOverride implements OracleConfig {
oracleCount(): number {
return constants.INT_ONE;
}
Expand Down Expand Up @@ -137,6 +137,6 @@ export class config implements Configurations {
tokenAddr: Address | null,
block: ethereum.Block | null
): OracleConfig | null {
return new someOverride();
return new SomeOverride();
}
}
6 changes: 3 additions & 3 deletions subgraphs/_reference_/src/prices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ export function getLiquidityBoundPrice(
.div(amount);

log.warning(
"[getLiquidityBoundPrice] reported (token price * amount): ({} * {}) bound to: {} for token: {} due to insufficient liquidity: {}",
"[getLiquidityBoundPrice] token: {} (reported price * amount): ({} * {}) bound to available liquidity: {}; new price: {}",
[
tokenAddress.toHexString(),
tokenPrice.usdPrice.toString(),
amount.toString(),
liquidityBoundPriceUSD.toString(),
tokenAddress.toHexString(),
liquidity.toString(),
liquidityBoundPriceUSD.toString(),
]
);
}
Expand Down
20 changes: 8 additions & 12 deletions subgraphs/_reference_/src/prices/routers/UniswapForksRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,6 @@ function getLpTokenLiquidityUsdc(
): CustomPriceType {
const uniSwapPair = UniswapPairContract.bind(lpAddress);

const token0Call = uniSwapPair.try_token0();
if (token0Call.reverted) return new CustomPriceType();
const token0Address = token0Call.value;

const token1Call = uniSwapPair.try_token1();
if (token1Call.reverted) return new CustomPriceType();
const token1Address = token1Call.value;
Expand All @@ -267,10 +263,8 @@ function getLpTokenLiquidityUsdc(
if (reservesCall.reverted) return new CustomPriceType();
const reserves = reservesCall.value;

let wethReserves = constants.BIGINT_ZERO;
if (token0Address == wethAddress) {
wethReserves = reserves.value0;
} else if (token1Address == wethAddress) {
let wethReserves = reserves.value0;
if (token1Address == wethAddress) {
wethReserves = reserves.value1;
}

Expand All @@ -282,13 +276,15 @@ function getLpTokenLiquidityUsdc(
const wethDecimals = utils.getTokenDecimals(wethAddress);

if (wethReserves.notEqual(constants.BIGINT_ZERO)) {
const liquidity = wethReserves
.toBigDecimal()
.div(constants.BIGINT_TEN.pow(wethDecimals.toI32() as u8).toBigDecimal())
const liquidityUSDC = utils
.bigIntToBigDecimal(
wethReserves,
wethDecimals.toI32() - constants.DEFAULT_USDC_DECIMALS
)
.times(wethPrice.usdPrice);

return CustomPriceType.initialize(
liquidity,
liquidityUSDC,
constants.DEFAULT_USDC_DECIMALS,
constants.OracleType.UNISWAP_FORKS_ROUTER
);
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/_reference_/src/sdk/protocols/bridge/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Bridge SDK
# Bridge SDK `1.0.1`

## Setup

Expand Down
13 changes: 13 additions & 0 deletions subgraphs/_reference_/src/sdk/protocols/bridge/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ import { BridgePoolType, TransactionType, TransferType } from "./enums";
import { getUnixDays, getUnixHours } from "../../util/events";
import { CustomEventType } from "../../util/events";

/**
* This file contains the AccountClass, which does
* the operations on the Account entity. This includes:
* - Creating a new Account
* - Updating an existing Account
*
* Schema Version: 1.2.0
* SDK Version: 1.0.1
* Author(s):
* - @jaimehgb
* - @dhruv-chauhan
*/

export class AccountManager {
protocol: Bridge;
tokens: TokenManager;
Expand Down
11 changes: 11 additions & 0 deletions subgraphs/_reference_/src/sdk/protocols/bridge/chainIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
import { BigInt, TypedMap } from "@graphprotocol/graph-ts";
import { Network } from "../../util/constants";

/**
* This file contains the network to chainId mapping
*
* Schema Version: 1.2.0
* SDK Version: 1.0.1
* Author(s):
* - @jaimehgb
* - @dhruv-chauhan
*/

export function chainIDToNetwork(chainID: BigInt): Network {
const network = chainIDs.get(chainID.toU64());
if (network) {
Expand Down Expand Up @@ -100,3 +110,4 @@ setChainID(1313161554, Network.AURORA);
setChainID(1666600000, Network.HARMONY);
setChainID(11297108109, Network.PALM);
setChainID(836542336838601, Network.CURIO);
setChainID(8453, Network.BASE);
37 changes: 32 additions & 5 deletions subgraphs/_reference_/src/sdk/protocols/bridge/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
BIGINT_ZERO,
RewardTokenType,
} from "../../util/constants";
import { bigIntToBigDecimal } from "../../util/numbers";
import {
sortArrayByReference,
sortBytesArray,
Expand All @@ -34,6 +33,17 @@ import { PoolSnapshot } from "./poolSnapshot";
import { SDK } from ".";
import { CustomEventType } from "../../util/events";

/**
* This file contains the PoolManager, which is used to
* initialize new pools in the protocol.
*
* Schema Version: 1.2.0
* SDK Version: 1.0.1
* Author(s):
* - @jaimehgb
* - @dhruv-chauhan
*/

type onCreatePoolCallback<T> = (
event: CustomEventType,
pool: Pool,
Expand Down Expand Up @@ -335,6 +345,25 @@ export class Pool {
this.setNetValueExportedUSD(val);
}

/**
* Utility function to update token price.
*
* @param token
* @returns
*/
setTokenPrice(token: Token): void {
if (
!token.lastPriceBlockNumber ||
(token.lastPriceBlockNumber &&
token.lastPriceBlockNumber! < this.protocol.event.block.number)
) {
const pricePerToken = this.protocol.getTokenPricer().getTokenPrice(token);
token.lastPriceUSD = pricePerToken;
token.lastPriceBlockNumber = this.protocol.event.block.number;
token.save();
}
}

/**
* Utility function to convert some amount of input token to USD.
*
Expand All @@ -343,11 +372,9 @@ export class Pool {
*/
getInputTokenAmountPrice(amount: BigInt): BigDecimal {
const token = this.getInputToken();
const price = this.protocol.getTokenPricer().getTokenPrice(token);
token.lastPriceUSD = price;
token.save();
this.setTokenPrice(token);

return bigIntToBigDecimal(amount, token.decimals).times(price);
return this.protocol.getTokenPricer().getAmountValueUSD(token, amount);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions subgraphs/_reference_/src/sdk/protocols/bridge/poolSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import {
} from "../../../../generated/schema";
import { SECONDS_PER_DAY_BI, SECONDS_PER_HOUR_BI } from "../../util/constants";

/**
* This file contains the PoolSnapshot, which is used to
* make all of the storage changes that occur in the pool daily and hourly snapshots.
*
* Schema Version: 1.2.0
* SDK Version: 1.0.1
* Author(s):
* - @jaimehgb
* - @dhruv-chauhan
*/

export class PoolSnapshot {
pool: PoolSchema;
event: CustomEventType;
Expand Down
11 changes: 11 additions & 0 deletions subgraphs/_reference_/src/sdk/protocols/bridge/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ import { chainIDToNetwork, networkToChainID } from "./chainIds";
import { SDK } from ".";
import { CustomEventType } from "../../util/events";

/**
* This file contains the Bridge class, which is used to
* make all of the storage changes that occur in a protocol.
*
* Schema Version: 1.2.0
* SDK Version: 1.0.1
* Author(s):
* - @jaimehgb
* - @dhruv-chauhan
*/

/**
* Bridge is a wrapper around the BridgeProtocolSchema entity that takes care of
* safely and conveniently updating the entity. Updating the Bridge entity using this
Expand Down
12 changes: 12 additions & 0 deletions subgraphs/_reference_/src/sdk/protocols/bridge/protocolSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import {
} from "../../util/constants";
import { CustomEventType } from "../../util/events";

/**
* This file contains the ProtocolSnapshot, which is used to
* make all of the storage changes that occur in the protocol's
* daily and hourly snapshots.
*
* Schema Version: 1.2.0
* SDK Version: 1.0.1
* Author(s):
* - @jaimehgb
* - @dhruv-chauhan
*/

const SnapshotHelperID = Bytes.fromUTF8("_ProtocolSnapshotHelper");
const ActivityHelperID = Bytes.fromUTF8("_ActivityHelper");

Expand Down
Loading
Loading