Skip to content

Commit

Permalink
feature:
Browse files Browse the repository at this point in the history
 Add read-only screens
  • Loading branch information
elefantel committed Jan 18, 2024
1 parent 40e92ec commit f6e35c1
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Example/metamask-ios-sdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
D107588D293F289500D6C66B /* SignView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D107588C293F289500D6C66B /* SignView.swift */; };
D107588F293F385700D6C66B /* TransactionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D107588E293F385700D6C66B /* TransactionView.swift */; };
D11D2F1B295075A3000E8003 /* Curvature.swift in Sources */ = {isa = PBXBuildFile; fileRef = D11D2F1A295075A3000E8003 /* Curvature.swift */; };
D131DF902B5957440055EF13 /* ReadOnlyCallsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D131DF8F2B5957440055EF13 /* ReadOnlyCallsView.swift */; };
D13A7535296E7EA0005EE461 /* ButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = D13A7534296E7EA0005EE461 /* ButtonStyle.swift */; };
D148AC75292FCF1B001791E5 /* ConnectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D148AC74292FCF1B001791E5 /* ConnectView.swift */; };
D1494FEB2970149B002D36D6 /* SwitchChainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1494FEA2970149B002D36D6 /* SwitchChainView.swift */; };
Expand Down Expand Up @@ -61,6 +62,7 @@
D107588C293F289500D6C66B /* SignView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignView.swift; sourceTree = "<group>"; };
D107588E293F385700D6C66B /* TransactionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransactionView.swift; sourceTree = "<group>"; };
D11D2F1A295075A3000E8003 /* Curvature.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Curvature.swift; sourceTree = "<group>"; };
D131DF8F2B5957440055EF13 /* ReadOnlyCallsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadOnlyCallsView.swift; sourceTree = "<group>"; };
D13A7534296E7EA0005EE461 /* ButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonStyle.swift; sourceTree = "<group>"; };
D148AC74292FCF1B001791E5 /* ConnectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectView.swift; sourceTree = "<group>"; };
D1494FEA2970149B002D36D6 /* SwitchChainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwitchChainView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -208,6 +210,7 @@
D14FA2F329E576EB00F3A059 /* ToastOverlay.swift */,
D14FA2F529E578B200F3A059 /* ViewExtension.swift */,
D14FA2F729E57B6900F3A059 /* ToastView.swift */,
D131DF8F2B5957440055EF13 /* ReadOnlyCallsView.swift */,
);
name = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -406,6 +409,7 @@
D11D2F1B295075A3000E8003 /* Curvature.swift in Sources */,
D14FA2F629E578B200F3A059 /* ViewExtension.swift in Sources */,
D14FA2F829E57B6900F3A059 /* ToastView.swift in Sources */,
D131DF902B5957440055EF13 /* ReadOnlyCallsView.swift in Sources */,
D1494FEB2970149B002D36D6 /* SwitchChainView.swift in Sources */,
D14FA2F429E576EB00F3A059 /* ToastOverlay.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions Example/metamask-ios-sdk/ConnectView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ struct ConnectView: View {
NavigationLink("Switch chain") {
SwitchChainView().environmentObject(metaMaskSDK)
}

NavigationLink("Read-only RPCs") {
ReadOnlyCallsView().environmentObject(metaMaskSDK)
}
}
}
}
Expand Down
171 changes: 171 additions & 0 deletions Example/metamask-ios-sdk/ReadOnlyCallsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import Foundation

//
// ReadOnlyCallsView.swift
// metamask-ios-sdk_Example
//

import SwiftUI
import Combine
import metamask_ios_sdk

@MainActor
struct ReadOnlyCallsView: View {
@EnvironmentObject var metamaskSDK: MetaMaskSDK

@State private var showProgressView = false

@State var balanceResult: String = ""
@State var gasPriceResult: String = ""
@State var web3ClientVersionResult: String = ""
@State private var errorMessage = ""
@State private var showError = false

private let signButtonTitle = "Sign"
private let connectAndSignButtonTitle = "Connect & Sign"

var body: some View {
NavigationView {
ZStack {
VStack(spacing: 16) {
Spacer()

VStack {
Button {
Task {
await getBalance()
}
} label: {
Text("Get Balance")
.modifier(TextButton())
.frame(maxWidth: .infinity, maxHeight: 32)
}
.modifier(ButtonStyle())

Text(balanceResult)
.modifier(TextCaption())
}

VStack {
Button {
Task {
await getGasPrice()
}
} label: {
Text("Get Gas Price")
.modifier(TextButton())
.frame(maxWidth: .infinity, maxHeight: 32)
}
.modifier(ButtonStyle())

Text(gasPriceResult)
.modifier(TextCaption())
}

VStack {
Button {
Task {
await getWeb3ClientVersion()
}
} label: {
Text("Get Web3 Client Version")
.modifier(TextButton())
.frame(maxWidth: .infinity, maxHeight: 32)
}
.modifier(ButtonStyle())

Text(web3ClientVersionResult)
.modifier(TextCaption())
}
}
.padding(.horizontal)

if showProgressView {
ProgressView()
.scaleEffect(1.5, anchor: .center)
.progressViewStyle(CircularProgressViewStyle(tint: .black))
}
}
.alert(isPresented: $showError) {
Alert(
title: Text("Error"),
message: Text(errorMessage)
)
}
.onAppear {
showProgressView = false
}
}
.navigationTitle("Read-Only Calls")
}

func getBalance() async {
let from = metamaskSDK.account
let params: [String] = [from, "latest"]
let getBalanceRequest = EthereumRequest(
method: .ethGetBalance,
params: params
)

showProgressView = true
let requestResult = await metamaskSDK.request(getBalanceRequest)
showProgressView = false

switch requestResult {
case let .success(value):
balanceResult = value
errorMessage = ""
case let .failure(error):
errorMessage = error.localizedDescription
showError = true
}
}

func getGasPrice() async {
let params: [String] = []
let getGasPriceRequest = EthereumRequest(
method: .ethGasPrice,
params: params
)

showProgressView = true
let requestResult = await metamaskSDK.request(getGasPriceRequest)
showProgressView = false

switch requestResult {
case let .success(value):
gasPriceResult = value
errorMessage = ""
case let .failure(error):
errorMessage = error.localizedDescription
showError = true
}
}

func getWeb3ClientVersion() async {
let params: [String] = []
let getWeb3ClientVersionRequest = EthereumRequest(
method: .web3ClientVersion,
params: params
)

showProgressView = true
let requestResult = await metamaskSDK.request(getWeb3ClientVersionRequest)
showProgressView = false

switch requestResult {
case let .success(value):
web3ClientVersionResult = value
errorMessage = ""
case let .failure(error):
errorMessage = error.localizedDescription
showError = true
}
}
}

struct ReadOnlyCalls_Previews: PreviewProvider {
static var previews: some View {
ReadOnlyCallsView()
}
}

0 comments on commit f6e35c1

Please sign in to comment.