-
Notifications
You must be signed in to change notification settings - Fork 24
/
contracts.mjs
181 lines (171 loc) · 4.31 KB
/
contracts.mjs
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
import { HttpBatchClient, Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination.js";
import fs from "fs";
import { MAINNET, RPCS, TESTNET } from "./lib/cjs/network.js";
import { kujiraQueryClient } from "./lib/cjs/queryClient.js";
const IDS = {
[MAINNET]: {
fin: [
// Current
283, 356,
// Oracle
370,
],
bow: [
294,
// LSD Strategy
322,
// Stable Strategy
161, 166, 355,
],
bowStaking: [244],
orca: [234, 344, 349],
uskMarket: [73],
uskMarginSwap: [87],
uskMarginLimit: [],
calc: [157],
ghostVault: [316, 384],
ghostMarket: [291],
ghostMargin: [],
pilot: [296, 301, 333],
bowMargin: [317, 337],
},
[TESTNET]: {
fin: [31, 2229, 3328, 3566],
bow: [1925, 2362, 3330],
bowStaking: [439, 855],
orca: [1952, 2923],
uskMarket: [66, 136],
uskMarginSwap: [131, 133],
uskMarginLimit: [1271, 1272],
calc: [1273, 1387],
ghostVault: [2348],
ghostMarket: [2172],
ghostMargin: [1950],
pilot: [3549],
bowMargin: [2666],
},
};
const fetchId = (client, protocol) => async (id) => ({
id,
contracts: await fetchContracts(client)(id).then(async (res) => {
return Promise.all(res.map(fetchContractData(client, protocol, id)));
}),
});
const fetchProtocol =
(client) =>
async ([protocol, ids]) => {
return {
protocol,
ids: await Promise.all(ids.map(fetchId(client, protocol))),
};
};
const fetchContractData = (client, protocol, id) => async (address) => {
return {
address,
config:
protocol === "ghostVault"
? {
...(await client.wasm
.queryContractSmart(address, {
config: {},
})
.catch(() => ({}))),
interest:
id === 106
? await client.wasm
.queryContractRaw(
address,
Uint8Array.from([115, 116, 97, 116, 101])
)
.then(({ data }) =>
JSON.parse(Buffer.from(data).toString())
)
.then(({ utilization_to_rate }) => ({
utilization_to_rate,
}))
: await client.wasm.queryContractSmart(address, {
interest_params: {},
}),
}
: await client.wasm
.queryContractSmart(address, {
config: {},
})
.catch(() => ({})),
pairs:
protocol === "calc" &&
(await client.wasm
.queryContractSmart(address, {
get_pairs: {},
})
.then(({ pairs }) => pairs)),
markets:
protocol === "ghostVault" &&
(await client.wasm
.queryContractSmart(address, {
markets: {},
})
.then(({ markets }) => markets)
.catch((err) => {
console.log(err);
return null;
})),
};
};
const fetchContracts = (client) => async (id, pageRequest) => {
const { contracts, pagination } = await client.wasm.listContractsByCodeId(
id,
pageRequest
);
return pagination.nextKey.length
? [
...contracts,
...(await fetchContracts(client)(
id,
PageRequest.fromPartial({ key: pagination.nextKey })
)),
]
: contracts;
};
const res = await Promise.all(
Object.entries(IDS).map(async ([chain, protocols]) => {
const rpc = RPCS[chain][0];
const tm = await Tendermint34Client.create(
new HttpBatchClient(rpc, {
dispatchInterval: 100,
batchSizeLimit: 200,
})
);
const client = kujiraQueryClient({ client: tm });
return {
chain,
protocols: await Promise.all(
Object.entries(protocols).map(fetchProtocol(client))
),
};
})
);
const flattened = res.reduce(
(a, m) => ({
...a,
[m.chain]: m.protocols.reduce(
(b, n) => ({
...b,
[n.protocol]: n.ids.flatMap((id) =>
id.contracts.map((contract) => ({
id: id.id,
...contract,
}))
),
}),
{}
),
}),
{}
);
fs.writeFileSync(
"./src/resources/contracts.json",
JSON.stringify(flattened, null, 2)
);
process.exit();