Skip to content

Commit fcaf23c

Browse files
authored
Fix Kinetiq-Markets (#17394)
1 parent d78d5ad commit fcaf23c

File tree

1 file changed

+32
-89
lines changed

1 file changed

+32
-89
lines changed

projects/kinetiq-markets/index.js

Lines changed: 32 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
const ADDRESSES = require('../helper/coreAssets.json')
2-
const { getLogs } = require('../helper/cache/getLogs')
1+
const router = '0x6AB31532382Ba5cD5E8b5D343Cf5995906bb8DD8'
32

4-
const WHYPE = ADDRESSES.hyperliquid.WHYPE
5-
const ROUTER = '0x6AB31532382Ba5cD5E8b5D343Cf5995906bb8DD8'
6-
const KHYPE_MANAGER = '0x393D0B87Ed38fc779FD9611144aE649BA6082109'
7-
8-
const FROM_BLOCK = 4800000
3+
const HYPE = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
4+
const kHypeManager = '0x393D0B87Ed38fc779FD9611144aE649BA6082109'
95

106
const abis = {
117
phase: "function phase(address exManager) view returns (uint8)",
@@ -15,115 +11,62 @@ const abis = {
1511
nextWithdrawalId: "function nextWithdrawalId(address) view returns (uint256)",
1612
withdrawalDelay: "function withdrawalDelay() view returns (uint256)",
1713
withdrawalRequests: "function withdrawalRequests(address user, uint256 id) view returns (uint256 hypeAmount, uint256 kHYPEAmount, uint256 kHYPEFee, uint256 bufferUsed, uint256 timestamp)",
18-
}
19-
20-
const eventAbi = "event ExManagerRegistered(address indexed exManager, address indexed exLST)"
14+
};
2115

22-
const DEFAULT_MANAGERS = [
23-
'0x4ef8bbacee867efd6faa684b30ecd12df74c4a48'
16+
const managers = [
17+
'0x4ef8bbacee867efd6faa684b30ecd12df74c4a48' // kmHYPE
2418
]
2519

26-
async function getExchangeManagers(api) {
27-
try {
28-
const logs = await getLogs({
29-
api,
30-
target: ROUTER,
31-
eventAbi,
32-
fromBlock: FROM_BLOCK,
33-
onlyArgs: true,
34-
})
35-
if (logs.length > 0) {
36-
return logs.map(log => log.exManager)
37-
}
38-
} catch (e) {
39-
// Fall back to known managers
40-
}
41-
return DEFAULT_MANAGERS
42-
}
43-
44-
async function tvl(api) {
45-
const managers = await getExchangeManagers(api)
46-
20+
const tvl = async (api) => {
4721
const [phases, exLSTs] = await Promise.all([
48-
api.multiCall({ target: ROUTER, calls: managers, abi: abis.phase }),
49-
api.multiCall({ target: ROUTER, calls: managers, abi: abis.exLST }),
22+
api.multiCall({ target: router, calls: managers, abi: abis.phase }),
23+
api.multiCall({ target: router, calls: managers, abi: abis.exLST }),
5024
])
5125

5226
for (const [index, exLST] of exLSTs.entries()) {
53-
if (!exLST || exLST === ADDRESSES.null) continue
54-
5527
const phase = phases[index]
5628
const manager = managers[index]
5729

5830
const supply = await api.call({ target: exLST, abi: 'erc20:totalSupply' })
59-
if (!supply || supply === '0') continue
60-
61-
const allowedAssets = await api.call({
62-
target: ROUTER,
63-
params: [phase],
64-
abi: abis.allowedTokensInOnDeposit,
65-
permitFailure: true
66-
})
31+
const allowedAssets = await api.call({ target: router, params: phase, abi: abis.allowedTokensInOnDeposit, permitFailure: true })
6732

6833
if (!allowedAssets?.length) continue
6934

7035
const assetOutputs = await api.multiCall({
7136
abi: abis.exLstToTokenOut,
7237
permitFailure: true,
73-
calls: allowedAssets.map((asset) => ({
74-
target: ROUTER,
75-
params: [manager, asset, supply]
76-
})),
38+
calls: allowedAssets.map((asset) => ({ target: router, params: [manager, asset, supply] })),
7739
})
7840

79-
for (let i = 0; i < allowedAssets.length; i++) {
80-
const asset = allowedAssets[i]
41+
allowedAssets.forEach((asset, i) => {
8142
const amount = assetOutputs[i]
43+
if (!amount) return
44+
api.add(asset, amount)
45+
})
8246

83-
if (!amount || amount === '0') continue
84-
85-
if (asset.toLowerCase() === WHYPE.toLowerCase() ||
86-
asset.toLowerCase() === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
87-
api.add(WHYPE, amount)
88-
break
89-
}
90-
}
91-
92-
try {
93-
const [nextWithdrawalId, withdrawalDelay] = await Promise.all([
94-
api.call({ target: KHYPE_MANAGER, params: [manager], abi: abis.nextWithdrawalId }),
95-
api.call({ target: KHYPE_MANAGER, abi: abis.withdrawalDelay })
96-
])
47+
const [nextWithdrawalId, withdrawalDelay] = await Promise.all([
48+
api.call({ target: kHypeManager, params: [manager], abi: abis.nextWithdrawalId }),
49+
api.call({ target: kHypeManager, abi: abis.withdrawalDelay })
50+
])
9751

98-
if (nextWithdrawalId && Number(nextWithdrawalId) > 0) {
99-
const withdrawalIds = [...Array(Math.min(Number(nextWithdrawalId), 100)).keys()]
52+
const withdrawalIds = [...Array(Number(nextWithdrawalId)).keys()]
10053

101-
const withdrawalRequests = await api.multiCall({
102-
target: KHYPE_MANAGER,
103-
abi: abis.withdrawalRequests,
104-
calls: withdrawalIds.map((id) => ({ params: [manager, id] })),
105-
permitFailure: true,
106-
})
54+
const withdrawalRequests = await api.multiCall({
55+
target: kHypeManager,
56+
abi: abis.withdrawalRequests,
57+
calls: withdrawalIds.map((id) => ({ params: [manager, id] })),
58+
permitFailure: true,
59+
})
10760

108-
const now = api.timestamp || Math.floor(Date.now() / 1000)
61+
if (!withdrawalRequests.length) continue
62+
const now = Math.floor(Date.now() / 1000)
10963

110-
withdrawalRequests.forEach((req) => {
111-
if (!req) return
112-
const { hypeAmount, timestamp } = req
113-
if (now >= Number(timestamp) + Number(withdrawalDelay)) {
114-
api.add(WHYPE, -BigInt(hypeAmount))
115-
}
116-
})
117-
}
118-
} catch (e) {
119-
// Continue without withdrawal adjustment
120-
}
64+
withdrawalRequests.forEach(({ hypeAmount, timestamp }) => {
65+
now < Number(timestamp) + Number(withdrawalDelay) ? api.add(HYPE, -hypeAmount) : api.add(HYPE, -0)
66+
})
12167
}
12268
}
12369

12470
module.exports = {
125-
doublecounted: true,
126-
timetravel: false,
127-
methodology: 'TVL is calculated from the total exLST supply converted to underlying HYPE value via router functions.',
12871
hyperliquid: { tvl }
129-
}
72+
}

0 commit comments

Comments
 (0)