|
7 | 7 | import Foundation |
8 | 8 | import BigInt |
9 | 9 |
|
10 | | -extension TransactionOptions: Decodable { |
11 | | - enum CodingKeys: String, CodingKey { |
12 | | - case to |
13 | | - case from |
14 | | - case gasPrice |
15 | | - case gas |
16 | | - case value |
17 | | - case nonce |
18 | | - case callOnBlock |
19 | | - } |
20 | | - |
21 | | - public init(from decoder: Decoder) throws { |
22 | | - let container = try decoder.container(keyedBy: CodingKeys.self) |
23 | | - |
24 | | - if let gasLimit = try? container.decodeHex(BigUInt.self, forKey: .gas) { |
25 | | - self.gasLimit = .manual(gasLimit) |
26 | | - } else { |
27 | | - self.gasLimit = .automatic |
28 | | - } |
29 | | - |
30 | | - if let gasPrice = try? container.decodeHex(BigUInt.self, forKey: .gasPrice) { |
31 | | - self.gasPrice = .manual(gasPrice) |
32 | | - } else { |
33 | | - self.gasPrice = .automatic |
34 | | - } |
35 | | - |
36 | | - let toString = try container.decode(String?.self, forKey: .to) |
37 | | - var to: EthereumAddress? |
38 | | - if toString == nil || toString == "0x" || toString == "0x0" { |
39 | | - to = EthereumAddress.contractDeploymentAddress() |
40 | | - } else { |
41 | | - guard let addressString = toString else {throw Web3Error.dataError} |
42 | | - guard let ethAddr = EthereumAddress(addressString) else {throw Web3Error.dataError} |
43 | | - to = ethAddr |
44 | | - } |
45 | | - self.to = to |
46 | | - let from = try container.decodeIfPresent(EthereumAddress.self, forKey: .to) |
47 | | - // var from: EthereumAddress? |
48 | | - // if fromString != nil { |
49 | | - // guard let ethAddr = EthereumAddress(toString) else {throw Web3Error.dataError} |
50 | | - // from = ethAddr |
51 | | - // } |
52 | | - self.from = from |
53 | | - |
54 | | - self.value = try container.decodeHex(BigUInt.self, forKey: .value) |
55 | | - |
56 | | - if let nonce = try? container.decodeHex(BigUInt.self, forKey: .nonce) { |
57 | | - self.nonce = .manual(nonce) |
58 | | - } else { |
59 | | - self.nonce = .pending |
60 | | - } |
61 | | - |
62 | | - if let callOnBlock = try? container.decodeHex(BigUInt.self, forKey: .callOnBlock) { |
63 | | - self.callOnBlock = .exactBlockNumber(callOnBlock) |
64 | | - } else { |
65 | | - self.callOnBlock = .pending |
66 | | - } |
67 | | - } |
68 | | -} |
69 | | - |
70 | | -extension EthereumTransaction: Decodable { |
71 | | - enum CodingKeys: String, CodingKey { |
72 | | - case to |
73 | | - case data |
74 | | - case input |
75 | | - case nonce |
76 | | - case v |
77 | | - case r |
78 | | - case s |
79 | | - case value |
80 | | - case type // present in EIP-1559 transaction objects |
81 | | - } |
82 | | - |
83 | | - public init(from decoder: Decoder) throws { |
84 | | - let options = try TransactionOptions(from: decoder) |
85 | | - let container = try decoder.container(keyedBy: CodingKeys.self) |
86 | | - |
87 | | - if let data = try? container.decodeHex(Data.self, forKey: .data) { |
88 | | - self.data = data |
89 | | - } else { |
90 | | - guard let data = try? container.decodeHex(Data.self, forKey: .input) else { throw Web3Error.dataError } |
91 | | - self.data = data |
92 | | - } |
93 | | - |
94 | | - nonce = try container.decodeHex(BigUInt.self, forKey: .nonce) |
95 | | - v = try container.decodeHex(BigUInt.self, forKey: .v) |
96 | | - r = try container.decodeHex(BigUInt.self, forKey: .r) |
97 | | - s = try container.decodeHex(BigUInt.self, forKey: .s) |
98 | | - |
99 | | - guard let to = options.to, |
100 | | - let gasLimit = options.gasLimit, |
101 | | - let gasPrice = options.gasPrice else { throw Web3Error.dataError } |
102 | | - |
103 | | - self.to = to |
104 | | - self.value = options.value |
105 | | - |
106 | | - switch gasPrice { |
107 | | - case let .manual(gasPriceValue): |
108 | | - self.gasPrice = gasPriceValue |
109 | | - default: |
110 | | - self.gasPrice = 5000000000 |
111 | | - } |
112 | | - |
113 | | - switch gasLimit { |
114 | | - case let .manual(gasLimitValue): |
115 | | - self.gasLimit = gasLimitValue |
116 | | - default: |
117 | | - self.gasLimit = 21000 |
118 | | - } |
119 | | - |
120 | | - let inferedChainID = self.inferedChainID |
121 | | - if self.inferedChainID != nil && self.v >= BigUInt(37) { |
122 | | - self.chainID = inferedChainID |
123 | | - } |
124 | | - } |
125 | | -} |
126 | | - |
127 | 10 | public struct TransactionDetails: Decodable { |
128 | 11 | public var blockHash: Data? |
129 | 12 | public var blockNumber: BigUInt? |
|
0 commit comments