How to send_transaction to solidity smart contract using WalletConnectV2 and iOS?

68 views Asked by At

I have the following code:

import SwiftUI
import WalletConnectV2SwiftUI
import WalletConnectModal
import WalletConnectSwift
import WalletConnectSign
import UIKit
import Combine



struct AccountDetails {
let chain: String
let methods: [String]
let account: String
}



struct ContentView: View {


@State var account : String = ""

let metadata = AppMetadata(name: "Swift Dapp",
                           description: "WalletConnect to App",
                           url:"www.walletconnect.com",
                           icons: [""])

@StateObject var walletConnect = WalletConnectView(
    projectId: "2cd853f680bd02428bb89b1d2943f44f",
    name: "Swift Dapp",
    description: "WalletConnect to App",
    url: "www.walletconnect.com",
    icons: [""],
    supportedChainIds: [
        "eip155": ProposalNamespace(
            chains: [
                Blockchain("eip155:137")!
            ],
            methods: [
                "eth_sendTransaction"
            ], events: []
        )])


var body: some View {
    
    
    VStack {
        Text("account: \(account)")
            .fontWeight(.thin)
        
        Spacer()
        
        
        if !account.isEmpty {
            Button(action: {
                
                
                // TODO  send Transaction
                
                
            }, label: {
                
                Text("send Transaction")
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 20).fill(Color.yellow.opacity(0.3)))
            })
        }else{
            
            
            Button {
                walletConnect.connectWithWallet()
            } label: {
                Text("Connect To Wallet")
                    .padding()
                    .background(RoundedRectangle(cornerRadius: 20).fill(Color.green.opacity(0.3)))
            }
            .padding(.vertical, 30)
        }
        
    }
    .padding()
    .onChange(of: walletConnect.account, perform: { accounts in
        
        self.account = accounts.first?.address ?? "..."
        
        print("Wallet Address: ", accounts.first?.address ?? "")
    })
    .onChange(of: walletConnect.rejectedReason, perform: { reason in
        print("Wallet Connection Error", reason)
    })
}

}

It is a very simple code where users can connect to Metamask or any other blockchain wallet using WalletConnectV2. My question now is, once I am connected how can I send transaction and interact with a smart contract. Lets say I have a smart contract address, ABI and a function I want to call. How can I integrate it inside my app? In the previous version V1, there was a demo but in V2 I can not find anything. Inside the official WalletConnect documentation there is only one example how to connect but I also want call some smart contract function after that.

0

There are 0 answers