Links
Comment on page

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
class ViewController: UIViewController {
public var portal: Portal?
@IBAction func getBalance(_ sender: UIButton) {
do {
// Obtain the public address from the keychain.
let address = try portal?.keychain.getAddress()
// Create the provider's request payload.
let payload = ETHRequestPayload(
method: ETHRequestMethods.GetBalance.rawValue,
params: [address!, "latest"]
)
// Make the provider request.
portal?.provider.request(payload: payload) {
(result: Result<RequestCompletionResult>) -> Void in
guard result.error == nil else {
// ❌ Handle errors getting the address' ETH balance.
return
}
// ✅ We now have the address' ETH balance as a hex! 🙌
let balance = (result.data!.result as! Dictionary<String, Any>)["result"]
}
} catch {
// ❌ Handle errors getting the address from keychain.
}
}
}
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.
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.