Skip to content

Commit

Permalink
Merge pull request #1307 from novasamatech/fix/swap-corner-cases
Browse files Browse the repository at this point in the history
Fix swap corner cases
  • Loading branch information
ERussel authored Dec 17, 2024
2 parents 6a366cf + 51ca28b commit e9f2a83
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class CrosschainExchangeOperationPrototype: AssetExchangeBaseOperationProt

private extension CrosschainExchangeOperationPrototype {
private func isChainWithExpensiveCrossChain(chainId: ChainModel.Id) -> Bool {
chainId == KnowChainId.polkadot && chainId == KnowChainId.statemint
chainId == KnowChainId.polkadot || chainId == KnowChainId.statemint
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol SwapDetailsViewModelFactoryProtocol: SwapBaseViewModelFactoryProtocol {

func slippageViewModel(slippage: BigRational, locale: Locale) -> String

func walletViewModel(walletAddress: WalletDisplayAddress) -> WalletAccountViewModel?
func walletViewModel(metaAccountResponse: MetaChainAccountResponse) -> WalletAccountViewModel?
}

final class SwapDetailsViewModelFactory: SwapBaseViewModelFactory {
Expand Down Expand Up @@ -72,7 +72,7 @@ extension SwapDetailsViewModelFactory: SwapDetailsViewModelFactoryProtocol {
slippage.decimalValue.map { percentForamatter.value(for: locale).stringFromDecimal($0) ?? "" } ?? ""
}

func walletViewModel(walletAddress: WalletDisplayAddress) -> WalletAccountViewModel? {
try? walletViewModelFactory.createViewModel(from: walletAddress)
func walletViewModel(metaAccountResponse: MetaChainAccountResponse) -> WalletAccountViewModel? {
try? walletViewModelFactory.createViewModel(from: metaAccountResponse)
}
}
2 changes: 1 addition & 1 deletion novawallet/Modules/Swaps/Base/SwapBasePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class SwapBasePresenter {
)
}

func getMaxModel() -> SwapMaxModel? {
func getMaxModel() -> SwapMaxModel {
.init(
payChainAsset: getPayChainAsset(),
feeChainAsset: getFeeChainAsset(),
Expand Down
9 changes: 3 additions & 6 deletions novawallet/Modules/Swaps/Confirm/SwapConfirmPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ final class SwapConfirmPresenter: SwapBasePresenter {
}

override func applySwapMax() {
let maxAmount = getMaxModel().calculate()

guard
let maxAmount = getMaxModel()?.calculate(),
maxAmount > 0,
let maxAmountInPlank = maxAmount.toSubstrateAmount(
precision: initState.chainAssetIn.assetDisplayInfo.assetPrecision
Expand Down Expand Up @@ -289,11 +290,7 @@ extension SwapConfirmPresenter {
return
}

guard let walletAddress = WalletDisplayAddress(response: chainAccountResponse) else {
view?.didReceiveWallet(viewModel: nil)
return
}
let viewModel = viewModelFactory.walletViewModel(walletAddress: walletAddress)
let viewModel = viewModelFactory.walletViewModel(metaAccountResponse: chainAccountResponse)

view?.didReceiveWallet(viewModel: viewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ struct SwapIssueCheckParams {
let payAssetExistense: AssetBalanceExistence?
let receiveAssetExistense: AssetBalanceExistence?
let quoteResult: Result<AssetExchangeQuote, Error>?
let fee: AssetExchangeFee?
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ final class SwapIssueViewModelFactory {
}

func detectInsufficientBalance(in model: SwapIssueCheckParams) -> SwapSetupViewIssue? {
if let payAmount = model.payAmount,
let payChainAsset = model.payChainAsset,
let balance = model.payAssetBalance?.transferable.decimal(precision: payChainAsset.asset.precision),
payAmount > balance {
return .insufficientBalance
} else {
guard
let payAmount = model.payAmount,
payAmount > 0,
let payChainAsset = model.payChainAsset
else {
return nil
}

let assetDisplayInfo = payChainAsset.assetDisplayInfo
let balance = model.payAssetBalance?.transferable.decimal(assetInfo: assetDisplayInfo) ?? 0
let fee = model.fee?.totalFeeInAssetIn(payChainAsset).decimal(assetInfo: assetDisplayInfo) ?? 0

return payAmount + fee > balance ? .insufficientBalance : nil
}

func detectMinBalanceViolationOnReceive(in model: SwapIssueCheckParams, locale: Locale) -> SwapSetupViewIssue? {
Expand All @@ -45,7 +50,7 @@ final class SwapIssueViewModelFactory {
let minBalance = model.receiveAssetExistense?.minBalance.decimal(
precision: receiveChainAsset.asset.precision
),
let beforeSwapBalance = model.receiveAssetBalance?.freeInPlank.decimal(
let beforeSwapBalance = model.receiveAssetBalance?.balanceCountingEd.decimal(
precision: receiveChainAsset.asset.precision
) else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ protocol SwapsSetupViewModelFactoryProtocol: SwapBaseViewModelFactoryProtocol, S

func payTitleViewModel(
assetDisplayInfo: AssetBalanceDisplayInfo?,
maxValue: BigUInt?,
maxValue: Decimal?,
locale: Locale
) -> TitleHorizontalMultiValueView.Model

Expand Down Expand Up @@ -130,21 +130,17 @@ extension SwapsSetupViewModelFactory: SwapsSetupViewModelFactoryProtocol {

func payTitleViewModel(
assetDisplayInfo: AssetBalanceDisplayInfo?,
maxValue: BigUInt?,
maxValue: Decimal?,
locale: Locale
) -> TitleHorizontalMultiValueView.Model {
let title = R.string.localizable.swapsSetupAssetSelectPayTitle(
preferredLanguages: locale.rLanguages
)

if let assetDisplayInfo = assetDisplayInfo, let maxValue = maxValue {
let amountDecimal = Decimal.fromSubstrateAmount(
maxValue,
precision: Int16(assetDisplayInfo.assetPrecision)
) ?? 0
let maxValueString = balanceViewModelFactoryFacade.amountFromValue(
targetAssetInfo: assetDisplayInfo,
value: amountDecimal
value: maxValue
).value(for: locale)

return .init(
Expand Down
31 changes: 22 additions & 9 deletions novawallet/Modules/Swaps/Setup/SwapSetupPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class SwapSetupPresenter: SwapBasePresenter {
return nil
}

let maxAmount = getMaxModel()?.calculate() ?? 0
let maxAmount = getMaxModel().calculate()
return payAmountInput.absoluteValue(from: maxAmount)
}

Expand Down Expand Up @@ -197,6 +197,7 @@ final class SwapSetupPresenter: SwapBasePresenter {
provideIssues()
provideFeeViewModel()
provideRouteViewModel()
providePayTitle()
switchFeeChainAssetIfNecessary()
}

Expand Down Expand Up @@ -236,6 +237,7 @@ final class SwapSetupPresenter: SwapBasePresenter {
provideButtonState()
}

providePayTitle()
provideIssues()
}

Expand All @@ -254,8 +256,8 @@ extension SwapSetupPresenter {
return nil
}

let maxAmount = getMaxModel()?.calculate()
return input.absoluteValue(from: maxAmount ?? 0)
let maxAmount = getMaxModel().calculate()
return input.absoluteValue(from: maxAmount)
}

func getIssueParams() -> SwapIssueCheckParams {
Expand All @@ -268,16 +270,26 @@ extension SwapSetupPresenter {
receiveAssetBalance: receiveAssetBalance,
payAssetExistense: payAssetBalanceExistense,
receiveAssetExistense: receiveAssetBalanceExistense,
quoteResult: quoteResult
quoteResult: quoteResult,
fee: fee
)
}

private func providePayTitle() {
let payTitleViewModel = viewModelFactory.payTitleViewModel(
assetDisplayInfo: payChainAsset?.assetDisplayInfo,
maxValue: payAssetBalance?.transferable,
locale: selectedLocale
)
let payTitleViewModel = if let payChainAsset, payAssetBalance != nil {
viewModelFactory.payTitleViewModel(
assetDisplayInfo: payChainAsset.assetDisplayInfo,
maxValue: getMaxModel().calculate(),
locale: selectedLocale
)
} else {
viewModelFactory.payTitleViewModel(
assetDisplayInfo: nil,
maxValue: nil,
locale: selectedLocale
)
}

view?.didReceiveTitle(payViewModel: payTitleViewModel)
}

Expand Down Expand Up @@ -715,6 +727,7 @@ extension SwapSetupPresenter: SwapSetupPresenterProtocol {
Swift.swap(&payChainAsset, &receiveChainAsset)
feeChainAsset = payChainAsset?.chain.utilityChainAsset()
canPayFeeInPayAsset = false
fee = nil

interactor.update(payChainAsset: payChainAsset)
interactor.update(receiveChainAsset: receiveChainAsset)
Expand Down

0 comments on commit e9f2a83

Please sign in to comment.