@effect-blockchain/evm 0.2.0 → 0.3.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.
- package/dist/EvmFeeHistory.d.ts +26 -0
- package/dist/EvmFeeHistory.d.ts.map +1 -0
- package/dist/EvmFeeHistory.js +25 -0
- package/dist/EvmFeeHistory.js.map +1 -0
- package/dist/EvmIndexer.d.ts +45 -1
- package/dist/EvmIndexer.d.ts.map +1 -1
- package/dist/EvmIndexer.js +91 -26
- package/dist/EvmIndexer.js.map +1 -1
- package/dist/EvmRelayer.d.ts +200 -0
- package/dist/EvmRelayer.d.ts.map +1 -0
- package/dist/EvmRelayer.js +281 -0
- package/dist/EvmRelayer.js.map +1 -0
- package/dist/EvmRpc.d.ts +87 -3
- package/dist/EvmRpc.d.ts.map +1 -1
- package/dist/EvmRpc.js +29 -4
- package/dist/EvmRpc.js.map +1 -1
- package/dist/EvmSigner.d.ts +78 -0
- package/dist/EvmSigner.d.ts.map +1 -0
- package/dist/EvmSigner.js +98 -0
- package/dist/EvmSigner.js.map +1 -0
- package/dist/EvmTransaction.d.ts +65 -0
- package/dist/EvmTransaction.d.ts.map +1 -0
- package/dist/EvmTransaction.js +60 -0
- package/dist/EvmTransaction.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/confirmations.d.ts +2 -0
- package/dist/internal/confirmations.d.ts.map +1 -0
- package/dist/internal/confirmations.js +13 -0
- package/dist/internal/confirmations.js.map +1 -0
- package/dist/internal/rpcMethods.js +53 -0
- package/dist/internal/rpcMethods.js.map +1 -1
- package/package.json +1 -1
- package/src/EvmFeeHistory.ts +28 -0
- package/src/EvmIndexer.ts +136 -33
- package/src/EvmRelayer.ts +361 -0
- package/src/EvmRpc.ts +105 -4
- package/src/EvmSigner.ts +128 -0
- package/src/EvmTransaction.ts +64 -0
- package/src/index.ts +20 -0
- package/src/internal/confirmations.ts +17 -0
- package/src/internal/rpcMethods.ts +63 -0
package/src/EvmRpc.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Typed
|
|
2
|
+
* Typed EVM JSON-RPC methods over an ordered list of endpoints with fallback.
|
|
3
3
|
*
|
|
4
4
|
* A call starts at the first endpoint and moves to the next one on failure, except on `WrongNetwork`. Each endpoint
|
|
5
|
-
* is checked once to serve the configured network (`eth_chainId`). Every method is a read,
|
|
6
|
-
* are safe. Methods are named after the JSON-RPC
|
|
5
|
+
* is checked once to serve the configured network (`eth_chainId`). Every method but `sendRawTransaction` is a read,
|
|
6
|
+
* so retries and fallback are safe; `sendRawTransaction` documents its own case. Methods are named after the JSON-RPC
|
|
7
|
+
* method without its namespace (`eth_getLogs` is `getLogs`).
|
|
7
8
|
*
|
|
8
9
|
* @example
|
|
9
10
|
* ```ts
|
|
@@ -42,9 +43,12 @@ import * as Ref from "effect/Ref"
|
|
|
42
43
|
import * as Schema from "effect/Schema"
|
|
43
44
|
import type { EvmAddress } from "./EvmAddress.ts"
|
|
44
45
|
import type { EvmBlock } from "./EvmBlock.ts"
|
|
46
|
+
import type { EvmFeeHistory } from "./EvmFeeHistory.ts"
|
|
45
47
|
import type { EvmHexData } from "./EvmHexData.ts"
|
|
46
48
|
import type { EvmLog } from "./EvmLog.ts"
|
|
47
49
|
import * as EvmNetwork from "./EvmNetwork.ts"
|
|
50
|
+
import type { EvmTransaction, EvmTransactionReceipt } from "./EvmTransaction.ts"
|
|
51
|
+
import type { EvmTransactionHash } from "./EvmTransactionHash.ts"
|
|
48
52
|
import * as RpcMethods from "./internal/rpcMethods.ts"
|
|
49
53
|
|
|
50
54
|
/**
|
|
@@ -100,6 +104,14 @@ export interface EvmLogsFilter {
|
|
|
100
104
|
readonly topics?: ReadonlyArray<EvmHexData | ReadonlyArray<EvmHexData> | null> | undefined
|
|
101
105
|
}
|
|
102
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The block a state read is made at: `latest` is the head, `pending` adds the transactions of the node's pool.
|
|
109
|
+
*
|
|
110
|
+
* @since 0.3.0
|
|
111
|
+
* @category models
|
|
112
|
+
*/
|
|
113
|
+
export type EvmBlockTag = "latest" | "pending"
|
|
114
|
+
|
|
103
115
|
/**
|
|
104
116
|
* @since 0.1.0
|
|
105
117
|
* @category services
|
|
@@ -117,12 +129,85 @@ export class EvmRpc extends Context.Service<EvmRpc, {
|
|
|
117
129
|
* requested one fails with `InvalidResponse`.
|
|
118
130
|
*/
|
|
119
131
|
readonly getBlockByNumber: (options: { readonly blockNumber: bigint }) => Effect.Effect<EvmBlock, EvmRpcError>
|
|
132
|
+
/**
|
|
133
|
+
* `eth_getBalance`: the balance of `address` in wei at the end of `blockNumber`, only answered by an endpoint whose
|
|
134
|
+
* head has reached `blockNumber`. A block older than the endpoint's state history needs an archive endpoint: pruned
|
|
135
|
+
* nodes answer with provider-specific codes (-32000, -32602, -32603), which fall back but never map to `Pruned`.
|
|
136
|
+
*
|
|
137
|
+
* @since 0.3.0
|
|
138
|
+
*/
|
|
139
|
+
readonly getBalance: (options: {
|
|
140
|
+
readonly address: EvmAddress
|
|
141
|
+
readonly blockNumber: bigint
|
|
142
|
+
}) => Effect.Effect<bigint, EvmRpcError>
|
|
120
143
|
/**
|
|
121
144
|
* `eth_getLogs`, only answered by an endpoint whose head has reached `toBlock`. Logs are returned in chain order:
|
|
122
145
|
* sorted by block then log index. A removed, pending or out-of-filter log fails with `InvalidResponse`. An empty
|
|
123
146
|
* range returns `[]`.
|
|
124
147
|
*/
|
|
125
148
|
readonly getLogs: (filter: EvmLogsFilter) => Effect.Effect<ReadonlyArray<EvmLog>, EvmRpcError>
|
|
149
|
+
/**
|
|
150
|
+
* `eth_getTransactionByHash`: the transaction, pending or included, or `null` when the answering endpoint does not
|
|
151
|
+
* know it. `null` does not fall back: an endpoint behind the head or with another pool answers it too.
|
|
152
|
+
*
|
|
153
|
+
* @since 0.3.0
|
|
154
|
+
*/
|
|
155
|
+
readonly getTransactionByHash: (options: {
|
|
156
|
+
readonly hash: EvmTransactionHash
|
|
157
|
+
}) => Effect.Effect<EvmTransaction | null, EvmRpcError>
|
|
158
|
+
/**
|
|
159
|
+
* `eth_getTransactionReceipt`: the receipt, or `null` until the answering endpoint has the transaction in a block.
|
|
160
|
+
* `null` does not fall back.
|
|
161
|
+
*
|
|
162
|
+
* @since 0.3.0
|
|
163
|
+
*/
|
|
164
|
+
readonly getTransactionReceipt: (options: {
|
|
165
|
+
readonly hash: EvmTransactionHash
|
|
166
|
+
}) => Effect.Effect<EvmTransactionReceipt | null, EvmRpcError>
|
|
167
|
+
/**
|
|
168
|
+
* `eth_sendRawTransaction`: submits a signed transaction and returns its hash. A retry or a fallback sends the same
|
|
169
|
+
* bytes, so it never creates a second transaction, but a node that already has them refuses the second copy. A
|
|
170
|
+
* refusal (`already known`, `nonce too low`, `replacement transaction underpriced`, `insufficient funds`...) is
|
|
171
|
+
* `Rejected`, with the node's message in `cause`.
|
|
172
|
+
*
|
|
173
|
+
* @since 0.3.0
|
|
174
|
+
*/
|
|
175
|
+
readonly sendRawTransaction: (options: {
|
|
176
|
+
readonly signed: EvmHexData
|
|
177
|
+
}) => Effect.Effect<EvmTransactionHash, EvmRpcError>
|
|
178
|
+
/**
|
|
179
|
+
* `eth_getTransactionCount`: the number of transactions sent by `address` at `blockTag`, which is the nonce of its
|
|
180
|
+
* next one. `pending` counts the transactions of the answering node's pool, which differs between nodes.
|
|
181
|
+
*
|
|
182
|
+
* @since 0.3.0
|
|
183
|
+
*/
|
|
184
|
+
readonly getTransactionCount: (options: {
|
|
185
|
+
readonly address: EvmAddress
|
|
186
|
+
readonly blockTag: EvmBlockTag
|
|
187
|
+
}) => Effect.Effect<bigint, EvmRpcError>
|
|
188
|
+
/**
|
|
189
|
+
* `eth_estimateGas`: the gas a call from `from` to `to` needs at the head. A call that reverts is `Rejected`, with
|
|
190
|
+
* the node's message in `cause`.
|
|
191
|
+
*
|
|
192
|
+
* @since 0.3.0
|
|
193
|
+
*/
|
|
194
|
+
readonly estimateGas: (options: {
|
|
195
|
+
readonly from: EvmAddress
|
|
196
|
+
readonly to: EvmAddress
|
|
197
|
+
readonly value?: bigint | undefined
|
|
198
|
+
readonly data?: EvmHexData | undefined
|
|
199
|
+
}) => Effect.Effect<bigint, EvmRpcError>
|
|
200
|
+
/**
|
|
201
|
+
* `eth_feeHistory`: the fees of the `blockCount` blocks ending at `newestBlock`, with the priority fees at
|
|
202
|
+
* `rewardPercentiles` (each between 0 and 100, in ascending order). A node may return fewer blocks.
|
|
203
|
+
*
|
|
204
|
+
* @since 0.3.0
|
|
205
|
+
*/
|
|
206
|
+
readonly feeHistory: (options: {
|
|
207
|
+
readonly blockCount: number
|
|
208
|
+
readonly newestBlock: EvmBlockTag
|
|
209
|
+
readonly rewardPercentiles: ReadonlyArray<number>
|
|
210
|
+
}) => Effect.Effect<EvmFeeHistory, EvmRpcError>
|
|
126
211
|
}>()("@effect-blockchain/evm/EvmRpc") {}
|
|
127
212
|
|
|
128
213
|
// the JSON-RPC codes viem 2.57.11 retries (`utils/buildRequest.ts`, `shouldRetry`): limit exceeded, internal error,
|
|
@@ -235,16 +320,32 @@ export const layer = (options: {
|
|
|
235
320
|
return Arr.sort(logs.map(({ removed: _removed, ...log }) => log), byBlockThenIndex)
|
|
236
321
|
})
|
|
237
322
|
|
|
323
|
+
const callOn = <Params, A>(method: JsonRpc.Method<Params, A>, params: Params) =>
|
|
324
|
+
rpc.withFallback((endpoint) => rpc.call(endpoint, method, params))
|
|
325
|
+
|
|
238
326
|
return EvmRpc.of({
|
|
239
327
|
network,
|
|
240
328
|
chainId: rpc.withFallback(rpc.checkNetwork),
|
|
241
329
|
blockNumber: rpc.withFallback(blockNumberOn),
|
|
242
330
|
getBlockByNumber: ({ blockNumber }) =>
|
|
243
331
|
rpc.withFallback((endpoint) => getBlockByNumberOn(endpoint, blockNumber)),
|
|
332
|
+
getBalance: ({ address, blockNumber }) =>
|
|
333
|
+
rpc.withFallback((endpoint) =>
|
|
334
|
+
rpc.requireHead(endpoint, RpcMethods.getBalance.name, blockNumber, blockNumberOn(endpoint)).pipe(
|
|
335
|
+
Effect.andThen(rpc.call(endpoint, RpcMethods.getBalance, [address, blockNumber]))
|
|
336
|
+
)
|
|
337
|
+
),
|
|
244
338
|
getLogs: (filter) =>
|
|
245
339
|
filter.fromBlock > filter.toBlock
|
|
246
340
|
? Effect.succeed([])
|
|
247
|
-
: rpc.withFallback((endpoint) => getLogsOn(endpoint, filter))
|
|
341
|
+
: rpc.withFallback((endpoint) => getLogsOn(endpoint, filter)),
|
|
342
|
+
getTransactionByHash: ({ hash }) => callOn(RpcMethods.getTransactionByHash, [hash]),
|
|
343
|
+
getTransactionReceipt: ({ hash }) => callOn(RpcMethods.getTransactionReceipt, [hash]),
|
|
344
|
+
sendRawTransaction: ({ signed }) => callOn(RpcMethods.sendRawTransaction, [signed]),
|
|
345
|
+
getTransactionCount: ({ address, blockTag }) => callOn(RpcMethods.getTransactionCount, [address, blockTag]),
|
|
346
|
+
estimateGas: (call) => callOn(RpcMethods.estimateGas, [call]),
|
|
347
|
+
feeHistory: ({ blockCount, newestBlock, rewardPercentiles }) =>
|
|
348
|
+
callOn(RpcMethods.feeHistory, [blockCount, newestBlock, rewardPercentiles])
|
|
248
349
|
})
|
|
249
350
|
})
|
|
250
351
|
)
|
package/src/EvmSigner.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signers of EVM transactions: a local private key, or a custody API that only signs a hash.
|
|
3
|
+
*
|
|
4
|
+
* A signer is a value and receives the whole transaction, never a hash chosen by its caller: signing an opaque hash
|
|
5
|
+
* hides what is being authorised, and custody policy engines and hardware wallets refuse it. Only EIP-1559
|
|
6
|
+
* transactions calling an address are signed.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.3.0
|
|
9
|
+
*/
|
|
10
|
+
import * as Effect from "effect/Effect"
|
|
11
|
+
import * as Redacted from "effect/Redacted"
|
|
12
|
+
import * as Schema from "effect/Schema"
|
|
13
|
+
import { isHex, keccak256, parseSignature, recoverTransactionAddress, serializeTransaction } from "viem"
|
|
14
|
+
import { privateKeyToAccount } from "viem/accounts"
|
|
15
|
+
import { EvmAddress } from "./EvmAddress.ts"
|
|
16
|
+
import { EvmHexData } from "./EvmHexData.ts"
|
|
17
|
+
import type { EvmChainId } from "./EvmNetwork.ts"
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A signature could not be produced: `cause` is the signer's own failure, or why its signature was refused. It never
|
|
21
|
+
* holds a private key.
|
|
22
|
+
*
|
|
23
|
+
* @since 0.3.0
|
|
24
|
+
* @category errors
|
|
25
|
+
*/
|
|
26
|
+
export class EvmSignerError extends Schema.TaggedError<EvmSignerError>()("EvmSignerError", {
|
|
27
|
+
cause: Schema.Defect()
|
|
28
|
+
}) {}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* An EIP-1559 transaction before its signature. `value` (default 0), `maxFeePerGas` and `maxPriorityFeePerGas` are in
|
|
32
|
+
* wei, `data` defaults to `0x`.
|
|
33
|
+
*
|
|
34
|
+
* @since 0.3.0
|
|
35
|
+
* @category models
|
|
36
|
+
*/
|
|
37
|
+
export interface EvmUnsignedTransaction {
|
|
38
|
+
readonly chainId: EvmChainId
|
|
39
|
+
readonly nonce: bigint
|
|
40
|
+
readonly to: EvmAddress
|
|
41
|
+
readonly value?: bigint | undefined
|
|
42
|
+
readonly data?: EvmHexData | undefined
|
|
43
|
+
readonly gas: bigint
|
|
44
|
+
readonly maxFeePerGas: bigint
|
|
45
|
+
readonly maxPriorityFeePerGas: bigint
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Signs transactions sent from `address`. `signTransaction` returns the signed transaction, serialized as
|
|
50
|
+
* `eth_sendRawTransaction` takes it.
|
|
51
|
+
*
|
|
52
|
+
* @since 0.3.0
|
|
53
|
+
* @category models
|
|
54
|
+
*/
|
|
55
|
+
export interface EvmSigner {
|
|
56
|
+
readonly address: EvmAddress
|
|
57
|
+
readonly signTransaction: (transaction: EvmUnsignedTransaction) => Effect.Effect<EvmHexData, EvmSignerError>
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const toError = (cause: unknown) => new EvmSignerError({ cause })
|
|
61
|
+
|
|
62
|
+
const serializable = (transaction: EvmUnsignedTransaction) => ({
|
|
63
|
+
type: "eip1559" as const,
|
|
64
|
+
chainId: transaction.chainId,
|
|
65
|
+
nonce: Number(transaction.nonce),
|
|
66
|
+
to: transaction.to,
|
|
67
|
+
value: transaction.value ?? BigInt(0),
|
|
68
|
+
data: transaction.data ?? ("0x" as const),
|
|
69
|
+
gas: transaction.gas,
|
|
70
|
+
maxFeePerGas: transaction.maxFeePerGas,
|
|
71
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* A signer holding a secp256k1 private key (32 bytes, 0x-prefixed hex) in memory. An invalid key fails with an
|
|
76
|
+
* {@link EvmSignerError} that does not repeat it.
|
|
77
|
+
*
|
|
78
|
+
* @since 0.3.0
|
|
79
|
+
* @category constructors
|
|
80
|
+
*/
|
|
81
|
+
export const fromPrivateKey = (key: Redacted.Redacted<string>): Effect.Effect<EvmSigner, EvmSignerError> =>
|
|
82
|
+
Effect.try({
|
|
83
|
+
try: () => {
|
|
84
|
+
const value = Redacted.value(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
|
*/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as BigInt_ from "effect/BigInt"
|
|
2
|
+
import * as Effect from "effect/Effect"
|
|
3
|
+
import * as EvmNetwork from "../EvmNetwork.ts"
|
|
4
|
+
import type * as EvmRpc from "../EvmRpc.ts"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The head block number minus `confirmations` (the network's default when unset), never below 0.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export const lastConfirmedBlock = (
|
|
12
|
+
rpc: EvmRpc.EvmRpc["Service"],
|
|
13
|
+
confirmations: number | undefined
|
|
14
|
+
): Effect.Effect<bigint, EvmRpc.EvmRpcError> => {
|
|
15
|
+
const depth = BigInt(confirmations ?? EvmNetwork.defaultConfirmations(rpc.network))
|
|
16
|
+
return Effect.map(rpc.blockNumber, (head) => BigInt_.max(head - depth, BigInt(0)))
|
|
17
|
+
}
|
|
@@ -2,8 +2,11 @@ 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
12
|
const make = <Params, Result>(method: JsonRpc.Method<Params, Result>): JsonRpc.Method<Params, Result> => method
|
|
@@ -12,6 +15,8 @@ const NoParams = Schema.Tuple([])
|
|
|
12
15
|
|
|
13
16
|
const Topics = Schema.Array(Schema.NullOr(Schema.Union([EvmHexData, Schema.Array(EvmHexData)])))
|
|
14
17
|
|
|
18
|
+
const BlockTag = Schema.Literals(["latest", "pending"])
|
|
19
|
+
|
|
15
20
|
/** @internal */
|
|
16
21
|
export const chainId = make({ name: "eth_chainId", params: NoParams, result: IntFromHex })
|
|
17
22
|
|
|
@@ -27,6 +32,13 @@ export const getBlockByNumber = make({
|
|
|
27
32
|
result: Schema.NullOr(EvmBlock)
|
|
28
33
|
})
|
|
29
34
|
|
|
35
|
+
/** @internal */
|
|
36
|
+
export const getBalance = make({
|
|
37
|
+
name: "eth_getBalance",
|
|
38
|
+
params: Schema.Tuple([EvmAddress, QuantityFromHex]),
|
|
39
|
+
result: QuantityFromHex
|
|
40
|
+
})
|
|
41
|
+
|
|
30
42
|
/** @internal */
|
|
31
43
|
export const getLogs = make({
|
|
32
44
|
name: "eth_getLogs",
|
|
@@ -41,3 +53,54 @@ export const getLogs = make({
|
|
|
41
53
|
// a log dropped by a reorg comes back with `removed: true` and must not be returned
|
|
42
54
|
result: Schema.Array(Schema.Struct({ ...EvmLog.fields, removed: Schema.optionalKey(Schema.Literal(false)) }))
|
|
43
55
|
})
|
|
56
|
+
|
|
57
|
+
/** @internal */
|
|
58
|
+
export const getTransactionByHash = make({
|
|
59
|
+
name: "eth_getTransactionByHash",
|
|
60
|
+
params: Schema.Tuple([EvmTransactionHash]),
|
|
61
|
+
// `null` when the node does not know the transaction
|
|
62
|
+
result: Schema.NullOr(EvmTransaction)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
export const getTransactionReceipt = make({
|
|
67
|
+
name: "eth_getTransactionReceipt",
|
|
68
|
+
params: Schema.Tuple([EvmTransactionHash]),
|
|
69
|
+
// `null` until the transaction is in a block
|
|
70
|
+
result: Schema.NullOr(EvmTransactionReceipt)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
/** @internal */
|
|
74
|
+
export const sendRawTransaction = make({
|
|
75
|
+
name: "eth_sendRawTransaction",
|
|
76
|
+
params: Schema.Tuple([EvmHexData]),
|
|
77
|
+
result: EvmTransactionHash
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
export const getTransactionCount = make({
|
|
82
|
+
name: "eth_getTransactionCount",
|
|
83
|
+
params: Schema.Tuple([EvmAddress, BlockTag]),
|
|
84
|
+
result: QuantityFromHex
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
/** @internal */
|
|
88
|
+
export const estimateGas = make({
|
|
89
|
+
name: "eth_estimateGas",
|
|
90
|
+
params: Schema.Tuple([
|
|
91
|
+
Schema.Struct({
|
|
92
|
+
from: EvmAddress,
|
|
93
|
+
to: EvmAddress,
|
|
94
|
+
value: JsonRpc.optionalOmitted(QuantityFromHex),
|
|
95
|
+
data: JsonRpc.optionalOmitted(EvmHexData)
|
|
96
|
+
})
|
|
97
|
+
]),
|
|
98
|
+
result: QuantityFromHex
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
/** @internal */
|
|
102
|
+
export const feeHistory = make({
|
|
103
|
+
name: "eth_feeHistory",
|
|
104
|
+
params: Schema.Tuple([IntFromHex, BlockTag, Schema.Array(Schema.Number)]),
|
|
105
|
+
result: EvmFeeHistory
|
|
106
|
+
})
|