-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from kousthubraja/integrate-newrl-protocol
Integrate Newrl RWA protocol
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import util from '../../../../util/blockchainUtil'; | ||
import { request, gql } from 'graphql-request'; | ||
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl'; | ||
import formatter from '../../../../util/formatter'; | ||
|
||
const START_BLOCK = 55129443; | ||
const PROTOCOL_ADDRESSES = ['0xbfeb0b78f9ab8223657b65c5acad846c12f8aa89']; | ||
const USDC_TOKEN_ADDRESS = '0x3c499c542cef5e3811e1192ce70d8cc03d5c3359'; | ||
const TRACKED_TOKENS = [ | ||
'0x01d6d93feaa0a7157b22cf034d09807e63d1e3d8', // SUGR | ||
]; | ||
const THEGRAPTH_ENDPOINT = | ||
'https://api.thegraph.com/subgraphs/name/messari/uniswap-v3-polygon'; | ||
const LAST_SWAP_QUERY = gql` | ||
query trades($block: Int!, $tokenIn: String!, $tokenOut: String!) { | ||
swaps( | ||
where: { tokenIn: $tokenIn, tokenOut: $tokenOut } | ||
orderBy: timestamp | ||
orderDirection: desc | ||
first: 1 # block: {number: $block} | ||
) { | ||
amountIn | ||
amountOut | ||
} | ||
} | ||
`; | ||
|
||
async function getConversion( | ||
block: number, | ||
fromToken: string, | ||
toToken: string, | ||
) { | ||
const data = await request(THEGRAPTH_ENDPOINT, LAST_SWAP_QUERY, { | ||
block: block, | ||
tokenIn: fromToken, | ||
tokenOut: toToken, | ||
}); | ||
// Get the Last Traded Price of the Token against USDC for conversion | ||
const swap = data['swaps'][0]; | ||
return swap['amountOut'] / swap['amountIn']; | ||
} | ||
|
||
async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> { | ||
const { block, chain, web3 } = params; | ||
if (block < START_BLOCK) { | ||
return {}; | ||
} | ||
|
||
let usdcValue = 0; | ||
|
||
const tokenBalances = await util.getTokenBalancesOfHolders( | ||
PROTOCOL_ADDRESSES, | ||
TRACKED_TOKENS, | ||
block, | ||
chain, | ||
web3, | ||
); | ||
const rawBalances = {}; | ||
formatter.sumMultiBalanceOf(rawBalances, tokenBalances); | ||
// rawBalances now contains the commodity tokens held in the protocol addresses | ||
|
||
for (const token of TRACKED_TOKENS) { | ||
if (!rawBalances[token]) { | ||
continue; | ||
} | ||
// UniswapV3 is used to get the conversion rate of the commodity tokens to USDC | ||
const conversionRate = await getConversion( | ||
block, | ||
token, | ||
USDC_TOKEN_ADDRESS, | ||
); | ||
const tokenBalance = rawBalances[token]; | ||
usdcValue += tokenBalance * conversionRate; | ||
} | ||
|
||
const balances = { [USDC_TOKEN_ADDRESS]: usdcValue.toString() }; | ||
|
||
return { balances }; | ||
} | ||
|
||
export { tvl }; |