Skip to content

Commit 6987712

Browse files
committed
fix(openclaw): mainnet token balances and Nakamoto block cadence
getTokenBalance hardcoded the ::mock-sbtc asset key, so on mainnet it always returned 0 for real tokens. Match the balance entry by contract-id prefix so any SIP-010 asset name resolves. Verified against a live USDA holder. Also correct the pre-Nakamoto block constants (BLOCKS_PER_DAY 144 -> 17_280, BLOCKS_PER_MONTH 4320 -> 518_400) and blocksToTimeString (10min -> 5s/block) to match the frontend and current chain cadence.
1 parent 6b9d71d commit 6987712

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

openclaw-service/src/config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export const STREAM_STATUS = {
2727
DEPLETED: 3,
2828
} as const;
2929

30-
export const BLOCKS_PER_DAY = 144;
31-
export const BLOCKS_PER_MONTH = 4320;
30+
// Nakamoto (Epoch 3.0+) cadence: Stacks blocks advance ~every 5s, so a day is
31+
// 17_280 blocks, not the pre-Nakamoto 144. Kept in sync with the frontend.
32+
export const BLOCK_TIME_SECONDS = 5;
33+
export const BLOCKS_PER_DAY = 17_280;
34+
export const BLOCKS_PER_MONTH = 518_400;
3235
export const MAX_STREAMS_PER_USER = 100;

openclaw-service/src/stacks-client.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,14 @@ export async function getTokenBalance(
247247
);
248248
const data: any = await res.json();
249249
const ftBalances = data.fungible_tokens || {};
250-
const key = `${tokenContract}::mock-sbtc`;
251-
return BigInt(ftBalances[key]?.balance ?? "0");
250+
// Balances are keyed by `<contractId>::<asset-name>`, and the asset name
251+
// varies per token (sbtc, usda, wrapped-bitcoin, ...). Match by contract
252+
// prefix so this works for any SIP-010 token, not just the testnet mock.
253+
const entry = Object.entries(ftBalances).find(([k]) =>
254+
k.startsWith(`${tokenContract}::`)
255+
);
256+
const balance = (entry?.[1] as { balance?: string } | undefined)?.balance;
257+
return BigInt(balance ?? "0");
252258
}
253259

254260
// ============================================================================

openclaw-service/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export function getStreamStatusLabel(status: number): string {
3030
}
3131
}
3232

33-
/** Estimate time remaining from blocks (Stacks ~10 min/block) */
33+
/** Estimate time remaining from blocks (Nakamoto ~5s/block) */
3434
export function blocksToTimeString(blocks: number): string {
35-
const minutes = blocks * 10;
35+
const minutes = Math.round((blocks * 5) / 60);
3636
if (minutes < 60) return `${minutes}m`;
3737
const hours = Math.floor(minutes / 60);
3838
if (hours < 24) return `${hours}h ${minutes % 60}m`;

0 commit comments

Comments
 (0)