List transactions newest first on both chains
This commit is contained in:
@@ -168,6 +168,33 @@ final class MoneroService {
|
||||
return response["mnemonic"] as? String ?? ""
|
||||
}
|
||||
|
||||
/// Block time for a height. wallet2 keeps no timestamp for transfers, so the
|
||||
/// only way to date a Monero transaction is to ask the daemon for the block
|
||||
/// it landed in.
|
||||
func blockTimestamp(height: UInt64) async throws -> Date? {
|
||||
guard let url = URL(string: "http://\(Self.daemonHost):18081/json_rpc") else { return nil }
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.timeoutInterval = 15
|
||||
request.httpBody = try JSONSerialization.data(withJSONObject: [
|
||||
"jsonrpc": "2.0",
|
||||
"id": "koins",
|
||||
"method": "get_block_header_by_height",
|
||||
"params": ["height": height],
|
||||
])
|
||||
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
guard let http = response as? HTTPURLResponse, (200..<300).contains(http.statusCode),
|
||||
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
|
||||
let result = json["result"] as? [String: Any],
|
||||
let header = result["block_header"] as? [String: Any],
|
||||
let timestamp = (header["timestamp"] as? NSNumber)?.doubleValue, timestamp > 0 else {
|
||||
return nil
|
||||
}
|
||||
return Date(timeIntervalSince1970: timestamp)
|
||||
}
|
||||
|
||||
// MARK: - Reads
|
||||
|
||||
func balance() async throws -> Balance {
|
||||
@@ -201,6 +228,21 @@ final class MoneroService {
|
||||
note: (entry["note"] as? String).flatMap { $0.isEmpty ? nil : $0 },
|
||||
weight: (entry["weight"] as? NSNumber)?.uint64Value)
|
||||
}
|
||||
.sorted { newestFirst($0, $1) }
|
||||
}
|
||||
|
||||
/// Newest first. wallet2 gives transfers no timestamp, but the block height is
|
||||
/// chronological and always present, so that is the ordering key; anything
|
||||
/// still in the pool (height 0) belongs at the top.
|
||||
private func newestFirst(_ left: TransactionRecord, _ right: TransactionRecord) -> Bool {
|
||||
func key(_ record: TransactionRecord) -> UInt64 {
|
||||
guard let height = record.height, height > 0 else { return .max }
|
||||
return height
|
||||
}
|
||||
let leftKey = key(left)
|
||||
let rightKey = key(right)
|
||||
if leftKey != rightKey { return leftKey > rightKey }
|
||||
return (left.timestamp ?? .distantFuture) > (right.timestamp ?? .distantFuture)
|
||||
}
|
||||
|
||||
// MARK: - Accounts
|
||||
|
||||
Reference in New Issue
Block a user