โœ๏ธSigning 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?

let fromAddress = try portal?.keychain.getAddress()
let toAddress = "0xTOADDRESS"

// Create the provider's request payload.
let payload = ETHTransactionPayload(
  method: ETHRequestMethods.SendTransaction.rawValue,
  params: ETHTransactionParam(
    from: fromAddress,
    to: toAddress,
    value: "0x9184e72a", // example value
    data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" // example data 
  )
)

// Make the provider request.
portal?.provider.request(payload: payload) {
  (result: Result<TransactionCompletionResult>) -> Void in

  guard result.error == nil else {
    // Handle errors.
    return
  }

  let txHash = (result.data!.result as! Dictionary<String, Any>)["result"]
}

In the example above, we use ETHRequestPayload. However, there's also a couple other request payload types you should be aware of, such as ETHTransactionPayload and ETHAddressPayload.

Depending on the type of payload the method requires, you will need to use one of those payload types. We also recommend exploring ETHRequestMethods for a list of all available methods you can use.

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 use the eth_estimateGas RPC call and pass in your transaction as the parameter.

import PortalSwift

public var portal: Portal?

let fromAddress = try portal?.keychain.getAddress()
let toAddress = "0xTOADDRESS"

let payload = ETHTransactionPayload(
  method: ETHRequestMethods.SendTransaction.rawValue,
  params: ETHTransactionParam(
    from: fromAddress,
    to: toAddress,
    value: "0x9184e72a", // example value
    data: "0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675" // example data 
  )
)

self.portal?.provider.request(payload: payload) { (result: Result<TransactionCompletionResult>) in
  guard result.error == nil else {
    // handle error
    return
  }

  // handle success
  let balance = (result.data!.result as! Dictionary<String, Any>)["result"]
}

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