-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceProvider.cs
More file actions
30 lines (25 loc) · 1018 Bytes
/
Copy pathPriceProvider.cs
File metadata and controls
30 lines (25 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System.Numerics;
using Nethereum.Web3;
public class PriceProvider(Web3 web3, Dictionary<string, string> chainlinkFeeds) : ITokenPriceProvider
{
private const string Abi = @"[
{
'inputs': [],
'name': 'latestAnswer',
'outputs': [
{ 'internalType': 'int256', 'name': '', 'type': 'int256' }
],
'stateMutability': 'view', 'type': 'function'
}
]";
public async Task<decimal> GetPriceInEthAsync(string tokenAddress)
{
if (!chainlinkFeeds.TryGetValue(tokenAddress, out var feedAddress))
throw new Exception("Missing Chainlink feed for token " + tokenAddress);
var contract = web3.Eth.GetContract(Abi, feedAddress);
var func = contract.GetFunction("latestAnswer");
var result = await func.CallAsync<BigInteger>();
decimal ethPrice = (decimal)result / (decimal)Math.Pow(10, 18); // assuming 18 decimals for ETH price
return ethPrice;
}
}