Sign a transaction

Want to implement your own style, but with all the functionality Portal offers? Use these functions to implement your own custom web3 UI for your users.

This example shows how the Portal Provider interacts with the blockchain.

The Provider has a address of our MPC wallet. The Provider then proxies the request for balance to the configured RPC url and returns the result.

Portal's web3 provider:

Ensure you have set the gateway URL correctly with Infura or Alchemy when you initialize the portal class.

import PortalSwift

public var portal: Portal?

@IBAction func sendEip155Transaction(_: UIButton) {
  Task {
    do {
      // Ethereum Sepolia
      let chainId = "eip155:11155111" // CAIP-2 format.
  
      // Obtain the eip155 address of the user's wallet.
      guard let eip155Address = await portal.getAddress(chainId) else {
        throw PortalExampleAppError.addressNotFound()
      }
  
      // Create the transaction object.
      let transaction = [
        "from": eip155Address,
        "to": "0xRecipientAddress",
        "value": "0x10", // Native token amount to send (WEI for eip155:11155111).
      ]
  
      // Make the eth_sendTransaction request.
      let requestResponse = try await portal.request(
        chainId,
        withMethod: .eth_sendTransaction,
        andParams: [transaction]
      )
      
      // Obtain the transaction hash.
      guard let transactionHash = requestResponse.result as? String else {
        throw PortalExampleAppError.invalidResponseTypeForRequest()
      }
      
      // ✅ Great! You sent your first transaction successfully.
    } catch {
      // Handle any errors during the Portal provider request.
    }
  }
}

In the example above, we use .eth_sendTransaction. However, there's many other PortalRequestMethods available for you to use, which you can find in the SDK.

Estimating Gas

By default, Portal will estimate and populate the gas property in a transaction object if the property is undefined.

To estimate the gas value manually, you'll want to use the eth_estimateGas RPC call and pass in your transaction as the parameter.

import PortalSwift

public var portal: Portal?

@IBAction func estimateGas(_: UIButton) {
  Task {
    do {
      // Ethereum Sepolia
      let chainId = "eip155:11155111" // CAIP-2 format.
      
      // Obtain the eip155 address of the user's wallet.
      guard let eip155Address = await portal.getAddress(chainId) else {
        throw PortalExampleAppError.addressNotFound()
      }
      
      // Create the transaction object.
      let transaction = [
        "from": eip155Address,
        "to": "0xRecipientAddress",
        "value": "0x10", // Native token amount to send (WEI for eip155:11155111).
      ]
  
      // Make the eth_estimateGas request with the transaction object.
      let requestResponse = try await portal.request(
        chainId,
        withMethod: .eth_estimateGas,
        andParams: [transaction]
      )
      
      // Retrieve the estimated gas value.
      guard let estimatedGas = requestResponse.result as? String else {
        throw PortalExampleAppError.invalidResponseTypeForRequest()
      }
      
      // ✅ Nice! You just retrieved the gas estimate.
    } catch {
      // Handle any errors during the Portal provider request.
    }
  }
}

And now you are signing transactions with Portal! 🙌 🚀 Next, we'll explore how to simulate a transaction so that you can create smoother experiences for your users.

Last updated