Skip to content

Commit 7aa69b3

Browse files
authored
Latch Project TVL Adapter (DefiLlama#12874)
1 parent 6ab4bef commit 7aa69b3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

projects/latch/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const BN = require('bn.js');
2+
const sdk = require('@defillama/sdk');
3+
4+
const atUSD = '0xc4af68Dd5b96f0A544c4417407773fEFDc97F58d';
5+
const atETH = '0xc314b8637B05A294Ae9D9C29300d5f667c748baD';
6+
const NAV = '0x5D3920CCC068039E5B6FE680CaB7Aa09fE8E053C';
7+
const abi = 'function getNavByTimestamp(address,uint48) view returns (uint256 , uint48 )';
8+
9+
const tvl = async (api) => {
10+
const chainEth = 'ethereum';
11+
const chainGravity = 'gravity';
12+
13+
const ethApi = new sdk.ChainApi({ chain: chainEth, timestamp: api.timestamp });
14+
const gravityApi = new sdk.ChainApi({ chain: chainGravity });
15+
16+
await Promise.all([ethApi.getBlock(), gravityApi.getBlock()]);
17+
18+
const [usdNav, ethNav, usdtSupply, ethSupply] = await Promise.all([
19+
ethApi.call({ target: NAV, params: [atUSD, api.timestamp], abi }),
20+
ethApi.call({ target: NAV, params: [atETH, api.timestamp], abi }),
21+
gravityApi.call({ abi: 'erc20:totalSupply', target: atUSD }),
22+
gravityApi.call({ abi: 'erc20:totalSupply', target: atETH }),
23+
]);
24+
25+
// Convert results to BN
26+
const usdNavBN = new BN(usdNav[0].toString());
27+
const ethNavBN = new BN(ethNav[0].toString());
28+
const usdtSupplyBN = new BN(usdtSupply.toString());
29+
const ethSupplyBN = new BN(ethSupply.toString());
30+
31+
// Perform calculations
32+
const usdtResult = usdtSupplyBN.mul(usdNavBN).div(new BN('10').pow(new BN(30)));
33+
const ethResult = ethSupplyBN.mul(ethNavBN).div(new BN('10').pow(new BN(18)));
34+
35+
// Add results to API
36+
api.add('0xdac17f958d2ee523a2206206994597c13d831ec7', usdtResult.toString());
37+
api.add('0x0000000000000000000000000000000000000000', ethResult.toString());
38+
};
39+
40+
module.exports = {
41+
ethereum: {
42+
tvl: tvl,
43+
},
44+
};

0 commit comments

Comments
 (0)