-
Notifications
You must be signed in to change notification settings - Fork 169
/
ZKSyncTransaction.swift
228 lines (204 loc) · 6.88 KB
/
ZKSyncTransaction.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
//
// web3.swift
// Copyright © 2022 Argent Labs Limited. All rights reserved.
//
import web3
import BigInt
import Foundation
import GenericJSON
// to be filled in by client
public struct ZKSyncTransaction: Equatable {
public static let eip712Type: UInt8 = 0x71
public static let defaultGasPerPubDataLimit: BigUInt = 50000
public let txType: UInt8 = Self.eip712Type
public var from: EthereumAddress
public var to: EthereumAddress
public var value: BigUInt
public var data: Data
public var chainId: Int?
public var nonce: Int?
public var gasPrice: BigUInt?
public var gasLimit: BigUInt?
public var gasPerPubData: BigUInt
public var maxFeePerGas: BigUInt?
public var maxPriorityFeePerGas: BigUInt?
public var paymasterParams: PaymasterParams
public init(
from: EthereumAddress,
to: EthereumAddress,
value: BigUInt,
data: Data,
chainId: Int? = nil,
nonce: Int? = nil,
gasPrice: BigUInt? = nil,
gasLimit: BigUInt? = nil,
gasPerPubData: BigUInt = ZKSyncTransaction.defaultGasPerPubDataLimit,
maxFeePerGas: BigUInt? = nil,
maxPriorityFeePerGas: BigUInt? = nil,
paymasterParams: PaymasterParams = .none
) {
self.from = from
self.to = to
self.value = value
self.data = data
self.chainId = chainId
self.nonce = nonce
self.gasPrice = gasPrice
self.gasLimit = gasLimit
self.gasPerPubData = gasPerPubData
self.maxFeePerGas = maxFeePerGas
self.maxPriorityFeePerGas = maxPriorityFeePerGas
self.paymasterParams = paymasterParams
}
public struct PaymasterParams: Equatable {
public var paymaster: EthereumAddress
public var input: Data
public init(
paymaster: EthereumAddress,
input: Data
) {
self.paymaster = paymaster
self.input = input
}
public var isEmpty: Bool {
self.paymaster == .zero
}
public static let none: PaymasterParams = .init(paymaster: .zero, input: Data())
}
public var maxFee: BigUInt {
maxFeePerGas ?? gasPrice ?? 0
}
public var maxPriorityFee: BigUInt {
maxPriorityFeePerGas ?? maxFee
}
public var paymaster: EthereumAddress {
paymasterParams.paymaster
}
var paymasterInput: Data {
paymasterParams.input
}
public var eip712Representation: TypedData {
let decoder = JSONDecoder()
let eip712 = try! decoder.decode(TypedData.self, from: eip712JSON)
return eip712
}
private var eip712JSON: Data {
"""
{
"types": {
"EIP712Domain": [
{"name": "name", "type": "string"},
{"name": "version", "type": "string"},
{"name": "chainId", "type": "uint256"}
],
"Transaction": [
{"name": "txType","type": "uint256"},
{"name": "from","type": "uint256"},
{"name": "to","type": "uint256"},
{"name": "gasLimit","type": "uint256"},
{"name": "gasPerPubdataByteLimit","type": "uint256"},
{"name": "maxFeePerGas", "type": "uint256"},
{"name": "maxPriorityFeePerGas", "type": "uint256"},
{"name": "paymaster", "type": "uint256"},
{"name": "nonce","type": "uint256"},
{"name": "value","type": "uint256"},
{"name": "data","type": "bytes"},
{"name": "factoryDeps","type": "bytes32[]"},
{"name": "paymasterInput", "type": "bytes"}
]
},
"primaryType": "Transaction",
"domain": {
"name": "zkSync",
"version": "2",
"chainId": \(chainId!)
},
"message": {
"txType" : \(txType),
"from" : "\(from.asNumber()!.description)",
"to" : "\(to.asNumber()!.description)",
"gasLimit" : "\(gasLimit!.description)",
"gasPerPubdataByteLimit" : "\(gasPerPubData.description)",
"maxFeePerGas" : "\(maxFee.description)",
"maxPriorityFeePerGas" : "\(maxPriorityFee.description)",
"paymaster" : "\(paymaster.asNumber()!.description)",
"nonce" : \(nonce!),
"value" : "\(value.description)",
"data" : "\(data.web3.hexString)",
"factoryDeps" : [],
"paymasterInput" : "\(paymasterInput.web3.hexString)"
}
}
""".data(using: .utf8)!
}
}
public struct ZKSyncSignedTransaction {
public let transaction: ZKSyncTransaction
public let signature: Signature
public init(
transaction: ZKSyncTransaction,
signature: Signature
) {
self.transaction = transaction
self.signature = signature
}
public var raw: Data? {
guard transaction.nonce != nil, transaction.chainId != nil,
transaction.gasPrice != nil, transaction.gasLimit != nil else {
return nil
}
var txArray: [Any?] = [
transaction.nonce,
transaction.maxPriorityFee,
transaction.maxFee,
transaction.gasLimit,
transaction.to,
transaction.value,
transaction.data
]
txArray.append(transaction.chainId)
txArray.append(Data())
txArray.append(Data())
txArray.append(transaction.chainId)
txArray.append(transaction.from)
txArray.append(transaction.gasPerPubData)
// TODO: factorydeps
txArray.append([])
txArray.append(signature.flattened)
if transaction.paymasterParams.isEmpty {
txArray.append([])
} else {
txArray.append([
transaction.paymaster,
transaction.paymasterInput
])
}
return RLP.encode(txArray).flatMap {
Data([transaction.txType]) + $0.web3.bytes
}
}
public var hash: Data? {
raw?.web3.keccak256
}
}
extension ABIFunction {
public func zkTransaction(
from: EthereumAddress,
value: BigUInt? = nil,
gasPrice: BigUInt? = nil,
gasLimit: BigUInt? = nil,
feeToken: EthereumAddress = .zero
) throws -> ZKSyncTransaction {
let encoder = ABIFunctionEncoder(Self.name)
try self.encode(to: encoder)
let data = try encoder.encoded()
return ZKSyncTransaction(
from: from,
to: contract,
value: value ?? 0,
data: data,
gasPrice: self.gasPrice ?? gasPrice ?? 0,
gasLimit: self.gasLimit ?? gasLimit ?? 0
)
}
}