(Note · · 6 min read)

Adding a second chain doubled the bugs

The first chain works. Supporting a second looks like copying the payment module and changing a few names, and that is how a codebase ends up with two implementations of the same business rule that slowly stop agreeing with each other.

By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka

Separate what differs from what does not

The business logic — an order is paid, a code is allocated, a receipt is recorded — is identical on every chain. What differs is addresses, units, libraries, confirmation rules and how a transfer is read from a transaction.

Put the differences behind one narrow interface: validate an address, watch for incoming transfers, fetch a transaction's transfers, report confirmations. Everything else stays chain-agnostic and is written once.

A gift card shop I built accepts the same token on two networks — one EVM chain and one non-EVM — through browser wallets or a hosted checkout. Two very different libraries, one set of rules about when an order is paid.

Addresses are not interchangeable

Formats differ in length, alphabet, checksum and case sensitivity. An address valid on one chain can be meaningless or, worse, superficially plausible on another.

Validate per chain, store the chain alongside every address, and never let a user-supplied chain identifier decide which validator runs without checking it against the ones you support. Paying to a correctly formatted address on the wrong network is an unrecoverable loss, so confirm the network in the interface before the transfer, not after.

Decimals belong to the token, not the chain

The same stablecoin can have different decimal precision on different networks. Hardcoding a conversion that happens to be right on your first chain is a silent factor-of-a-thousand error on the second.

Read the decimals from the token's own configuration, store amounts as integers in the token's base unit, and convert only for display. Never convert through a floating point number on the way.

Confirmation rules are per chain

Block times, reorganisation behaviour and finality guarantees differ, so the number of confirmations you wait for has to be configured per network rather than shared. One value applied to both chains is simultaneously too slow for one and too risky for the other.

Make it configuration, not a constant in the payment code. The number will change, and when it does it should change in one place with a comment saying why.

Test against a real network before mainnet

Each chain has test networks. Use them for the full path — wallet connection, transfer, confirmation watching, allocation, delivery — because the failures that matter are integration failures that no unit test with a mocked client will surface.

Keep a chain registry listing, per network, the contract address, decimals, confirmations, explorer and endpoints, so adding a third network is a configuration entry plus one adapter rather than another copy of the module.

Not everyone has a wallet

Supporting browser wallets covers people who already hold crypto. A hosted checkout covering everyone else is a different integration with a different failure model — it notifies you by webhook, which brings signature verification, duplicate delivery and out-of-order events.

Route both into the same order state machine. Two payment paths are acceptable; two definitions of what it means for an order to be paid are not.

Common questions

How do I add a second blockchain without duplicating code?

Separate the chain-specific parts — address validation, watching for transfers, reading a transaction's transfers, confirmation counts — behind one narrow interface, and keep the business logic chain-agnostic and written once.

Why can't I reuse address validation across chains?

Formats differ in length, alphabet, checksum and case sensitivity, so an address valid on one chain may be meaningless or superficially plausible on another. Validate per chain, store the chain with every address, and confirm the network before the transfer.

Do token decimals differ between networks?

Yes — the same stablecoin can have different precision on different chains, so a hardcoded conversion correct on your first chain is a silent factor-of-a-thousand error on the second. Read decimals from the token, store integers in base units, and never convert through a float.

Should confirmation counts be shared between chains?

No. Block times, reorganisation behaviour and finality differ, so one value is simultaneously too slow for one chain and too risky for the other. Make it per-network configuration rather than a constant in the payment code.

Need something like this built?