|
| 1 | +type TKongVaultListItem = { |
| 2 | + chainId: number |
| 3 | + address: string |
| 4 | + name: string |
| 5 | + symbol: string | null |
| 6 | + origin?: string | null |
| 7 | + inclusion?: { |
| 8 | + isYearn?: boolean |
| 9 | + [key: string]: boolean | undefined |
| 10 | + } | null |
| 11 | + isHidden: boolean |
| 12 | + isRetired: boolean |
| 13 | + kind: string | null |
| 14 | + type: string | null |
| 15 | + category: string | null |
| 16 | +} |
| 17 | + |
| 18 | +type TKongVaultSnapshot = { |
| 19 | + address: string |
| 20 | + chainId: number |
| 21 | + name?: string |
| 22 | + symbol?: string |
| 23 | + meta?: { |
| 24 | + address?: string |
| 25 | + chainId?: number |
| 26 | + name?: string |
| 27 | + isHidden?: boolean |
| 28 | + isRetired?: boolean |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +type TAuditRecord = { |
| 33 | + address: string |
| 34 | + chainId: number |
| 35 | + inListAll: boolean |
| 36 | + inListOriginYearn: boolean |
| 37 | + listAllMetadata: { |
| 38 | + origin: string | null |
| 39 | + inclusionIsYearn: boolean | null |
| 40 | + isHidden: boolean | null |
| 41 | + isRetired: boolean | null |
| 42 | + kind: string | null |
| 43 | + type: string | null |
| 44 | + category: string | null |
| 45 | + } | null |
| 46 | + snapshot: { |
| 47 | + found: boolean |
| 48 | + name: string | null |
| 49 | + symbol: string | null |
| 50 | + isHidden: boolean | null |
| 51 | + isRetired: boolean | null |
| 52 | + } |
| 53 | + likelyExclusionReason: |
| 54 | + | 'missing_upstream_list_item' |
| 55 | + | 'excluded_by_origin_filter' |
| 56 | + | 'excluded_by_catalog_tag_filter' |
| 57 | + | 'missing_snapshot' |
| 58 | + | 'included_in_catalog' |
| 59 | +} |
| 60 | + |
| 61 | +const DEFAULT_CHAIN_ID = 1 |
| 62 | +const DEFAULT_ADDRESSES = ['0x7A26C6c1628c86788526eFB81f37a2ffac243A98', '0xf91a9A1C782a1C11B627f6E576d92C7d72CDd4AF'] |
| 63 | +const KONG_REST_BASE = (process.env.KONG_REST_BASE || 'https://kong.yearn.fi/api/rest').replace(/\/$/, '') |
| 64 | + |
| 65 | +function normalizeAddress(address: string): string { |
| 66 | + return address.trim().toLowerCase() |
| 67 | +} |
| 68 | + |
| 69 | +function parseArgs(argv: string[]): { chainId: number; addresses: string[] } { |
| 70 | + let chainId = DEFAULT_CHAIN_ID |
| 71 | + const addresses: string[] = [] |
| 72 | + |
| 73 | + for (let index = 0; index < argv.length; index++) { |
| 74 | + const current = argv[index] |
| 75 | + if (current === '--chain' || current === '-c') { |
| 76 | + const value = argv[index + 1] |
| 77 | + if (!value) { |
| 78 | + throw new Error('Missing value for --chain') |
| 79 | + } |
| 80 | + chainId = Number(value) |
| 81 | + index += 1 |
| 82 | + continue |
| 83 | + } |
| 84 | + if (current.startsWith('--chain=')) { |
| 85 | + chainId = Number(current.split('=')[1]) |
| 86 | + continue |
| 87 | + } |
| 88 | + addresses.push(current) |
| 89 | + } |
| 90 | + |
| 91 | + if (!Number.isInteger(chainId) || chainId <= 0) { |
| 92 | + throw new Error(`Invalid chain id: ${chainId}`) |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + chainId, |
| 97 | + addresses: addresses.length > 0 ? addresses : DEFAULT_ADDRESSES |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +async function fetchJson<T>(url: string): Promise<T> { |
| 102 | + const response = await fetch(url) |
| 103 | + if (!response.ok) { |
| 104 | + throw new Error(`HTTP ${response.status} for ${url}`) |
| 105 | + } |
| 106 | + return (await response.json()) as T |
| 107 | +} |
| 108 | + |
| 109 | +async function tryFetchSnapshot(chainId: number, address: string): Promise<TKongVaultSnapshot | null> { |
| 110 | + const url = `${KONG_REST_BASE}/snapshot/${chainId}/${address}` |
| 111 | + const response = await fetch(url) |
| 112 | + if (response.status === 404) { |
| 113 | + return null |
| 114 | + } |
| 115 | + if (!response.ok) { |
| 116 | + throw new Error(`HTTP ${response.status} for ${url}`) |
| 117 | + } |
| 118 | + return (await response.json()) as TKongVaultSnapshot |
| 119 | +} |
| 120 | + |
| 121 | +function indexByAddress(list: TKongVaultListItem[]): Map<string, TKongVaultListItem> { |
| 122 | + const map = new Map<string, TKongVaultListItem>() |
| 123 | + for (const item of list) { |
| 124 | + map.set(normalizeAddress(item.address), item) |
| 125 | + } |
| 126 | + return map |
| 127 | +} |
| 128 | + |
| 129 | +function deriveLikelyReason(params: { |
| 130 | + inListAll: boolean |
| 131 | + inListOriginYearn: boolean |
| 132 | + snapshotFound: boolean |
| 133 | + origin: string | null |
| 134 | + inclusionIsYearn: boolean | null |
| 135 | +}): TAuditRecord['likelyExclusionReason'] { |
| 136 | + const { inListAll, inListOriginYearn, snapshotFound, origin, inclusionIsYearn } = params |
| 137 | + |
| 138 | + if (!snapshotFound) { |
| 139 | + return 'missing_snapshot' |
| 140 | + } |
| 141 | + if (!inListAll) { |
| 142 | + return 'missing_upstream_list_item' |
| 143 | + } |
| 144 | + if (!inListOriginYearn && origin !== 'yearn') { |
| 145 | + return 'excluded_by_origin_filter' |
| 146 | + } |
| 147 | + if (origin !== 'yearn' && inclusionIsYearn !== true) { |
| 148 | + return 'excluded_by_catalog_tag_filter' |
| 149 | + } |
| 150 | + return 'included_in_catalog' |
| 151 | +} |
| 152 | + |
| 153 | +async function main(): Promise<void> { |
| 154 | + const { chainId, addresses } = parseArgs(process.argv.slice(2)) |
| 155 | + const [listAll, listOriginYearn] = await Promise.all([ |
| 156 | + fetchJson<TKongVaultListItem[]>(`${KONG_REST_BASE}/list/vaults`), |
| 157 | + fetchJson<TKongVaultListItem[]>(`${KONG_REST_BASE}/list/vaults?origin=yearn`) |
| 158 | + ]) |
| 159 | + |
| 160 | + const listAllByAddress = indexByAddress(listAll) |
| 161 | + const listOriginYearnByAddress = indexByAddress(listOriginYearn) |
| 162 | + |
| 163 | + const results: TAuditRecord[] = [] |
| 164 | + for (const addressInput of addresses) { |
| 165 | + const address = normalizeAddress(addressInput) |
| 166 | + const listAllItem = listAllByAddress.get(address) |
| 167 | + const listOriginYearnItem = listOriginYearnByAddress.get(address) |
| 168 | + const snapshot = await tryFetchSnapshot(chainId, address) |
| 169 | + |
| 170 | + const origin = listAllItem?.origin ?? null |
| 171 | + const inclusionIsYearn = listAllItem?.inclusion?.isYearn ?? null |
| 172 | + const inListAll = Boolean(listAllItem) |
| 173 | + const inListOriginYearn = Boolean(listOriginYearnItem) |
| 174 | + |
| 175 | + results.push({ |
| 176 | + address, |
| 177 | + chainId, |
| 178 | + inListAll, |
| 179 | + inListOriginYearn, |
| 180 | + listAllMetadata: listAllItem |
| 181 | + ? { |
| 182 | + origin, |
| 183 | + inclusionIsYearn, |
| 184 | + isHidden: listAllItem.isHidden ?? null, |
| 185 | + isRetired: listAllItem.isRetired ?? null, |
| 186 | + kind: listAllItem.kind ?? null, |
| 187 | + type: listAllItem.type ?? null, |
| 188 | + category: listAllItem.category ?? null |
| 189 | + } |
| 190 | + : null, |
| 191 | + snapshot: { |
| 192 | + found: Boolean(snapshot), |
| 193 | + name: snapshot?.name ?? snapshot?.meta?.name ?? null, |
| 194 | + symbol: snapshot?.symbol ?? null, |
| 195 | + isHidden: snapshot?.meta?.isHidden ?? null, |
| 196 | + isRetired: snapshot?.meta?.isRetired ?? null |
| 197 | + }, |
| 198 | + likelyExclusionReason: deriveLikelyReason({ |
| 199 | + inListAll, |
| 200 | + inListOriginYearn, |
| 201 | + snapshotFound: Boolean(snapshot), |
| 202 | + origin, |
| 203 | + inclusionIsYearn |
| 204 | + }) |
| 205 | + }) |
| 206 | + } |
| 207 | + |
| 208 | + console.log( |
| 209 | + JSON.stringify( |
| 210 | + { |
| 211 | + chainId, |
| 212 | + base: KONG_REST_BASE, |
| 213 | + totals: { |
| 214 | + listAll: listAll.length, |
| 215 | + listOriginYearn: listOriginYearn.length |
| 216 | + }, |
| 217 | + results |
| 218 | + }, |
| 219 | + null, |
| 220 | + 2 |
| 221 | + ) |
| 222 | + ) |
| 223 | +} |
| 224 | + |
| 225 | +void main() |
0 commit comments