@effect-blockchain/starknet 0.1.0 → 0.2.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/StarknetIndexer.d.ts +40 -2
- package/dist/StarknetIndexer.d.ts.map +1 -1
- package/dist/StarknetIndexer.js +89 -14
- package/dist/StarknetIndexer.js.map +1 -1
- package/dist/StarknetNetwork.d.ts +9 -0
- package/dist/StarknetNetwork.d.ts.map +1 -1
- package/dist/StarknetNetwork.js +9 -0
- package/dist/StarknetNetwork.js.map +1 -1
- package/dist/StarknetRelayer.d.ts +273 -0
- package/dist/StarknetRelayer.d.ts.map +1 -0
- package/dist/StarknetRelayer.js +352 -0
- package/dist/StarknetRelayer.js.map +1 -0
- package/dist/StarknetRpc.d.ts +107 -5
- package/dist/StarknetRpc.d.ts.map +1 -1
- package/dist/StarknetRpc.js +83 -5
- package/dist/StarknetRpc.js.map +1 -1
- package/dist/StarknetSigner.d.ts +75 -0
- package/dist/StarknetSigner.d.ts.map +1 -0
- package/dist/StarknetSigner.js +84 -0
- package/dist/StarknetSigner.js.map +1 -0
- package/dist/StarknetTransaction.d.ts +74 -0
- package/dist/StarknetTransaction.d.ts.map +1 -0
- package/dist/StarknetTransaction.js +2 -0
- package/dist/StarknetTransaction.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -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 +135 -0
- package/dist/internal/rpcMethods.js.map +1 -1
- package/dist/internal/transactionHash.d.ts +2 -0
- package/dist/internal/transactionHash.d.ts.map +1 -0
- package/dist/internal/transactionHash.js +60 -0
- package/dist/internal/transactionHash.js.map +1 -0
- package/package.json +1 -1
- package/src/StarknetIndexer.ts +141 -26
- package/src/StarknetNetwork.ts +12 -0
- package/src/StarknetRelayer.ts +437 -0
- package/src/StarknetRpc.ts +198 -6
- package/src/StarknetSigner.ts +115 -0
- package/src/StarknetTransaction.ts +68 -0
- package/src/index.ts +15 -0
- package/src/internal/confirmations.ts +17 -0
- package/src/internal/rpcMethods.ts +136 -0
- package/src/internal/transactionHash.ts +64 -0
package/src/StarknetRpc.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Typed
|
|
2
|
+
* Typed Starknet 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 (`starknet_chainId`). Every method
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* is checked once to serve the configured network (`starknet_chainId`). Every method but `addInvokeTransaction`
|
|
6
|
+
* and `addDeployAccountTransaction` is a read, so retries and fallback are safe; these two document their own case.
|
|
7
|
+
* Methods are named after the JSON-RPC method without its namespace (`starknet_getEvents` is `getEvents`). A
|
|
8
|
+
* `getEvents` page cursor is only sent to the endpoint that issued it, with no fallback.
|
|
8
9
|
*
|
|
9
10
|
* @example
|
|
10
11
|
* ```ts
|
|
@@ -32,7 +33,7 @@
|
|
|
32
33
|
* @since 0.1.0
|
|
33
34
|
*/
|
|
34
35
|
import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
|
|
35
|
-
import
|
|
36
|
+
import * as Arr from "effect/Array"
|
|
36
37
|
import * as Config from "effect/Config"
|
|
37
38
|
import * as Context from "effect/Context"
|
|
38
39
|
import type * as Duration from "effect/Duration"
|
|
@@ -47,6 +48,12 @@ import type { StarknetAddress } from "./StarknetAddress.ts"
|
|
|
47
48
|
import type { StarknetBlock } from "./StarknetBlock.ts"
|
|
48
49
|
import type { StarknetEvent } from "./StarknetEvent.ts"
|
|
49
50
|
import * as StarknetNetwork from "./StarknetNetwork.ts"
|
|
51
|
+
import type {
|
|
52
|
+
StarknetDeployAccountTransaction,
|
|
53
|
+
StarknetInvokeTransaction,
|
|
54
|
+
StarknetResourceBounds,
|
|
55
|
+
StarknetTransaction
|
|
56
|
+
} from "./StarknetTransaction.ts"
|
|
50
57
|
import type { StarknetTransactionHash } from "./StarknetTransactionHash.ts"
|
|
51
58
|
|
|
52
59
|
/**
|
|
@@ -150,6 +157,45 @@ export interface StarknetTransactionReceipt {
|
|
|
150
157
|
readonly events: ReadonlyArray<Pick<StarknetEvent, "fromAddress" | "keys" | "data">>
|
|
151
158
|
}
|
|
152
159
|
|
|
160
|
+
/**
|
|
161
|
+
* The `starknet_getTransactionStatus` result of the JSON-RPC spec v0.9. `executionStatus` is absent until the
|
|
162
|
+
* transaction is executed (`RECEIVED`, `CANDIDATE`), and `failureReason` is the node's revert message of a `REVERTED`
|
|
163
|
+
* transaction.
|
|
164
|
+
*
|
|
165
|
+
* @since 0.2.0
|
|
166
|
+
* @category models
|
|
167
|
+
*/
|
|
168
|
+
export interface StarknetTransactionStatusResult {
|
|
169
|
+
readonly finalityStatus: "RECEIVED" | "CANDIDATE" | "PRE_CONFIRMED" | "ACCEPTED_ON_L2" | "ACCEPTED_ON_L1"
|
|
170
|
+
readonly executionStatus?: "SUCCEEDED" | "REVERTED" | undefined
|
|
171
|
+
readonly failureReason?: string | undefined
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The block a state read is made at: the latest accepted block, or the pre-confirmed block being built on top of it.
|
|
176
|
+
*
|
|
177
|
+
* @since 0.2.0
|
|
178
|
+
* @category models
|
|
179
|
+
*/
|
|
180
|
+
export type StarknetBlockTag = "latest" | "pre_confirmed"
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* One `starknet_estimateFee` result: the amount of each resource a transaction consumes and its price, in FRI (10^-18
|
|
184
|
+
* STRK) per unit. `overallFee` is their total.
|
|
185
|
+
*
|
|
186
|
+
* @since 0.2.0
|
|
187
|
+
* @category models
|
|
188
|
+
*/
|
|
189
|
+
export interface StarknetFeeEstimate {
|
|
190
|
+
readonly l1GasConsumed: bigint
|
|
191
|
+
readonly l1GasPrice: bigint
|
|
192
|
+
readonly l1DataGasConsumed: bigint
|
|
193
|
+
readonly l1DataGasPrice: bigint
|
|
194
|
+
readonly l2GasConsumed: bigint
|
|
195
|
+
readonly l2GasPrice: bigint
|
|
196
|
+
readonly overallFee: bigint
|
|
197
|
+
}
|
|
198
|
+
|
|
153
199
|
/**
|
|
154
200
|
* @since 0.1.0
|
|
155
201
|
* @category services
|
|
@@ -173,12 +219,78 @@ export class StarknetRpc extends Context.Service<StarknetRpc, {
|
|
|
173
219
|
* out-of-filter event fails with `InvalidResponse`, and an empty range returns no events.
|
|
174
220
|
*/
|
|
175
221
|
readonly getEvents: (filter: StarknetEventsFilter) => Effect.Effect<StarknetEventsPage, StarknetRpcError>
|
|
222
|
+
/**
|
|
223
|
+
* `starknet_call`, the raw felts a view function returns at the end of `blockNumber`, only answered by an endpoint
|
|
224
|
+
* whose head has reached `blockNumber`.
|
|
225
|
+
*/
|
|
226
|
+
readonly call: (options: {
|
|
227
|
+
readonly contractAddress: StarknetAddress
|
|
228
|
+
readonly entryPointSelector: string
|
|
229
|
+
readonly calldata: ReadonlyArray<string>
|
|
230
|
+
readonly blockNumber: bigint
|
|
231
|
+
}) => Effect.Effect<ReadonlyArray<string>, StarknetRpcError>
|
|
176
232
|
/** `starknet_getTransactionReceipt`. A pre-confirmed receipt fails with `InvalidResponse`. */
|
|
177
233
|
readonly getTransactionReceipt: (
|
|
178
234
|
options: { readonly transactionHash: StarknetTransactionHash }
|
|
179
235
|
) => Effect.Effect<StarknetTransactionReceipt, StarknetRpcError>
|
|
236
|
+
/**
|
|
237
|
+
* `starknet_getTransactionStatus`, `null` when the last endpoint tried answers `TXN_HASH_NOT_FOUND` (29): an endpoint
|
|
238
|
+
* that does not know the transaction falls back to the next one first.
|
|
239
|
+
*/
|
|
240
|
+
readonly getTransactionStatus: (
|
|
241
|
+
options: { readonly transactionHash: StarknetTransactionHash }
|
|
242
|
+
) => Effect.Effect<StarknetTransactionStatusResult | null, StarknetRpcError>
|
|
243
|
+
/**
|
|
244
|
+
* `starknet_getNonce`: the nonce of the next transaction of the account at `address`. `pre_confirmed` counts the
|
|
245
|
+
* transactions of the block being built, not the ones still waiting in the mempool. An address without a contract
|
|
246
|
+
* is `Rejected` with the code 20 (`CONTRACT_NOT_FOUND`).
|
|
247
|
+
*
|
|
248
|
+
* @since 0.2.0
|
|
249
|
+
*/
|
|
250
|
+
readonly getNonce: (options: {
|
|
251
|
+
readonly address: StarknetAddress
|
|
252
|
+
readonly blockTag: StarknetBlockTag
|
|
253
|
+
}) => Effect.Effect<bigint, StarknetRpcError>
|
|
254
|
+
/**
|
|
255
|
+
* `starknet_estimateFee`: one estimate per transaction, each executed on top of the previous ones at `blockTag`
|
|
256
|
+
* (a reply without an estimate is an `InvalidResponse`).
|
|
257
|
+
* `skipValidate` sends the `SKIP_VALIDATE` flag, which accepts unsigned transactions. A transaction that fails is
|
|
258
|
+
* `Rejected` with the code 41 (`TRANSACTION_EXECUTION_ERROR`) and the node's message in `cause`.
|
|
259
|
+
*
|
|
260
|
+
* @since 0.2.0
|
|
261
|
+
*/
|
|
262
|
+
readonly estimateFee: (options: {
|
|
263
|
+
readonly transactions: Arr.NonEmptyReadonlyArray<StarknetTransaction>
|
|
264
|
+
readonly skipValidate?: boolean | undefined
|
|
265
|
+
readonly blockTag: StarknetBlockTag
|
|
266
|
+
}) => Effect.Effect<Arr.NonEmptyReadonlyArray<StarknetFeeEstimate>, StarknetRpcError>
|
|
267
|
+
/**
|
|
268
|
+
* `starknet_addInvokeTransaction`: submits a signed `INVOKE` transaction and returns its hash. A retry or a fallback
|
|
269
|
+
* sends the same transaction, so it never creates a second one, but a node that already has it refuses the second
|
|
270
|
+
* copy. A refusal (`DUPLICATE_TX`, `INVALID_TRANSACTION_NONCE`, `INSUFFICIENT_ACCOUNT_BALANCE`...) is `Rejected`,
|
|
271
|
+
* with the spec's error code as `code`.
|
|
272
|
+
*
|
|
273
|
+
* @since 0.2.0
|
|
274
|
+
*/
|
|
275
|
+
readonly addInvokeTransaction: (options: {
|
|
276
|
+
readonly transaction: StarknetInvokeTransaction
|
|
277
|
+
}) => Effect.Effect<StarknetTransactionHash, StarknetRpcError>
|
|
278
|
+
/**
|
|
279
|
+
* `starknet_addDeployAccountTransaction`: submits a signed `DEPLOY_ACCOUNT` transaction and returns its hash and
|
|
280
|
+
* the address of the account. Retries, fallback and refusals are those of `addInvokeTransaction`.
|
|
281
|
+
*
|
|
282
|
+
* @since 0.2.0
|
|
283
|
+
*/
|
|
284
|
+
readonly addDeployAccountTransaction: (options: {
|
|
285
|
+
readonly transaction: StarknetDeployAccountTransaction
|
|
286
|
+
}) => Effect.Effect<
|
|
287
|
+
{ readonly transactionHash: StarknetTransactionHash; readonly contractAddress: StarknetAddress },
|
|
288
|
+
StarknetRpcError
|
|
289
|
+
>
|
|
180
290
|
}>()("@effect-blockchain/starknet/StarknetRpc") {}
|
|
181
291
|
|
|
292
|
+
const TXN_HASH_NOT_FOUND = 29
|
|
293
|
+
|
|
182
294
|
// error codes from the Starknet JSON-RPC spec v0.9 (starknet_api_openrpc.json), plus the common provider ones
|
|
183
295
|
const codes: ReadonlyMap<number, JsonRpc.CodeReason> = new Map([
|
|
184
296
|
// UNEXPECTED_ERROR, JSON-RPC internal error and provider rate limits: the endpoint is struggling, retry then fall back
|
|
@@ -188,9 +300,45 @@ const codes: ReadonlyMap<number, JsonRpc.CodeReason> = new Map([
|
|
|
188
300
|
[429, "Unavailable"],
|
|
189
301
|
// BLOCK_NOT_FOUND, TXN_HASH_NOT_FOUND: another endpoint may be further ahead
|
|
190
302
|
[24, "Lagging"],
|
|
191
|
-
[
|
|
303
|
+
[TXN_HASH_NOT_FOUND, "Lagging"]
|
|
192
304
|
])
|
|
193
305
|
|
|
306
|
+
const bound = (bound: StarknetResourceBounds["l1Gas"]) => ({
|
|
307
|
+
max_amount: bound.maxAmount,
|
|
308
|
+
max_price_per_unit: bound.maxPricePerUnit
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
const v3 = (transaction: StarknetTransaction) => ({
|
|
312
|
+
version: "0x3" as const,
|
|
313
|
+
signature: transaction.signature,
|
|
314
|
+
nonce: transaction.nonce,
|
|
315
|
+
resource_bounds: {
|
|
316
|
+
l1_gas: bound(transaction.resourceBounds.l1Gas),
|
|
317
|
+
l2_gas: bound(transaction.resourceBounds.l2Gas),
|
|
318
|
+
l1_data_gas: bound(transaction.resourceBounds.l1DataGas)
|
|
319
|
+
},
|
|
320
|
+
tip: transaction.tip,
|
|
321
|
+
paymaster_data: [] as const,
|
|
322
|
+
nonce_data_availability_mode: "L1" as const,
|
|
323
|
+
fee_data_availability_mode: "L1" as const
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
const invoke = (transaction: StarknetInvokeTransaction) => ({
|
|
327
|
+
type: transaction.type,
|
|
328
|
+
sender_address: transaction.senderAddress,
|
|
329
|
+
calldata: transaction.calldata,
|
|
330
|
+
account_deployment_data: [] as const,
|
|
331
|
+
...v3(transaction)
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
const deployAccount = (transaction: StarknetDeployAccountTransaction) => ({
|
|
335
|
+
type: transaction.type,
|
|
336
|
+
class_hash: transaction.classHash,
|
|
337
|
+
contract_address_salt: transaction.contractAddressSalt,
|
|
338
|
+
constructor_calldata: transaction.constructorCalldata,
|
|
339
|
+
...v3(transaction)
|
|
340
|
+
})
|
|
341
|
+
|
|
194
342
|
/**
|
|
195
343
|
* Builds a {@link StarknetRpc} over `endpoints`, tried in order, sending requests with the provided `HttpClient`
|
|
196
344
|
* (e.g. `FetchHttpClient.layer`). `timeout` (default 30 seconds) bounds each attempt, `retries` (default 3) and
|
|
@@ -301,9 +449,53 @@ export const layer = (options: {
|
|
|
301
449
|
)
|
|
302
450
|
),
|
|
303
451
|
getEvents,
|
|
452
|
+
call: (options) =>
|
|
453
|
+
rpc.withFallback((endpoint) =>
|
|
454
|
+
rpc.requireHead(endpoint, RpcMethods.call.name, options.blockNumber, blockNumberOn(endpoint)).pipe(
|
|
455
|
+
Effect.andThen(rpc.call(endpoint, RpcMethods.call, {
|
|
456
|
+
request: {
|
|
457
|
+
contract_address: options.contractAddress,
|
|
458
|
+
entry_point_selector: options.entryPointSelector,
|
|
459
|
+
calldata: options.calldata
|
|
460
|
+
},
|
|
461
|
+
block_id: { block_number: options.blockNumber }
|
|
462
|
+
}))
|
|
463
|
+
)
|
|
464
|
+
),
|
|
304
465
|
getTransactionReceipt: ({ transactionHash }) =>
|
|
305
466
|
rpc.withFallback((endpoint) =>
|
|
306
467
|
rpc.call(endpoint, RpcMethods.getTransactionReceipt, { transaction_hash: transactionHash })
|
|
468
|
+
),
|
|
469
|
+
getTransactionStatus: ({ transactionHash }) =>
|
|
470
|
+
rpc.withFallback((endpoint) =>
|
|
471
|
+
rpc.call(endpoint, RpcMethods.getTransactionStatus, { transaction_hash: transactionHash })
|
|
472
|
+
).pipe(
|
|
473
|
+
Effect.catchIf((error) => error.code === TXN_HASH_NOT_FOUND, () => Effect.succeed(null))
|
|
474
|
+
),
|
|
475
|
+
getNonce: ({ address, blockTag }) =>
|
|
476
|
+
rpc.withFallback((endpoint) =>
|
|
477
|
+
rpc.call(endpoint, RpcMethods.getNonce, { block_id: blockTag, contract_address: address })
|
|
478
|
+
),
|
|
479
|
+
estimateFee: ({ blockTag, skipValidate, transactions }) =>
|
|
480
|
+
rpc.withFallback((endpoint) =>
|
|
481
|
+
rpc.call(endpoint, RpcMethods.estimateFee, {
|
|
482
|
+
request: Arr.map(
|
|
483
|
+
transactions,
|
|
484
|
+
(transaction) => transaction.type === "INVOKE" ? invoke(transaction) : deployAccount(transaction)
|
|
485
|
+
),
|
|
486
|
+
simulation_flags: skipValidate === true ? ["SKIP_VALIDATE"] : [],
|
|
487
|
+
block_id: blockTag
|
|
488
|
+
})
|
|
489
|
+
),
|
|
490
|
+
addInvokeTransaction: ({ transaction }) =>
|
|
491
|
+
rpc.withFallback((endpoint) =>
|
|
492
|
+
rpc.call(endpoint, RpcMethods.addInvokeTransaction, { invoke_transaction: invoke(transaction) })
|
|
493
|
+
).pipe(Effect.map(({ transactionHash }) => transactionHash)),
|
|
494
|
+
addDeployAccountTransaction: ({ transaction }) =>
|
|
495
|
+
rpc.withFallback((endpoint) =>
|
|
496
|
+
rpc.call(endpoint, RpcMethods.addDeployAccountTransaction, {
|
|
497
|
+
deploy_account_transaction: deployAccount(transaction)
|
|
498
|
+
})
|
|
307
499
|
)
|
|
308
500
|
})
|
|
309
501
|
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Signers of Starknet 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 V3 `INVOKE` and
|
|
6
|
+
* `DEPLOY_ACCOUNT` transactions are signed.
|
|
7
|
+
*
|
|
8
|
+
* A Starknet signature is whatever the account contract at `address` validates, so a signer returns felts and the
|
|
9
|
+
* address is given, not derived from the key: `[r, s]` on the Stark curve for OpenZeppelin and Argent accounts.
|
|
10
|
+
*
|
|
11
|
+
* @since 0.2.0
|
|
12
|
+
*/
|
|
13
|
+
import * as Effect from "effect/Effect"
|
|
14
|
+
import * as Redacted from "effect/Redacted"
|
|
15
|
+
import * as Schema from "effect/Schema"
|
|
16
|
+
import { ec, num } from "starknet"
|
|
17
|
+
import { transactionHash } from "./internal/transactionHash.ts"
|
|
18
|
+
import type { StarknetAddress } from "./StarknetAddress.ts"
|
|
19
|
+
import type { StarknetDeployAccountTransaction, StarknetInvokeTransaction } from "./StarknetTransaction.ts"
|
|
20
|
+
import type { StarknetTransactionHash } from "./StarknetTransactionHash.ts"
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A signature could not be produced: `cause` is the signer's own failure, or why its signature was refused. It never
|
|
24
|
+
* holds a private key.
|
|
25
|
+
*
|
|
26
|
+
* @since 0.2.0
|
|
27
|
+
* @category errors
|
|
28
|
+
*/
|
|
29
|
+
export class StarknetSignerError extends Schema.TaggedError<StarknetSignerError>()("StarknetSignerError", {
|
|
30
|
+
cause: Schema.Defect()
|
|
31
|
+
}) {}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A V3 transaction before its signature.
|
|
35
|
+
*
|
|
36
|
+
* @since 0.2.0
|
|
37
|
+
* @category models
|
|
38
|
+
*/
|
|
39
|
+
export type StarknetUnsignedTransaction =
|
|
40
|
+
| Omit<StarknetInvokeTransaction, "signature">
|
|
41
|
+
| Omit<StarknetDeployAccountTransaction, "signature">
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Signs transactions sent from the account contract at `address`. `signTransaction` returns the signature felts, in
|
|
45
|
+
* the format that account contract validates.
|
|
46
|
+
*
|
|
47
|
+
* @since 0.2.0
|
|
48
|
+
* @category models
|
|
49
|
+
*/
|
|
50
|
+
export interface StarknetSigner {
|
|
51
|
+
readonly address: StarknetAddress
|
|
52
|
+
readonly signTransaction: (
|
|
53
|
+
transaction: StarknetUnsignedTransaction
|
|
54
|
+
) => Effect.Effect<ReadonlyArray<string>, StarknetSignerError>
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const toError = (cause: unknown) => cause instanceof StarknetSignerError ? cause : new StarknetSignerError({ cause })
|
|
58
|
+
|
|
59
|
+
const isFelts = Schema.is(Schema.Array(Schema.String.check(Schema.isPattern(/^0x[0-9a-fA-F]{1,64}$/))))
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A signer for a key held elsewhere (a custody API, an HSM) that only signs a hash. `signHash` receives the V3
|
|
63
|
+
* transaction hash and returns the signature felts of the account at `address`, `[r, s]` for OpenZeppelin and Argent
|
|
64
|
+
* accounts. A failure of `signHash` or a signature that is not 0x-prefixed felts fails with a
|
|
65
|
+
* {@link StarknetSignerError}; the signature itself is only checked by the account contract.
|
|
66
|
+
*
|
|
67
|
+
* @since 0.2.0
|
|
68
|
+
* @category constructors
|
|
69
|
+
*/
|
|
70
|
+
export const fromHashSigner = <E>(options: {
|
|
71
|
+
readonly address: StarknetAddress
|
|
72
|
+
readonly signHash: (hash: StarknetTransactionHash) => Effect.Effect<ReadonlyArray<string>, E>
|
|
73
|
+
}): StarknetSigner => ({
|
|
74
|
+
address: options.address,
|
|
75
|
+
signTransaction: Effect.fnUntraced(function*(transaction) {
|
|
76
|
+
const hash = yield* Effect.try({ try: () => transactionHash(transaction), catch: toError })
|
|
77
|
+
const signature = yield* Effect.mapError(options.signHash(hash), toError)
|
|
78
|
+
if (!isFelts(signature)) return yield* toError(new Error("the signature is not an array of 0x-prefixed felts"))
|
|
79
|
+
return signature
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A signer holding the Stark curve private key (hex) of the account at `address` in memory, signing
|
|
85
|
+
* `[r, s]` as OpenZeppelin and Argent accounts validate. An invalid key fails with a {@link StarknetSignerError} that
|
|
86
|
+
* does not repeat it.
|
|
87
|
+
*
|
|
88
|
+
* @since 0.2.0
|
|
89
|
+
* @category constructors
|
|
90
|
+
*/
|
|
91
|
+
export const fromPrivateKey = (options: {
|
|
92
|
+
readonly address: StarknetAddress
|
|
93
|
+
readonly key: Redacted.Redacted<string>
|
|
94
|
+
}): Effect.Effect<StarknetSigner, StarknetSignerError> =>
|
|
95
|
+
Effect.try({
|
|
96
|
+
try: () => {
|
|
97
|
+
const key = Redacted.value(options.key)
|
|
98
|
+
ec.starkCurve.getStarkKey(key)
|
|
99
|
+
return key
|
|
100
|
+
},
|
|
101
|
+
// the thrown error can quote the key
|
|
102
|
+
catch: () => new StarknetSignerError({ cause: new Error("invalid private key") })
|
|
103
|
+
}).pipe(Effect.map((key) =>
|
|
104
|
+
fromHashSigner({
|
|
105
|
+
address: options.address,
|
|
106
|
+
signHash: (hash) =>
|
|
107
|
+
Effect.try({
|
|
108
|
+
try: () => {
|
|
109
|
+
const { r, s } = ec.starkCurve.sign(hash, key)
|
|
110
|
+
return [num.toHex(r), num.toHex(s)]
|
|
111
|
+
},
|
|
112
|
+
catch: toError
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
))
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Starknet V3 transactions an account sends: an `INVOKE` of its `__execute__` entrypoint and the `DEPLOY_ACCOUNT`
|
|
3
|
+
* of its own contract.
|
|
4
|
+
*
|
|
5
|
+
* Fees are paid in STRK. Nonce and fee data availability modes are `L1`, and paymaster and account deployment data
|
|
6
|
+
* are empty: these are the only values the protocol accepts. Felts are 0x-prefixed hexadecimal strings.
|
|
7
|
+
*
|
|
8
|
+
* @since 0.2.0
|
|
9
|
+
*/
|
|
10
|
+
import type { StarknetAddress } from "./StarknetAddress.ts"
|
|
11
|
+
import type { StarknetChainId } from "./StarknetNetwork.ts"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The most a V3 transaction pays for each resource: `maxAmount` units at `maxPricePerUnit` FRI (10^-18 STRK) at most.
|
|
15
|
+
*
|
|
16
|
+
* @since 0.2.0
|
|
17
|
+
* @category models
|
|
18
|
+
*/
|
|
19
|
+
export interface StarknetResourceBounds {
|
|
20
|
+
readonly l1Gas: { readonly maxAmount: bigint; readonly maxPricePerUnit: bigint }
|
|
21
|
+
readonly l1DataGas: { readonly maxAmount: bigint; readonly maxPricePerUnit: bigint }
|
|
22
|
+
readonly l2Gas: { readonly maxAmount: bigint; readonly maxPricePerUnit: bigint }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An `INVOKE` V3 transaction: `calldata` is the calldata of the `__execute__` entrypoint of `senderAddress`, and
|
|
27
|
+
* `signature` the felts its `__validate__` entrypoint checks.
|
|
28
|
+
*
|
|
29
|
+
* @since 0.2.0
|
|
30
|
+
* @category models
|
|
31
|
+
*/
|
|
32
|
+
export interface StarknetInvokeTransaction {
|
|
33
|
+
readonly type: "INVOKE"
|
|
34
|
+
readonly chainId: StarknetChainId
|
|
35
|
+
readonly senderAddress: StarknetAddress
|
|
36
|
+
readonly calldata: ReadonlyArray<string>
|
|
37
|
+
readonly nonce: bigint
|
|
38
|
+
readonly resourceBounds: StarknetResourceBounds
|
|
39
|
+
readonly tip: bigint
|
|
40
|
+
readonly signature: ReadonlyArray<string>
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A `DEPLOY_ACCOUNT` V3 transaction: deploys the account contract of class `classHash` at the address its class hash,
|
|
45
|
+
* salt and constructor calldata determine. That address pays the fee, so it holds STRK before the deployment.
|
|
46
|
+
*
|
|
47
|
+
* @since 0.2.0
|
|
48
|
+
* @category models
|
|
49
|
+
*/
|
|
50
|
+
export interface StarknetDeployAccountTransaction {
|
|
51
|
+
readonly type: "DEPLOY_ACCOUNT"
|
|
52
|
+
readonly chainId: StarknetChainId
|
|
53
|
+
readonly classHash: string
|
|
54
|
+
readonly contractAddressSalt: string
|
|
55
|
+
readonly constructorCalldata: ReadonlyArray<string>
|
|
56
|
+
readonly nonce: bigint
|
|
57
|
+
readonly resourceBounds: StarknetResourceBounds
|
|
58
|
+
readonly tip: bigint
|
|
59
|
+
readonly signature: ReadonlyArray<string>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A signed transaction, as `starknet_addInvokeTransaction` and `starknet_addDeployAccountTransaction` take it.
|
|
64
|
+
*
|
|
65
|
+
* @since 0.2.0
|
|
66
|
+
* @category models
|
|
67
|
+
*/
|
|
68
|
+
export type StarknetTransaction = StarknetInvokeTransaction | StarknetDeployAccountTransaction
|
package/src/index.ts
CHANGED
|
@@ -29,11 +29,26 @@ export * as StarknetIndexer from "./StarknetIndexer.ts"
|
|
|
29
29
|
*/
|
|
30
30
|
export * as StarknetNetwork from "./StarknetNetwork.ts"
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* @since 0.2.0
|
|
34
|
+
*/
|
|
35
|
+
export * as StarknetRelayer from "./StarknetRelayer.ts"
|
|
36
|
+
|
|
32
37
|
/**
|
|
33
38
|
* @since 0.1.0
|
|
34
39
|
*/
|
|
35
40
|
export * as StarknetRpc from "./StarknetRpc.ts"
|
|
36
41
|
|
|
42
|
+
/**
|
|
43
|
+
* @since 0.2.0
|
|
44
|
+
*/
|
|
45
|
+
export * as StarknetSigner from "./StarknetSigner.ts"
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @since 0.2.0
|
|
49
|
+
*/
|
|
50
|
+
export * as StarknetTransaction from "./StarknetTransaction.ts"
|
|
51
|
+
|
|
37
52
|
/**
|
|
38
53
|
* @since 0.1.0
|
|
39
54
|
*/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as BigInt_ from "effect/BigInt"
|
|
2
|
+
import * as Effect from "effect/Effect"
|
|
3
|
+
import * as StarknetNetwork from "../StarknetNetwork.ts"
|
|
4
|
+
import type * as StarknetRpc from "../StarknetRpc.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: StarknetRpc.StarknetRpc["Service"],
|
|
13
|
+
confirmations: number | undefined
|
|
14
|
+
): Effect.Effect<bigint, StarknetRpc.StarknetRpcError> => {
|
|
15
|
+
const depth = BigInt(confirmations ?? StarknetNetwork.defaultConfirmations(rpc.network))
|
|
16
|
+
return Effect.map(rpc.blockNumber, (head) => BigInt_.max(head - depth, BigInt(0)))
|
|
17
|
+
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import * as JsonRpc from "@effect-blockchain/core/JsonRpc"
|
|
2
|
+
import { identity } from "effect/Function"
|
|
3
|
+
import * as Option from "effect/Option"
|
|
4
|
+
import * as Predicate from "effect/Predicate"
|
|
2
5
|
import * as Schema from "effect/Schema"
|
|
3
6
|
import * as SchemaTransformation from "effect/SchemaTransformation"
|
|
4
7
|
import { StarknetAddress } from "../StarknetAddress.ts"
|
|
@@ -50,6 +53,20 @@ export const getBlockWithTxHashes = make({
|
|
|
50
53
|
result: StarknetBlock
|
|
51
54
|
})
|
|
52
55
|
|
|
56
|
+
/** @internal */
|
|
57
|
+
export const call = make({
|
|
58
|
+
name: "starknet_call",
|
|
59
|
+
params: Schema.Struct({
|
|
60
|
+
request: Schema.Struct({
|
|
61
|
+
contract_address: StarknetAddress,
|
|
62
|
+
entry_point_selector: Felt,
|
|
63
|
+
calldata: Schema.Array(Felt)
|
|
64
|
+
}),
|
|
65
|
+
block_id: BlockId
|
|
66
|
+
}),
|
|
67
|
+
result: Schema.Array(PaddedFelt)
|
|
68
|
+
})
|
|
69
|
+
|
|
53
70
|
/** @internal */
|
|
54
71
|
export const getEvents = make({
|
|
55
72
|
name: "starknet_getEvents",
|
|
@@ -69,6 +86,32 @@ export const getEvents = make({
|
|
|
69
86
|
})
|
|
70
87
|
})
|
|
71
88
|
|
|
89
|
+
/** @internal */
|
|
90
|
+
export const getTransactionStatus = make({
|
|
91
|
+
name: "starknet_getTransactionStatus",
|
|
92
|
+
params: Schema.Struct({ transaction_hash: StarknetTransactionHash }),
|
|
93
|
+
result: Schema.Struct({
|
|
94
|
+
finalityStatus: Schema.Literals(["RECEIVED", "CANDIDATE", "PRE_CONFIRMED", "ACCEPTED_ON_L2", "ACCEPTED_ON_L1"]),
|
|
95
|
+
executionStatus: Schema.optionalKey(Schema.Literals(["SUCCEEDED", "REVERTED"])),
|
|
96
|
+
// starknet-devnet 0.7.1 answers `"failure_reason": null` for a transaction that did not revert
|
|
97
|
+
failureReason: Schema.optionalKey(Schema.NullOr(Schema.String)).pipe(
|
|
98
|
+
Schema.decodeTo(
|
|
99
|
+
Schema.optionalKey(Schema.String),
|
|
100
|
+
SchemaTransformation.transformOptional<string, string | null>({
|
|
101
|
+
decode: Option.filter(Predicate.isNotNull),
|
|
102
|
+
encode: identity
|
|
103
|
+
})
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
}).pipe(
|
|
107
|
+
Schema.encodeKeys({
|
|
108
|
+
finalityStatus: "finality_status",
|
|
109
|
+
executionStatus: "execution_status",
|
|
110
|
+
failureReason: "failure_reason"
|
|
111
|
+
})
|
|
112
|
+
)
|
|
113
|
+
})
|
|
114
|
+
|
|
72
115
|
/** @internal */
|
|
73
116
|
export const getTransactionReceipt = make({
|
|
74
117
|
name: "starknet_getTransactionReceipt",
|
|
@@ -86,3 +129,96 @@ export const getTransactionReceipt = make({
|
|
|
86
129
|
Schema.encodeKeys({ transactionHash: "transaction_hash", blockNumber: "block_number", blockHash: "block_hash" })
|
|
87
130
|
)
|
|
88
131
|
})
|
|
132
|
+
|
|
133
|
+
// a felt holding an integer: a nonce, an amount of gas, a price
|
|
134
|
+
const BigIntFromFelt = Schema.String.check(Schema.isPattern(/^0x[0-9a-fA-F]{1,64}$/)).pipe(
|
|
135
|
+
Schema.decodeTo(
|
|
136
|
+
Schema.BigInt,
|
|
137
|
+
SchemaTransformation.transform({ decode: BigInt, encode: (value) => `0x${value.toString(16)}` })
|
|
138
|
+
)
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
const BlockTag = Schema.Literals(["latest", "pre_confirmed"])
|
|
142
|
+
|
|
143
|
+
const ResourceBound = Schema.Struct({ max_amount: BigIntFromFelt, max_price_per_unit: BigIntFromFelt })
|
|
144
|
+
|
|
145
|
+
const v3 = {
|
|
146
|
+
version: Schema.Literal("0x3"),
|
|
147
|
+
signature: Schema.Array(Felt),
|
|
148
|
+
nonce: BigIntFromFelt,
|
|
149
|
+
resource_bounds: Schema.Struct({ l1_gas: ResourceBound, l2_gas: ResourceBound, l1_data_gas: ResourceBound }),
|
|
150
|
+
tip: BigIntFromFelt,
|
|
151
|
+
paymaster_data: Schema.Tuple([]),
|
|
152
|
+
nonce_data_availability_mode: Schema.Literal("L1"),
|
|
153
|
+
fee_data_availability_mode: Schema.Literal("L1")
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const InvokeTransaction = Schema.Struct({
|
|
157
|
+
type: Schema.Literal("INVOKE"),
|
|
158
|
+
sender_address: StarknetAddress,
|
|
159
|
+
calldata: Schema.Array(Felt),
|
|
160
|
+
account_deployment_data: Schema.Tuple([]),
|
|
161
|
+
...v3
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
const DeployAccountTransaction = Schema.Struct({
|
|
165
|
+
type: Schema.Literal("DEPLOY_ACCOUNT"),
|
|
166
|
+
class_hash: Felt,
|
|
167
|
+
contract_address_salt: Felt,
|
|
168
|
+
constructor_calldata: Schema.Array(Felt),
|
|
169
|
+
...v3
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
/** @internal */
|
|
173
|
+
export const getNonce = make({
|
|
174
|
+
name: "starknet_getNonce",
|
|
175
|
+
params: Schema.Struct({ block_id: BlockTag, contract_address: StarknetAddress }),
|
|
176
|
+
result: BigIntFromFelt
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
/** @internal */
|
|
180
|
+
export const estimateFee = make({
|
|
181
|
+
name: "starknet_estimateFee",
|
|
182
|
+
params: Schema.Struct({
|
|
183
|
+
request: Schema.NonEmptyArray(Schema.Union([InvokeTransaction, DeployAccountTransaction])),
|
|
184
|
+
simulation_flags: Schema.Array(Schema.Literal("SKIP_VALIDATE")),
|
|
185
|
+
block_id: BlockTag
|
|
186
|
+
}),
|
|
187
|
+
result: Schema.NonEmptyArray(
|
|
188
|
+
Schema.Struct({
|
|
189
|
+
l1GasConsumed: BigIntFromFelt,
|
|
190
|
+
l1GasPrice: BigIntFromFelt,
|
|
191
|
+
l1DataGasConsumed: BigIntFromFelt,
|
|
192
|
+
l1DataGasPrice: BigIntFromFelt,
|
|
193
|
+
l2GasConsumed: BigIntFromFelt,
|
|
194
|
+
l2GasPrice: BigIntFromFelt,
|
|
195
|
+
overallFee: BigIntFromFelt
|
|
196
|
+
}).pipe(Schema.encodeKeys({
|
|
197
|
+
l1GasConsumed: "l1_gas_consumed",
|
|
198
|
+
l1GasPrice: "l1_gas_price",
|
|
199
|
+
l1DataGasConsumed: "l1_data_gas_consumed",
|
|
200
|
+
l1DataGasPrice: "l1_data_gas_price",
|
|
201
|
+
l2GasConsumed: "l2_gas_consumed",
|
|
202
|
+
l2GasPrice: "l2_gas_price",
|
|
203
|
+
overallFee: "overall_fee"
|
|
204
|
+
}))
|
|
205
|
+
)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
/** @internal */
|
|
209
|
+
export const addInvokeTransaction = make({
|
|
210
|
+
name: "starknet_addInvokeTransaction",
|
|
211
|
+
params: Schema.Struct({ invoke_transaction: InvokeTransaction }),
|
|
212
|
+
result: Schema.Struct({ transactionHash: StarknetTransactionHash }).pipe(
|
|
213
|
+
Schema.encodeKeys({ transactionHash: "transaction_hash" })
|
|
214
|
+
)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
/** @internal */
|
|
218
|
+
export const addDeployAccountTransaction = make({
|
|
219
|
+
name: "starknet_addDeployAccountTransaction",
|
|
220
|
+
params: Schema.Struct({ deploy_account_transaction: DeployAccountTransaction }),
|
|
221
|
+
result: Schema.Struct({ transactionHash: StarknetTransactionHash, contractAddress: StarknetAddress }).pipe(
|
|
222
|
+
Schema.encodeKeys({ transactionHash: "transaction_hash", contractAddress: "contract_address" })
|
|
223
|
+
)
|
|
224
|
+
})
|