-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
149 lines (122 loc) · 4.07 KB
/
test.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import 'dotenv/config'
import _ from 'lodash'
import got from 'got'
import { humanizeNumber } from '@defillama/sdk/build/computeTVL/humanizeNumber'
import { abi } from '@defillama/sdk/build/api'
import { Chain } from '@defillama/sdk/build/general'
import { GetEventsReturns, GetPorfolioReturns, GetPorfolioChainParam } from './adapterTypes'
const project = process.argv[2]
const address = process.argv[3]
;(async () => {
const adapter = await import(`./projects/${project}`)
const events: GetEventsReturns = await adapter.getEvents()
// TODO
// if (address == undefined) {
// }
const chains: GetPorfolioChainParam = Object.values(_.groupBy(events, (e) => e.chainName)).map(
(events): GetPorfolioChainParam[0] => ({
chainName: events[0].chainName,
addresses: events.map((x) => x.address),
})
)
const chainOutputs: GetPorfolioReturns = await adapter.getPorfolio(chains, address)
const coins = await getCoins(
chainOutputs.map((x) => {
const suppliedIds = x.supplied.map((y) =>
Array.isArray(y) ? y.map((z) => `${x.chainName}:${z.address}`) : `${x.chainName}:${y.address}`
)
const borrowedIds = x.borrowed?.map((y) => `${x.chainName}:${y.address}`) ?? []
return suppliedIds.concat(borrowedIds.filter((x) => !suppliedIds.includes(x)))
})
)
chainOutputs.forEach((chainOutput) => {
console.log(`--- ${chainOutput.chainName} ---`)
let total = 0
chainOutput.supplied.forEach((s) => {
;(Array.isArray(s) ? s : [s]).forEach((supplied) => {
if (supplied.balance !== '0') {
const coin = coins[`${chainOutput.chainName}:${supplied.address}`]
const balance = Number(supplied.balance) / 10 ** coin.decimals
const value = balance * coin.price
total += value
console.log(`${coin.symbol.padEnd(30)} ${humanizeNumber(balance).padEnd(30)} $${humanizeNumber(value)}`)
}
})
})
if (chainOutput.borrowed && chainOutput.borrowed.length > 0) {
console.log('\nBorrowed:')
chainOutput.borrowed.forEach((borrowed) => {
if (borrowed.balance !== '0') {
const coin = coins[`${chainOutput.chainName}:${borrowed.address}`]
const balance = Number(borrowed.balance) / 10 ** coin.decimals
const value = balance * coin.price
total -= value
console.log(`${coin.symbol.padEnd(30)} ${humanizeNumber(balance).padEnd(30)} $${humanizeNumber(value)}`)
}
})
}
console.log(`\n${'Total:'.padEnd(61)} $${humanizeNumber(total)}`)
if (chainOutput.healthFactor) {
console.log(`${'Health factor:'.padEnd(61)} ${chainOutput.healthFactor}`)
}
console.log('')
})
})()
async function getCoins(ids: (string | string[])[][]): Promise<{
[key: string]: {
decimals: number
symbol: string
price: number
timestamp: number
confidence: number
}
}> {
const uniqIds = _.uniq(_.flattenDeep(ids))
const coins = _.merge(
{},
...(await Promise.all(
_.chunk(uniqIds, 100).map(async (ids) => {
const { coins } = await got(`https://coins.llama.fi/prices/current/${ids.join(',')}`).json<{
coins: {
[key: string]: {
decimals: number
symbol: string
price: number
timestamp: number
confidence: number
}
}
}>()
return coins
})
))
)
const coinsNotFound = uniqIds.filter((id) => !coins[id])
await Promise.all(
coinsNotFound.map(async (coin) => {
const [chain, address] = coin.split(':')
const symbol = await (
await abi.call({
chain: chain as Chain,
target: address,
abi: 'erc20:symbol',
})
).output
const decimals = await (
await abi.call({
chain: chain as Chain,
target: address,
abi: 'erc20:decimals',
})
).output
coins[coin] = {
decimals,
symbol,
price: 0,
timestamp: Math.floor(new Date().getTime() / 1000),
confidence: 0,
}
})
)
return coins
}