Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from soramitsu/dev
Browse files Browse the repository at this point in the history
Added whitelist array in the config file
  • Loading branch information
0xxlegolas authored Nov 21, 2022
2 parents cdb695d + c62ccd1 commit 94126c5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions subgraphs/exchange/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function handleSync(event: Sync): void {
let factoryAddress = context.getString('DexFactory')
let factory = Factory.load(factoryAddress)!;

// reset factory liquidity by subtracting onluy tarcked liquidity
// reset factory liquidity by subtracting only tracked liquidity
factory.totalLiquidityKLAY = factory.totalLiquidityKLAY.minus(pair.trackedReserveKLAY as BigDecimal);

// reset token total liquidity amounts
Expand Down Expand Up @@ -401,7 +401,7 @@ export function handleSwap(event: Swap): void {
let amount0Total = amount0Out.plus(amount0In);
let amount1Total = amount1Out.plus(amount1In);

// BNB/USD prices
// WKLAY/USD prices
let bundle = Bundle.load(KlayOracleAddress)!;

// get total amounts of derived USD and KLAY for tracking
Expand Down
31 changes: 14 additions & 17 deletions subgraphs/exchange/src/priceUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { KlayOracle } from "../generated/templates/DexPair/KlayOracle";
import { Pair, Token, Bundle } from "../generated/schema";
import { log } from '@graphprotocol/graph-ts';
import { ONE_BD, ZERO_BD, getFactoryContract, ADDRESS_ZERO, USDT_PRECISION } from "./utils";
import { WKLAY_ADDRESS, KlayOracleAddress } from "./utils/config";
import { WKLAY_ADDRESS, KlayOracleAddress, whitelistedAddresses } from "./utils/config";

/**
* @todo update to be derived WKLAY (add stablecoin estimates) (optional)
*/
export function getKlayPriceInUSD(): BigDecimal {
let contract = KlayOracle.bind(Address.fromString(KlayOracleAddress));
let priceResult = contract.try_valueFor(Bytes.fromHexString("0x5d9add3300000000000000000000000000000000000000000000000000000000"))
Expand All @@ -14,17 +17,11 @@ export function getKlayPriceInUSD(): BigDecimal {
return ZERO_BD;
}

// token where amounts should contribute to tracked volume and liquidity
let WHITELIST: string[] = [
WKLAY_ADDRESS.toLowerCase(), // WKLAY
]

// minimum liquidity for price to get tracked
let MINIMUM_LIQUIDITY_THRESHOLD_KLAY = BigDecimal.fromString('1')

/**
* Search through graph to find derived BNB per token.
* @todo update to be derived BNB (add stablecoin estimates)
* Search through graph to find derived WKLAY per token.
**/
export function findKlayPerToken(token: Token, factoryAddress: string): BigDecimal {
let res = token.id == WKLAY_ADDRESS.toLowerCase();
Expand All @@ -33,9 +30,9 @@ let MINIMUM_LIQUIDITY_THRESHOLD_KLAY = BigDecimal.fromString('1')
return ONE_BD;
}
let factoryContract = getFactoryContract(factoryAddress);
// loop through whitelist and check if paired with any
for (let i = 0; i < WHITELIST.length; ++i) {
let pairAddress = factoryContract.getPair(Address.fromString(token.id), Address.fromString(WHITELIST[i]));
// loop through whitelistedAddresses and check if paired with any
for (let i = 0; i < whitelistedAddresses.length; ++i) {
let pairAddress = factoryContract.getPair(Address.fromString(token.id), Address.fromString(whitelistedAddresses[i]));
if (pairAddress.toHex() != ADDRESS_ZERO) {
let pair = Pair.load(pairAddress.toHex())!;
if (pair.token0 == token.id && pair.reserveKLAY.gt(MINIMUM_LIQUIDITY_THRESHOLD_KLAY)) {
Expand Down Expand Up @@ -68,17 +65,17 @@ let MINIMUM_LIQUIDITY_THRESHOLD_KLAY = BigDecimal.fromString('1')
let price1 = token1.derivedKLAY.times(bundle.klayPrice);

// both are whitelist tokens, take average of both amounts
if (WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
if (whitelistedAddresses.includes(token0.id) && whitelistedAddresses.includes(token1.id)) {
return tokenAmount0.times(price0).plus(tokenAmount1.times(price1)).div(BigDecimal.fromString("2"));
}

// take full value of the whitelisted token amount
if (WHITELIST.includes(token0.id) && !WHITELIST.includes(token1.id)) {
if (whitelistedAddresses.includes(token0.id) && !whitelistedAddresses.includes(token1.id)) {
return tokenAmount0.times(price0);
}

// take full value of the whitelisted token amount
if (!WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
if (!whitelistedAddresses.includes(token0.id) && whitelistedAddresses.includes(token1.id)) {
return tokenAmount1.times(price1);
}

Expand All @@ -103,17 +100,17 @@ let MINIMUM_LIQUIDITY_THRESHOLD_KLAY = BigDecimal.fromString('1')
let price1 = token1.derivedKLAY.times(bundle.klayPrice);

// both are whitelist tokens, take average of both amounts
if (WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
if (whitelistedAddresses.includes(token0.id) && whitelistedAddresses.includes(token1.id)) {
return tokenAmount0.times(price0).plus(tokenAmount1.times(price1));
}

// take double value of the whitelisted token amount
if (WHITELIST.includes(token0.id) && !WHITELIST.includes(token1.id)) {
if (whitelistedAddresses.includes(token0.id) && !whitelistedAddresses.includes(token1.id)) {
return tokenAmount0.times(price0).times(BigDecimal.fromString('2'));
}

// take double value of the whitelisted token amount
if (!WHITELIST.includes(token0.id) && WHITELIST.includes(token1.id)) {
if (!whitelistedAddresses.includes(token0.id) && whitelistedAddresses.includes(token1.id)) {
return tokenAmount1.times(price1).times(BigDecimal.fromString('2'));
}

Expand Down
12 changes: 12 additions & 0 deletions subgraphs/exchange/src/utils/config.example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Example of a config.ts file
export const WKLAY_ADDRESS = "0x6d1c5403bab8ff5fca04dab0a5d820ae0fa615e9";
export const KlayOracleAddress = "0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0";
// Please dont't forget to add your token address in lower case to the whitelist, otherwise it will not be tracked
// Also WKLAY_ADDRESS should be copied to the whitelist manually (also in lower case)
// Ready to copy example with WKLAY, USDT, USDC, WETH, WBTC, DAI tokens from Baobab network deployed by Klaytn team
export const whitelistedAddresses = ["0x6d1c5403bab8ff5fca04dab0a5d820ae0fa615e9", "0x63b3ace91d182013fed2ebaa0a8dd9aea243e865",
"0xae33ac5c631b80757a0cf1395d1ce18f3eaa46c9", "0x53aa07e823e3ef9034fd8fb19cddd1b6323eaae3",
"0xb61786e9d895ffc77f6b755b2ba479b706388bc1", "0x59d83777b3f1ebac785d2bae542b6e0846494855"];
// In the exchange subgraph we use KLAY-USDT oreacle to get KLAY price in USD
// So better not to create WKLAY-USDT, WKLAY-USDC, WKLAY-DAI pairs, it will lead to incorrect prices for these tokens
// between WKLAY based on pairs and oracle
3 changes: 2 additions & 1 deletion subgraphs/exchange/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const WKLAY_ADDRESS = "0x73365f8f27de98d7634be67a167f229b32e7bf6c";
export const KlayOracleAddress = "0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0";
export const KlayOracleAddress = "0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0";
export const whitelistedAddresses = ["0x73365f8f27de98d7634be67a167f229b32e7bf6c"];

0 comments on commit 94126c5

Please sign in to comment.