-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit.js
284 lines (244 loc) · 11.2 KB
/
audit.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import {AddressExplorerByChainID, bitcoinChainID, esploraAPIURL, evmURL,
nodeURL, RPCByChainID,externalChainIDs, renderHeader}
from './common.js';
import './web3.min.js';
await renderHeader();
class AuditPage {
constructor() {
}
async initialize() {
await this.getABIs();
await this.getTssAddress();
await this.getZRC20Addresses();
this.getWeb3();
await this.getSystemContracts();
await this.getSystemPools();
await this.getERC20Custody();
}
async getSystemContracts() {
const resource = `${nodeURL}/zeta-chain/fungible/system_contract`;
const p = await fetch(resource, {
method: 'GET',
});
this.systemContractsAddresses = (await p.json()).SystemContract;
console.log("system contract address", this.systemContractsAddresses);
this.systemContract = new this.web3zevm.eth.Contract(this.SystemContractABI, this.systemContractsAddresses.system_contract);
let p2 = await this.systemContract.methods.uniswapv2FactoryAddress().call();
this.uniswapv2FactoryAddress = p2;
this.uniswapv2Factory = new this.web3zevm.eth.Contract(this.UniswapV2PairABI, this.uniswapv2FactoryAddress);
}
async getERC20Custody() {
this.erc20CustodyAddress = {};
for (let i=0; i<externalChainIDs.length; i++) {
const chainID = externalChainIDs[i];
if (chainID == 8332) continue;
const resource = `${nodeURL}/zeta-chain/observer/get_chain_params_for_chain/${chainID}`;
const p = await fetch(resource, {
method: 'GET',
});
const chainParams = await p.json();
console.log("chainParams", chainParams);
const erc20CustodyAddress = chainParams.chain_params.erc20_custody_contract_address;
console.log("erc20CustodyAddress", erc20CustodyAddress);
this.erc20CustodyAddress[`${chainID}`] = erc20CustodyAddress;
}
}
async getTssAddress() {
if (this.tss) return;
const resource = "zeta-chain/observer/get_tss_address";
const p = await fetch(`${nodeURL}/${resource}/${bitcoinChainID}`, {
method: 'GET',
});
this.tss = await p.json();
console.log("tss", this.tss)
}
async getZRC20Addresses() {
const resource = "zeta-chain/fungible/foreign_coins";
const p = await fetch(`${nodeURL}/${resource}`, {
method: 'GET',
});
this.zrc20s = (await p.json()).foreignCoins;
console.log("zrc20s", this.zrc20s);
}
async getSystemPools() {
console.log("system contract", this.systemContract.methods);
const chainIDs = externalChainIDs;
this.systemPoolAddresses = {};
for (let i = 0; i < chainIDs.length; i++) {
const chainID = chainIDs[i];
const poolAddress = await this.systemContract.methods.gasZetaPoolByChainId(chainID).call();
this.systemPoolAddresses[chainID] = poolAddress;
}
}
async getABIs() {
let p1 = await fetch('abi/SystemContract.json');
let data1 = await p1.json();
this.SystemContractABI = data1.abi;
let p2 = await fetch('abi/UniswapV2Pair.json');
let data2 = await p2.json();
this.UniswapV2PairABI = data2.abi;
let p3 = await fetch('abi/ZRC20.json');
let data3 = await p3.json();
this.ZRC20ABI = data3.abi;
let p4 = await fetch('abi/UniswapV2Factory.json');
let data4 = await p4.json();
this.UniswapV2FactoryABI = data4.abi;
console.assert(this.SystemContractABI.length > 0);
let p5 = await fetch('abi/ZetaConnectorEth.json');
let data5 = await p5.json();
this.EthConnectorABI = data5.abi;
console.assert(this.EthConnectorABI.length > 0);
let p6 = await fetch('abi/ZetaNonEth.json');
let data6 = await p6.json();
this.ZetaNonEthABI = data6.abi;
console.assert(this.ZetaNonEthABI.length > 0);
}
getWeb3() {
this.web3zevm = new Web3(evmURL);
this.web3ByChainId = {};
for (const i in this.zrc20s) {
const fcoin = this.zrc20s[i];
const chainID = fcoin.foreign_chain_id;
if (chainID != 18332 && chainID != 8332) {
if (!this.web3ByChainId[chainID]) {
this.web3ByChainId[chainID] = new Web3(`${RPCByChainID[chainID]}`);
}
}
}
}
async TSSReserveComponent(fcoin) {
const chainID = fcoin.foreign_chain_id;
const symbol = fcoin.symbol;
const decimals = fcoin.decimals;
let assets, liabilities;
let tssAddr;
if (fcoin.coin_type == "Gas") {
if (chainID != 18332 && chainID != 8332) {
const foreignWeb3 = this.web3ByChainId[chainID];
tssAddr = this.tss.eth;
assets = await foreignWeb3.eth.getBalance(this.tss.eth);
assets = assets / Math.pow(10, decimals);
} else { // bitcoin clients
tssAddr = this.tss.btc;
const resource = `${esploraAPIURL}/address/${this.tss.btc}`;
const p = await fetch(resource, {
method: 'GET',
});
const data = await p.json();
console.log("btc address info", data);
assets = (data.chain_stats.funded_txo_sum - data.chain_stats.spent_txo_sum) / Math.pow(10, 8);
}
} else if (fcoin.coin_type == "ERC20") {
const foreignWeb3 = this.web3ByChainId[chainID];
const erc20CustodyAddress = this.erc20CustodyAddress[fcoin.foreign_chain_id];
console.log("erc20CustodyAddress", erc20CustodyAddress);
console.log("fcoin.asset fcoin.chainid", fcoin.asset, fcoin.foreign_chain_id);
const erc20 = new foreignWeb3.eth.Contract(this.ZRC20ABI, fcoin.asset);
const r = await erc20.methods.balanceOf(erc20CustodyAddress).call();
console.log("erc20 balance", r);
assets = r / Math.pow(10, decimals);
}
const zrc20 = new this.web3zevm.eth.Contract(this.ZRC20ABI, fcoin.zrc20_contract_address);
liabilities = await zrc20.methods.totalSupply().call();
liabilities = liabilities / Math.pow(10, decimals);
const fungibleModuleAddress = "0x735b14BB79463307AAcBED86DAf3322B1e6226aB";
let fungibleBalance = await zrc20.methods.balanceOf(fungibleModuleAddress).call();
fungibleBalance = fungibleBalance / Math.pow(10, decimals);
liabilities -= fungibleBalance;
// assets += fungibleBalance;
let poolBalance;
if (fcoin.coin_type == "Gas") {
const poolAddress = this.systemPoolAddresses[chainID];
poolBalance = await zrc20.methods.balanceOf(poolAddress).call();
poolBalance = poolBalance / Math.pow(10, decimals);
} else if (fcoin.coin_type == "ERC20") {
}
console.log("address explorer", AddressExplorerByChainID[chainID]);
const flag = (assets >= liabilities) ? "🟢" : "🔴";
const surplus = assets - liabilities;
return DIV(
H3(`${flag}: Reserve of ${symbol} ZRC20 address ${fcoin.zrc20_contract_address} on chain ${chainID}`),
PRE(TEXT(`Chain ID: ${chainID}, Symbol: ${symbol}, Decimals: ${decimals}`)),
PRE(
TEXT(`Liabilities (ZRC20 Supply): ${liabilities}`), BR(),
TEXT(` System UniswapV2 Pool asset-wZETA: ${poolBalance}`), BR(),
TEXT(` Rest: ${liabilities - fungibleBalance - poolBalance}`), BR(),
),
PRE(
A(`Assets (External Chain Balance): ${assets}`)
.att$("href", `${AddressExplorerByChainID[chainID]}/${tssAddr}`)
.att$("target", "_blank"), BR(),
),
PRE(
TEXT(`Surplus: ${surplus}`), BR(),
),
PRE(
TEXT(`Fungile Module Balance: ${fungibleBalance}`), BR(),
),
);
}
async ZETASupplyComponent() {
const p = await fetch(`${nodeURL}/cosmos/bank/v1beta1/supply/by_denom?denom=azeta`);
const data = await p.json();
const zetaSupplyZeta = parseInt(data.amount.amount) / Math.pow(10, 18);
const p2 = await fetch(`${nodeURL}/zeta-chain/observer/get_chain_params`);
const data2 = await p2.json();
console.log("data2", data2);
const chainIDs = externalChainIDs;
const zetaContractByChainID = {};
let zetaLocked;
const zetaSupplies = {};
for (let params of data2.chain_params.chain_params) {
const chainID = params.chain_id;
const zetaContract = params.zeta_token_contract_address;
zetaContractByChainID[chainID] = zetaContract;
const RPC = RPCByChainID[chainID];
const web3 = new Web3(RPC);
if (chainID == 5 || chainID == 1) {
const ethConnectorAddress = params.connector_contract_address;
console.log("ethConnectorAddress", ethConnectorAddress);
const connector = new web3.eth.Contract(this.EthConnectorABI, ethConnectorAddress);
zetaLocked = await connector.methods.getLockedAmount().call();
zetaLocked = parseInt(zetaLocked) / Math.pow(10, 18);
console.log("eth chain zeta locked", zetaLocked);
} else if (chainID == 97 || chainID == 80001 || chainID == 56) {
const zeta = new web3.eth.Contract(this.ZetaNonEthABI, zetaContract);
let zetaSupply = await zeta.methods.totalSupply().call();
zetaSupply = parseInt(zetaSupply) / Math.pow(10, 18);
console.log("chainID", chainID, "zetaSupply", zetaSupply);
zetaSupplies[chainID] = zetaSupply;
}
}
console.log("zetaContractByChainID", zetaContractByChainID);
return DIV(
PRE(TEXT(`Assets (ZETA Supply): ${zetaLocked}`), BR(),
TEXT(` ZETA locked on Ethereum: ${zetaLocked}`)
),
PRE(TEXT(`Liabilities: `), BR(),
TEXT(`ZETA Supply on ZetaChain:${zetaSupplyZeta}`), BR(),
TEXT(`ZETA locked on BSC: ${zetaSupplies[56]}`), BR(),
// TEXT(`ZETA locked on Polygon: ${zetaSupplies[80001]}`), BR()
),
);
}
async render() {
const entry = document.getElementById("entry");
entry.appendChild(H1("Foreign Reserves"));
for (const i in this.zrc20s) {
const fcoin = this.zrc20s[i];
console.log("foreign coin", fcoin);
// entry.appendChild(await this.TSSReserveComponent(fcoin));
this.TSSReserveComponent(fcoin).then((v) => {
entry.appendChild(v);
});
}
entry.appendChild(H1("ZETA Supply"));
entry.appendChild(await this.ZETASupplyComponent());
}
}
const page = new AuditPage();
await page.initialize();
console.log(page.tss);
console.log(page.zrc20s);
console.log("web3zevm", page.web3zevm);
await page.render();