-
Notifications
You must be signed in to change notification settings - Fork 0
/
directContract.ts
244 lines (204 loc) · 7.32 KB
/
directContract.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import "isomorphic-fetch";
import { Abi, Address, createPublicClient, http, parseAbi } from "viem";
import { mainnet, polygon } from "viem/chains";
import cometAbi from "../abis/comet.json";
import erc20Abi from "../abis/Erc20.json";
import configuratorAbi from "../abis/Configurator.json";
// Make BigInt serializable...
(BigInt.prototype as any).toJSON = function() {
return this.toString();
};
const READ_BLOCK = BigInt(18314678);
const PRICE_FEED_DECIMALS = 8;
const MAINNET_USDC_COMET_PROXY = "0xc3d688b66703497daa19211eedff47f25384cdc3";
const MAINNET_ETHER_COMET_PROXY = "0xa17581a9e3356d9a858b789d68b4d866e593ae94";
const COMET_ADDRESS = MAINNET_USDC_COMET_PROXY;
const client = createPublicClient({
chain: mainnet,
transport: http(),
});
function getNoInputViewFunctions(abi: Abi): string[] {
const noInputViewFunctions = [];
for (const param of abi) {
if (param.type == "function" && param.stateMutability == "view" && param.inputs.length == 0) {
noInputViewFunctions.push(param.name);
}
}
return noInputViewFunctions;
}
async function multiReadContract(
contractAddress: Address,
abi: Abi,
functions: string[],
args: any[] | null,
blockNumber: bigint
): Promise<void> {
const multicallParams = functions.map((f, i) => {
return {
address: contractAddress,
abi: abi,
functionName: f,
arg: args ? args[i] : [],
};
});
// console.log(multicallParams);
const results = await client.multicall({
contracts: multicallParams,
blockNumber: blockNumber,
});
console.log("CONTRACT: ", contractAddress);
console.log("BLOCK: ", blockNumber);
results.map((result, i) => {
console.log(`${functions[i]}: ${JSON.stringify(result.result)}`);
});
}
async function marketCollateralInfo(cometAddress: Address, collateralAddress: Address, blockNumber: bigint) {
const functionNames = ["getAssetInfoByAddress", "totalsCollateral", "getCollateralReserves"];
console.log("COLLATERAL INFO - ", collateralAddress);
for (let fnName of functionNames) {
const res = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: fnName,
args: [collateralAddress],
blockNumber: blockNumber,
});
console.log(fnName, res);
}
}
async function getPositionInfo(cometAddress: Address, accountAddress: Address, blockNumber: bigint) {
const basicRes = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "userBasic",
args: [accountAddress],
blockNumber: blockNumber,
});
const supplyBalRes = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "balanceOf",
args: [accountAddress],
blockNumber: blockNumber,
});
const borrowBalRes = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "borrowBalanceOf",
args: [accountAddress],
blockNumber: blockNumber,
});
const basePriceFeed = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "baseTokenPriceFeed",
args: [],
blockNumber: blockNumber,
});
const basePrice = (await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "getPrice",
args: [basePriceFeed],
blockNumber: blockNumber,
})) as bigint;
const baseAddress = (await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "baseToken",
args: [],
blockNumber: blockNumber,
})) as Address;
const baseDecimals = (await client.readContract({
address: baseAddress,
abi: erc20Abi,
functionName: "decimals",
args: [],
blockNumber: blockNumber,
})) as number;
const baseBal = supplyBalRes != BigInt(0) ? (supplyBalRes as bigint) : (borrowBalRes as bigint) * BigInt(-1);
console.log("Principal: ", (basicRes as Array<any>)[0]);
console.log("Base balance: ", baseBal);
console.log("baseTrackingIndex: ", (basicRes as Array<any>)[1]);
console.log("baseTrackingAccured: ", (basicRes as Array<any>)[2]);
console.log(
"Base balance USD or ETH: ",
(baseBal * basePrice) / BigInt(10 ** (baseDecimals + PRICE_FEED_DECIMALS))
);
const numAssets = (await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "numAssets",
blockNumber: blockNumber,
})) as number;
let totalColBalUsd = BigInt(0);
for (let i = 0; i < numAssets; i++) {
const assetInfo = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "getAssetInfo",
args: [i],
blockNumber: blockNumber,
});
const colAddress = (assetInfo as any)["asset"] as Address;
const colRes = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "userCollateral",
args: [accountAddress, colAddress],
blockNumber: blockNumber,
});
const colPrice = await client.readContract({
address: cometAddress,
abi: cometAbi,
functionName: "getPrice",
args: [(assetInfo as any)["priceFeed"]],
blockNumber: blockNumber,
});
const decimals = (await client.readContract({
address: colAddress,
abi: erc20Abi,
functionName: "decimals",
args: [],
blockNumber: blockNumber,
})) as number;
const colSymbol = (await client.readContract({
address: colAddress,
abi: erc20Abi,
functionName: "symbol",
args: [],
blockNumber: blockNumber,
})) as string;
const colBal = (colRes as Array<any>)[0];
const colBalUsd = ((colBal as bigint) * (colPrice as bigint)) / BigInt(10 ** (decimals + PRICE_FEED_DECIMALS));
console.log("COLLATERAL BALANCE: ", colAddress, colSymbol);
console.log("Balance - ", colBal);
console.log("Balance USD or ETH - ", colBalUsd);
totalColBalUsd += colBalUsd;
}
console.log("TOTAL Collateral Balance USD or ETH: ", totalColBalUsd);
}
async function main() {
// await multiReadContract(COMET_ADDRESS, cometAbi as Abi, getNoInputViewFunctions(cometAbi as Abi), null, READ_BLOCK);
// console.log("\n\n\n");
// await marketCollateralInfo(COMET_ADDRESS, "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599", BigInt(18315072));
// console.log("\n\n\n");
// await getPositionInfo(
// "0xa17581a9e3356d9a858b789d68b4d866e593ae94",
// "0x10d88638be3c26f3a47d861b8b5641508501035d",
// BigInt(17607287)
// );
const client = createPublicClient({
chain: polygon,
transport: http(),
});
const f = await client.readContract({
address: "0x83E0F742cAcBE66349E3701B171eE2487a26e738",
abi: configuratorAbi,
functionName: "factory",
args: ["0xF25212E676D1F7F89Cd72fFEe66158f541246445"],
blockNumber: BigInt(39413523),
});
console.log(f);
}
main();