@effect-blockchain/evm 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +79 -1
  2. package/dist/EvmFeeHistory.d.ts +26 -0
  3. package/dist/EvmFeeHistory.d.ts.map +1 -0
  4. package/dist/EvmFeeHistory.js +25 -0
  5. package/dist/EvmFeeHistory.js.map +1 -0
  6. package/dist/EvmIndexer.d.ts +47 -9
  7. package/dist/EvmIndexer.d.ts.map +1 -1
  8. package/dist/EvmIndexer.js +95 -36
  9. package/dist/EvmIndexer.js.map +1 -1
  10. package/dist/EvmNetwork.d.ts.map +1 -1
  11. package/dist/EvmNetwork.js +8 -6
  12. package/dist/EvmNetwork.js.map +1 -1
  13. package/dist/EvmRelayer.d.ts +256 -0
  14. package/dist/EvmRelayer.d.ts.map +1 -0
  15. package/dist/EvmRelayer.js +330 -0
  16. package/dist/EvmRelayer.js.map +1 -0
  17. package/dist/EvmRpc.d.ts +88 -8
  18. package/dist/EvmRpc.d.ts.map +1 -1
  19. package/dist/EvmRpc.js +30 -9
  20. package/dist/EvmRpc.js.map +1 -1
  21. package/dist/EvmSigner.d.ts +78 -0
  22. package/dist/EvmSigner.d.ts.map +1 -0
  23. package/dist/EvmSigner.js +96 -0
  24. package/dist/EvmSigner.js.map +1 -0
  25. package/dist/EvmTransaction.d.ts +65 -0
  26. package/dist/EvmTransaction.d.ts.map +1 -0
  27. package/dist/EvmTransaction.js +60 -0
  28. package/dist/EvmTransaction.js.map +1 -0
  29. package/dist/index.d.ts +16 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +16 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/internal/rpcMethods.js +57 -5
  34. package/dist/internal/rpcMethods.js.map +1 -1
  35. package/package.json +2 -2
  36. package/src/EvmFeeHistory.ts +28 -0
  37. package/src/EvmIndexer.ts +142 -49
  38. package/src/EvmNetwork.ts +8 -6
  39. package/src/EvmRelayer.ts +425 -0
  40. package/src/EvmRpc.ts +106 -9
  41. package/src/EvmSigner.ts +128 -0
  42. package/src/EvmTransaction.ts +64 -0
  43. package/src/index.ts +20 -0
  44. package/src/internal/rpcMethods.ts +67 -6
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Signers of EIP-1559 transactions: a private key held in memory, or a custody API that only signs a hash. The
3
+ * reason a signer receives the whole transaction is described in
4
+ * [Concepts](https://github.com/spiko-tech/effect-blockchain/blob/main/docs/concepts.md#signers).
5
+ *
6
+ * @since 0.3.0
7
+ */
8
+ import * as Effect from "effect/Effect"
9
+ import * as Redacted from "effect/Redacted"
10
+ import * as Schema from "effect/Schema"
11
+ import { isHex, keccak256, parseSignature, recoverTransactionAddress, serializeTransaction } from "viem"
12
+ import { privateKeyToAccount } from "viem/accounts"
13
+ import { EvmAddress } from "./EvmAddress.ts"
14
+ import { EvmHexData } from "./EvmHexData.ts"
15
+ import type { EvmChainId } from "./EvmNetwork.ts"
16
+
17
+ /**
18
+ * A signature could not be produced: `cause` is the signer's own failure, or why its signature was refused. It never
19
+ * holds a private key.
20
+ *
21
+ * @since 0.3.0
22
+ * @category errors
23
+ */
24
+ export class EvmSignerError extends Schema.TaggedError<EvmSignerError>()("EvmSignerError", {
25
+ cause: Schema.Defect()
26
+ }) {}
27
+
28
+ /**
29
+ * An EIP-1559 transaction before its signature. `value` (default 0), `maxFeePerGas` and `maxPriorityFeePerGas` are in
30
+ * wei, `data` defaults to `0x`.
31
+ *
32
+ * @since 0.3.0
33
+ * @category models
34
+ */
35
+ export interface EvmUnsignedTransaction {
36
+ readonly chainId: EvmChainId
37
+ readonly nonce: bigint
38
+ readonly to: EvmAddress
39
+ readonly value?: bigint | undefined
40
+ readonly data?: EvmHexData | undefined
41
+ readonly gas: bigint
42
+ readonly maxFeePerGas: bigint
43
+ readonly maxPriorityFeePerGas: bigint
44
+ }
45
+
46
+ /**
47
+ * Signs transactions sent from `address`. `signTransaction` returns the signed transaction, serialized as
48
+ * `eth_sendRawTransaction` takes it.
49
+ *
50
+ * @since 0.3.0
51
+ * @category models
52
+ */
53
+ export interface EvmSigner {
54
+ readonly address: EvmAddress
55
+ readonly signTransaction: (transaction: EvmUnsignedTransaction) => Effect.Effect<EvmHexData, EvmSignerError>
56
+ }
57
+
58
+ const toError = (cause: unknown) => new EvmSignerError({ cause })
59
+
60
+ const serializable = (transaction: EvmUnsignedTransaction) => ({
61
+ type: "eip1559" as const,
62
+ chainId: transaction.chainId,
63
+ nonce: Number(transaction.nonce),
64
+ to: transaction.to,
65
+ value: transaction.value ?? BigInt(0),
66
+ data: transaction.data ?? ("0x" as const),
67
+ gas: transaction.gas,
68
+ maxFeePerGas: transaction.maxFeePerGas,
69
+ maxPriorityFeePerGas: transaction.maxPriorityFeePerGas
70
+ })
71
+
72
+ /**
73
+ * A signer holding a secp256k1 private key (32 bytes, 0x-prefixed hex) in memory. An invalid key fails with an
74
+ * {@link EvmSignerError} that does not repeat it.
75
+ *
76
+ * @since 0.3.0
77
+ * @category constructors
78
+ */
79
+ export const fromPrivateKey = (options: {
80
+ readonly key: Redacted.Redacted<string>
81
+ }): Effect.Effect<EvmSigner, EvmSignerError> =>
82
+ Effect.try({
83
+ try: () => {
84
+ const value = Redacted.value(options.key)
85
+ if (!isHex(value)) throw new Error("not hex")
86
+ return privateKeyToAccount(value)
87
+ },
88
+ // the thrown error can quote the key
89
+ catch: () => new EvmSignerError({ cause: new Error("invalid private key") })
90
+ }).pipe(Effect.map((account) => ({
91
+ address: Schema.decodeSync(EvmAddress)(account.address),
92
+ signTransaction: (transaction) =>
93
+ Effect.tryPromise({ try: () => account.signTransaction(serializable(transaction)), catch: toError }).pipe(
94
+ Effect.map((signed) => EvmHexData.make(signed))
95
+ )
96
+ })))
97
+
98
+ /**
99
+ * A signer for a key held elsewhere (a custody API, an HSM) that only signs a 32-byte hash. `signHash` receives the
100
+ * keccak256 of the unsigned EIP-1559 serialization and returns the 65-byte signature `r || s || v`, `v` being 0, 1, 27
101
+ * or 28. A failure of `signHash`, a malformed signature or one that does not recover to `address` fails with an
102
+ * {@link EvmSignerError}.
103
+ *
104
+ * @since 0.3.0
105
+ * @category constructors
106
+ */
107
+ export const fromHashSigner = <E>(options: {
108
+ readonly address: EvmAddress
109
+ readonly signHash: (hash: EvmHexData) => Effect.Effect<EvmHexData, E>
110
+ }): EvmSigner => ({
111
+ address: options.address,
112
+ signTransaction: Effect.fnUntraced(function*(transaction) {
113
+ const unsigned = serializable(transaction)
114
+ const hash = yield* Effect.try({ try: () => keccak256(serializeTransaction(unsigned)), catch: toError })
115
+ const signature = yield* Effect.mapError(options.signHash(EvmHexData.make(hash)), toError)
116
+ return yield* Effect.tryPromise({
117
+ try: async () => {
118
+ const signed = serializeTransaction(unsigned, parseSignature(signature))
119
+ const signer = await recoverTransactionAddress({ serializedTransaction: signed })
120
+ if (signer.toLowerCase() !== options.address) {
121
+ throw new Error(`signed by ${signer}, expected ${options.address}`)
122
+ }
123
+ return EvmHexData.make(signed)
124
+ },
125
+ catch: toError
126
+ })
127
+ })
128
+ })
@@ -0,0 +1,64 @@
1
+ /**
2
+ * EVM transactions and their receipts, as returned by `eth_getTransactionByHash` and `eth_getTransactionReceipt`.
3
+ *
4
+ * @since 0.3.0
5
+ */
6
+ import * as Schema from "effect/Schema"
7
+ import { EvmAddress } from "./EvmAddress.ts"
8
+ import { EvmHexData } from "./EvmHexData.ts"
9
+ import { EvmLog } from "./EvmLog.ts"
10
+ import { EvmTransactionHash } from "./EvmTransactionHash.ts"
11
+ import { IntFromHex, QuantityFromHex } from "./internal/QuantityFromHex.ts"
12
+
13
+ /**
14
+ * A transaction known to a node, decoded from the raw JSON-RPC transaction. `blockNumber`, `blockHash` and
15
+ * `transactionIndex` are `null` while it is pending, `to` is `null` for a contract creation. Every other field (the
16
+ * fee fields, which depend on the transaction type, and the signature) is ignored.
17
+ *
18
+ * @since 0.3.0
19
+ * @category schemas
20
+ */
21
+ export const EvmTransaction = Schema.Struct({
22
+ hash: EvmTransactionHash,
23
+ from: EvmAddress,
24
+ to: Schema.NullOr(EvmAddress),
25
+ nonce: QuantityFromHex,
26
+ value: QuantityFromHex,
27
+ input: EvmHexData,
28
+ gas: QuantityFromHex,
29
+ blockNumber: Schema.NullOr(QuantityFromHex),
30
+ blockHash: Schema.NullOr(EvmHexData),
31
+ transactionIndex: Schema.NullOr(IntFromHex)
32
+ }).annotate({ identifier: "EvmTransaction", title: "EVM transaction" })
33
+
34
+ /**
35
+ * @since 0.3.0
36
+ * @category models
37
+ */
38
+ export type EvmTransaction = typeof EvmTransaction.Type
39
+
40
+ /**
41
+ * The receipt of a transaction included in a block. `status` is 1 for a success and 0 for a revert, and
42
+ * `effectiveGasPrice` is in wei per gas. A receipt older than the Byzantium fork has no `status` and does not decode.
43
+ *
44
+ * @since 0.3.0
45
+ * @category schemas
46
+ */
47
+ export const EvmTransactionReceipt = Schema.Struct({
48
+ transactionHash: EvmTransactionHash,
49
+ transactionIndex: IntFromHex,
50
+ blockNumber: QuantityFromHex,
51
+ blockHash: EvmHexData,
52
+ from: EvmAddress,
53
+ to: Schema.NullOr(EvmAddress),
54
+ status: IntFromHex.check(Schema.isBetween({ minimum: 0, maximum: 1 })),
55
+ gasUsed: QuantityFromHex,
56
+ effectiveGasPrice: QuantityFromHex,
57
+ logs: Schema.Array(EvmLog)
58
+ }).annotate({ identifier: "EvmTransactionReceipt", title: "EVM transaction receipt" })
59
+
60
+ /**
61
+ * @since 0.3.0
62
+ * @category models
63
+ */
64
+ export type EvmTransactionReceipt = typeof EvmTransactionReceipt.Type
package/src/index.ts CHANGED
@@ -14,6 +14,11 @@ export * as EvmAddress from "./EvmAddress.ts"
14
14
  */
15
15
  export * as EvmBlock from "./EvmBlock.ts"
16
16
 
17
+ /**
18
+ * @since 0.3.0
19
+ */
20
+ export * as EvmFeeHistory from "./EvmFeeHistory.ts"
21
+
17
22
  /**
18
23
  * @since 0.1.0
19
24
  */
@@ -34,11 +39,26 @@ export * as EvmLog from "./EvmLog.ts"
34
39
  */
35
40
  export * as EvmNetwork from "./EvmNetwork.ts"
36
41
 
42
+ /**
43
+ * @since 0.3.0
44
+ */
45
+ export * as EvmRelayer from "./EvmRelayer.ts"
46
+
37
47
  /**
38
48
  * @since 0.1.0
39
49
  */
40
50
  export * as EvmRpc from "./EvmRpc.ts"
41
51
 
52
+ /**
53
+ * @since 0.3.0
54
+ */
55
+ export * as EvmSigner from "./EvmSigner.ts"
56
+
57
+ /**
58
+ * @since 0.3.0
59
+ */
60
+ export * as EvmTransaction from "./EvmTransaction.ts"
61
+
42
62
  /**
43
63
  * @since 0.1.0
44
64
  */
@@ -2,24 +2,27 @@ import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
2
2
  import * as Schema from "effect/Schema"
3
3
  import { EvmAddress } from "../EvmAddress.ts"
4
4
  import { EvmBlock } from "../EvmBlock.ts"
5
+ import { EvmFeeHistory } from "../EvmFeeHistory.ts"
5
6
  import { EvmHexData } from "../EvmHexData.ts"
6
7
  import { EvmLog } from "../EvmLog.ts"
8
+ import { EvmTransaction, EvmTransactionReceipt } from "../EvmTransaction.ts"
9
+ import { EvmTransactionHash } from "../EvmTransactionHash.ts"
7
10
  import { IntFromHex, QuantityFromHex } from "./QuantityFromHex.ts"
8
11
 
9
- const make = <Params, Result>(method: JsonRpc.Method<Params, Result>): JsonRpc.Method<Params, Result> => method
10
-
11
12
  const NoParams = Schema.Tuple([])
12
13
 
13
14
  const Topics = Schema.Array(Schema.NullOr(Schema.Union([EvmHexData, Schema.Array(EvmHexData)])))
14
15
 
16
+ const BlockTag = Schema.Literals(["latest", "pending"])
17
+
15
18
  /** @internal */
16
- export const chainId = make({ name: "eth_chainId", params: NoParams, result: IntFromHex })
19
+ export const chainId = JsonRpc.method({ name: "eth_chainId", params: NoParams, result: IntFromHex })
17
20
 
18
21
  /** @internal */
19
- export const blockNumber = make({ name: "eth_blockNumber", params: NoParams, result: QuantityFromHex })
22
+ export const blockNumber = JsonRpc.method({ name: "eth_blockNumber", params: NoParams, result: QuantityFromHex })
20
23
 
21
24
  /** @internal */
22
- export const getBlockByNumber = make({
25
+ export const getBlockByNumber = JsonRpc.method({
23
26
  name: "eth_getBlockByNumber",
24
27
  // `false`: transaction hashes only, never the full transactions
25
28
  params: Schema.Tuple([QuantityFromHex, Schema.Literal(false)]),
@@ -28,7 +31,14 @@ export const getBlockByNumber = make({
28
31
  })
29
32
 
30
33
  /** @internal */
31
- export const getLogs = make({
34
+ export const getBalance = JsonRpc.method({
35
+ name: "eth_getBalance",
36
+ params: Schema.Tuple([EvmAddress, QuantityFromHex]),
37
+ result: QuantityFromHex
38
+ })
39
+
40
+ /** @internal */
41
+ export const getLogs = JsonRpc.method({
32
42
  name: "eth_getLogs",
33
43
  params: Schema.Tuple([
34
44
  Schema.Struct({
@@ -41,3 +51,54 @@ export const getLogs = make({
41
51
  // a log dropped by a reorg comes back with `removed: true` and must not be returned
42
52
  result: Schema.Array(Schema.Struct({ ...EvmLog.fields, removed: Schema.optionalKey(Schema.Literal(false)) }))
43
53
  })
54
+
55
+ /** @internal */
56
+ export const getTransactionByHash = JsonRpc.method({
57
+ name: "eth_getTransactionByHash",
58
+ params: Schema.Tuple([EvmTransactionHash]),
59
+ // `null` when the node does not know the transaction
60
+ result: Schema.NullOr(EvmTransaction)
61
+ })
62
+
63
+ /** @internal */
64
+ export const getTransactionReceipt = JsonRpc.method({
65
+ name: "eth_getTransactionReceipt",
66
+ params: Schema.Tuple([EvmTransactionHash]),
67
+ // `null` until the transaction is in a block
68
+ result: Schema.NullOr(EvmTransactionReceipt)
69
+ })
70
+
71
+ /** @internal */
72
+ export const sendRawTransaction = JsonRpc.method({
73
+ name: "eth_sendRawTransaction",
74
+ params: Schema.Tuple([EvmHexData]),
75
+ result: EvmTransactionHash
76
+ })
77
+
78
+ /** @internal */
79
+ export const getTransactionCount = JsonRpc.method({
80
+ name: "eth_getTransactionCount",
81
+ params: Schema.Tuple([EvmAddress, BlockTag]),
82
+ result: QuantityFromHex
83
+ })
84
+
85
+ /** @internal */
86
+ export const estimateGas = JsonRpc.method({
87
+ name: "eth_estimateGas",
88
+ params: Schema.Tuple([
89
+ Schema.Struct({
90
+ from: EvmAddress,
91
+ to: EvmAddress,
92
+ value: JsonRpc.optionalOmitted(QuantityFromHex),
93
+ data: JsonRpc.optionalOmitted(EvmHexData)
94
+ })
95
+ ]),
96
+ result: QuantityFromHex
97
+ })
98
+
99
+ /** @internal */
100
+ export const feeHistory = JsonRpc.method({
101
+ name: "eth_feeHistory",
102
+ params: Schema.Tuple([IntFromHex, BlockTag, Schema.Array(Schema.Number)]),
103
+ result: EvmFeeHistory
104
+ })