-
Notifications
You must be signed in to change notification settings - Fork 0
/
camelot.js
76 lines (68 loc) · 2.99 KB
/
camelot.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { ethers } = require('ethers');
const { formatUnits } = require('ethers/lib/utils');
const CamelotNitroPool = require('./abis/CamelotNitroPool.json');
const CamelotPool = require('./abis/CamelotPool.json');
const ERC20 = require('./abis/ERC20.json');
const getContract = (address, abi) => {
const jsonRpcProvider = new ethers.getDefaultProvider('https://arb1.arbitrum.io/rpc')
return new ethers.Contract(address, abi, jsonRpcProvider)
}
const fromBigNumber = (number, decimals = 18) => {
return parseFloat(formatUnits(number.toString(), decimals))
}
const fetchUserPoolDetails = async (poolAddress, nitroPoolAddress, userAddress) => {
// Fetch both pools
const camelotNitroPool = getContract(nitroPoolAddress, CamelotNitroPool.abi)
// WE NEED TO USE THIS TO CALCULATE THE USER'S POOL PERCENTAGE
const camelotPool = getContract(poolAddress, CamelotPool.abi)
// Fetch collateral tokens for pools
const collateral0 = await camelotPool.token0()
const collateral1 = await camelotPool.token1()
const collateral0Contract = getContract(collateral0, ERC20.abi)
const collateral1Contract = getContract(collateral1, ERC20.abi)
const response = await fetch('https://api.camelot.exchange/nitros')
const res = await response.json()
const tvlUSD = res.data.nitros[nitroPoolAddress] ?? 0
const collateralTokens = [
{
symbol: await collateral0Contract.symbol(),
userBalance: fromBigNumber(await collateral0Contract.balanceOf(userAddress)),
nitroPoolBalance: fromBigNumber(await collateral0Contract.balanceOf(nitroPoolAddress)),
poolBalance: fromBigNumber(await collateral0Contract.balanceOf(poolAddress)),
address: collateral0,
},
{
symbol: await collateral1Contract.symbol(),
userBalance: fromBigNumber(await collateral1Contract.balanceOf(userAddress)),
nitroPoolBalance: fromBigNumber(await collateral1Contract.balanceOf(nitroPoolAddress)),
poolBalance: fromBigNumber(await collateral1Contract.balanceOf(poolAddress)),
address: collateral1,
}
]
const userInfo = await camelotNitroPool.userInfo(userAddress)
const userDepositAmount = fromBigNumber(userInfo.totalDepositAmount)
const totalDepositAmount = fromBigNumber(await camelotNitroPool.totalDepositAmount())
// What percentage of the pool does the user own?
const userPoolPercentage = userDepositAmount / totalDepositAmount * 100
const userDollarValue = (userPoolPercentage / 100) * tvlUSD;
console.log(
collateralTokens,
userInfo,
userDepositAmount,
totalDepositAmount,
userPoolPercentage,
userDollarValue,
tvlUSD,
`User has ${userPoolPercentage}% of the pool, which is worth $${userDollarValue}`
)
return {
collateralTokens,
userInfo,
userDepositAmount,
totalDepositAmount,
userPoolPercentage,
userDollarValue,
}
}
// poolAddress, nitroPoolAddress, userAddress
fetchUserPoolDetails('0x824959a55907d5350e73e151Ff48DabC5A37a657', '0x53F973256F410d1D8b10ce72D03D8dBBD3b1066E', '0xb919e09bb077013d5f93c898dafcc1d0c75559fe')