Skip to content

Commit 29b0b3b

Browse files
[trello.com/c/An2zqhSr] Feat: add public message in direct transfer for KLY
1 parent 89d02f4 commit 29b0b3b

22 files changed

+215
-26
lines changed

Adamant/Modules/Wallets/Bitcoin/BtcTransferViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ final class BtcTransferViewController: TransferViewControllerBase {
4242
let transaction = try await service.createTransaction(
4343
recipient: recipient,
4444
amount: amount,
45-
fee: transactionFee
45+
fee: transactionFee,
46+
comment: nil
4647
)
4748

4849
if await !doesNotContainSendingTx() {

Adamant/Modules/Wallets/Bitcoin/BtcWalletService+Send.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ extension BtcWalletService: WalletServiceTwoStepSend {
1414
typealias T = BitcoinKit.Transaction
1515

1616
// MARK: Create & Send
17-
func createTransaction(recipient: String, amount: Decimal, fee: Decimal) async throws -> BitcoinKit.Transaction {
17+
func createTransaction(
18+
recipient: String,
19+
amount: Decimal,
20+
fee: Decimal,
21+
comment: String?
22+
) async throws -> BitcoinKit.Transaction {
1823
// MARK: 1. Prepare
1924
guard let wallet = self.btcWallet else {
2025
throw WalletServiceError.notLogged

Adamant/Modules/Wallets/Dash/DashTransferViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ final class DashTransferViewController: TransferViewControllerBase {
6666
let transaction = try await service.createTransaction(
6767
recipient: recipient,
6868
amount: amount,
69-
fee: transactionFee
69+
fee: transactionFee,
70+
comment: nil
7071
)
7172

7273
if await !doesNotContainSendingTx() {

Adamant/Modules/Wallets/Dash/DashWalletService+Send.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ extension DashWalletService: WalletServiceTwoStepSend {
1919
return try await createTransaction(
2020
recipient: recipient,
2121
amount: amount,
22-
fee: transactionFee
22+
fee: transactionFee,
23+
comment: nil
2324
)
2425
}
2526

@@ -34,14 +35,16 @@ extension DashWalletService: WalletServiceTwoStepSend {
3435
return try await createTransaction(
3536
recipient: recipient,
3637
amount: amount,
37-
fee: transactionFee
38+
fee: transactionFee,
39+
comment: nil
3840
)
3941
}
4042

4143
func createTransaction(
4244
recipient: String,
4345
amount: Decimal,
44-
fee: Decimal
46+
fee: Decimal,
47+
comment: String?
4548
) async throws -> BitcoinKit.Transaction {
4649
// MARK: 1. Prepare
4750
guard let wallet = self.dashWallet else {

Adamant/Modules/Wallets/Doge/DogeTransferViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ final class DogeTransferViewController: TransferViewControllerBase {
4343
let transaction = try await service.createTransaction(
4444
recipient: recipient,
4545
amount: amount,
46-
fee: transactionFee
46+
fee: transactionFee,
47+
comment: nil
4748
)
4849

4950
if await !doesNotContainSendingTx() {

Adamant/Modules/Wallets/Doge/DogeWalletService+Send.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ extension DogeWalletService: WalletServiceTwoStepSend {
2424
func createTransaction(
2525
recipient: String,
2626
amount: Decimal,
27-
fee: Decimal
27+
fee: Decimal,
28+
comment: String?
2829
) async throws -> BitcoinKit.Transaction {
2930
// Prepare
3031
guard let wallet = self.dogeWallet else {

Adamant/Modules/Wallets/ERC20/ERC20TransferViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ final class ERC20TransferViewController: TransferViewControllerBase {
5050
let transaction = try await service.createTransaction(
5151
recipient: recipient,
5252
amount: amount,
53-
fee: transactionFee
53+
fee: transactionFee,
54+
comment: nil
5455
)
5556

5657
if await !doesNotContainSendingTx(

Adamant/Modules/Wallets/ERC20/ERC20WalletService+Send.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ extension ERC20WalletService: WalletServiceTwoStepSend {
1919
func createTransaction(
2020
recipient: String,
2121
amount: Decimal,
22-
fee: Decimal
22+
fee: Decimal,
23+
comment: String?
2324
) async throws -> CodableTransaction {
2425
guard let ethWallet = ethWallet else {
2526
throw WalletServiceError.notLogged

Adamant/Modules/Wallets/Ethereum/EthTransferViewController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ final class EthTransferViewController: TransferViewControllerBase {
4444
let transaction = try await service.createTransaction(
4545
recipient: recipient,
4646
amount: amount,
47-
fee: transactionFee
47+
fee: transactionFee,
48+
comment: nil
4849
)
4950

5051
if await !doesNotContainSendingTx(

Adamant/Modules/Wallets/Ethereum/EthWalletService+Send.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ extension EthWalletService: WalletServiceTwoStepSend {
2525
func createTransaction(
2626
recipient: String,
2727
amount: Decimal,
28-
fee: Decimal
28+
fee: Decimal,
29+
comment: String?
2930
) async throws -> CodableTransaction {
3031
try await ethApiService.requestWeb3 { [weak self] web3 in
3132
guard let self = self else { throw WalletServiceError.internalError(.unknownError) }

Adamant/Modules/Wallets/Klayr/KlyTransferViewController.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ final class KlyTransferViewController: TransferViewControllerBase {
1818

1919
private let prefix = "kly"
2020

21+
override var blockchainCommentsEnabled: Bool {
22+
!commentsEnabled
23+
}
24+
25+
override var transactionFee: Decimal {
26+
let blockchainComment: String = (form.rowBy(
27+
tag: BaseRows.blockchainComments(
28+
coin: walletCore.tokenName
29+
).tag
30+
) as? TextAreaRow)?.value ?? .empty
31+
32+
let baseFee = walletCore.getFee(comment: blockchainComment)
33+
let additionalyFee = walletCore.additionalFee
34+
35+
return addAdditionalFee
36+
? baseFee + additionalyFee
37+
: baseFee
38+
}
39+
2140
override func checkForAdditionalFee() {
2241
Task {
2342
guard let recipientAddress = recipientAddress,
@@ -49,6 +68,12 @@ final class KlyTransferViewController: TransferViewControllerBase {
4968
comments = ""
5069
}
5170

71+
let blockchainComment: String? = (form.rowBy(
72+
tag: BaseRows.blockchainComments(
73+
coin: walletCore.tokenName
74+
).tag
75+
) as? TextAreaRow)?.value
76+
5277
guard let service = walletCore as? KlyWalletService,
5378
let recipient = recipientAddress,
5479
let amount = amount
@@ -64,7 +89,8 @@ final class KlyTransferViewController: TransferViewControllerBase {
6489
let transaction = try await service.createTransaction(
6590
recipient: recipient,
6691
amount: amount,
67-
fee: transactionFee
92+
fee: transactionFee,
93+
comment: blockchainComment
6894
)
6995

7096
if await !doesNotContainSendingTx(

Adamant/Modules/Wallets/Klayr/WalletService/KlyWalletService+Send.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ extension KlyWalletService: WalletServiceTwoStepSend {
1717
func createTransaction(
1818
recipient: String,
1919
amount: Decimal,
20-
fee: Decimal
20+
fee: Decimal,
21+
comment: String?
2122
) async throws -> TransactionEntity {
2223
// MARK: 1. Prepare
2324
guard let wallet = klyWallet,
@@ -35,7 +36,8 @@ extension KlyWalletService: WalletServiceTwoStepSend {
3536
fee: fee,
3637
nonce: wallet.nonce,
3738
senderPublicKey: wallet.keyPair.publicKeyString,
38-
recipientAddressBinary: binaryAddress
39+
recipientAddressBinary: binaryAddress,
40+
comment: comment ?? .empty
3941
)
4042

4143
let signedTransaction = transaction.sign(with: keys, for: Constants.chainID)

Adamant/Modules/Wallets/Klayr/WalletService/KlyWalletService.swift

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class KlyWalletService: WalletCoreProtocol {
4444
@Atomic private(set) var isWarningGasPrice = false
4545
@Atomic private(set) var state: WalletServiceState = .notInitiated
4646
@Atomic private(set) var lastHeight: UInt64 = .zero
47+
@Atomic private(set) var lastMinFeePerByte: UInt64 = .zero
4748

4849
@ObservableValue private(set) var transactions: [TransactionDetails] = []
4950
@ObservableValue private(set) var hasMoreOldTransactions: Bool = true
@@ -53,7 +54,9 @@ final class KlyWalletService: WalletCoreProtocol {
5354
coreDataStack: coreDataStack,
5455
blockchainType: richMessageType
5556
)
56-
57+
58+
let salt = "adm"
59+
5760
// MARK: Notifications
5861

5962
let walletUpdatedNotification = Notification.Name("adamant.klyWallet.walletUpdated")
@@ -94,8 +97,17 @@ final class KlyWalletService: WalletCoreProtocol {
9497
try await getBalance(for: address)
9598
}
9699

97-
func getCurrentFee() async throws -> (fee: BigUInt, lastHeight: UInt64) {
98-
try await getFees()
100+
func getCurrentFee() async throws -> (fee: BigUInt, lastHeight: UInt64, minFeePerByte: UInt64) {
101+
try await getFees(comment: .empty)
102+
}
103+
104+
func getFee(comment: String) -> Decimal {
105+
let fee = try? getFee(
106+
minFeePerByte: lastMinFeePerByte,
107+
comment: comment
108+
).asDecimal(exponent: Self.currencyExponent)
109+
110+
return fee ?? transactionFee
99111
}
100112

101113
func getWalletAddress(byAdamantAddress address: String) async throws -> String {
@@ -214,11 +226,12 @@ private extension KlyWalletService {
214226
wallet.nonce = nonce
215227
}
216228

217-
if let result = try? await getFees() {
229+
if let result = try? await getFees(comment: .empty) {
218230
self.lastHeight = result.lastHeight
219231
self.transactionFeeRaw = result.fee > KlyWalletService.defaultFee
220232
? result.fee
221233
: KlyWalletService.defaultFee
234+
self.lastMinFeePerByte = result.minFeePerByte
222235
}
223236

224237
if let balance = try? await getBalance() {
@@ -286,7 +299,7 @@ private extension KlyWalletService {
286299
return UInt64(nonce) ?? .zero
287300
}
288301

289-
func getFees() async throws -> (fee: BigUInt, lastHeight: UInt64) {
302+
func getFees(comment: String) async throws -> (fee: BigUInt, lastHeight: UInt64, minFeePerByte: UInt64) {
290303
guard let wallet = klyWallet else {
291304
throw WalletServiceError.notLogged
292305
}
@@ -300,7 +313,8 @@ private extension KlyWalletService {
300313
fee: 0.00141,
301314
nonce: wallet.nonce,
302315
senderPublicKey: wallet.keyPair.publicKeyString,
303-
recipientAddressBinary: wallet.binaryAddress
316+
recipientAddressBinary: wallet.binaryAddress,
317+
comment: comment
304318
).sign(
305319
with: wallet.keyPair,
306320
for: Constants.chainID
@@ -315,7 +329,30 @@ private extension KlyWalletService {
315329

316330
let height = UInt64(lastBlock.header.height)
317331

318-
return (fee: fee, lastHeight: height)
332+
return (fee: fee, lastHeight: height, minFeePerByte: minFeePerByte)
333+
}
334+
335+
func getFee(minFeePerByte: UInt64, comment: String) throws -> BigUInt {
336+
guard let wallet = klyWallet else {
337+
throw WalletServiceError.notLogged
338+
}
339+
340+
let tempTransaction = TransactionEntity().createTx(
341+
amount: 100000000.0,
342+
fee: 0.00141,
343+
nonce: wallet.nonce,
344+
senderPublicKey: wallet.keyPair.publicKeyString,
345+
recipientAddressBinary: wallet.binaryAddress,
346+
comment: comment
347+
).sign(
348+
with: wallet.keyPair,
349+
for: Constants.chainID
350+
)
351+
352+
let feeValue = tempTransaction.getFee(with: minFeePerByte)
353+
let fee = BigUInt(feeValue)
354+
355+
return fee
319356
}
320357

321358
func setState(_ newState: WalletServiceState, silent: Bool = false) {
@@ -352,7 +389,7 @@ private extension KlyWalletService {
352389
do {
353390
let keyPair = try LiskKit.Crypto.keyPair(
354391
fromPassphrase: passphrase,
355-
salt: "adm"
392+
salt: salt
356393
)
357394

358395
let address = LiskKit.Crypto.address(fromPublicKey: keyPair.publicKeyString)

0 commit comments

Comments
 (0)