Storing crypto balances as float almost cost me 3 ETH

marcus1987

New Member
Messages
66
Reaction score
0
Location
Sydney, NSW

Spent most of last night debugging a payout discrepancy on a site I help run and it came down to the dumbest thing possible. My balances column was DOUBLE PRECISION. Set it up two years ago in a hurry because "it's just a number" and I never touched it again.

Yeah. 0.1 + 0.2 does not equal 0.3, and when you're counting in wei you're not even in the same zip code. The whole thing surfaced because a guy withdrew 0.05 ETH and his balance didn't zero out. Showed like 0.0000000000000002 left sitting there. He screenshotted it, dropped it in our TG, and within an hour everyone's convinced we're skimming pennies. Six hours of my life to prove it was floating point and not theft.

Moved everything to NUMERIC(38,18) and I'm never going back. Same weekend I also found a nonce counter stored as a plain INT that wrapped negative after a busy stretch, which was a great surprise at 3am when withdrawals started failing silently.

And before anyone says just use a regulated processor, I've been down that road, they'll hold your funds for a week and charge you for the privilege. Hard pass.

If you're storing wei or satoshis anywhere, use numeric or bigint from day one. Anyone else get wrecked by this early on? Curious how many of you caught it before a user did.

da_real_mvp

New Member
Messages
69
Reaction score
0
Location
Atlanta, GA

ok the float thing, yeah, we all learn that one the hard way. mine was a satoshi ledger in MySQL FLOAT back in 2017, same deal, someone's withdrawal left dust behind and the Discord turned into a witch hunt within the hour. so I feel you on the six hours of proving you're not a thief.

but two things in your post are bugging me.

first, bigint for wei is a trap. bigint tops out around 9.22e18, which is like 9.2 ETH. that's it. one whale deposit and you're dead. if you're actually storing wei you want NUMERIC(78,0) since 2^256 is about 1.15e77, or you're back to splitting across columns. and honestly NUMERIC(38,18) is weird for wei too, wei has no fractional part! scale 18 on an indivisible unit is just inviting someone to accidentally write 0.5 wei somewhere and now you've got the same rounding ghost you just killed, except now it's YOUR code doing it. I'd go scale 0 and only format to ETH at the display layer.

second, your nonce INT wrapping negative after "a busy stretch"? that's 2.1 billion increments. in one stretch?? either you had a runaway loop hammering it, or something was resetting the counter, or it wasn't actually an int wrap at all. what did the root cause turn out to be, because that part reads like a totally different bug wearing an int costume.

emilyb85

New Member
Messages
67
Reaction score
0
Location
Amsterdam, NL

NUMERIC(78,0) is great advice right up until you try it in MySQL and the engine refuses to create the column. DECIMAL tops out at 65 digits of precision there. Not 78. It's a hard error, not a warning. So if you're the MySQL guy from your 2017 story, you're stuck at DECIMAL(65,0), which is roughly 1e65 wei, still short of 2^256 but close enough that nobody alive is depositing that much. Works fine in practice. Just don't sell it as the clean 78 when half the people reading this are on MySQL and will file a bug report against you personally.

Agreed on the bigint cap though, 9.2 ETH is nothing. One whale and the column just falls over.

And yeah, your nonce read is the right one. 2.1 billion increments is not a busy stretch, that's a runaway. My money's on a retry loop bumping the counter without ever broadcasting, chewing through the range overnight while everyone was asleep. INT wasn't the bug. The loop was the bug, and INT was just the smoke alarm going off in the worst possible way. I've seen that exact pattern with an order ID sequence that someone wired to a failed-payment handler by accident.

There's a whole backend section on here if you want to keep the MySQL vs Postgres fight going, it's basically a permanent argument at this point

https://websiteforums.info/forums/php-and-backend-development.6/

Still want to know what the actual increment rate turned out to be. Did you ever pull the logs?

jen_93

Member
Messages
62
Reaction score
0
Location
Cape Town, ZA

emilyb85's engine ceiling point is the one that eventually bit me, except mine was a ceiling of zero. SQLite. There is no DECIMAL type. Not 65 digits, not 78, nothing. You can declare NUMERIC(38,18) until your fingers fall off and SQLite just shrugs, assigns NUMERIC affinity, and stores the thing as an 8 byte integer if it fits, or silently promotes it to REAL if it doesn't. Which means a big enough wei balance comes back as a float. Not an error, not a warning. The exact bug OP spent six hours explaining to an angry Telegram, except now it's invisible until someone reconciles a ledger three months later. I found it because a customer's balance read 1.0000000000000002e+19. That "e+19" gave me a physical reaction. Moved that column to TEXT and did all the math in the app layer with a bigint library.

The other thing nobody's flagged yet is the boundary, not the storage. Even with NUMERIC(78,0) dialled in perfectly, the number spends a few microseconds as a JavaScript Number before it reaches your column, because the JSON-RPC hands you a hex string and somebody somewhere calls parseInt or a bare Number() on it. Ethers v5 will throw on unsafe values, which is the correct behaviour, but plenty of hand-rolled clients won't. So you get the float rounding back, upstream of your beautiful schema, and the column gets the damage report.

+1 to da_real_mvp on the scale 0 thing though, scale 18 on wei is asking for trouble. And I still want to know what the nonce root cause actually was. The retry loop theory sounds right but I've also seen a Redis counter that had no TTL get restored from an old snapshot, which would produce the same "busy stretch" symptom. Did you ever confirm it?

tommy2k

New Member
Messages
69
Reaction score
0
Location
Mexico City, MX

da_real_mvp the scale 0 thing reads clean until you touch anything that isn't a plain transfer. Fee splits, interest accrual, gas rebates, partial refunds. Now you're doing integer division in app code and deciding where the leftover wei goes, and someone on the team WILL round it the wrong direction at 2am. I'd rather keep the fractional column and be loud about the rule that nothing downstream gets to divide it.

jen_93 got closest to the actual killer though. The storage engine was never the real problem. The JSON boundary is. We ran a payout service for over a year with a perfectly fine numeric column and still leaked money because the response serializer called Number() on the hex before it ever reached our own API. Balances were pristine in psql and garbage in the frontend. Everyone kept staring at the column while the bug was two layers up.

Genuine question for whoever's solved this properly, how are you getting bigints through JSON without a custom replacer on every response? We ended up stringifying at the boundary and it works but it's ugly and half the team forgets and does a parseInt anyway.

You must log in or register to reply here.