Todas as lições · Apps and UI · Começa aqui · Glossário
Exact CKB-to-shannon conversion with BigIntPráticoDe uma correção
De uma correção · Verificação: em parte certa · Verificada em 2026-11-19
Convert user-entered CKB amounts to shannons with BigInt string arithmetic: BigInt(whole) * 100000000n + BigInt(fraction.padEnd(8, '0')). Never use parseFloat(amount) * 1e8. First validate the input with /^\d+(\.\d{1,8})?$/, which rejects exponents, hex and commas, and reject any result above u64::MAX (0xffffffffffffffffn).
Porque importa: Float or Number arithmetic loses shannon precision for amounts past the safe integer range, such as 99999999999.99999999 CKB, and loose parsing accepts inputs like '1e3' or '0x100' that become the wrong capacity.
Padrão
Wrong: Math.round(parseFloat(amount) * 1e8). Preferred: BigInt(whole) * 100000000n + BigInt(fraction.padEnd(8, '0')), after checking /^\d+(\.\d{1,8})?$/ and an upper bound of 0xffffffffffffffffn.Um teste mudou no mesmo commit
Está ao lado da correcção. Nós não o corremos.
- Hanssen0-Alter-Ego/ckb-vm-js:
tests/browser/playground.spec.js,tests/js/guided.test.js
- Conceitos
- Data encoding (molecule, hex, bigints), Capacity and cell size
- Evidência
- De uma correção: de um projeto, verificada contra o commit
- Vista pela primeira vez
- 2026-11-15
- Última mudança
- 2026-11-15
- Última verificação
- 2026-11-19
- Verificação contra o commit
- em parte certa: um núcleo verdadeiro, com uma parte solta ou demasiado larga
Fontes (1)
- 2026-11-15 · Hanssen0-Alter-Ego/ckb-vm-js · mais deste projeto
Mais sobre Apps and UI
- Occupied capacity depends on the actual lock's args length
- Pinning JS codecs and derivations to Rust test vectors
- Pre-sign checkers must mirror every on-chain comparison
Manténs um projeto de onde esta lição veio? Confirma ou corrige
Citar esta lição
LusoCryptoLabs, "Exact CKB-to-shannon conversion with BigInt", CKB lessons, https://pulse.lusocryptolabs.com/learned/apps/exact-ckb-to-shannon-conversion-with-bigint, CC BY 4.0, last changed 2026-11-15.