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

feat: Add price ref for Stableswap #256

Merged
merged 4 commits into from
Feb 9, 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
132 changes: 132 additions & 0 deletions subgraphs/exchange-stableswap/arb/abis/PriceLens0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
[
{
"inputs": [
{ "internalType": "address", "name": "nativePriceAggregator", "type": "address" },
{ "internalType": "address", "name": "wnative", "type": "address" }
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{ "inputs": [], "name": "IncorrectBaseAddress", "type": "error" },
{ "inputs": [], "name": "IncorrectChainlinkPrice", "type": "error" },
{ "inputs": [], "name": "IncorrectSourceAddress", "type": "error" },
{ "inputs": [], "name": "UnknowPrice", "type": "error" },
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "user", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "newOwner", "type": "address" }
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "token", "type": "address" },
{ "indexed": true, "internalType": "enum PriceLens0.SourceType", "name": "sourceType", "type": "uint8" },
{ "indexed": false, "internalType": "address", "name": "sourceAddress", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "baseAddress", "type": "address" }
],
"name": "UpdateSource",
"type": "event"
},
{
"inputs": [],
"name": "DECIMALS",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "_nativePriceAggregator",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "_wnative",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{ "internalType": "address", "name": "token", "type": "address" }],
"name": "getNativePrice",
"outputs": [{ "internalType": "uint256", "name": "nativePrice", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{ "internalType": "address", "name": "token", "type": "address" }],
"name": "getUsd",
"outputs": [{ "internalType": "uint256", "name": "price", "type": "uint256" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{ "internalType": "address", "name": "nativePriceAggregator", "type": "address" }],
"name": "setNativePriceAggregator",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{ "internalType": "address", "name": "token", "type": "address" },
{ "internalType": "enum PriceLens0.SourceType", "name": "sourceType", "type": "uint8" },
{ "internalType": "address", "name": "sourceAddress", "type": "address" },
{ "internalType": "address", "name": "baseAddress", "type": "address" }
],
"name": "setSource",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"components": [
{ "internalType": "address", "name": "token", "type": "address" },
{ "internalType": "enum PriceLens0.SourceType", "name": "sourceType", "type": "uint8" },
{ "internalType": "address", "name": "sourceAddress", "type": "address" },
{ "internalType": "address", "name": "baseAddress", "type": "address" }
],
"internalType": "struct PriceLens0.SourceList[]",
"name": "sourceList",
"type": "tuple[]"
}
],
"name": "setSourceList",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "sources",
"outputs": [
{ "internalType": "enum PriceLens0.SourceType", "name": "sourceType", "type": "uint8" },
{ "internalType": "address", "name": "sourceAddress", "type": "address" },
{ "internalType": "address", "name": "baseAddress", "type": "address" }
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [{ "internalType": "address", "name": "newOwner", "type": "address" }],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
46 changes: 11 additions & 35 deletions subgraphs/exchange-stableswap/arb/mappings/services/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { BigDecimal, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts";
import { Bundle, Pair, Swap, Token, Transaction } from "../../generated/schema";
import { BIG_DECIMAL_ZERO, BIG_INT_ONE, convertTokenToDecimal } from "../utils";
import { getTrackedFeeVolumeUSD, getTrackedVolumeUSD } from "../utils/pricing";
import { getOrCreateFactory } from "../utils/data";
import { updatePairDayData, updatePairHourData, updatePancakeDayData, updateTokenDayData } from "./dayUpdates";

Expand Down Expand Up @@ -52,48 +51,25 @@ export function swap(event: ethereum.Event, params: SwapParams): void {
}
let derivedFeeAmountUSD = derivedFeeAmountBNB.times(bundle.ethPrice);

// only accounts for volume through white listed tokens
let trackedAmountUSD = getTrackedVolumeUSD(
bundle as Bundle,
amount0Total,
token0 as Token,
amount1Total,
token1 as Token
);
let trackedFeeAmountUSD = getTrackedFeeVolumeUSD(
bundle as Bundle,
amount0Total,
token0 as Token,
amount1Total,
token1 as Token
);

let price0 = token0.derivedETH.times(bundle.ethPrice);
let price1 = token1.derivedETH.times(bundle.ethPrice);

let trackedAmountBNB: BigDecimal;
if (bundle.ethPrice.equals(BIG_DECIMAL_ZERO)) {
trackedAmountBNB = BIG_DECIMAL_ZERO;
} else {
trackedAmountBNB = trackedAmountUSD.div(bundle.ethPrice);
}

// update token0 global volume and token liquidity stats
token0.tradeVolume = token0.tradeVolume.plus(amount0In.plus(amount0Out));
token0.tradeVolumeUSD = token0.tradeVolumeUSD.plus(trackedAmountUSD);
token0.tradeVolumeUSD = token0.tradeVolumeUSD.plus(derivedAmountUSD);
token0.untrackedVolumeUSD = token0.untrackedVolumeUSD.plus(derivedAmountUSD);

// update token1 global volume and token liquidity stats
token1.tradeVolume = token1.tradeVolume.plus(amount1In.plus(amount1Out));
token1.tradeVolumeUSD = token1.tradeVolumeUSD.plus(trackedAmountUSD);
token1.tradeVolumeUSD = token1.tradeVolumeUSD.plus(derivedAmountUSD);
token1.untrackedVolumeUSD = token1.untrackedVolumeUSD.plus(derivedAmountUSD);

// update txn counts
token0.totalTransactions = token0.totalTransactions.plus(BIG_INT_ONE);
token1.totalTransactions = token1.totalTransactions.plus(BIG_INT_ONE);

// update pair volume data, use tracked amount if we have it as its probably more accurate
pair.volumeUSD = pair.volumeUSD.plus(trackedAmountUSD);
pair.volumeUSD = pair.volumeUSD.plus(derivedAmountUSD);
pair.volumeOutUSD = pair.volumeOutUSD.plus(amount0Out.times(price0)).plus(amount1Out.times(price1));
pair.volumeToken0 = pair.volumeToken0.plus(amount0Total);
pair.volumeToken1 = pair.volumeToken1.plus(amount1Total);
Expand All @@ -103,8 +79,8 @@ export function swap(event: ethereum.Event, params: SwapParams): void {

// update global values, only used tracked amounts for volume
let factory = getOrCreateFactory(pair.factory);
factory.totalVolumeUSD = factory.totalVolumeUSD.plus(trackedAmountUSD);
factory.totalVolumeETH = factory.totalVolumeETH.plus(trackedAmountBNB);
factory.totalVolumeUSD = factory.totalVolumeUSD.plus(derivedAmountUSD);
factory.totalVolumeETH = factory.totalVolumeETH.plus(derivedAmountBNB);
factory.untrackedVolumeUSD = factory.untrackedVolumeUSD.plus(derivedAmountUSD);
factory.totalTransactions = factory.totalTransactions.plus(BIG_INT_ONE);

Expand Down Expand Up @@ -140,8 +116,8 @@ export function swap(event: ethereum.Event, params: SwapParams): void {
swap.from = event.transaction.from;
swap.logIndex = event.logIndex;
// use the tracked amount if we have it
swap.amountUSD = trackedAmountUSD === BIG_DECIMAL_ZERO ? derivedAmountUSD : trackedAmountUSD;
swap.amountFeeUSD = trackedFeeAmountUSD === BIG_DECIMAL_ZERO ? derivedFeeAmountUSD : trackedFeeAmountUSD;
swap.amountUSD = derivedAmountUSD;
swap.amountFeeUSD = derivedFeeAmountUSD;
swap.save();

// update the transaction
Expand All @@ -160,21 +136,21 @@ export function swap(event: ethereum.Event, params: SwapParams): void {
let token1DayData = updateTokenDayData(token1 as Token, event);

// swap specific updating
pancakeDayData.dailyVolumeUSD = pancakeDayData.dailyVolumeUSD.plus(trackedAmountUSD);
pancakeDayData.dailyVolumeETH = pancakeDayData.dailyVolumeETH.plus(trackedAmountBNB);
pancakeDayData.dailyVolumeUSD = pancakeDayData.dailyVolumeUSD.plus(derivedAmountUSD);
pancakeDayData.dailyVolumeETH = pancakeDayData.dailyVolumeETH.plus(derivedAmountBNB);
pancakeDayData.dailyVolumeUntracked = pancakeDayData.dailyVolumeUntracked.plus(derivedAmountUSD);
pancakeDayData.save();

// swap specific updating for pair
pairDayData.dailyVolumeToken0 = pairDayData.dailyVolumeToken0.plus(amount0Total);
pairDayData.dailyVolumeToken1 = pairDayData.dailyVolumeToken1.plus(amount1Total);
pairDayData.dailyVolumeUSD = pairDayData.dailyVolumeUSD.plus(trackedAmountUSD);
pairDayData.dailyVolumeUSD = pairDayData.dailyVolumeUSD.plus(derivedAmountUSD);
pairDayData.save();

// update hourly pair data
pairHourData.hourlyVolumeToken0 = pairHourData.hourlyVolumeToken0.plus(amount0Total);
pairHourData.hourlyVolumeToken1 = pairHourData.hourlyVolumeToken1.plus(amount1Total);
pairHourData.hourlyVolumeUSD = pairHourData.hourlyVolumeUSD.plus(trackedAmountUSD);
pairHourData.hourlyVolumeUSD = pairHourData.hourlyVolumeUSD.plus(derivedAmountUSD);
pairHourData.save();

// swap specific updating for token0
Expand Down
13 changes: 12 additions & 1 deletion subgraphs/exchange-stableswap/arb/mappings/services/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,22 @@ export function sync(pairAddress: Address): void {
bundle.save();

let t0derivedETH = findBnbPerToken(token0 as Token);
let t1derivedETH = findBnbPerToken(token1 as Token);

// if t0 bnb is not found
if (t0derivedETH.equals(BIG_DECIMAL_ZERO) && t1derivedETH.gt(BIG_DECIMAL_ZERO)) {
t0derivedETH = t1derivedETH.times(pair.token1Price);
}

// if t1 bnb is not found
if (t1derivedETH.equals(BIG_DECIMAL_ZERO) && t0derivedETH.gt(BIG_DECIMAL_ZERO)) {
t1derivedETH = t0derivedETH.times(pair.token0Price);
}

token0.derivedETH = t0derivedETH;
token0.derivedUSD = t0derivedETH.times(bundle.ethPrice);
token0.save();

let t1derivedETH = findBnbPerToken(token1 as Token);
token1.derivedETH = t1derivedETH;
token1.derivedUSD = t1derivedETH.times(bundle.ethPrice);
token1.save();
Expand Down
6 changes: 6 additions & 0 deletions subgraphs/exchange-stableswap/arb/mappings/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Address, BigDecimal, BigInt } from "@graphprotocol/graph-ts";
// import { ERC20 } from "../../../exchange/generated/Factory/ERC20";
// import { ERC20NameBytes } from "../../../exchange/generated/Factory/ERC20NameBytes";
// import { ERC20SymbolBytes } from "../../../exchange/generated/Factory/ERC20SymbolBytes";
import { PriceLens0 } from "../../generated/StableSwapFactory/PriceLens0";
import { StableSwapFactory } from "../../generated/StableSwapFactory/StableSwapFactory";
import { ERC20 } from "../../generated/StableSwapFactory/ERC20";
import { ERC20SymbolBytes } from "../../generated/StableSwapFactory/ERC20SymbolBytes";
Expand All @@ -18,6 +19,10 @@ export let PENDLE_ADDRESS = "0x0c880f6761F1af8d9Aa9C466984b80DAb9a8c9e8";
//When new factory was deployed, and SC address into list
export let FACTORIES: string[] = [STABLESWAP_FACTORY_ADDRESS];

export let PRICE_LENS_ADDRESS = "0xe604940c06df1b6a9851f8e8d8d22468cb932e38";

export let priceLensContract = PriceLens0.bind(Address.fromString(PRICE_LENS_ADDRESS));

export let USDT_ADDR = Address.fromString(USDT_ADDRESS);
export let USDC_ADDR = Address.fromString(USDC_ADDRESS);
export let WETH_ADDR = Address.fromString(WETH_ADDRESS);
Expand All @@ -29,6 +34,7 @@ export let BIG_DECIMAL_ZERO = BigDecimal.fromString("0");
export let BIG_DECIMAL_ONE = BigDecimal.fromString("1");
export let BIG_DECIMAL_TEN = BigDecimal.fromString("10");
export let BIG_INT_18 = BigInt.fromI32(18);
export let BIG_INT_6 = BigInt.fromI32(6);

export let stableSwapFactoryContract = StableSwapFactory.bind(Address.fromString(STABLESWAP_FACTORY_ADDRESS));
export function exponentToBigDecimal(decimals: BigInt): BigDecimal {
Expand Down
Loading
Loading