Skip to content

Commit 327bce3

Browse files
committed
Merge branch 'feature/mainNetUpdates' into develop
2 parents 5755cb5 + bb4e198 commit 327bce3

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

Adamant/Models/ChatType.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import Foundation
1010

11+
/// - messageExpensive: Old message type, with 0.005 transaction fee
12+
/// - message: new and main message type, with 0.001 transaction fee
1113
enum ChatType: Int, Codable {
12-
case message = 0
14+
case messageOld = 0
15+
case message = 1
1316
}

Adamant/ServiceProtocols/DataProviders/ChatsProvider.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum ChatsProviderResult {
1717
enum ChatsProviderError: Error {
1818
case notLogged
1919
case messageNotValid(ValidateMessageResult)
20+
case notEnoughtMoneyToSend
2021
case serverError(Error)
2122
case accountNotFound(String)
2223
case dependencyError(String)

Adamant/ServiceProtocols/FeeCalculator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
import Foundation
1010

1111
protocol FeeCalculator {
12-
func estimatedFeeFor(message: String) -> UInt
12+
func estimatedFeeFor(message: AdamantMessage) -> UInt
1313
func estimatedFeeFor(transfer: UInt) -> UInt
1414
}

Adamant/Services/ApiService/AdamantApi+Chats.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ extension AdamantApiService.ApiCommands {
2020
extension AdamantApiService {
2121
func getChatTransactions(account: String, height: Int?, offset: Int?, completion: @escaping (ApiServiceResult<[Transaction]>) -> Void) {
2222
// MARK: 1. Prepare params
23-
var queryItems: [URLQueryItem] = [URLQueryItem(name: "isIn", value: account)]
23+
var queryItems: [URLQueryItem] = [URLQueryItem(name: "isIn", value: account),
24+
URLQueryItem(name: "orderBy", value: "timestamp:desc")]
2425
if let height = height, height > 0 { queryItems.append(URLQueryItem(name: "fromHeight", value: String(height))) }
2526
if let offset = offset { queryItems.append(URLQueryItem(name: "offset", value: String(offset))) }
2627

Adamant/Services/DataProviders/AdamantChatsProvider.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AdamantChatsProvider: ChatsProvider {
1616
var stack: CoreDataStack!
1717
var adamantCore: AdamantCore!
1818
var accountsProvider: AccountsProvider!
19+
var feeCalculator: FeeCalculator!
1920

2021
// MARK: Properties
2122
private(set) var state: State = .empty
@@ -154,6 +155,11 @@ extension AdamantChatsProvider {
154155
return
155156
}
156157

158+
guard loggedAccount.balance >= feeCalculator.estimatedFeeFor(message: message) else {
159+
completion(.error(.notEnoughtMoneyToSend))
160+
return
161+
}
162+
157163
switch validateMessage(message) {
158164
case .isValid:
159165
break

Adamant/Services/HardFeeCalculator.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99
import Foundation
1010

1111
class HardFeeCalculator: FeeCalculator {
12-
func estimatedFeeFor(message: String) -> UInt {
13-
return AdamantUtilities.from(double: ceil(Double(message.count) / 255.0) * 0.005)
12+
func estimatedFeeFor(message: AdamantMessage) -> UInt {
13+
switch message {
14+
case .text(let text):
15+
return AdamantUtilities.from(double: ceil(Double(text.count) / 255.0) * 0.001)
16+
}
1417
}
1518

1619
func estimatedFeeFor(transfer: UInt) -> UInt {
1720
return AdamantUtilities.from(double: 0.5)
1821
}
19-
20-
2122
}

Adamant/Stories/Chats/ChatViewController.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class ChatViewController: MessagesViewController {
118118

119119
if let delegate = delegate, let address = chatroom.partner?.address, let message = delegate.getPreservedMessageFor(address: address, thenRemoveIt: true) {
120120
messageInputBar.inputTextView.text = message
121-
setEstimatedFee(feeCalculator.estimatedFeeFor(message: message))
121+
setEstimatedFee(feeCalculator.estimatedFeeFor(message: AdamantMessage.text(message)))
122122
}
123123
}
124124

@@ -345,6 +345,10 @@ extension ChatViewController: MessageInputBarDelegate {
345345
case .internalError(let error): message = "Internal error: \(error)"
346346
case .notLogged: message = "Internal error: User not logged."
347347
case .serverError(let error): message = "Server error: \(error)"
348+
349+
case .notEnoughtMoneyToSend:
350+
message = "Not enough money to send a message."
351+
348352
case .messageNotValid(let problem):
349353
switch problem {
350354
case .tooLong:
@@ -369,7 +373,7 @@ extension ChatViewController: MessageInputBarDelegate {
369373

370374
func messageInputBar(_ inputBar: MessageInputBar, textViewTextDidChangeTo text: String) {
371375
if text.count > 0 {
372-
let fee = feeCalculator.estimatedFeeFor(message: text)
376+
let fee = feeCalculator.estimatedFeeFor(message: .text(text))
373377
setEstimatedFee(fee)
374378
} else {
375379
setEstimatedFee(0)

0 commit comments

Comments
 (0)