|
| 1 | +// |
| 2 | +// APIRequest+ComputedProperties.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Yaroslav Yashin on 12.07.2022. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension APIRequest { |
| 11 | + var method: REST { |
| 12 | + switch self { |
| 13 | + default: return .POST |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + public var encodedBody: Data { |
| 18 | + let request = RequestBody(method: self.call, params: self.parameters) |
| 19 | + // this is safe to force try this here |
| 20 | + // Because request must failed to compile if it not conformable with `Encodable` protocol |
| 21 | + return try! JSONEncoder().encode(request) |
| 22 | + } |
| 23 | + |
| 24 | + var parameters: [RequestParameter] { |
| 25 | + switch self { |
| 26 | + case .gasPrice, .blockNumber, .getNetwork, .getAccounts, .getTxPoolStatus, .getTxPoolContent, .getTxPoolInspect: |
| 27 | + return [RequestParameter]() |
| 28 | + |
| 29 | + case .estimateGas(let transactionParameters, let blockNumber): |
| 30 | + return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)] |
| 31 | + |
| 32 | + case let .sendRawTransaction(hash): |
| 33 | + return [RequestParameter.string(hash)] |
| 34 | + |
| 35 | + case let .sendTransaction(transactionParameters): |
| 36 | + return [RequestParameter.transaction(transactionParameters)] |
| 37 | + |
| 38 | + case .getTransactionByHash(let hash): |
| 39 | + return [RequestParameter.string(hash)] |
| 40 | + |
| 41 | + case .getTransactionReceipt(let receipt): |
| 42 | + return [RequestParameter.string(receipt)] |
| 43 | + |
| 44 | + case .getLogs(let eventFilterParameters): |
| 45 | + return [RequestParameter.eventFilter(eventFilterParameters)] |
| 46 | + |
| 47 | + case .personalSign(let address, let string): |
| 48 | + return [RequestParameter.string(address), RequestParameter.string(string)] |
| 49 | + |
| 50 | + case .call(let transactionParameters, let blockNumber): |
| 51 | + return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)] |
| 52 | + |
| 53 | + case .getTransactionCount(let address, let blockNumber): |
| 54 | + return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)] |
| 55 | + |
| 56 | + case .getBalance(let address, let blockNumber): |
| 57 | + return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)] |
| 58 | + |
| 59 | + case .getStorageAt(let address, let bigUInt, let blockNumber): |
| 60 | + return [RequestParameter.string(address), RequestParameter.string(bigUInt.hexString), RequestParameter.string(blockNumber.stringValue)] |
| 61 | + |
| 62 | + case .getCode(let address, let blockNumber): |
| 63 | + return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)] |
| 64 | + |
| 65 | + case .getBlockByHash(let hash, let bool): |
| 66 | + return [RequestParameter.string(hash), RequestParameter.bool(bool)] |
| 67 | + |
| 68 | + case .getBlockByNumber(let block, let bool): |
| 69 | + return [RequestParameter.string(block.stringValue), RequestParameter.bool(bool)] |
| 70 | + |
| 71 | + case .feeHistory(let uInt, let blockNumber, let array): |
| 72 | + return [RequestParameter.string(uInt.hexString), RequestParameter.string(blockNumber.stringValue), RequestParameter.doubleArray(array)] |
| 73 | + |
| 74 | + case .createAccount(let string): |
| 75 | + return [RequestParameter.string(string)] |
| 76 | + |
| 77 | + case .unlockAccount(let address, let string, let uInt): |
| 78 | + return [RequestParameter.string(address), RequestParameter.string(string), RequestParameter.uint(uInt ?? 0)] |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public var call: String { |
| 83 | + switch self { |
| 84 | + case .gasPrice: return "eth_gasPrice" |
| 85 | + case .blockNumber: return "eth_blockNumber" |
| 86 | + case .getNetwork: return "net_version" |
| 87 | + case .getAccounts: return "eth_accounts" |
| 88 | + case .sendRawTransaction: return "eth_sendRawTransaction" |
| 89 | + case .sendTransaction: return "eth_sendTransaction" |
| 90 | + case .getTransactionByHash: return "eth_getTransactionByHash" |
| 91 | + case .getTransactionReceipt: return "eth_getTransactionReceipt" |
| 92 | + case .personalSign: return "eth_sign" |
| 93 | + case .getLogs: return "eth_getLogs" |
| 94 | + case .call: return "eth_call" |
| 95 | + case .estimateGas: return "eth_estimateGas" |
| 96 | + case .getTransactionCount: return "eth_getTransactionCount" |
| 97 | + case .getBalance: return "eth_getBalance" |
| 98 | + case .getStorageAt: return "eth_getStorageAt" |
| 99 | + case .getCode: return "eth_getCode" |
| 100 | + case .getBlockByHash: return "eth_getBlockByHash" |
| 101 | + case .getBlockByNumber: return "eth_getBlockByNumber" |
| 102 | + case .feeHistory: return "eth_feeHistory" |
| 103 | + |
| 104 | + case .unlockAccount: return "personal_unlockAccount" |
| 105 | + case .createAccount: return "personal_createAccount" |
| 106 | + case .getTxPoolStatus: return "txpool_status" |
| 107 | + case .getTxPoolContent: return "txpool_content" |
| 108 | + case .getTxPoolInspect: return "txpool_inspect" |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments