|
| 1 | +// |
| 2 | +// BlockExporter+GetTransactionHistory.swift |
| 3 | +// web3swift-iOS |
| 4 | +// |
| 5 | +// Created by Георгий Фесенко on 19/06/2018. |
| 6 | +// Copyright © 2018 Bankex Foundation. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import PromiseKit |
| 11 | +import BigInt |
| 12 | + |
| 13 | +extension BlockExplorer { |
| 14 | + public func getTransactionHistory(address: EthereumAddress, tokenName name: String = "Ether", page: Int = 1, size: Int = 50) -> Promise<[TransactionHistoryRecord]> { |
| 15 | + let address = address.address |
| 16 | + return getTransactionsHistory(address: address, tokenName: name, page: page, size: size) |
| 17 | + } |
| 18 | + |
| 19 | + public func getTransactionsHistory(address publicAddress: String, tokenName name: String = "Ether", page: Int = 1, size: Int = 50) -> Promise<[TransactionHistoryRecord]> { |
| 20 | + |
| 21 | + //Configuring http request |
| 22 | + let listId: ListId = (name == "Ether") ? .listOfETH : .listOfTokens |
| 23 | + let url = URL(string: urlStringList)! |
| 24 | + var request = URLRequest(url: url) |
| 25 | + request.httpMethod = "POST" |
| 26 | + let internalParams = InternalParam(entityId: publicAddress, page: page, size: size) |
| 27 | + let parameters = Body(listId: listId.rawValue, moduleId: "address", params: internalParams) |
| 28 | + |
| 29 | + return Promise<[TransactionHistoryRecord]> {seal in |
| 30 | + do { |
| 31 | + request.httpBody = try JSONEncoder().encode(parameters) |
| 32 | + } catch { |
| 33 | + seal.reject(error) |
| 34 | + } |
| 35 | + //Performing the request |
| 36 | + let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in |
| 37 | + if let error = error { seal.reject(error); return } |
| 38 | + guard let data = data else { return } |
| 39 | + |
| 40 | + do { |
| 41 | + //Parsing JSON |
| 42 | + let jsonResponce = try JSONDecoder().decode(Response.self, from: data) |
| 43 | + if listId == .listOfETH { |
| 44 | + seal.fulfill(jsonResponce.rows) |
| 45 | + } else { |
| 46 | + seal.fulfill( jsonResponce.rows.filter { $0.token.name == name } ) |
| 47 | + } |
| 48 | + } catch { |
| 49 | + seal.reject(error) |
| 50 | + } |
| 51 | + }) |
| 52 | + task.resume() |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +//MARK: - Decodable structures |
| 58 | + |
| 59 | +public struct Response: Decodable { |
| 60 | + let rows: [TransactionHistoryRecord] |
| 61 | + let head: Head |
| 62 | +} |
| 63 | + |
| 64 | +public struct TransactionHistoryRecord: Decodable { |
| 65 | + |
| 66 | + let id: String |
| 67 | + let hash: Data |
| 68 | + let block: BigUInt |
| 69 | + let addressFrom: EthereumAddress |
| 70 | + let addressTo: EthereumAddress |
| 71 | + let isoTime: String |
| 72 | + let type: TransactionType |
| 73 | + let status: TransactionStatus |
| 74 | + let error: String |
| 75 | + let isContract: Bool |
| 76 | + let isInner: Bool |
| 77 | + let value: BigUInt // in wei |
| 78 | + let token: Token |
| 79 | + let txFee: BigUInt // in wei |
| 80 | + let gasUsed: BigUInt // in wei |
| 81 | + let gasCost: BigUInt // in wei |
| 82 | + |
| 83 | + public init(from decoder: Decoder) throws { |
| 84 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 85 | + id = try container.decode(String.self, forKey: CodingKeys.id) |
| 86 | + let hashString = try container.decode(String.self, forKey: CodingKeys.hash) |
| 87 | + guard let hashData = hashString.interpretAsBinaryData() else { |
| 88 | + throw Web3Error.transactionSerializationError |
| 89 | + } |
| 90 | + hash = hashData |
| 91 | + let intBlock = try container.decode(UInt64.self, forKey: CodingKeys.block) |
| 92 | + block = BigUInt.init(integerLiteral: intBlock) |
| 93 | + let stringAddressFrom = try container.decode(String.self, forKey: CodingKeys.addressFrom) |
| 94 | + guard let nativeAddressFrom = EthereumAddress(stringAddressFrom, type: .normal, ignoreChecksum: true) else { |
| 95 | + throw Web3Error.transactionSerializationError |
| 96 | + } |
| 97 | + addressFrom = nativeAddressFrom |
| 98 | + let stringAddressTo = try container.decode(String.self, forKey: CodingKeys.addressTo) |
| 99 | + |
| 100 | + guard let nativeAddressTo = EthereumAddress(stringAddressTo, type: .normal, ignoreChecksum: true) else { |
| 101 | + throw Web3Error.transactionSerializationError |
| 102 | + } |
| 103 | + addressTo = nativeAddressTo |
| 104 | + isoTime = try container.decode(String.self, forKey: CodingKeys.isoTime) |
| 105 | + let stringType = try container.decode(String.self, forKey: CodingKeys.type) |
| 106 | + var nativeType: TransactionType |
| 107 | + switch stringType { |
| 108 | + case "tx": |
| 109 | + nativeType = .tx |
| 110 | + case "call": |
| 111 | + nativeType = .call |
| 112 | + case "create": |
| 113 | + nativeType = .create |
| 114 | + case "suicide": |
| 115 | + nativeType = .suicide |
| 116 | + case "token": |
| 117 | + nativeType = .token |
| 118 | + default: |
| 119 | + nativeType = .tx |
| 120 | + } |
| 121 | + type = nativeType |
| 122 | + |
| 123 | + let intStatus = try container.decode(Int.self, forKey: CodingKeys.status) |
| 124 | + status = intStatus == 0 ? .failed : .succeeded |
| 125 | + error = try container.decode(String.self, forKey: CodingKeys.error) |
| 126 | + let intIsContract = try container.decode(Int.self, forKey: CodingKeys.isContract) |
| 127 | + isContract = intIsContract == 0 ? false : true |
| 128 | + let intIsInner = try container.decode(Int.self, forKey: CodingKeys.isInner) |
| 129 | + isInner = intIsInner == 0 ? false : true |
| 130 | + let stringValue = try container.decode(String.self, forKey: CodingKeys.value) |
| 131 | + guard let uintValue = UInt64(stringValue, radix: 16) else { |
| 132 | + throw Web3Error.transactionSerializationError |
| 133 | + } |
| 134 | + value = BigUInt(integerLiteral: uintValue) |
| 135 | + token = try container.decode(Token.self, forKey: CodingKeys.token) |
| 136 | + let stringTxFee = try container.decode(String.self, forKey: CodingKeys.txFee) |
| 137 | + guard let uintTxFee = UInt64(stringTxFee, radix: 16) else { |
| 138 | + throw Web3Error.transactionSerializationError |
| 139 | + } |
| 140 | + |
| 141 | + txFee = BigUInt.init(integerLiteral: uintTxFee) |
| 142 | + let intGasUsed = try container.decode(UInt64.self, forKey: CodingKeys.gasUsed) |
| 143 | + gasUsed = BigUInt(integerLiteral: intGasUsed) |
| 144 | + let intGasCost = try container.decode(UInt64.self, forKey: CodingKeys.gasCost) |
| 145 | + gasCost = BigUInt(integerLiteral: intGasCost) |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + enum CodingKeys: String, CodingKey { |
| 150 | + case id = "_id" |
| 151 | + case hash |
| 152 | + case block |
| 153 | + case addressFrom = "addrfrom" |
| 154 | + case addressTo = "addrto" |
| 155 | + case isoTime = "isotime" |
| 156 | + case type |
| 157 | + case status |
| 158 | + case error |
| 159 | + case isContract = "iscontract" |
| 160 | + case isInner = "isinner" |
| 161 | + case value |
| 162 | + case token |
| 163 | + case txFee = "txfee" |
| 164 | + case gasUsed = "gasused" |
| 165 | + case gasCost = "gascost" |
| 166 | + } |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | +public struct Token: Decodable { |
| 171 | + let address: EthereumAddress? |
| 172 | + let name: String |
| 173 | + let symbol: String |
| 174 | + let decimal: Int |
| 175 | + |
| 176 | + enum CodingKeys: String, CodingKey { |
| 177 | + case address = "addr" |
| 178 | + case name |
| 179 | + case symbol = "smbl" |
| 180 | + case decimal = "dcm" |
| 181 | + } |
| 182 | + |
| 183 | + public init(from decoder: Decoder) throws { |
| 184 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 185 | + let stringAddress = try container.decode(String.self, forKey: CodingKeys.address) |
| 186 | + if !stringAddress.isEmpty { |
| 187 | + guard let nativeAddress = EthereumAddress(stringAddress, type: .normal, ignoreChecksum: true) else { |
| 188 | + throw Web3Error.transactionSerializationError |
| 189 | + } |
| 190 | + address = nativeAddress |
| 191 | + } else { |
| 192 | + address = nil |
| 193 | + } |
| 194 | + name = try container.decode(String.self, forKey: .name) |
| 195 | + symbol = try container.decode(String.self, forKey: .symbol) |
| 196 | + decimal = try container.decode(Int.self, forKey: .decimal) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +public struct Head: Decodable { |
| 201 | + let totalEntities: Int |
| 202 | + let pageNumber: Int |
| 203 | + let pageSize: Int |
| 204 | + let listId: String |
| 205 | + let moduleId: String |
| 206 | + let entityId: String |
| 207 | + let updateTime: String |
| 208 | +} |
| 209 | + |
| 210 | +//MARK: - enums |
| 211 | +public enum TransactionType { |
| 212 | + case tx, call, create, suicide, token |
| 213 | +} |
| 214 | + |
| 215 | +public enum TransactionStatus { |
| 216 | + case failed, succeeded |
| 217 | +} |
| 218 | + |
| 219 | +public enum ListId: String { |
| 220 | + case listOfETH |
| 221 | + case listOfTokens |
| 222 | +} |
| 223 | + |
| 224 | +//MARK: - HTTP body structures |
| 225 | +public struct Body:Codable { |
| 226 | + let listId:String |
| 227 | + let moduleId:String |
| 228 | + let params:InternalParam |
| 229 | +} |
| 230 | + |
| 231 | +public struct InternalParam:Codable { |
| 232 | + let entityId:String |
| 233 | + let page:Int |
| 234 | + let size:Int |
| 235 | +} |
| 236 | + |
| 237 | + |
| 238 | + |
0 commit comments