fix: parse slot0 response as array — viem v2 returns tuples, not named objects (#2)

viem v2 readContract returns multi-output functions as plain arrays.
slot0() tick is at index [1], not .tick property. Handles both array
and record shapes for forward compatibility.
This commit is contained in:
johba 2026-04-05 16:14:50 +00:00
parent 2f25febfe6
commit 0eb1816c22

View file

@ -555,12 +555,14 @@ async function loadLiquidityStats({ notify = false }: { notify?: boolean } = {})
functionName: 'slot0',
});
let currentTick: number | null = null;
if (isRecord(slot0Response) && typeof slot0Response.tick === 'number') {
// viem v2 returns multi-output calls as arrays, not named objects.
// slot0() returns [sqrtPriceX96, tick, observationIndex, ...] tick is at index 1.
let currentTick: number;
if (Array.isArray(slot0Response) && slot0Response.length >= 2) {
currentTick = Number(slot0Response[1]);
} else if (isRecord(slot0Response) && typeof slot0Response.tick === 'number') {
currentTick = slot0Response.tick;
}
if (currentTick === null) {
} else {
throw new Error('Unable to determine current pool tick');
}