Add get_account_history api request

This commit is contained in:
Johan Nordberg 2018-06-20 13:15:44 +02:00
parent 206b456e34
commit a5da299f09
2 changed files with 52 additions and 0 deletions

View File

@ -1,6 +1,7 @@
/// Steem RPC requests and responses.
/// - Author: Johan Nordberg <johan@steemit.com>
import AnyCodable
import Foundation
/// Steem RPC API request- and response-types.
@ -138,4 +139,44 @@ public struct API {
self.params = RequestParams([names])
}
}
public struct OperationObject: Decodable {
public let trxId: Data
public let block: UInt32
public let trxInBlock: UInt32
public let opInTrx: UInt32
public let virtualOp: UInt32
public let timestamp: Date
private let op: AnyOperation
public var operation: OperationType {
return self.op.operation
}
}
public struct AccountHistoryObject: Decodable {
public let id: UInt32
public let value: OperationObject
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
self.id = try container.decode(UInt32.self)
self.value = try container.decode(OperationObject.self)
}
}
public struct GetAccountHistory: Request, Encodable {
public typealias Response = [AccountHistoryObject]
public let method = "get_account_history"
public var params: RequestParams<AnyEncodable>? {
return RequestParams([AnyEncodable(self.account), AnyEncodable(self.from), AnyEncodable(self.limit)])
}
public var account: String
public var from: Int
public var limit: Int
public init(account: String, from: Int = -1, limit: Int = 100) {
self.account = account
self.from = from
self.limit = limit
}
}
}

View File

@ -116,4 +116,15 @@ class ClientTest: XCTestCase {
XCTAssertEqual(account.name, "almost-digital")
XCTAssertEqual(account.created, Date(timeIntervalSince1970: 1_496_691_060))
}
func testGetAccountHistory() throws {
let req = API.GetAccountHistory(account: "almost-digital", from: 0, limit: 0)
let result = try client.sendSynchronous(req)
guard let r = result?.first else {
XCTFail("No results returned")
return
}
let createOp = r.value.operation as? Steem.Operation.AccountCreateWithDelegation
XCTAssertEqual(createOp?.newAccountName, "almost-digital")
}
}