-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathStarknetAccountProtocol.swift
451 lines (405 loc) · 22.1 KB
/
StarknetAccountProtocol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import Foundation
public protocol StarknetAccountProtocol {
/// Address of starknet account.
var address: Felt { get }
/// Chain id of the Starknet provider.
var chainId: StarknetChainId { get }
/// Sign list of calls as invoke transaction v1
///
/// - Parameters:
/// - calls: list of calls to be signed
/// - params: additional params for a given transaction
/// - forFeeEstimation: Flag indicating whether the different version of transaction should be used; such transaction can only be used for fee estimation
///
/// - Returns: Signed invoke v1 transaction
func signV1(calls: [StarknetCall], params: StarknetInvokeParamsV1, forFeeEstimation: Bool) throws -> StarknetInvokeTransactionV1
/// Sign list of calls as invoke transaction v3
///
/// - Parameters:
/// - calls: list of calls to be signed
/// - params: additional params for a given transaction
/// - forFeeEstimation: Flag indicating whether the different version of transaction should be used; such transaction can only be used for fee estimation
///
/// - Returns: Signed invoke v3 transaction
func signV3(calls: [StarknetCall], params: StarknetInvokeParamsV3, forFeeEstimation: Bool) throws -> StarknetInvokeTransactionV3
/// Create and sign deploy account transaction v1
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - params: additional params for a given transaction
/// - forFeeEstimation: Flag indicating whether the different version of transaction should be used; such transaction can only be used for fee estimation
///
/// - Returns: Signed deploy account transaction v1
func signDeployAccountV1(classHash: Felt, calldata: StarknetCalldata, salt: Felt, params: StarknetDeployAccountParamsV1, forFeeEstimation: Bool) throws -> StarknetDeployAccountTransactionV1
/// Create and sign deploy account transaction v3
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - params: additional params for a given transaction
/// - forFeeEstimation: Flag indicating whether the different version of transaction should be used; such transaction can only be used for fee estimation
///
/// - Returns: Signed deploy account transaction v3
func signDeployAccountV3(classHash: Felt, calldata: StarknetCalldata, salt: Felt, params: StarknetDeployAccountParamsV3, forFeeEstimation: Bool) throws -> StarknetDeployAccountTransactionV3
/// Sign TypedData for off-chain usage with this account's privateKey.
///
/// - Parameters:
/// - typedData: a TypedData object to sign
///
/// - Returns: a signature for provided TypedData object.
func sign(typedData: StarknetTypedData) throws -> StarknetSignature
/// Verify a signature of TypedData on Starknet.
///
/// - Parameters:
/// - signature: a signature of typedData
/// - typedData: a TypedData instance which signature will be verified
///
/// - Returns: Boolean indicating whether the signature is valid.
func verify(signature: StarknetSignature, for typedData: StarknetTypedData) async throws -> Bool
/// Execute list of calls as invoke transaction v1
///
/// - Parameters:
/// - calls: list of calls to be executed.
/// - params: additional params for a given transaction.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(calls: [StarknetCall], params: StarknetOptionalInvokeParamsV1) async throws -> StarknetRequest<StarknetInvokeTransactionResponse>
/// Execute list of calls as invoke transaction v3
///
/// - Parameters:
/// - calls: list of calls to be executed.
/// - params: additional params for a given transaction.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(calls: [StarknetCall], params: StarknetOptionalInvokeParamsV3) async throws -> StarknetRequest<StarknetInvokeTransactionResponse>
/// Execute list of calls as invoke transaction v1 with automatically estimated fee that will be multiplied by the specified multiplier when max fee is calculated.
///
/// - Parameters:
/// - calls: list of calls to be executed.
/// - estimateFeeMultiplier: multiplier for the estimated fee.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(calls: [StarknetCall], estimateFeeMultiplier: Double) async throws -> StarknetRequest<StarknetInvokeTransactionResponse>
/// Execute list of calls as invoke transaction v3 with automatically estimated fee that will be multiplied by the specified multipliers when resource bounds are calculated.
///
/// - Parameters:
/// - calls: list of calls to be executed.
/// - estimateAmountMultiplier: multiplier for the estimated amount.
/// - estimateUnitPriceMultiplier: multiplier for the estimated unit price.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(calls: [StarknetCall], estimateAmountMultiplier: Double, estimateUnitPriceMultiplier: Double) async throws -> StarknetRequest<StarknetInvokeTransactionResponse>
/// Execute list of calls as invoke transaction v1 with automatically estimated fee
///
/// - Parameters:
/// - calls: list of calls to be executed.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(calls: [StarknetCall]) async throws -> StarknetRequest<StarknetInvokeTransactionResponse>
/// Execute list of calls as invoke transaction v3 with automatically estimated fee
///
/// - Parameters:
/// - calls: list of calls to be executed.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(calls: [StarknetCall]) async throws -> StarknetRequest<StarknetInvokeTransactionResponse>
/// Estimate fee for a list of calls as invoke transaction v1
///
/// - Parameters:
/// - calls: list of calls, for which the fee should be estimated.
/// - nonce: nonce of the account.
/// - skipValidate: Flag indicating whether validation of the transaction should be skipped.
///
/// - Returns: struct containing fee estimate
func estimateFeeV1(calls: [StarknetCall], nonce: Felt, skipValidate: Bool) async throws -> StarknetRequest<[StarknetFeeEstimate]>
/// Estimate fee for a list of calls as invoke transaction v3
///
/// - Parameters:
/// - calls: list of calls, for which the fee should be estimated.
/// - nonce: nonce of the account.
/// - skipValidate: Flag indicating whether validation of the transaction should be skipped.
/// - Returns: struct containing fee estimate
func estimateFeeV3(calls: [StarknetCall], nonce: Felt, skipValidate: Bool) async throws -> StarknetRequest<[StarknetFeeEstimate]>
/// Estimate fee for a deploy account transaction v1
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - nonce: nonce of the account to be deployed
/// - skipValidate: flag indicating whether validation of the transaction should be skipped
///
/// - Returns: struct containing fee estimate
func estimateDeployAccountFeeV1(classHash: Felt, calldata: StarknetCalldata, salt: Felt, nonce: Felt, skipValidate: Bool) async throws -> StarknetRequest<[StarknetFeeEstimate]>
/// Estimate fee for a deploy account transaction v3
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - nonce: nonce of the account to be deployed
/// - skipValidate: flag indicating whether validation of the transaction should be skipped
///
/// - Returns: struct containing fee estimate
func estimateDeployAccountFeeV3(classHash: Felt, calldata: StarknetCalldata, salt: Felt, nonce: Felt, skipValidate: Bool) async throws -> StarknetRequest<[StarknetFeeEstimate]>
/// Get current nonce of the account
///
/// - Returns: current nonce, as felt value.
func getNonce() async throws -> StarknetRequest<Felt>
}
public extension StarknetAccountProtocol {
/// Sign list of calls for exectution as invoke transaction v1.
/// Avoid using this method to sign calls for fee estimation.
///
/// - Parameters:
/// - calls: list of calls to be signed.
/// - params: additional params for a given transaction
///
/// - Returns: Signed invoke transaction v1
func signV1(calls: [StarknetCall], params: StarknetInvokeParamsV1) throws -> StarknetInvokeTransactionV1 {
try signV1(calls: calls, params: params, forFeeEstimation: false)
}
/// Sign list of calls for execution as invoke transaction v3.
/// Avoid using this method to sign calls for fee estimation.
///
/// - Parameters:
/// - calls: list of calls to be signed.
/// - params: additional params for a given transaction
///
/// - Returns: Signed invoke transaction v3
func signV3(calls: [StarknetCall], params: StarknetInvokeParamsV3) throws -> StarknetInvokeTransactionV3 {
try signV3(calls: calls, params: params, forFeeEstimation: false)
}
/// Create and sign deploy account transaction v1
/// Avoid using this method to sign transaction for fee estimation.
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - maxFee: max acceptable fee for the transaction
///
/// - Returns: Signed deploy account transaction v1
func signDeployAccountV1(classHash: Felt, calldata: StarknetCalldata, salt: Felt, maxFee: Felt) throws -> StarknetDeployAccountTransactionV1 {
try signDeployAccountV1(classHash: classHash, calldata: calldata, salt: salt, params: StarknetDeployAccountParamsV1(nonce: .zero, maxFee: maxFee), forFeeEstimation: false)
}
/// Create and sign deploy account transaction v3
/// Avoid using this method to sign transaction for fee estimation.
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - resourceBounds: max acceptable l1 and l2 resource bounds
///
/// - Returns: Signed deploy account transaction v3
func signDeployAccountV3(classHash: Felt, calldata: StarknetCalldata, salt: Felt, resourceBounds: StarknetResourceBoundsMapping) throws -> StarknetDeployAccountTransactionV3 {
try signDeployAccountV3(classHash: classHash, calldata: calldata, salt: salt, params: StarknetDeployAccountParamsV3(nonce: .zero, resourceBounds: resourceBounds), forFeeEstimation: false)
}
/// Sign a call as invoke transaction v1
///
/// - Parameters:
/// - call: a call to be signed.
/// - params: additional params for a given transaction
/// - forFeeEstimation: Flag indicating whether the different version of transaction should be used; such transaction can only be used for fee estimation
///
/// - Returns: Signed invoke transaction v1
func signV1(call: StarknetCall, params: StarknetInvokeParamsV1, forFeeEstimation: Bool = false) async throws -> StarknetInvokeTransactionV1 {
try signV1(calls: [call], params: params, forFeeEstimation: forFeeEstimation)
}
/// Sign a call as invoke transaction v3
/// - Parameters:
/// - call: a call to be signed.
/// - params: additional params for a given transaction
/// - forFeeEstimation: Flag indicating whether the different version of transaction should be used; such transaction can only be used for fee estimation
///
/// - Returns: Signed invoke transaction v3
func signV3(call: StarknetCall, params: StarknetInvokeParamsV3, forFeeEstimation: Bool = false) throws -> StarknetInvokeTransactionV3 {
try signV3(calls: [call], params: params, forFeeEstimation: forFeeEstimation)
}
/// Execute list of calls as invoke transaction v1
///
/// - Parameters:
/// - calls: list of calls to be executed.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(calls: [StarknetCall]) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV1(calls: calls, params: StarknetOptionalInvokeParamsV1())
}
/// Execute list of calls using invoke transaction v3
///
/// - Parameters:
/// - calls: list of calls to be executed.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(calls: [StarknetCall]) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV3(calls: calls, params: StarknetOptionalInvokeParamsV3())
}
/// Execute a call as invoke transaction v1
///
/// - Parameters:
/// - call: a call to be executed.
/// - params: additional params for a given transaction
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(call: StarknetCall, params: StarknetOptionalInvokeParamsV1) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV1(calls: [call], params: params)
}
/// Execute a call as invoke transaction v3
///
/// - Parameters:
/// - call: a call to be executed.
/// - params: additional params for a given transaction
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(call: StarknetCall, params: StarknetOptionalInvokeParamsV3) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV3(calls: [call], params: params)
}
/// Execute a call as invoke transaction v1 with automatically estimated fee that will be multiplied by the specified multiplier when max fee is calculated.
///
/// - Parameters:
/// - call: a call to be executed.
/// - estimateFeeMultiplier: multiplier for the estimated fee.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(call: StarknetCall, estimateFeeMultiplier: Double) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV1(calls: [call], estimateFeeMultiplier: estimateFeeMultiplier)
}
/// Execute a call as invoke transaction v3 with automatically estimated fee that will be multiplied by the specified multipliers when resource bounds are calculated.
///
/// - Parameters:
/// - call: a call to be executed.
/// - estimateAmountMultiplier: multiplier for the estimated amount.
/// - estimateUnitPriceMultiplier: multiplier for the estimated unit price.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(call: StarknetCall, estimateAmountMultiplier: Double, estimateUnitPriceMultiplier: Double) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV3(calls: [call], estimateAmountMultiplier: estimateAmountMultiplier, estimateUnitPriceMultiplier: estimateUnitPriceMultiplier)
}
/// Execute a call as invoke transaction v1 with automatically estimated fee
///
/// - Parameters:
/// - call: a call to be executed.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV1(call: StarknetCall) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV1(calls: [call])
}
/// Execute a call as invoke transaction v3 with automatically estimated fee
///
/// - Parameters:
/// - call: a call to be executed.
///
/// - Returns: InvokeTransactionResponse, containing transaction hash of submitted transaction.
func executeV3(call: StarknetCall) async throws -> StarknetRequest<StarknetInvokeTransactionResponse> {
try await executeV3(calls: [call])
}
/// Estimate fee for a list of calls as invoke transaction v1
///
/// - Parameters:
/// - calls: list of calls, for which the fee should be estimated.
/// - nonce: nonce of the account.
/// - Returns: struct containing fee estimate
func estimateFeeV1(calls: [StarknetCall], nonce: Felt) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateFeeV1(calls: calls, nonce: nonce, skipValidate: false)
}
/// Estimate fee for a list of calls as invoke transaction v3
///
/// - Parameters:
/// - calls: list of calls, for which the fee should be estimated.
/// - nonce: nonce of the account.
/// - Returns: struct containing fee estimate
func estimateFeeV3(calls: [StarknetCall], nonce: Felt) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateFeeV3(calls: calls, nonce: nonce, skipValidate: false)
}
/// Estimate fee for a list of calls as invoke transaction v1
///
/// - Parameters:
/// - calls: list of calls, for which the fee should be estimated.
/// - skipValidate: flag indicating whether validation of the transaction should be skipped.
/// - Returns: struct containing fee estimate
func estimateFeeV1(calls: [StarknetCall], provider: StarknetProviderProtocol, skipValidate: Bool = false) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
let nonce = try await provider.send(request: getNonce())
return try await estimateFeeV1(calls: calls, nonce: nonce, skipValidate: skipValidate)
}
/// Estimate fee for a list of calls as invoke transaction v3
///
/// - Parameters:
/// - calls: list of calls, for which the fee should be estimated.
/// - skipValidate: flag indicating whether validation of the transaction should be skipped.
///
/// - Returns: struct containing fee estimate
func estimateFeeV3(calls: [StarknetCall], provider: StarknetProviderProtocol, skipValidate: Bool = false) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
let nonce = try await provider.send(request: getNonce())
return try await estimateFeeV3(calls: calls, nonce: nonce, skipValidate: skipValidate)
}
/// Estimate fee for a call as invoke transaction v1
///
/// - Parameters:
/// - call: a call for which the fee should be estimated.
/// - nonce: a nonce to be used in a transaction.
/// - skipValidate: flag indicating whether validation of the transaction should be skipped.
///
/// - Returns: struct containing fee estimate
func estimateFeeV1(call: StarknetCall, nonce: Felt, skipValidate: Bool = false) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateFeeV1(calls: [call], nonce: nonce, skipValidate: skipValidate)
}
/// Estimate fee for a call as invoke transaction v3
///
/// - Parameters:
/// - call: a call for which the fee should be estimated.
/// - nonce: a nonce to be used in a transaction.
/// - skipValidate: flag indicating whether validation of the transaction should be skipped.
///
/// - Returns: struct containing fee estimate
func estimateFeeV3(call: StarknetCall, nonce: Felt, skipValidate: Bool = false) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateFeeV3(calls: [call], nonce: nonce, skipValidate: skipValidate)
}
/// Estimate fee for a call as invoke transaction v1
///
/// - Parameters:
/// - call: a call for which the fee should be estimated.
/// - skipValidate: flag indicating whether validation of the transaction should be skipped.
///
/// - Returns: struct containing fee estimate
func estimateFeeV1(call: StarknetCall, provider: StarknetProviderProtocol, skipValidate: Bool = false) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateFeeV1(calls: [call], provider: provider, skipValidate: skipValidate)
}
/// Estimate fee for a call as invoke transaction v3
///
/// - Parameters:
/// - call: a call for which the fee should be estimated.
/// - skipValidate: flag indicating whether validation of the transaction should be skipped.
///
/// - Returns: struct containing fee estimate
func estimateFeeV3(call: StarknetCall, provider: StarknetProviderProtocol, skipValidate: Bool = false) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateFeeV3(calls: [call], provider: provider, skipValidate: skipValidate)
}
/// Estimate fee for a deploy account transaction v1
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - nonce: nonce of the account to be deployed
///
/// - Returns: struct containing fee estimate
func estimateDeployAccountFeeV1(classHash: Felt, calldata: StarknetCalldata, salt: Felt, nonce: Felt = .zero) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateDeployAccountFeeV1(classHash: classHash, calldata: calldata, salt: salt, nonce: nonce, skipValidate: false)
}
/// Estimate fee for a deploy account transaction v3
///
/// - Parameters:
/// - classHash: class hash of account to be deployed
/// - calldata: constructor calldata
/// - salt: contract salt
/// - nonce: nonce of the account to be deployed
///
/// - Returns: struct containing fee estimate
func estimateDeployAccountFeeV3(classHash: Felt, calldata: StarknetCalldata, salt: Felt, nonce: Felt = .zero) async throws -> StarknetRequest<[StarknetFeeEstimate]> {
try await estimateDeployAccountFeeV3(classHash: classHash, calldata: calldata, salt: salt, nonce: nonce, skipValidate: false)
}
}