154 lines
4.6 KiB
Swift
154 lines
4.6 KiB
Swift
//
|
|
// NetworkServiceAPIActions.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 05.11.2020.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import Alamofire
|
|
import Foundation
|
|
|
|
extension Network.Service {
|
|
class Actions: Common.Service.Provider {}
|
|
}
|
|
|
|
extension Network.Service.Actions {
|
|
func fetch<T: Decodable>(
|
|
account: String,
|
|
limit: Int,
|
|
filter: String? = nil,
|
|
sort: Sort = .desc,
|
|
skip: Int? = nil,
|
|
after: String? = nil,
|
|
type: T.Type,
|
|
completion: @escaping (([Action<T>],Int,Error?) -> Void)
|
|
) {
|
|
var parameters = [String: Any]()
|
|
parameters["account"] = account
|
|
parameters["limit"] = limit
|
|
parameters["filter"] = filter
|
|
parameters["skip"] = skip
|
|
parameters["sort"] = sort.rawValue
|
|
parameters["after"] = after
|
|
AF.request(
|
|
"\(ApplicationEnvironment.shared().current.hyperion.orCreate(""))/v2/history/get_actions",
|
|
method: .get,
|
|
parameters: parameters
|
|
).responseDecodable(of: Response<T>.self) { completion($0.value?.actions ?? [], $0.value?.total.value ?? 0, $0.error) }
|
|
}
|
|
}
|
|
|
|
extension Network.Service.Actions {
|
|
func fetchNode(
|
|
account: String,
|
|
limit: Int,
|
|
skip: Int? = nil,
|
|
completion: @escaping (([NodeAction],Error?) -> Void)
|
|
) {
|
|
var parameters = [String: Any]()
|
|
parameters["account_name"] = account
|
|
parameters["offset"] = -limit
|
|
parameters["pos"] = skip
|
|
AF.request(
|
|
"https://eos.greymass.com/v1/history/get_actions",
|
|
method: .get,
|
|
parameters: parameters
|
|
).responseDecodable(of: NodeResponse.self) { completion($0.value?.actions ?? [], $0.error) }
|
|
}
|
|
}
|
|
|
|
extension Network.Service.Actions {
|
|
struct Response<T: Decodable>: Decodable {
|
|
let total: Total
|
|
let actions: [Action<T>]
|
|
}
|
|
struct Act<T: Decodable>: Decodable {
|
|
let data: T
|
|
let name: String
|
|
let account: String
|
|
let authorization: [NodeActAuthorization]
|
|
}
|
|
// swiftlint:disable identifier_name
|
|
struct Action<T: Decodable>: Decodable {
|
|
let act: Act<T>
|
|
let trx_id: String
|
|
var timestamp: String
|
|
let global_sequence: Int
|
|
let block_num: Int
|
|
}
|
|
struct Total: Decodable {
|
|
let value: Int
|
|
}
|
|
|
|
// MARK: - Node Structures
|
|
struct NodeResponse: Decodable {
|
|
let actions: [NodeAction]
|
|
}
|
|
// swiftlint:disable identifier_name
|
|
struct NodeAction: Decodable {
|
|
let account_action_seq: Int
|
|
let action_trace: NodeActionTrace
|
|
let global_action_seq: Int
|
|
let irreversible: Bool
|
|
}
|
|
// swiftlint:disable identifier_name
|
|
struct NodeActionTrace: Decodable {
|
|
let act: NodeAct
|
|
let block_num: Int
|
|
let block_time: String
|
|
let receiver: String?
|
|
let trx_id: String
|
|
}
|
|
struct NodeAct: Decodable {
|
|
let data: NodeData
|
|
let name: String
|
|
let authorization: [NodeActAuthorization]
|
|
let account: String
|
|
}
|
|
struct NodeData: Decodable {
|
|
var data: [String: Any]
|
|
|
|
private struct DynamicCodingKeys: CodingKey {
|
|
// Use for string-keyed dictionary
|
|
var stringValue: String
|
|
init?(stringValue: String) {
|
|
self.stringValue = stringValue
|
|
}
|
|
|
|
// Use for integer-keyed dictionary
|
|
var intValue: Int?
|
|
init?(intValue: Int) {
|
|
// We are not using this, thus just return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
init(from decoder: Decoder) throws {
|
|
// Create a decoding container using DynamicCodingKeys
|
|
// The container will contain all the JSON first level key
|
|
let container = try decoder.container(keyedBy: DynamicCodingKeys.self)
|
|
|
|
var tempData = [String: Any]()
|
|
|
|
// Loop through each key
|
|
for key in container.allKeys {
|
|
if let value = try? container.decode(String.self, forKey: key) {
|
|
tempData[key.stringValue] = value
|
|
} else if let value = try? container.decode(Int.self, forKey: key) {
|
|
tempData[key.stringValue] = value
|
|
} else if let value = try? container.decode(NodeData.self, forKey: key) {
|
|
tempData[key.stringValue] = value.data
|
|
}
|
|
}
|
|
|
|
// Finish decoding all Student objects. Thus assign tempArray to array.
|
|
data = tempData
|
|
}
|
|
}
|
|
struct NodeActAuthorization: Codable {
|
|
let actor: String
|
|
let permission: String
|
|
}
|
|
}
|