Skip to content

Commit

Permalink
Check for internet on approve
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-lsvk committed Sep 4, 2023
1 parent 12518f1 commit c07e556
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ final class SessionProposalInteractor {
let supportedChains = [Blockchain("eip155:1")!, Blockchain("eip155:137")!]
let supportedAccounts = [Account(blockchain: Blockchain("eip155:1")!, address: ETHSigner.address)!, Account(blockchain: Blockchain("eip155:137")!, address: ETHSigner.address)!]
*/
do {
let sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)
} catch {
print(error)
}
let sessionNamespaces = try AutoNamespaces.build(
sessionProposal: proposal,
chains: Array(supportedChains),
methods: Array(supportedMethods),
events: Array(supportedEvents),
accounts: supportedAccounts
)
try await Web3Wallet.instance.approve(proposalId: proposal.id, namespaces: sessionNamespaces, sessionProperties: proposal.sessionProperties)
}

func reject(proposal: Session.Proposal) async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ final class SessionProposalPresenter: ObservableObject {
let sessionProposal: Session.Proposal
let verified: Bool?

@Published var showError = false
@Published var errorMessage = "Error"

private var disposeBag = Set<AnyCancellable>()

init(
Expand All @@ -30,14 +33,24 @@ final class SessionProposalPresenter: ObservableObject {

@MainActor
func onApprove() async throws {
try await interactor.approve(proposal: sessionProposal, account: importAccount.account)
router.dismiss()
do {
try await interactor.approve(proposal: sessionProposal, account: importAccount.account)
router.dismiss()
} catch {
errorMessage = error.localizedDescription
showError.toggle()
}
}

@MainActor
func onReject() async throws {
try await interactor.reject(proposal: sessionProposal)
router.dismiss()
do {
try await interactor.reject(proposal: sessionProposal)
router.dismiss()
} catch {
errorMessage = error.localizedDescription
showError.toggle()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ struct SessionProposalView: View {
Spacer()
}
}
.alert(presenter.errorMessage, isPresented: $presenter.showError) {
Button("OK", role: .cancel) {}
}
.edgesIgnoringSafeArea(.all)
}
//private func sessionProposalView(chain: String) -> some View {
Expand Down
30 changes: 30 additions & 0 deletions Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class ApproveEngine {
case pairingNotFound
case sessionNotFound
case agreementMissingOrInvalid
case networkNotConnected
}

var onSessionProposal: ((Session.Proposal, VerifyContext?) -> Void)?
Expand Down Expand Up @@ -63,6 +64,11 @@ final class ApproveEngine {
guard let payload = try proposalPayloadsStore.get(key: proposerPubKey) else {
throw Errors.wrongRequestParams
}

let networkConnectionStatus = await resolveNetworkConnectionStatus()
guard networkConnectionStatus == .connected else {
throw Errors.networkNotConnected
}

let proposal = payload.request
let pairingTopic = payload.topic
Expand Down Expand Up @@ -378,4 +384,28 @@ private extension ApproveEngine {
}
onSessionSettle?(session.publicRepresentation())
}

func resolveNetworkConnectionStatus() async -> NetworkConnectionStatus {
return await withCheckedContinuation { continuation in
let cancellable = networkingInteractor.networkConnectionStatusPublisher.sink { value in
continuation.resume(returning: value)
}

Task(priority: .high) {
await withTaskCancellationHandler {
cancellable.cancel()
} onCancel: { }
}
}
}
}

// MARK: - LocalizedError
extension ApproveEngine.Errors: LocalizedError {
var errorDescription: String? {
switch self {
case .networkNotConnected: return "Action failed. You seem to be offline"
default: return ""
}
}
}

0 comments on commit c07e556

Please sign in to comment.