Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/zone-id-other-source-chains.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ox": patch
---

Fixed `ZoneId.fromChainId` for zones of Tempo chains other than mainnet and Moderato, whose chain IDs carry the source chain ID in the high 32 bits and the zone ID in the low 32 bits.
11 changes: 11 additions & 0 deletions src/tempo/ZoneId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ describe('fromChainId', () => {
)
})

test('other source chains', () => {
// Source chain 31318, zone 1: 31318 * 2^32 + 1.
expect(ZoneId.fromChainId(134_509_785_776_129)).toMatchInlineSnapshot(`1`)
expect(
ZoneId.fromChainId(134_509_785_776_129, 42_431),
).toMatchInlineSnapshot(`1`)
expect(ZoneId.fromChainId(2 ** 32 + 4_294_967_295)).toMatchInlineSnapshot(
`4294967295`,
)
})

test('unsupported source id', () => {
expect(() =>
// @ts-expect-error Unsupported source ID.
Expand Down
8 changes: 7 additions & 1 deletion src/tempo/ZoneId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const chainIdConfig = {

const defaultSourceId = 4_217

/** Chain IDs at or above this value carry their source chain ID in the high 32 bits. */
const genericChainIdBase = 2 ** 32

/**
* Base offset for deriving Presto zone chain IDs.
*/
Expand All @@ -24,7 +27,9 @@ export type SourceId = keyof typeof chainIdConfig
/**
* Derives a zone ID from a zone chain ID.
*
* Zone chain IDs use the base assigned to their Tempo source chain.
* Zone chain IDs use the base assigned to their Tempo source chain. Zones of other Tempo chains
* (such as devnets) encode the source chain ID in the high 32 bits and the zone ID in the low 32
* bits, so the zone ID is read from the chain ID alone.
*
* @example
* ```ts twoslash
Expand All @@ -42,6 +47,7 @@ export function fromChainId(
chainId: number,
sourceId: SourceId = defaultSourceId,
): number {
if (chainId >= genericChainIdBase) return chainId % genericChainIdBase
return chainId - getChainIdConfig(sourceId).base
}

Expand Down