From 118232b77811a7224479592565d4f3246af3c2e6 Mon Sep 17 00:00:00 2001 From: ftm1337 <84405345+ftm1337@users.noreply.github.com> Date: Sat, 20 Jul 2024 09:53:10 -0400 Subject: [PATCH 1/4] Add elocks adapter --- projects/elocks/index.js | 104 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 projects/elocks/index.js diff --git a/projects/elocks/index.js b/projects/elocks/index.js new file mode 100644 index 000000000000..1508253f6d02 --- /dev/null +++ b/projects/elocks/index.js @@ -0,0 +1,104 @@ +const sdk = require('@defillama/sdk'); +const { sumTokens2 } = require('../helper/unwrapLPs') +const { getTokenPrices, sumUnknownTokens } = require('../helper/unknownTokens') +const { unwrapUniswapLPs } = require('../helper/unwrapLPs') + +const IELOCKS = { + totalSupply: "uint256:totalSupply", + tvl: "uint256:tvl", + lockers: "function lockers(uint) external view returns(address)", + lockedAssets: "function lockedAssets(uint) public view returns (address, uint, uint)" +} + +const elocks = { + "fantom": "0x2f20A659601d1c161A108E0725FEF31256a907ad" +} + +module.exports = { + misrepresentedTokens: true, + methodology: "USD-denominated value", +} + + +async function findTvl(chain, nft, block, api) { + let { output: len } = await sdk.api.abi.call({ + target: nft, + abi: IELOCKS.totalSupply, + chain, + }) + len = Number(len); + //console.log("nft.ts", len); + + + /* + /// Using TVL GURU (on-chain price calcs) + + let calls = [] + for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } + let { output: lockaddrs } = await sdk.api.abi.multiCall({ + target: nft, + abi: IELOCKS.lockers, + calls, + chain, + }) + lockaddrs = lockaddrs.map(i=>i.output) + //console.log("lockaddrs",lockaddrs) + + const ltvls = ( + await sdk.api.abi.multiCall({ + abi: IELOCKS.tvl, + calls: lockaddrs.map((la) => ({ target: la })), + chain + }) + ).output.map((tvl) => Number(tvl.output)/1e18); + //console.log("ltvls",ltvls) + + return { tether: ltvls.reduce((a,b)=>a+b) }; + */ + + + /// Using DefiLlama pricing + + let calls = [] + for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } + let { output: las } = await sdk.api.abi.multiCall({ + target: nft, + abi: IELOCKS.lockedAssets, + calls, + chain, + }) + /* + /// Using { 0x123: 123, 0xtoken2: 456, ... } + + //las = las.map(i=> ({lpt: i.output[0], amt: Number(i.output[1])/1e0}) ) + las = las.map(i=> ({ [i.output[0].toLowerCase()] : Number(i.output[1])/1e0 }) ) + let lpamts = {}; + las.forEach(obj => { + for (let key in obj) { + if (lpamts[key]) { + lpamts[key] += obj[key]; + } else { + lpamts[key] = obj[key]; + } + } + }); + //console.log("las",las,lpamts) + console.log(await getTokenPrices(lpamts)) + return lpamts; + */ + lpBalances = las.map(i=> ({token: i.output[0], balance: Number(i.output[1])/1e0}) ) + //console.log("las",las,lpBalances) + tokens = lpBalances.map(i=>i.token) + bals = lpBalances.map(i=>i.balance) + api.addTokens(tokens, bals) + //return await sumUnknownTokens({ api, tokensAndOwners: [], useDefaultCoreAssets: true, lps: tokens, resolveLP: true, }) + //await unwrapUniswapLPs(lpBalances, chain) + return await unwrapUniswapLPs(bals, lpBalances, block, chain) +} + + +Object.entries(elocks).forEach(([chain, target, block]) => { + module.exports[chain] = { + tvl: async (api) => findTvl(chain, target, block, api), + } +}) From 17b837369c5cbe28e28ad460b910d0de22247f93 Mon Sep 17 00:00:00 2001 From: ftm1337 <84405345+ftm1337@users.noreply.github.com> Date: Sun, 21 Jul 2024 12:43:43 -0400 Subject: [PATCH 2/4] fix lint issues --- projects/elocks/index.js | 53 ---------------------------------------- 1 file changed, 53 deletions(-) diff --git a/projects/elocks/index.js b/projects/elocks/index.js index 1508253f6d02..271e64aad656 100644 --- a/projects/elocks/index.js +++ b/projects/elocks/index.js @@ -27,37 +27,6 @@ async function findTvl(chain, nft, block, api) { chain, }) len = Number(len); - //console.log("nft.ts", len); - - - /* - /// Using TVL GURU (on-chain price calcs) - - let calls = [] - for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } - let { output: lockaddrs } = await sdk.api.abi.multiCall({ - target: nft, - abi: IELOCKS.lockers, - calls, - chain, - }) - lockaddrs = lockaddrs.map(i=>i.output) - //console.log("lockaddrs",lockaddrs) - - const ltvls = ( - await sdk.api.abi.multiCall({ - abi: IELOCKS.tvl, - calls: lockaddrs.map((la) => ({ target: la })), - chain - }) - ).output.map((tvl) => Number(tvl.output)/1e18); - //console.log("ltvls",ltvls) - - return { tether: ltvls.reduce((a,b)=>a+b) }; - */ - - - /// Using DefiLlama pricing let calls = [] for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } @@ -67,32 +36,10 @@ async function findTvl(chain, nft, block, api) { calls, chain, }) - /* - /// Using { 0x123: 123, 0xtoken2: 456, ... } - - //las = las.map(i=> ({lpt: i.output[0], amt: Number(i.output[1])/1e0}) ) - las = las.map(i=> ({ [i.output[0].toLowerCase()] : Number(i.output[1])/1e0 }) ) - let lpamts = {}; - las.forEach(obj => { - for (let key in obj) { - if (lpamts[key]) { - lpamts[key] += obj[key]; - } else { - lpamts[key] = obj[key]; - } - } - }); - //console.log("las",las,lpamts) - console.log(await getTokenPrices(lpamts)) - return lpamts; - */ lpBalances = las.map(i=> ({token: i.output[0], balance: Number(i.output[1])/1e0}) ) - //console.log("las",las,lpBalances) tokens = lpBalances.map(i=>i.token) bals = lpBalances.map(i=>i.balance) api.addTokens(tokens, bals) - //return await sumUnknownTokens({ api, tokensAndOwners: [], useDefaultCoreAssets: true, lps: tokens, resolveLP: true, }) - //await unwrapUniswapLPs(lpBalances, chain) return await unwrapUniswapLPs(bals, lpBalances, block, chain) } From b51e9e7b2b0450044c20ccce94df67c83a16c654 Mon Sep 17 00:00:00 2001 From: ftm1337 <84405345+ftm1337@users.noreply.github.com> Date: Sun, 21 Jul 2024 12:46:45 -0400 Subject: [PATCH 3/4] fix lint errors --- projects/elocks/index.js | 59 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/projects/elocks/index.js b/projects/elocks/index.js index 271e64aad656..c58ccecdffaa 100644 --- a/projects/elocks/index.js +++ b/projects/elocks/index.js @@ -27,6 +27,37 @@ async function findTvl(chain, nft, block, api) { chain, }) len = Number(len); + //console.log("nft.ts", len); + + + /* + /// Using TVL GURU (on-chain price calcs) + + let calls = [] + for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } + let { output: lockaddrs } = await sdk.api.abi.multiCall({ + target: nft, + abi: IELOCKS.lockers, + calls, + chain, + }) + lockaddrs = lockaddrs.map(i=>i.output) + //console.log("lockaddrs",lockaddrs) + + const ltvls = ( + await sdk.api.abi.multiCall({ + abi: IELOCKS.tvl, + calls: lockaddrs.map((la) => ({ target: la })), + chain + }) + ).output.map((tvl) => Number(tvl.output)/1e18); + //console.log("ltvls",ltvls) + + return { tether: ltvls.reduce((a,b)=>a+b) }; + */ + + + /// Using DefiLlama pricing let calls = [] for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } @@ -36,10 +67,32 @@ async function findTvl(chain, nft, block, api) { calls, chain, }) - lpBalances = las.map(i=> ({token: i.output[0], balance: Number(i.output[1])/1e0}) ) - tokens = lpBalances.map(i=>i.token) - bals = lpBalances.map(i=>i.balance) + /* + /// Using { 0x123: 123, 0xtoken2: 456, ... } + + //las = las.map(i=> ({lpt: i.output[0], amt: Number(i.output[1])/1e0}) ) + las = las.map(i=> ({ [i.output[0].toLowerCase()] : Number(i.output[1])/1e0 }) ) + let lpamts = {}; + las.forEach(obj => { + for (let key in obj) { + if (lpamts[key]) { + lpamts[key] += obj[key]; + } else { + lpamts[key] = obj[key]; + } + } + }); + //console.log("las",las,lpamts) + console.log(await getTokenPrices(lpamts)) + return lpamts; + */ + let lpBalances = las.map(i=> ({token: i.output[0], balance: Number(i.output[1])/1e0}) ) + //console.log("las",las,lpBalances) + let tokens = lpBalances.map(i=>i.token) + let bals = lpBalances.map(i=>i.balance) api.addTokens(tokens, bals) + //return await sumUnknownTokens({ api, tokensAndOwners: [], useDefaultCoreAssets: true, lps: tokens, resolveLP: true, }) + //await unwrapUniswapLPs(lpBalances, chain) return await unwrapUniswapLPs(bals, lpBalances, block, chain) } From c06810c8372ff18a13baac400ed34d3b4b9da3d0 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Mon, 22 Jul 2024 09:56:32 +0300 Subject: [PATCH 4/4] code refactor --- projects/elocks/index.js | 107 +++------------------- projects/helper/cache/sumUnknownTokens.js | 2 +- 2 files changed, 14 insertions(+), 95 deletions(-) diff --git a/projects/elocks/index.js b/projects/elocks/index.js index c58ccecdffaa..400caf55ffee 100644 --- a/projects/elocks/index.js +++ b/projects/elocks/index.js @@ -1,104 +1,23 @@ -const sdk = require('@defillama/sdk'); -const { sumTokens2 } = require('../helper/unwrapLPs') -const { getTokenPrices, sumUnknownTokens } = require('../helper/unknownTokens') -const { unwrapUniswapLPs } = require('../helper/unwrapLPs') +const { sumTokens2 } = require("../helper/unwrapLPs") const IELOCKS = { - totalSupply: "uint256:totalSupply", - tvl: "uint256:tvl", - lockers: "function lockers(uint) external view returns(address)", - lockedAssets: "function lockedAssets(uint) public view returns (address, uint, uint)" + lockedAssets: "function lockedAssets(uint) public view returns (address token, uint balance, uint)" } const elocks = { - "fantom": "0x2f20A659601d1c161A108E0725FEF31256a907ad" + fantom: "0x2f20A659601d1c161A108E0725FEF31256a907ad" } module.exports = { - misrepresentedTokens: true, - methodology: "USD-denominated value", + misrepresentedTokens: true, } - -async function findTvl(chain, nft, block, api) { - let { output: len } = await sdk.api.abi.call({ - target: nft, - abi: IELOCKS.totalSupply, - chain, - }) - len = Number(len); - //console.log("nft.ts", len); - - - /* - /// Using TVL GURU (on-chain price calcs) - - let calls = [] - for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } - let { output: lockaddrs } = await sdk.api.abi.multiCall({ - target: nft, - abi: IELOCKS.lockers, - calls, - chain, - }) - lockaddrs = lockaddrs.map(i=>i.output) - //console.log("lockaddrs",lockaddrs) - - const ltvls = ( - await sdk.api.abi.multiCall({ - abi: IELOCKS.tvl, - calls: lockaddrs.map((la) => ({ target: la })), - chain - }) - ).output.map((tvl) => Number(tvl.output)/1e18); - //console.log("ltvls",ltvls) - - return { tether: ltvls.reduce((a,b)=>a+b) }; - */ - - - /// Using DefiLlama pricing - - let calls = [] - for (let i = 1; i <= len; i++) { calls.push({ params: [i] }) } - let { output: las } = await sdk.api.abi.multiCall({ - target: nft, - abi: IELOCKS.lockedAssets, - calls, - chain, - }) - /* - /// Using { 0x123: 123, 0xtoken2: 456, ... } - - //las = las.map(i=> ({lpt: i.output[0], amt: Number(i.output[1])/1e0}) ) - las = las.map(i=> ({ [i.output[0].toLowerCase()] : Number(i.output[1])/1e0 }) ) - let lpamts = {}; - las.forEach(obj => { - for (let key in obj) { - if (lpamts[key]) { - lpamts[key] += obj[key]; - } else { - lpamts[key] = obj[key]; - } - } - }); - //console.log("las",las,lpamts) - console.log(await getTokenPrices(lpamts)) - return lpamts; - */ - let lpBalances = las.map(i=> ({token: i.output[0], balance: Number(i.output[1])/1e0}) ) - //console.log("las",las,lpBalances) - let tokens = lpBalances.map(i=>i.token) - let bals = lpBalances.map(i=>i.balance) - api.addTokens(tokens, bals) - //return await sumUnknownTokens({ api, tokensAndOwners: [], useDefaultCoreAssets: true, lps: tokens, resolveLP: true, }) - //await unwrapUniswapLPs(lpBalances, chain) - return await unwrapUniswapLPs(bals, lpBalances, block, chain) -} - - -Object.entries(elocks).forEach(([chain, target, block]) => { - module.exports[chain] = { - tvl: async (api) => findTvl(chain, target, block, api), - } -}) +Object.keys(elocks).forEach(chain => { + module.exports[chain] = { + tvl: async (api) => { + const data = await api.fetchList({ lengthAbi: 'totalSupply', itemAbi: IELOCKS.lockedAssets, target: elocks[api.chain], startFromOne: true, }) + data.forEach(i => api.addToken(i.token, i.balance)) + return sumTokens2({ api, resolveLP: true}) + }, + } +}) \ No newline at end of file diff --git a/projects/helper/cache/sumUnknownTokens.js b/projects/helper/cache/sumUnknownTokens.js index b23f98858d50..e494f6871c4d 100644 --- a/projects/helper/cache/sumUnknownTokens.js +++ b/projects/helper/cache/sumUnknownTokens.js @@ -365,7 +365,7 @@ async function getTokenPrices({ async function sumUnknownTokens({ api, tokensAndOwners = [], balances, coreAssets = [], owner, tokens, chain = 'ethereum', block, restrictTokenRatio, blacklist = [], skipConversion = false, onlyLPs, minLPRatio, log_coreAssetPrices = [], log_minTokenValue = 1e6, owners = [], lps = [], useDefaultCoreAssets = false, cache = {}, resolveLP = false, abis, - ownerTokens, + ownerTokens = [], }) { if (api) { chain = api.chain ?? chain