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 screen displays how the Portal Provider interacts with the MPC wallet and the blockchain.

The Provider has an input box to set a toAddress for the transaction of sending 1 wei from our MPC wallet. The Provider then receives a signed transaction from our mobile MPC library and submits that to chain using the configured RPC url.

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

NativeSendTx.tsx
import React, { FC, useState } from 'react'
import { Button, Text, TextInput, View } from 'react-native'
import { usePortal } from '@portal-hq/core'

/** 
 * In this example, we're using the `ethers` package.
 * This is not included with any of the Portal packages.
 *
 * If you'd like to run this example yourself, you should
 * manually install the `ethers` package.
 */
import { BigNumber } from 'ethers'

const NativeSendTx: FC = () => {
  const portal = usePortal()
  const [toAddress, setToAddress] = useState<string>('')
  const [txHash, setTxHash] = useState<string>('')

  const handleSend = async () => {
    // Build a transaction to be sent
    const transaction = {
      data: '',
      to: toAddress,
      value: BigNumber.from('1').toHexString(),
      gasPrice: '0x6000',
      from: await portal.keychain.getAddress(),
    }

    // Use the Portal Web3 Provider to make requests
    const newTxHash = await portal.request({
      method: 'eth_sendTransaction',
      params: transaction,
    })

    setTxHash(newTxHash)
    setToAddress('')
  }

  return (
    <View>
      <Text>Send 1 WEI</Text>
      <TextInput
        onChangeText={setToAddress}
        placeholder="Enter an address"
        value={toAddress}
      />

      <View style={{ marginTop: 10 }}>
        <Button
          disabled={!portal || !toAddress || !toAddress.length}
          onPress={handleSend}
          title="Send 1 Wei"
        />
      </View>

      {txHash && txHash.length > 0 && (
        <View>
          <Text>Successfully sent transaction!</Text>
          <Text>TXHash: {txHash}</Text>
        </View>
      )}
    </View>
  )
}

export default NativeSendTx

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.

// Build a transaction to be sent
const transaction = {
  from: await portal.keychain.getAddress(),
  to: toAddress,
  data: '', // empty data
  value: BigNumber.from('1').toHexString(), // example value
}

// Use the Portal Web3 Provider to make requests
const gas = await portal.request({
  method: 'eth_estimateGas',
  params: transaction,
})

console.log(gas)
// "0x5208"

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