From 35b1fbc6e8aecb68b67a8f1af5b9b27df44293d6 Mon Sep 17 00:00:00 2001 From: Huy Do Date: Wed, 6 Mar 2024 12:18:46 +0700 Subject: [PATCH] update crunchy token and tvl with farmV2, wtz contracts and deep freezers --- src/factory/providers/tezos/crunchy/data.json | 6 +- src/factory/providers/tezos/crunchy/index.ts | 117 +++++++++++++++--- 2 files changed, 102 insertions(+), 21 deletions(-) diff --git a/src/factory/providers/tezos/crunchy/data.json b/src/factory/providers/tezos/crunchy/data.json index 77b8264e..6fd29c11 100644 --- a/src/factory/providers/tezos/crunchy/data.json +++ b/src/factory/providers/tezos/crunchy/data.json @@ -1,7 +1,7 @@ { - "address": "KT1BHCumksALJQJ8q8to2EPigPW6qpyTr7Ng", - "name": "Crunchy Network Token", - "symbol": "CRUNCH", + "address": "KT1914CUZ7EegAFPbfgQMRkw8Uz5mYkEz2ui", + "name": "Crunchy.Network CRNCHY Token", + "symbol": "CRNCHY", "decimals": 8, "logo": "https://assets.coingecko.com/coins/images/19463/small/crunch.jpeg" } \ No newline at end of file diff --git a/src/factory/providers/tezos/crunchy/index.ts b/src/factory/providers/tezos/crunchy/index.ts index 955f6d24..a8801d07 100644 --- a/src/factory/providers/tezos/crunchy/index.ts +++ b/src/factory/providers/tezos/crunchy/index.ts @@ -4,7 +4,33 @@ import formatter from '../../../../util/formatter'; import basicUtil from '../../../../util/basicUtil'; import util from '../../../../util/blockchainUtil'; -const FARM_ADDRESS = 'KT1KnuE87q1EKjPozJ5sRAjQA24FPsP57CE3'; +const DEEP_FREEZERS_ADDRESS = 'KT1LjcQ4h5hCy9RcveFz9Pq8LtmF6oun7vNd'; + +const FARM_ADDRESSES = [ + 'KT1KnuE87q1EKjPozJ5sRAjQA24FPsP57CE3', + 'KT1L1WZgdsfjEyP5T4ZCYVvN5vgzrNbu18kX', +]; + +const WTZ_ADDRESSES = [ + 'KT1BB3oNr5vUSw1CuPNb2zpYEVp376XrXWaJ', + 'KT1H25LW5k4HQGm9hmNXzaxf3nqjsAEhQPah', + 'KT1LpGZnT6dj6STSxHXmvSPqx39ZdPXAMpFz', + 'KT1NBgqqJacbdoeNAg9MvrgPT9h6q6AGWvFA', + 'KT1NGTDBKDPMrAYEufb72CLwuQJ7jU7jL6jD', +]; + +async function getTezosBalance(address, block, web3) { + try { + const tezosBalance = await web3.eth.getBalance(address, block); + + return { + token: 'xtz', + balance: BigNumber(tezosBalance), + }; + } catch {} + return null; +} + let pools = {}; async function tvl(params: ITvlParams): Promise> { @@ -17,36 +43,91 @@ async function tvl(params: ITvlParams): Promise> { pools = await basicUtil.readFromCache('cache/pools.json', chain, provider); } catch {} - const contract = new web3.eth.Contract(null, FARM_ADDRESS); - await contract.init(); - const farmLength = await contract.methods.nextFarmId().call(null, block); - const farms = await contract.methods.getBigmap('farms').call(null, block); + const balanceResults = []; + let globalIndex = 0; - console.log(`farm length: ${farmLength}`); + for (let i = 0; i < FARM_ADDRESSES.length; i++) { + const contract = new web3.eth.Contract(null, FARM_ADDRESSES[i]); + await contract.init(); - const balanceResults = []; + const farms = await contract.methods.getBigmap('farms').call(null, block); + const totalFarms = farms.map((farm) => farm.key); + const farmLength = totalFarms.length; - for (let i = 0; i < farmLength; i++) { + console.log(`Farm length for address ${FARM_ADDRESSES[i]}: ${farmLength}`); try { - if (!pools[i]) { + for (let j = 0; j < farmLength; j++) { + const farmIndex = j + i * globalIndex; + + if (!pools[farmIndex]) { + let tokenId; + try { + const tokenType = Object.keys(farms[j].value.poolToken.tokenType); + if (tokenType[0] == 'fa2') { + tokenId = farms[j].value.poolToken.tokenId; + } + } catch {} + pools[farmIndex] = tokenId + ? `${farms[j].value.poolToken.address}_${tokenId}` + : farms[j].value.poolToken.address; + } + + balanceResults.push({ + token: pools[farmIndex], + balance: BigNumber(farms[j].value.poolBalance), + }); + } + globalIndex += farmLength; + } catch {} + } + + + // Fetch WTZ Liquid Staking + for (let i = 0; i < WTZ_ADDRESSES.length; i++) { + const wtzIndex = i + globalIndex; + pools[wtzIndex] = WTZ_ADDRESSES[i]; + const wtz = await getTezosBalance(WTZ_ADDRESSES[i], block, web3); + balanceResults.push(wtz); + } + globalIndex += WTZ_ADDRESSES.length; + + + // Fetch Deep Freezers TVL + const contract = new web3.eth.Contract(null, DEEP_FREEZERS_ADDRESS); + await contract.init(); + + const locks = await contract.methods.getBigmap('locks').call(null, block); + const totalLocks = locks.map((lock) => lock.key); + const locksLength = totalLocks.length; + + console.log( + `Lock length for address $DEEP_FREEZERS_ADDRESS}: ${locksLength}`, + ); + try { + for (let i = 0; i < locksLength; i++) { + const lockIndex = i + globalIndex; + + if (!pools[lockIndex]) { let tokenId; try { - const tokenType = Object.keys(farms[i].value.poolToken.tokenType); + const tokenType = Object.keys(locks[i].value.token.tokenType); if (tokenType[0] == 'fa2') { - tokenId = farms[i].value.poolToken.tokenId; + tokenId = locks[i].value.token.tokenId; } } catch {} - pools[i] = tokenId - ? `${farms[i].value.poolToken.address}_${tokenId}` - : farms[i].value.poolToken.address; + pools[lockIndex] = tokenId + ? `${locks[i].value.token.address}_${tokenId}` + : locks[i].value.token.address; } balanceResults.push({ - token: pools[i], - balance: BigNumber(farms[i].value.poolBalance), + token: pools[lockIndex], + balance: BigNumber(locks[i].value.amountLocked), }); - } catch {} - } + } + globalIndex += Number(locksLength); + } catch {} + await basicUtil.saveIntoCache(pools, 'cache/pools.json', chain, provider); const tokenBalances = {};