Skip to content

Commit

Permalink
Merge pull request #1696 from p2p-org/feature/pwn-949
Browse files Browse the repository at this point in the history
[ETH-949] Доработки по KeyApp
  • Loading branch information
lisemyon authored Feb 14, 2024
2 parents 7f1d40c + 9045318 commit 80a448d
Show file tree
Hide file tree
Showing 18 changed files with 89 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ extension SendInputBusinessLogic {
feeInSol: state.fee,
token: state.token,
services: services,
whitelistMints: state.feePayableTokenMints
whitelistMints: state.feePayableTokenMints,
prechosenFeeTokenAddress: state.prechosenFeeTokenAddress
)

state = state.copy(
Expand Down Expand Up @@ -107,9 +108,13 @@ extension SendInputBusinessLogic {
feeInSol: FeeAmount,
token: SolanaAccount,
services: SendInputServices,
whitelistMints: [String]
whitelistMints: [String],
prechosenFeeTokenAddress: String?
) async -> (token: SolanaAccount, fee: FeeAmount?) {
var preferOrder = ["SOL": 2]
if let prechosenSymbol = userWallets.first(where: { $0.mintAddress == prechosenFeeTokenAddress })?.symbol {
preferOrder[prechosenSymbol] = 0
}
if !preferOrder.keys.contains(token.symbol) {
preferOrder[token.symbol] = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ extension SendInputBusinessLogic {
let state = state.copy(
fee: fee,
tokenFee: feeToken,
feeInToken: feeInToken
feeInToken: feeInToken,
prechosenFeeTokenAddress: feeToken.mintAddress
)

return await validateFee(state: state)
Expand Down
18 changes: 13 additions & 5 deletions Packages/KeyAppKit/Sources/Send/Input/SendInputState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public struct SendInputState: Equatable {
sendViaLinkSeed != nil
}

public let prechosenFeeTokenAddress: String?

public init(
status: Status,
recipient: Recipient,
Expand All @@ -138,7 +140,8 @@ public struct SendInputState: Equatable {
lamportsPerSignature: UInt64,
minimumRelayAccountBalance: UInt64,
limit: SendServiceLimitResponse,
sendViaLinkSeed: String?
sendViaLinkSeed: String?,
prechosenFeeTokenAddress: String?
) {
self.status = status
self.recipient = recipient
Expand All @@ -156,6 +159,7 @@ public struct SendInputState: Equatable {
self.minimumRelayAccountBalance = minimumRelayAccountBalance
self.limit = limit
self.sendViaLinkSeed = sendViaLinkSeed
self.prechosenFeeTokenAddress = prechosenFeeTokenAddress
}

public static func zero(
Expand All @@ -166,7 +170,8 @@ public struct SendInputState: Equatable {
feeToken: SolanaAccount,
userWalletState: UserWalletEnvironments,
feePayableTokenMints: [String] = [],
sendViaLinkSeed: String?
sendViaLinkSeed: String?,
prechosenFeeTokenAddress: String?
) -> SendInputState {
.init(
status: status,
Expand All @@ -190,7 +195,8 @@ public struct SendInputState: Equatable {
),
tokenAccountRent: .init(remainingAmount: 0, remainingTransactions: 0)
),
sendViaLinkSeed: sendViaLinkSeed
sendViaLinkSeed: sendViaLinkSeed,
prechosenFeeTokenAddress: prechosenFeeTokenAddress
)
}

Expand All @@ -210,7 +216,8 @@ public struct SendInputState: Equatable {
lamportsPerSignature: UInt64? = nil,
minimumRelayAccountBalance: UInt64? = nil,
limit: SendServiceLimitResponse? = nil,
sendViaLinkSeed: String?? = nil
sendViaLinkSeed: String?? = nil,
prechosenFeeTokenAddress: String?? = nil
) -> SendInputState {
.init(
status: status ?? self.status,
Expand All @@ -229,7 +236,8 @@ public struct SendInputState: Equatable {
lamportsPerSignature: lamportsPerSignature ?? self.lamportsPerSignature,
minimumRelayAccountBalance: minimumRelayAccountBalance ?? self.minimumRelayAccountBalance,
limit: limit ?? self.limit,
sendViaLinkSeed: sendViaLinkSeed ?? self.sendViaLinkSeed
sendViaLinkSeed: sendViaLinkSeed ?? self.sendViaLinkSeed,
prechosenFeeTokenAddress: prechosenFeeTokenAddress ?? self.prechosenFeeTokenAddress
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/onevcat/Kingfisher.git",
"state" : {
"revision" : "3ec0ab0bca4feb56e8b33e289c9496e89059dd08",
"version" : "7.10.2"
"revision" : "5b92f029fab2cce44386d28588098b5be0824ef5",
"version" : "7.11.0"
}
},
{
Expand Down
3 changes: 3 additions & 0 deletions p2p_wallet/Common/Services/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ extension DefaultsKeys {

// Send
var isTokenInputTypeChosen: DefaultsKey<Bool> { .init(#function, defaultValue: false) }
var accountCreationLastTokenAddress: DefaultsKey<String?> {
.init(#function, defaultValue: nil)
}

var solanaNegativeStatusFrequency: DefaultsKey<String?> {
.init(
Expand Down
1 change: 1 addition & 0 deletions p2p_wallet/Common/Services/Storage/UserWalletManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class UserWalletManager: ObservableObject {
Defaults.ethBannerShouldHide = false
Defaults.isSellInfoPresented = false
Defaults.isTokenInputTypeChosen = false
Defaults.accountCreationLastTokenAddress = nil
Defaults.fromTokenAddress = nil
Defaults.toTokenAddress = nil
Defaults.referrerRegistered = false
Expand Down
2 changes: 1 addition & 1 deletion p2p_wallet/Resources/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@
"Based on absolute and relative profitability of each trade. It shows the relative potential %% profits or losses of your trading strategy." = "Based on absolute and relative profitability of each trade. It shows the relative potential %% profits or losses of your trading strategy.";
"%@%% last 24h" = "%@%% last 24h";
"Result of an investment, trading strategy per 24 hours" = "Result of an investment, trading strategy per 24 hours";
"KeyApp swap fee" = "KeyApp swap fee";
"Referral reward" = "Referral reward";
"Open SOLScan" = "Open SOLScan";
"Terms & Conditions" = "Terms & Conditions";
"Swap fee" = "Swap fee";
2 changes: 1 addition & 1 deletion p2p_wallet/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@
"Based on absolute and relative profitability of each trade. It shows the relative potential %% profits or losses of your trading strategy." = "Based on absolute and relative profitability of each trade. It shows the relative potential %% profits or losses of your trading strategy.";
"%@%% last 24h" = "%@%% last 24h";
"Result of an investment, trading strategy per 24 hours" = "Result of an investment, trading strategy per 24 hours";
"KeyApp swap fee" = "KeyApp swap fee";
"Referral reward" = "Referral reward";
"Open SOLScan" = "Open SOLScan";
"Terms & Conditions" = "Terms & Conditions";
"Swap fee" = "Swap fee";
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct RendableListHistoryTransactionItem: RendableListTransactionItem {
case let .referralReward(data):
return L10n.referralReward
case .unknown, .none:
return L10n.unknown
return L10n.transaction
}
}

Expand All @@ -156,7 +156,7 @@ struct RendableListHistoryTransactionItem: RendableListTransactionItem {
case .referralReward:
return date.string(withFormat: "HH:mm")
default:
return "\(L10n.signature): \(RecipientFormatter.shortSignature(signature: trx.signature))"
return "\(RecipientFormatter.longSignature(signature: trx.signature))"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct RendableListPendingTransactionItem: RendableListTransactionItem {
.from(RecipientFormatter
.shortFormat(destination: transaction.claimableTokenInfo.keypair.publicKey.base58EncodedString))
default:
return L10n.unknown
return L10n.transaction
}
}

Expand Down
8 changes: 6 additions & 2 deletions p2p_wallet/Scenes/Main/Send/Input/SendInputViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ final class SendInputViewModel: BaseViewModel, ObservableObject {
sourceWallet = tokenInWallet

let feeTokenInWallet = wallets
.first(where: { $0.token.mintAddress == TokenMetadata.usdc.mintAddress }) ??
.first(where: {
$0.token.mintAddress == Defaults.accountCreationLastTokenAddress ?? TokenMetadata.usdc.mintAddress
}) ??
.classicSPLTokenAccount(address: "", lamports: 0, token: .usdc)

var exchangeRate = [String: TokenPrice]()
Expand All @@ -156,7 +158,8 @@ final class SendInputViewModel: BaseViewModel, ObservableObject {
token: tokenInWallet,
feeToken: feeTokenInWallet,
userWalletState: env,
sendViaLinkSeed: sendViaLinkSeed
sendViaLinkSeed: sendViaLinkSeed,
prechosenFeeTokenAddress: Defaults.accountCreationLastTokenAddress
)

stateMachine = .init(
Expand Down Expand Up @@ -362,6 +365,7 @@ private extension SendInputViewModel {
self.isFeeLoading = true
_ = await self.stateMachine.accept(action: .changeFeeToken(newFeeToken))
self.isFeeLoading = false
Defaults.accountCreationLastTokenAddress = newFeeToken.mintAddress
}
.store(in: &subscriptions)

Expand Down
8 changes: 6 additions & 2 deletions p2p_wallet/Scenes/Main/Send/RecipientFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ enum RecipientFormatter {
}
}

static func shortSignature(signature: String) -> String {
"...\(signature.suffix(4))"
static func longSignature(signature: String) -> String {
if signature.count > 16 {
return "\(signature.prefix(8))...\(signature.suffix(8))"
} else {
return signature
}
}

static func signature(signature: String) -> String {
Expand Down
7 changes: 3 additions & 4 deletions p2p_wallet/Scenes/Main/Swap/State/JupiterSwapState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ struct JupiterSwapState: Equatable {
var liquidityFee: [SwapFeeInfo] {
guard let route else { return [] }
return route.routePlan
.map { ($0.swapInfo.feeAmount, $0.swapInfo.feeMint) }
.compactMap { feeAmount, feeMint -> SwapFeeInfo? in
.map { ($0.swapInfo.feeAmount, $0.swapInfo.feeMint, $0.percent) }
.compactMap { feeAmount, feeMint, percent -> SwapFeeInfo? in
guard let token = swapTokens.map(\.token).first(where: { $0.mintAddress == feeMint }),
let amount = UInt64(feeAmount)?.convertToBalance(decimals: token.decimals)
else {
Expand All @@ -297,8 +297,7 @@ struct JupiterSwapState: Equatable {
tokenSymbol: token.symbol,
tokenName: token.name,
tokenPriceInCurrentFiat: tokensPriceMap[token.mintAddress],
// TODO(jupiter): Pct in v6 is not provided for each route plan. We have summarize value in response.
pct: 0,
pct: Decimal(percent),
canBePaidByKeyApp: false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ final class SwapSettingsCoordinator: Coordinator<SwapSettingsCoordinatorResult>
strategy = .accountCreationFee
case .liquidityFee:
strategy = .liquidityFee
case .platformFee:
return
case .minimumReceived:
strategy = .minimumReceived
}
Expand Down Expand Up @@ -174,17 +172,26 @@ private extension JupiterSwapState {
return .loading
}

return .loaded(
info.liquidityFee.map { lqFee in
SwapSettingsInfoViewModel.Fee(
title: L10n.liquidityFee(
lqFee.tokenName ?? L10n.unknownToken,
"\(lqFee.pct == nil ? L10n.unknown : "\(NSDecimalNumber(decimal: lqFee.pct!).doubleValue.toString(maximumFractionDigits: 9))")%"
),
subtitle: lqFee.amount.tokenAmountFormattedString(symbol: lqFee.tokenSymbol ?? "UNKNOWN"),
amount: lqFee.amountInFiatDescription
)
}
)
var liquidutyFees = info.liquidityFee.map { lqFee in
SwapSettingsInfoViewModel.Fee(
title: L10n.liquidityFee(
lqFee.tokenName ?? L10n.unknownToken,
"\(lqFee.pct == nil ? L10n.unknown : "\(NSDecimalNumber(decimal: lqFee.pct!).doubleValue.toString(maximumFractionDigits: 9))")%"
),
subtitle: lqFee.amount.tokenAmountFormattedString(symbol: lqFee.tokenSymbol ?? "UNKNOWN"),
amount: lqFee.amountInFiatDescription
)
}

if let platformFeeAmount = info.platformFee {
liquidutyFees.append(SwapSettingsInfoViewModel.Fee(
title: L10n.swapFee,
subtitle: platformFeeAmount.amount
.tokenAmountFormattedString(symbol: platformFeeAmount.tokenSymbol ?? "UNKNOWN"),
amount: platformFeeAmount.amountInFiatDescription
))
}

return .loaded(liquidutyFees)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class SwapSettingsInfoViewModel: BaseViewModel, ObservableObject {
buttonTitle = L10n.okay + "👍"
case .transferFee:
image = .accountCreationFeeHand
title = L10n.transferFee
title = L10n.token2022TransferFee
subtitle = L10n.ChargeThatYouNeedToPayToSendOrReceiveTokens2022
.itHelpsMaintainTheNetworkAndEnsureSmoothTransactions
buttonTitle = L10n.gotIt + "👍"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ struct JupiterSwapStateInfo: Equatable {
let networkFee: SwapFeeInfo
let accountCreationFee: SwapFeeInfo?
let liquidityFee: [SwapFeeInfo]
let platformFee: PlatformFee?
let platformFeeAmount: SwapFeeInfo?
let platformFee: SwapFeeInfo?
let minimumReceived: SwapTokenAmountInfo?
let transferFee: String?
let transferFeeFiat: Double?
let exchangeRateInfo: String?

var estimatedFees: String? {
var fees = (liquidityFee + [networkFee, accountCreationFee, platformFeeAmount]
var fees = (liquidityFee + [networkFee, accountCreationFee, platformFee]
.compactMap { $0 })
.compactMap(\.amountInFiat)
.reduce(0.0, +)
Expand All @@ -40,6 +39,14 @@ struct JupiterSwapStateInfo: Equatable {

return fees > 0 ? "" + fees.formattedFiat() : nil
}

var displayableLiquidityFees: [SwapFeeInfo] {
var fees = liquidityFee
if let platformFee {
fees.append(platformFee)
}
return fees
}
}

// MARK: - Extensions
Expand Down Expand Up @@ -75,8 +82,7 @@ extension JupiterSwapState {
),
accountCreationFee: accountCreationFee,
liquidityFee: liquidityFee,
platformFee: route?.platformFee,
platformFeeAmount: platformFeeAmount,
platformFee: platformFeeAmount,
minimumReceived: minimumReceivedAmount == nil ? nil : .init(
amount: minimumReceivedAmount!,
token: toToken.token.symbol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ struct SwapSettingsView: View {
)

if viewModel.info.transferFee != nil {
commonRow(title: L10n.transferFee, subtitle: viewModel.info.transferFee, identifier: .transferFee)
commonRow(
title: L10n.token2022TransferFee,
subtitle: viewModel.info.transferFee,
identifier: .transferFee
)
}

// Account creation fee
Expand All @@ -86,26 +90,15 @@ struct SwapSettingsView: View {

// Liquidity fee
if viewModel.isLoadingOrRouteNotNil {
if !viewModel.info.liquidityFee.isEmpty {
if !viewModel.info.displayableLiquidityFees.isEmpty {
feeRow(
title: L10n.liquidityFee,
fees: viewModel.info.liquidityFee,
fees: viewModel.info.displayableLiquidityFees,
identifier: .liquidityFee
)
}
}

// Platform fee
if let platformFee = viewModel.info.platformFee {
commonRow(
title: L10n.keyAppSwapFee,
subtitle: nil,
trailingSubtitle: feeBpsFormatter(platformFee.feeBps),
trailingView: EmptyView().castToAnyView(),
identifier: .platformFee
)
}

// Estimated fee
if viewModel.isLoadingOrRouteNotNil, viewModel.info.estimatedFees != nil {
HStack {
Expand Down Expand Up @@ -322,7 +315,6 @@ extension SwapSettingsView {
case transferFee
case accountCreationFee
case liquidityFee
case platformFee
case minimumReceived
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ struct RendableDetailHistoryTransaction: RenderableTransactionDetail {
values: [.init(text: value)]
)
)
return result // Do not show trx.fees after this switch case for this type of transaction

default:
result.append(
Expand Down

0 comments on commit 80a448d

Please sign in to comment.