Adding Wallet Functionalities

The page provides instructions on how to add wallet/Bolt functionalities to the sample app.

Wallet Directories

The following directories lies inside the xcode project. Here is the xcode screenshot for example:

Bolt/Wallet Functionalities

Description for the directories and files are provided below:

Describing Code in Directories

Bolt/App/BoltHome.swift

Describing Code

  • Line 8 import SwiftUI gives you access to SwiftUI-specific functionality . If you are writing UI Views you need to import SwiftUI.

  • Line 10 defines a BoltHome structure that helps us build the HomeUI of your Bolt(Wallet functionalities) code.

  • Line 11 Defines an Environment Object boltVM in BoltHome structure for sharing data between many views in your app.@EnvironmentObject property wrapper ensures views automatically stay updated when that data changes.

  • Line 12 defines a private variable sortOrder with @State wrapper to hold state without losing it. SwiftUI can destroy and recreate our struct whenever needed. sortOrder is storing state Transaction.creationDate via KeyPathComparator that uses a sort comparator to provide the comparison of Transaction Creation values at a key path.

  • Line 13 defines the variable bodyfor holding the main view of BoltHome screen.

  • Line 14 utilizes GeometryReader from SwiftUI library imported above to calculate screen position and size information .

  • Line 15 to 42 builds app layouts with stack views. Individually, HStack positions views in a horizontal line, VStack positions them in a vertical line, and ZStack overlays views on top of one another.

    • According to code, ZStack has its views on bottom with VStack defined inside it. View positioned vertically also includes available balance listed retrieved via AvailableBalanceBlock() method(Line 18).

    • WalletActionStack method is called at line 20 to provide relative sizes using GeometryReader for vertically positioned items.

    • Views for WalletApp that has to be positioned horizontally (HStack )

    • Line 28 bolds the whole text inside the HStack.

    • Line 29 defines a scrollable view within the scrollable content region .

    • Scroll indicators are set to false by default.

    • Line 30 defines what the scrollable view should contain which is for each sorted transaction by creation date list them as rows.

  • Line 43 to 53 defines the layout for the main View defined at Line 13

    • Padding for the main view is set to 20 pexels.

    • Environment Object boltVm is passed which holds the BoltViewModel instance.

    • Navigation Title which is set at top of app windows is named (Bolt)

    • App Title should be displayed in large texts.

    • .onAppear() adds an action(which is getting transaction details) before the main view appears.

    • Present an Alert for the user to receive ZCN

    • Print the Wallet Address by fetching the WalletClientID form Utils.swift

    • Present an alert for Sending ZCN and error(in case not able to send ZCN)

  • Line 57 to 67 defines a function for setting view of TransactionRow . The @ViewBuilder attribute is used with the function to create child views for a under the parent view which is BoltHome defined at line 10. The layout for transaction rows are as follows in HStack(Horizontally Stacked Views) : TransactionHash : Transaction Date Formatted

  • Line 69 to 76 defines variable sendAlert for holding alerts with the @ViewBuilder attribute to create child views.

    a) "some View” in means that the body will always be implementing the View protocol, but the implementation type does not need to be known by the caller.

    b) Text field for Wallet clientID and amount is specified and the button is implemented for executing send ZCN token transactions.

  • Line 78 defines variable recieveAlert for a child view that defines a button for copying the WalletClientID.

  • Line 84 to 95 defines a Previewprovider type that produces view for the declared View structures .

    a) Here we will pass our BoltHome structure defined at line 10 to be available as a View preview.(Line 92)

    b) At line 93 BoltHome is passed with an environment object boltVM for sharing data between many views in your app.

    c)boltVM is assigned a created BoltViewModel instance at line 87

    d)At line 88 boltVM populates the Transactions Array with transaction details accessible under BoltViewModel.swift.

Bolt/App/TransactionDetails.swift

Describing Code:

  • Line 8 import SwiftUI gives you access to iOS UI-specific functionality . If you are writing SwiftUi View you need to import SwiftUI.

  • Line 10 defines a structure TransactionDetails which contains various properties and fields for creating view for transaction details.

  • Line 11 defines a transaction variable that points to Transaction Structure

  • Line 12 defines the variable bodyfor holding the main view of Transaction Details

  • Line 13 defines a List for displaying a collection of data

  • Inside that at line 14 ,to add a section around some cells, a Section statement with title is added.

  • Line 15 to 20 will create rows under the list that holds Transaction data.

  • Line 23 to 28 again adds a section under the list with the rows that holds Transaction Amount Details (Status.Value,Fee,Date)

  • Line 30 to 33 adds another section in the list for Explorer under which a redirecting link to all-in -one network dashboard is listed to view Transaction details in more detail.

  • Line 34 defines the navigation title for TransactionDetails structure.

  • Line 37 to 45 defines a function DictionarySection implementing a dictionary for holding transaction data that has to be populated in the list rows(Line 40 and 41) .

  • The @ViewBuilder attribute is used with the function to create child views under the parent view which is TransactionDetails struct defined at Line 10.

  • Line 47 to 51 defines a PreviewProvider type that produces view previews for the declared TransactionDetails struct .

  • Line 53 to 70 defines a structure on how how transaction list in terms of rows should be created.

  • According to the structure two variables are defined -- a variable for List title is defined and an optional string variable value that might hold or not hold value depending u[on the case .

  • Line 56 defines a boolean variable holding the toggle state which is set to false by default

  • Line 58 defines how views should be stacked horizontally.

  • First ,title should be listed and then comes value variable horizontally to that.(Line 60 and 61 )

  • The maximum number of lines that the text can occupy in a view is 10(Line 62)

  • Line 64 utilizes .onTapGesture function which adds an action to perform when the view recognizes a tap gesture.

  • Line 65 utilizes withAnimation function which returns the view’s body result with the defined animation which is self.opened.toggle() .

  • self refers to the current object within the ListRow struct.

  • The toggle () function will switch the bool from true to false.

Bolt/Model/Balance.swift

Describing Code:

  • Line 8 import Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

  • Line 9 import Zcncore is required to access Gosdk functions.

  • Line 11 defines a structure for how wallet balance should be retrieved. Codable means structure allows decoding data for custom types and encoding data to be saved or transferred.

  • Line 13 to 16 defines private variables in the struct for organizing balance details.

a)txn: holds transaction hash ​ b)round : holds block rounds information c) balance : holds wallet balance ​ d)error: hold balance error information

  • Line 18 to 22 internally initializes instance of a Balance structure .self refers to the current object within the Balance structure.

  • Line 25 to 30 defines constants or enums(not changable variable) for the Balance structure.

  • Line 32 to 34 defines balance function which will return wallet balance as integer.

  • Line 36 to 39 defines balanceToken function with a getter and setter methods.A getter method here allows access to private balance.tokens property, and where as setter method allows the private balance property to be set with a new value which is ZCN tokens to value of choice(Double) via ZcncoreConvertToValue function.

  • Line 41 to 48 defines functionusd for getting ZCN token balance in terms of USD

    a) A usd variable of type Double that gets value via Utils.zcnUsdRate function in Utils.swift.

    b) A amount variable of type Double that gets its value from balanceToken at line 43

    c) A dollarString variable that returns ZCN token value in terms of USD.

Bolt/Model/Transaction.swift

Descriibing Code :

  • Line 8 import Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

  • Line 12 defines a Transaction structure for app transactions.

  • Line 39 to 60 internally initializes instance of a Balance structure with variablesvariables .self refers to the current instance within the Balance structure.

  • Line 62 to 73 defines optional variables( can hold None and Some) for holding various transaction details.

  • Line 95 to 97 defines function for returning formattedDate that will be required in transaction information. self refers to the current instance within the Balance structure.

Bolt/Model/Wallet.swift

Describing Code:

  • Line 8 import Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

  • Line 10 defines a Wallet structure for getting wallet information in the app.

  • Line 11 to 15 defines variables in the wallet structure for holding wallet details.

a) client_id: holds wallet client_id ​ b) client_key : holds wallet client_key ​ c) keys : holds wallet public and private keys ​ d) mnemonics: holds wallet mnemonics ​ e) version : holds wallet version.

  • Line 17 to 20 defines a function for returning wallet information .

  • Line 22 to 25 defines Key structure for wallet public and private keys.

Bolt/View/AvailableBalanceBlock.swift

Describing Code:

  • Line 8 import SwiftUI gives you access to SwiftUI-specific functionality . If you are writing UI Views in your iOS app you need to import SwiftUI.

  • Line 10 defines a AvailableBalanceBlock structure that helps us build the AvailableBalanceBlock in the Bolt(Wallet) App. <insert image here>

  • Line 11 defines a BoltViewModel variable boltVM in for sharing data between various Bolt app views in your app(here ).@EnvironmentObject property wrapper ensures views automatically stay updated when that data changes.

  • Line 12 defines a integer variable balance with @AppStorage wrapper for reading values from UserDefaults, The wrapper effectively watches a balance in UserDefaults, and will refresh your UI if that key changes.

  • Line 14 defines the variable bodyfor holding the AvailableBalanceBlock view on the app screen.

  • Line 15 to 42 builds app layouts with stack views. Individually, HStack positions views in a horizontal line, VStack positions them in a vertical line. a) Vertically positioned views (VStack ) are as follows: "Available Balance(text)" with spacing then amount of tokens fetched and rounded off from method in Balance.swift horizontally stacked. (Line 15 to 27)

b) More Horizontally positioned views (HStack) are as follows : Total Balance with token balance in ZCN then its ZCN equivalent in USD. (Line 29 to 41)

  • Line 43 to 50 defines a Previewprovider type that produces view for the declared AvailableBalanceBlock structure. a) Here we will pass our AvailableBalanceBlock structure defined at line 10 to be available as a View preview.(Line 45) b) At line 46 BoltViewModel is passed with an environment object for sharing data and access methods between views in your app. c) 20 pixels for padding and spacing between previews.(Line47) d)Set size constrains and layout for the preview according to the amount of space view requires(Line 48)

Bolt/View/WalletActionStack.swift

Describing Code:

  • Line 8 import SwiftUI gives you access to SwiftUI-specific functionality . If you are writing UI Views in your iOS app you need to import SwiftUI.

  • Line 10 defines a WalletActionStack structure that helps us define functionalities for the wallet (sending or receiving wallet tokens).

  • Line 11 defines a BoltViewModel variable boltVM in for sharing data between main app and wallet stack view in your app(here ).@EnvironmentObject property wrapper ensures views automatically stay updated when that data changes.

  • Line 12 creates a property that reads the color scheme of the current view using the key path of the colorScheme property.

  • Line 13 defines a variable width pointing to CGFloat for floating-point scalar values in Core Graphics and related frameworks.

  • Line 15 to 28 builds app layouts with stack views. Individually, HStack positions views in a horizontal line,

    a) Horizontally Stacked views (HStack ) are as follows: For each Wallet Action these are the following layouts:

    Frame: Positions view within an invisible frame with the specified size. Background: System background uses the default background color based on light/dark mode. Corner Radius: Round the corners of a view by 12 points. Shadow: Adds a dark shadow to this view. Padding: Adds horizontal padding 10pt to specific edges of this view.

  • Line 30 implements a WalletActionButton structure which define properties for implementing buttons for wallet actions.

  • Line 31 defines a variable width pointing to CGFloat for floating-point scalar values in Core Graphics and related frameworks.

  • A action variable which can switch between different WalletActionType.

  • A button variable which can holds buttons for different WalletActionType.

  • Line 35 to 39 initializes WalletActionButton instance with its properties.

  • Line 41 to 53 defines view layout for the WalletActionButton

    a) VStack positions views in a vertical line with the following properties. (Line 42)

    b) A button image with .resizable() mode: Sets the mode by which SwiftUI resizes an image to fit its space.(Line 43 and 44)

    c) .frame(width:width/13): Positions this view within an invisible frame with the specified size(width of window/13)(Line 46).

    d) A button title with text and font size and weight specified(Line 47 and 48)

    e) Total frame width for the complete button layout with title will be 1/3 of actual size .

f) Line 51 utilizes .onTapGesture function which adds an action to perform when the view recognizes a button click.

  • Line 55 to to 75 define Swift enumerations to store WalletActionType values which are send(send tokens),receive(receive tokens) and faucet(get tokens into wallet)

  • Line 60 to 66 uses switch control statements for flow of WalletActionTypes titles.

    The switch statement evaluates an expression inside paranthesis which is self(WalletActionType Instance)(Line 61) . If the result of the expression is equal to a particular case that is (send,.receive,and .faucet) then statements for that particular case are executed.

  • Line 68 to 75 uses control statements for flow of WalletActionTypes images.

    The switch statement evaluates an expression inside paranthesis which is self(WalletActionType Instance)(Line 69) . If the result of the expression is equal to a particular case that is (.send,.receive,and .faucet) then statements for that particular case are executed.

  • Line 77 to 83 defines a PreviewProvider type that produces view for the declared WalletActionStack structure.

    a) Here we will pass our WalletActionStack instance defined as a structure at line 10 to be available as a View preview.(Line 45)

    b) At line 80 BoltViewModel is passed as an environment object for sharing data and access methods between views in your app.

    c) Set size constrains and layout for the preview according to the amount of space view requires(Line 81)

Bolt/ViewModel/BoltViewModel.swift

Describing Code:

  • Line 8 to 11 import libraries Foundation is required to provide a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.

    Zcncore is required to access Gosdk functions.

    SwiftUI gives you access to SwiftUI-specific functionality . If you are writing UI Views in your iOS app you need to import SwiftUI.

    Combine gives handling of asynchronous events.

  • Line 13 BoltViewModel class utilizes the zcncore libraries and provide the following functionalities for Bolt UI: a) getBalance(): Retrieve Wallet Balance(Line 51 to 55) b) recieveFaucet(): Receive tokens in wallet (Line 68 to 92) c) sendZCN() : Send ZCN Tokens (Line 94 to 142) d) copyClientID() : Copy Wallet ClientID(Line 144 to 146) e) getTransactions(): Get Wallet Transactions(Line 148 to 158) f) onTransactionComplete(): A function handling responses for successful transaction. (Line 160 to 165) g) onTransactionFailed(): A function handling responses for failed transactions. (Line 171 to 178)

  • ZcncoreGetBalanceCallbackProtocol() extends new functionality to the BoltViewModel structure with the onBalanceAvailable method which return ZCN token balance in callback.(Line 180 to 193)

  • ZcncoreTransactionCallbackProtocol() extends new functionality to the BoltViewModel structure with onTransactionComplete method which is called back when transaction is complete.(Line 198 to 214)

  • ZcncoreGetInfoCallbackProtocol() extends new functionality to the BoltviewModel structure with onInfoAvailable method which is called back for transaction information. (Line 221 to 243)

Last updated