Skip to content

Commit

Permalink
Merge pull request #145 from dappradar/hedera_integrations
Browse files Browse the repository at this point in the history
Heliswap integrations
  • Loading branch information
Sonmezturk authored Jun 26, 2023
2 parents a88a587 + f15b796 commit 7a1ec72
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 89 deletions.
110 changes: 110 additions & 0 deletions src/factory/providers/hedera/heliswap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import formatter from '../../../../util/formatter';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import axios from 'axios';
import BigNumber from 'bignumber.js';
import { hethers } from '@hashgraph/hethers';

const START_BLOCK = 1675123200;

const TOKEN_ENDPOINT =
'https://heliswap-api.ey.r.appspot.com/tokens/whitelisted/';
const HELISWAP_ENDPOINT = 'https://heliswap-prod-362307.oa.r.appspot.com/query';

const query = `
query getWhitelistedPools($tokens: [String]!) {
poolsConsistingOf(tokens: $tokens) {
token0
token1
token0Amount
token1Amount
tvl
}
}`;
const converToken = {
'0.0.1055477': {
sourceDecimal: 8,
targetDecimal: 6,
targetAddress: '0.0.456858',
},
'0.0.1055483': {
sourceDecimal: 8,
targetDecimal: 0,
targetAddress: 'coingecko_btc',
},
'0.0.541564': {
sourceDecimal: 8,
targetDecimal: 0,
targetAddress: 'coingecko_weth',
},
'0.0.540318': {
sourceDecimal: 8,
targetDecimal: 0,
targetAddress: 'coingecko_wmatic',
},
'0.0.2967328': {
sourceDecimal: 8,
targetDecimal: 0,
targetAddress: 'coingecko_pepe',
},
'0.0.2934819': {
sourceDecimal: 8,
targetDecimal: 0,
targetAddress: 'coingecko_hedera',
},
};

async function getWhitelistedTokenAddresses() {
const response = await axios(TOKEN_ENDPOINT);
const { data: whitelistedTokens } = response;

return whitelistedTokens;
}

async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;

if (block < START_BLOCK) {
return {};
}
const balances = {};

const whitelistedAddresses = await getWhitelistedTokenAddresses();
const requestData = {
query,
variables: {
tokens: whitelistedAddresses,
},
};

const {
data: {
data: { poolsConsistingOf },
},
} = await axios({
url: HELISWAP_ENDPOINT,
method: 'post',
data: requestData,
});

poolsConsistingOf.forEach((pool) => {
if (balances[hethers.utils.asAccountString(pool.token0)]) {
balances[hethers.utils.asAccountString(pool.token0)] = BigNumber(
balances[hethers.utils.asAccountString(pool.token0)],
).plus(pool.token0Amount);
} else {
balances[hethers.utils.asAccountString(pool.token0)] = pool.token0Amount;
}
if (balances[hethers.utils.asAccountString(pool.token1)]) {
balances[hethers.utils.asAccountString(pool.token1)] = BigNumber(
balances[hethers.utils.asAccountString(pool.token1)],
).plus(pool.token1Amount);
} else {
balances[hethers.utils.asAccountString(pool.token1)] = pool.token1Amount;
}
});
formatter.swapTokenAddresses(balances, converToken);
formatter.convertBalancesToFixed(balances);
return { balances };
}

export { tvl };
7 changes: 0 additions & 7 deletions src/factory/providers/hedera/tangent/data.json

This file was deleted.

82 changes: 0 additions & 82 deletions src/factory/providers/hedera/tangent/index.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/util/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ function convertBalancesToFixed(balances) {
balances[token] = balances[token].toFixed();
} catch {}
}
return balances;
}

function convertBalancesToBigNumber(balances) {
for (const token in balances) {
balances[token] = new BigNumber(balances[token]);
}
return balances;
}

function sum(balanceArray) {
Expand Down Expand Up @@ -111,6 +113,32 @@ function sumMultiBalanceOf(balances, results, chain = '', provider = '') {
}
}

function swapTokenAddresses(
tokens: { [key: string]: any },
rule: {
[key: string]: {
sourceDecimal: number;
targetDecimal: number;
targetAddress: string;
};
},
) {
Object.keys(tokens).forEach((address) => {
if (rule[address]) {
tokens[rule[address].targetAddress] = BigNumber(
tokens[rule[address].targetAddress] || 0,
)
.plus(
BigNumber(tokens[address]).shiftedBy(
rule[address].targetDecimal - rule[address].sourceDecimal,
),
)
.toFixed();
delete tokens[address];
}
});
}

export default {
encodeParameters,
decodeParameters,
Expand All @@ -119,4 +147,5 @@ export default {
convertBalancesToBigNumber,
sum,
sumMultiBalanceOf,
swapTokenAddresses,
};

0 comments on commit 7a1ec72

Please sign in to comment.