Skip to content

Commit

Permalink
Add ChampagneSwap to Defi Llama
Browse files Browse the repository at this point in the history
  • Loading branch information
Champagneswap committed Apr 12, 2022
1 parent 62583be commit e5440b6
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
43 changes: 43 additions & 0 deletions dexVolumes/champagneswap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DexVolumeAdapter } from "../dexVolume.type";

const {
getChainVolume,
DEFAULT_TOTAL_VOLUME_FIELD,
DEFAULT_DAILY_VOLUME_FIELD,
} = require("../helper/getUniSubgraphVolume");
const { BSC } = require("../helper/chains");
const { getStartTimestamp } = require("../helper/getStartTimestamp");
const endpoints = {
[BSC]: "https://api.thegraph.com/subgraphs/name/champagneswap/exchangev3",
};

const DAILY_VOLUME_FACTORY = "champagneDayData";

const graphs = getChainVolume({
graphUrls: {
[BSC]: endpoints[BSC],
},
totalVolume: {
factory: "champagneFactories",
field: DEFAULT_TOTAL_VOLUME_FIELD,
},
dailyVolume: {
factory: DAILY_VOLUME_FACTORY,
field: DEFAULT_DAILY_VOLUME_FIELD,
},
});

const adapter: DexVolumeAdapter = {
volume: {
[BSC]: {
fetch: graphs(BSC),
start: getStartTimestamp({
endpoints,
chain: BSC,
dailyDataField: `${DAILY_VOLUME_FACTORY}s`,
}),
},
},
};

export default adapter;
2 changes: 2 additions & 0 deletions dexVolumes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import oneInch from "./1inch";
import balancer from "./balancer";
import bancor from "./bancor";
import champagneswap from "./champagneswap";
import curve from "./curve";
import dodo from "./dodo";
import katana from "./katana";
Expand All @@ -24,6 +25,7 @@ export default {
"1inch": oneInch,
balancer,
bancor,
champagneswap,
curve,
dodo,
katana,
Expand Down
99 changes: 99 additions & 0 deletions projects/champagne-swap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const { request, gql } = require("graphql-request");
const sdk = require('@defillama/sdk')
const { toUSDTBalances } = require('./helper/balances')

async function fetch() {
let response = await utils.fetchURL('https://api.thegraph.com/subgraphs/name/champagneswap/exchangev3')
return response.data.total_value_locked_all;
}

const graphEndpoint = 'https://api.thegraph.com/subgraphs/name/champagneswap/exchangev3'
const currentQuery = gql`
query champagneFactories {
champagneFactories(first: 1) {
totalLiquidityUSD
}
}
`
const historicalQuery = gql`
query champagneDayDatas {
champagneDayDatas(
first: 1000
orderBy: date
orderDirection: asc
) {
date
dailyVolumeUSD
totalLiquidityUSD
__typename
}
}
`

const graphUrl = 'https://api.thegraph.com/subgraphs/name/champagneswap/exchangev3'
const graphQuery = gql`
query get_tvl($block: Int) {
uniswapFactories(
block: { number: $block }
) {
totalVolumeUSD
totalLiquidityUSD
}
}
`;
async function tvl(timestamp, ethBlock, chainBlocks) {
if (Math.abs(timestamp - Date.now() / 1000) < 3600) {
const tvl = await request(graphEndpoint, currentQuery, {}, {
"referer": "https://champagne.finance/",
"origin": "https://champagne.finance",
})
return toUSDTBalances(tvl.champagneFactories[0].totalLiquidityUSD)
} else {
const tvl = (await request(graphEndpoint, historicalQuery)).champagneDayDatas
let closest = tvl[0]
tvl.forEach(dayTvl => {
if (Math.abs(dayTvl.date - timestamp) < Math.abs(closest.date - timestamp)) {
closest = dayTvl
}
})
if(Math.abs(closest.date - timestamp) > 3600*24){ // Oldest data is too recent
const {uniswapFactories} = await request(
graphUrl,
graphQuery,
{
block: chainBlocks['bsc'],
}
);
const usdTvl = Number(uniswapFactories[0].totalLiquidityUSD)

return toUSDTBalances(usdTvl)
}
return toUSDTBalances(closest.totalLiquidityUSD)
}
}

const factory = '0xb31A337f1C3ee7fA2b2B83c6F8ee0CA643D807a0'
const champagneToken = '0x4957c1c073557BFf33C01A7cA1436D0d2409d439'
const masterChef = '0x15C17442eb2Cd3a56139e877ec7784b2dbD97270'
async function staking(timestamp, ethBlock, chainBlocks) {
const balances = {}
const stakedCham = sdk.api.erc20.balanceOf({
target: champagneToken,
owner: masterChef,
chain: 'bsc',
block: chainBlocks.bsc
})

sdk.util.sumSingleBalance(balances, 'bsc:' + champagneToken, (await stakedCham).output)
return balances
}

module.exports = {
timetravel: true,
misrepresentedTokens: true,
methodology: 'TVL accounts for the liquidity on all AMM pools, using the TVL chart on https://champagne.finance/ as the source. Staking accounts for the CHAM locked in MasterChef (0x15C17442eb2Cd3a56139e877ec7784b2dbD97270)',
bsc: {
staking,
tvl
},
}
4 changes: 4 additions & 0 deletions volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ https://docs.dydx.exchange/#get-markets
#### Pancake Swap
https://api.pancakeswap.finance/api/v1/stat

#### Champagne Swap
https://api.thegraph.com/subgraphs/name/champagneswap/exchangev3


#### Perp
https://thegraph.com/explorer/subgraph/perpetual-protocol/perp-position-subgraph
https://perp.gq/pairs
Expand Down

0 comments on commit e5440b6

Please sign in to comment.