Skip to content
LogoLogo

Send a transaction

Use the OFGetEthereumProviderParams method on the Ethereum provider to send a transaction with an Ethereum wallet.

Create a transaction

Usage

do {
    guard let provider = try await openfort.getEthereumProvider(params: OFGetEthereumProviderParams()) else {
        return
    }
    
    let tx: [String: String] = [
        "to": "0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7",
        "from": "0xDeaDbeefdEAdbeefdEadbEEFdeadbeefDEADbEEF",
        "gas": "0x76c0", // 30400 in hex
        "value": "0x8ac7230489e80000", // 10 ETH in wei hex
        "data": "0x",
        "gasPrice": "0x4a817c800" // 20 gwei
    ]

    let request = RPCRequest<[ [String: String] ]>(
        id: 1,                        // your jsonrpc id
        jsonrpc: "2.0",
        method: "eth_sendTransaction",
        params: [tx]   // array of transaction objects
    )

    provider.send(request: request) { (resp: Web3Response<String>) in
        if let chainIdHex: String = resp.result {
            let chainIdDec = Int(chainIdHex.dropFirst(2), radix: 16) ?? -1
            print("chainId (hex): \(chainIdHex), (dec): \(chainIdDec)")
        }
    }
} catch {
    print("\(error)")
}

Parameters

struct RPCRequest<Params: Codable>: Codable {

    /// The rpc id
    public let id: Int

    /// The jsonrpc version. Typically 2.0
    public let jsonrpc: String

    /// The jsonrpc method to be called
    public let method: String

    /// The jsonrpc parameters
    public let params: Params
}

Transaction parameters

- **from** – the sender’s address (required).
- **to** – the recipient’s address (optional for contract creation, omit to deploy a contract).
- **gas** – maximum amount of gas units allowed for execution.
- **gasPrice** – price per unit of gas (legacy gas pricing).
- **maxFeePerGas** – maximum total fee per gas (EIP-1559).
- **maxPriorityFeePerGas** – priority fee (tip) per gas (EIP-1559).
- **value** – amount of wei to send (use hex string).
- **data** – ABI-encoded call data (for contract interaction).
- **nonce** – the transaction nonce (useful if not letting the provider pick it).

Returns

Web3Response<Result: Codable>

Create a sponsored transaction

Usage

do {
    let params = OFGetEthereumProviderParams(
        policy: "YOUR_POLICY_ID", // [!code focus]
        announceProvider: true                    // optional
        // chains / providerInfo if you need them…
    )
    guard let provider = try await openfort.getEthereumProvider(params: params) else {
        print("No provider")
        return
    }

    let tx: [String: String] = [
        "to":   "0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7",
        "from": "0xDeaDbeefdEAdbeefdEadbEEFdeadbeefDEADbEEF",
        "value":"0x8ac7230489e80000", // 10 ETH in wei (hex)
        "data": "0x"                  // contract call? put ABI-encoded data here
        // leave out "gas" / "gasPrice" for sponsored flow
    ]

    let request = RPCRequest<[[String:String]]>(
        id: 1,                        // your jsonrpc id
        jsonrpc: "2.0",
        method: "eth_sendTransaction",
        params: [tx]
    )

    provider.send(request: request) { (resp: Web3Response<String>) in
        
    }
} catch {
    print("Provider error: \(error)")
}

Parameters

struct RPCRequest<Params: Codable>: Codable {

    /// The rpc id
    public let id: Int

    /// The jsonrpc version. Typically 2.0
    public let jsonrpc: String

    /// The jsonrpc method to be called
    public let method: String

    /// The jsonrpc parameters
    public let params: Params
}

Transaction parameters

- **from** – the sender’s address (required).
- **to** – the recipient’s address (optional for contract creation, omit to deploy a contract).
- **gas** – maximum amount of gas units allowed for execution.
- **gasPrice** – price per unit of gas (legacy gas pricing).
- **maxFeePerGas** – maximum total fee per gas (EIP-1559).
- **maxPriorityFeePerGas** – priority fee (tip) per gas (EIP-1559).
- **value** – amount of wei to send (use hex string).
- **data** – ABI-encoded call data (for contract interaction).
- **nonce** – the transaction nonce (useful if not letting the provider pick it).

Returns

Web3Response<Result: Codable>