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

Deployment: Mode v2 #185

Merged
merged 3 commits into from
Nov 12, 2024
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
5 changes: 5 additions & 0 deletions scripts/generate_whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const CHAIN_ARGS = {
RPC_URL: Deno.env.get("OPTIMISM_RPC_URL"),
TOKENS_FILE: "src/constants/Velodrome-Whitelisted.csv",
OUTPUT_FILE: "src/constants/optimismWhitelistedTokens.json"
},
"mode": {
RPC_URL: Deno.env.get("MODE_RPC_URL"),
TOKENS_FILE: "src/constants/Mode-Whitelisted.csv",
OUTPUT_FILE: "src/constants/modeWhitelistedTokens.json"
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ const MODE_CONSTANTS: chainConstants = {
usdc: findToken(MODE_WHITELISTED_TOKENS, "USDC"),
oracle: {
getAddress: (blockNumber: number) => {
return "0xC7d87726753d2e7a8823db6c96a4f44e4D502a21";
return "0xE50621a0527A43534D565B67D64be7C79807F269";
},
startBlock: 15407790,
startBlock: 15591759,
updateDelta: 60 * 60, // 1 hour
},
rewardToken: {
Expand Down
64 changes: 37 additions & 27 deletions src/EventHandlers/CLPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,34 @@ function updateCLPoolFees(
token0Instance: Token | undefined,
token1Instance: Token | undefined
) {

let tokenUpdateData = {
totalFees0: liquidityPoolAggregator.totalFees0,
totalFees1: liquidityPoolAggregator.totalFees1,
totalFeesUSD: liquidityPoolAggregator.totalFeesUSD,
};

tokenUpdateData.totalFees0 += event.params.amount0;
tokenUpdateData.totalFees1 += event.params.amount1;

if (token0Instance) {
const incomingFees0 = normalizeTokenAmountTo1e18(
const normalizedFees0 = normalizeTokenAmountTo1e18(
event.params.amount0,
Number(token0Instance.decimals)
);
tokenUpdateData.totalFees0 += incomingFees0;
tokenUpdateData.totalFeesUSD += multiplyBase1e18(
incomingFees0,
normalizedFees0,
token0Instance.pricePerUSDNew
);
}

if (token1Instance) {
const incomingFees1 = normalizeTokenAmountTo1e18(
const normalizedFees1 = normalizeTokenAmountTo1e18(
event.params.amount1,
Number(token1Instance.decimals)
);
tokenUpdateData.totalFees1 += incomingFees1;
tokenUpdateData.totalFeesUSD += multiplyBase1e18(
incomingFees1,
normalizedFees1,
token1Instance.pricePerUSDNew
);
}
Expand All @@ -81,31 +83,37 @@ function updateCLPoolLiquidity(
token0Instance: Token | undefined,
token1Instance: Token | undefined
) {

let tokenUpdateData = {
totalLiquidityUSD: 0n,
reserve0: 0n,
reserve1: 0n,
normalizedReserve0: 0n,
normalizedReserve1: 0n,
};

// Update normalized reserves regardles of whether the token is priced
tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18(
event.params.amount0,
Number(token0Instance?.decimals || 18)
);
// Update reserves regardles of whether the token is priced
tokenUpdateData.reserve0 += event.params.amount0;

tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18(
event.params.amount1,
Number(token1Instance?.decimals || 18)
);
tokenUpdateData.reserve1 += event.params.amount1;

if (token0Instance) {
tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18(
event.params.amount0,
Number(token0Instance.decimals || 18)
);

tokenUpdateData.totalLiquidityUSD += multiplyBase1e18(
tokenUpdateData.normalizedReserve0,
liquidityPoolAggregator.token0Price
);
}

if (token1Instance) {
tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18(
event.params.amount1,
Number(token1Instance.decimals || 18)
);
tokenUpdateData.totalLiquidityUSD += multiplyBase1e18(
tokenUpdateData.normalizedReserve1,
liquidityPoolAggregator.token1Price
Expand Down Expand Up @@ -162,9 +170,9 @@ CLPool.Burn.handlerWithLoader({

const liquidityPoolDiff = {
reserve0:
liquidityPoolAggregator.reserve0 + tokenUpdateData.normalizedReserve0,
liquidityPoolAggregator.reserve0 + tokenUpdateData.reserve0,
reserve1:
liquidityPoolAggregator.reserve1 + tokenUpdateData.normalizedReserve1,
liquidityPoolAggregator.reserve1 + tokenUpdateData.reserve1,
totalLiquidityUSD:
liquidityPoolAggregator.totalLiquidityUSD +
tokenUpdateData.totalLiquidityUSD,
Expand Down Expand Up @@ -385,9 +393,9 @@ CLPool.Mint.handlerWithLoader({

const liquidityPoolDiff = {
reserve0:
liquidityPoolAggregator.reserve0 + tokenUpdateData.normalizedReserve0,
liquidityPoolAggregator.reserve0 + tokenUpdateData.reserve0,
reserve1:
liquidityPoolAggregator.reserve1 + tokenUpdateData.normalizedReserve1,
liquidityPoolAggregator.reserve1 + tokenUpdateData.reserve1,
totalLiquidityUSD:
liquidityPoolAggregator.totalLiquidityUSD +
tokenUpdateData.totalLiquidityUSD,
Expand Down Expand Up @@ -460,6 +468,7 @@ CLPool.Swap.handlerWithLoader({
if (loaderReturn && loaderReturn.liquidityPoolAggregator) {
const { liquidityPoolAggregator, token0Instance, token1Instance } = loaderReturn;

// Delta that will be added to the liquidity pool aggregator
let tokenUpdateData = {
netAmount0: 0n,
netAmount1: 0n,
Expand All @@ -468,26 +477,27 @@ CLPool.Swap.handlerWithLoader({
volumeInUSD: 0n,
};

tokenUpdateData.netAmount0 = abs(event.params.amount0);
tokenUpdateData.netAmount1 = abs(event.params.amount1);

if (token0Instance) {
tokenUpdateData.netAmount0 = normalizeTokenAmountTo1e18(
event.params.amount0,
const normalizedAmount0 = normalizeTokenAmountTo1e18(
abs(event.params.amount0),
Number(token0Instance.decimals)
);
tokenUpdateData.netAmount0 = abs(tokenUpdateData.netAmount0);
tokenUpdateData.netVolumeToken0USD = multiplyBase1e18(
tokenUpdateData.netAmount0,
normalizedAmount0,
token0Instance.pricePerUSDNew
);
}

if (token1Instance) {
tokenUpdateData.netAmount1 = normalizeTokenAmountTo1e18(
event.params.amount1,
const normalizedAmount1 = normalizeTokenAmountTo1e18(
abs(event.params.amount1),
Number(token1Instance.decimals)
);
tokenUpdateData.netAmount1 = abs(tokenUpdateData.netAmount1);
tokenUpdateData.netVolumeToken1USD = multiplyBase1e18(
tokenUpdateData.netAmount1,
normalizedAmount1,
token1Instance.pricePerUSDNew
);
}
Expand Down
36 changes: 19 additions & 17 deletions src/EventHandlers/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Pool.Fees.handlerWithLoader({
totalFeesUSD: 0n,
};

tokenUpdateData.totalFees0 = event.params.amount0
if (token0Instance) {
tokenUpdateData.totalFees0 = normalizeTokenAmountTo1e18(
event.params.amount0,
Expand All @@ -80,6 +81,7 @@ Pool.Fees.handlerWithLoader({
);
}

tokenUpdateData.totalFees1 = event.params.amount1
if (token1Instance) {
tokenUpdateData.totalFees1 = normalizeTokenAmountTo1e18(
event.params.amount1,
Expand Down Expand Up @@ -159,26 +161,26 @@ Pool.Swap.handlerWithLoader({
volumeInUSD: 0n,
};

tokenUpdateData.netAmount0 = event.params.amount0In + event.params.amount0Out;
if (token0Instance) {
tokenUpdateData.netAmount0 = normalizeTokenAmountTo1e18(
const normalizedAmount0 = normalizeTokenAmountTo1e18(
event.params.amount0In + event.params.amount0Out,
Number(token0Instance.decimals)
);
tokenUpdateData.netAmount0 = abs(tokenUpdateData.netAmount0);
tokenUpdateData.netVolumeToken0USD = multiplyBase1e18(
tokenUpdateData.netAmount0,
normalizedAmount0,
token0Instance.pricePerUSDNew
);
}

tokenUpdateData.netAmount1 = event.params.amount1In + event.params.amount1Out;
if (token1Instance) {
tokenUpdateData.netAmount1 = normalizeTokenAmountTo1e18(
const normalizedAmount1 = normalizeTokenAmountTo1e18(
event.params.amount1In + event.params.amount1Out,
Number(token1Instance.decimals)
);
tokenUpdateData.netAmount1 = abs(tokenUpdateData.netAmount1);
tokenUpdateData.netVolumeToken1USD = multiplyBase1e18(
tokenUpdateData.netAmount1,
normalizedAmount1,
token1Instance.pricePerUSDNew
);
}
Expand Down Expand Up @@ -295,18 +297,13 @@ Pool.Sync.handlerWithLoader({
token1PricePerUSDNew: token1Instance?.pricePerUSDNew ?? liquidityPool.token1Price,
};

tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18(
event.params.reserve0,
Number(token0Instance?.decimals || 18)
);

tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18(
event.params.reserve1,
Number(token1Instance?.decimals || 18)
);

// Update price and liquidity if the token is priced
if (token0Instance) {
tokenUpdateData.normalizedReserve0 += normalizeTokenAmountTo1e18(
event.params.reserve0,
Number(token0Instance?.decimals || 18)
);
tokenUpdateData.token0PricePerUSDNew = token0Instance.pricePerUSDNew;
tokenUpdateData.totalLiquidityUSD += multiplyBase1e18(
tokenUpdateData.normalizedReserve0,
Expand All @@ -315,6 +312,11 @@ Pool.Sync.handlerWithLoader({
}

if (token1Instance) {
tokenUpdateData.normalizedReserve1 += normalizeTokenAmountTo1e18(
event.params.reserve1,
Number(token1Instance?.decimals || 18)
);

tokenUpdateData.token1PricePerUSDNew = token1Instance.pricePerUSDNew;
tokenUpdateData.totalLiquidityUSD += multiplyBase1e18(
tokenUpdateData.normalizedReserve1,
Expand All @@ -323,8 +325,8 @@ Pool.Sync.handlerWithLoader({
}

const liquidityPoolDiff = {
reserve0: tokenUpdateData.normalizedReserve0,
reserve1: tokenUpdateData.normalizedReserve1,
reserve0: event.params.reserve0,
reserve1: event.params.reserve1,
totalLiquidityUSD: tokenUpdateData.totalLiquidityUSD || liquidityPool.totalLiquidityUSD,
token0Price: tokenUpdateData.token0PricePerUSDNew,
token1Price: tokenUpdateData.token1PricePerUSDNew,
Expand Down
6 changes: 3 additions & 3 deletions src/PriceOracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function set_whitelisted_prices(
): Promise<void> {
// Skip if not yet available
let startBlock =
CHAIN_CONSTANTS[chainId].oracle.startBlock || Number.MAX_SAFE_INTEGER;
CHAIN_CONSTANTS[chainId].oracle.startBlock;

if (blockNumber < startBlock) return;

Expand All @@ -105,8 +105,7 @@ export async function set_whitelisted_prices(
if (!tokensNeedUpdate) return;

// Get token data for chain
const tokenData =
chainId === 10 ? OPTIMISM_WHITELISTED_TOKENS : BASE_WHITELISTED_TOKENS;
const tokenData = CHAIN_CONSTANTS[chainId].whitelistedTokens;

// Get prices from oracle and filter if token is not created yet
const addresses = tokenData
Expand Down Expand Up @@ -177,6 +176,7 @@ export async function set_whitelisted_prices(
chainId: chainId,
lastUpdatedTimestamp: blockDatetime,
};

context.TokenPriceSnapshot.set(tokenPrice);
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants/Mode-Whitelisted.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
address,blocktime
0x4200000000000000000000000000000000000006,15405187
0xd988097fb8612cc24eeC14542bC03424c656005f,15405187
0x4200000000000000000000000000000000000006,15405187
0xDfc7C877a950e49D2610114102175A06C2e3167a,15405187
0x82af49447d8a07e3bd255e69ecc69b783092f3b8,15405187
0x8b2EeA0999876AAB1E7955fe01A5D261b570452C,15405187
Expand Down
12 changes: 6 additions & 6 deletions src/constants/modeWhitelistedTokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
"decimals": 18,
"createdBlock": 15405187
},
{
"address": "0xd988097fb8612cc24eeC14542bC03424c656005f",
"symbol": "USDC",
"decimals": 6,
"createdBlock": 15405187
},
{
"address": "0x4200000000000000000000000000000000000006",
"symbol": "WETH",
"decimals": 18,
"createdBlock": 15405187
},
{
"address": "0xd988097fb8612cc24eeC14542bC03424c656005f",
"symbol": "USDC",
"decimals": 6,
"createdBlock": 15405187
}
]
Loading
Loading