Quantity
A JSON-RPC quantity: a non-negative integer sent as hex with no leading zeros, such as "0x0"
or "0x1b4". The module has two schemas: one decodes to bigint, the other to number.
import { Effect, Schema } from "effect";import { Quantity } from "@ethect/core";
const program = Effect.gen(function* () { const wei = yield* Schema.decodeUnknownEffect(Quantity.schema)("0xde0b6b3a7640000"); const chainId = yield* Schema.decodeUnknownEffect(Quantity.numberSchema)("0x7a69"); return { wei, chainId }; // { wei: 1000000000000000000n, chainId: 31337 }});| Export | Kind | Decodes to | Used for |
|---|---|---|---|
schema |
schema | bigint |
amounts that can outgrow a double: wei, gas, block numbers, timestamps |
numberSchema |
schema | number |
small counters and indexes: chain ids, nonces, positions in a block |
You rarely call them yourself: every method decodes its quantities with one of the two. See values on the wire.
schema
Section titled “schema”A quantity as a bigint.
import { Effect, Schema } from "effect";import { Quantity } from "@ethect/core";
const program = Effect.gen(function* () { const value = yield* Schema.decodeUnknownEffect(Quantity.schema)("0x1b4"); // 436n const wire = yield* Schema.encodeEffect(Quantity.schema)(value); // "0x1b4" return { value, wire };});- Type:
bigint. Encoded: a hexstring.
What it decodes:
| Wire value | Result |
|---|---|
"0x0" |
0n |
"0x1b4" |
436n |
"0x0001" |
1n: leading zeros are not canonical, but nodes send them |
"0x", "1", "0xg", "-0x1" |
rejected: not a hex quantity |
1, null |
rejected: not a string |
What it encodes:
| Value | Result |
|---|---|
0n |
"0x0" |
436n |
"0x1b4" |
2n ** 256n - 1n |
"0x" followed by 64 f |
-1n |
rejected: a quantity is not negative |
numberSchema
Section titled “numberSchema”A quantity as a number, for values that always fit a safe integer.
import { Effect, Schema } from "effect";import { Quantity } from "@ethect/core";
const program = Effect.gen(function* () { const decode = Schema.decodeUnknownEffect(Quantity.numberSchema); const id = yield* decode("0x7a69"); // 31337 const wire = yield* Schema.encodeEffect(Quantity.numberSchema)(id); // "0x7a69" return { id, wire };});- Type:
number. Encoded: a hexstring.
It accepts the same wire values as schema, up to Number.MAX_SAFE_INTEGER.
| Value | Result |
|---|---|
decoding "0x7a69" |
31337 |
decoding "0x1fffffffffffff" |
9007199254740991, the largest safe integer |
decoding "0x20000000000000" |
rejected: Expected a value between 0 and 9007199254740991 |
encoding 31337 |
"0x7a69" |
encoding 1.5 |
rejected: not an integer |
encoding 2 ** 53 |
rejected: above the largest safe integer |
encoding -1 |
rejected: a quantity is not negative |
Behavior
Section titled “Behavior”- The shortest form is written. Encoding never writes a leading zero:
0nis"0x0". - Leading zeros are read. Decoding accepts
"0x0001", and both letter cases. "0x"is zero. A quantity written with no digit at all decodes to0n, or0.- Rejected, not rounded. A value that cannot be a quantity fails instead of being written. Nothing is truncated or clamped.
- A failure is a value. Used directly, a schema fails with a
SchemaError. Inside a method, the failure is aCodecError:on: "params"when a param cannot be encoded, and then nothing is sent, oron: "result"when the node’s answer cannot be decoded.
A param that is not a quantity never reaches the node.
import { Effect } from "effect";import { Wallet } from "@ethect/core/rpc";
// a chain id must be an integerconst switching = Wallet.switchEthereumChain({ chainId: 1.5 }).pipe( Effect.catchTag("CodecError", (error) => Effect.succeed(error.on)), // "params");Use the schemas in your own methods.
import { Quantity } from "@ethect/core";import { Method } from "@ethect/core/rpc";
// a block number can outgrow a double: bigintconst blockNumber = Method.make()("eth_blockNumber", [], Quantity.schema);// a peer count cannot: numberconst peerCount = Method.make()("net_peerCount", [], Quantity.numberSchema);