Function Signature
Future<PortalProviderResponse> request({
required String chainId,
required String method,
required List<Object?> params,
String? signatureApprovalMemo,
String? idempotencyKey,
})
Description
Make a generic JSON-RPC request to the blockchain. This allows you to call any RPC method supported by the chain.Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
chainId | String | Yes | The chain ID in CAIP-2 format |
method | String | Yes | The JSON-RPC method name |
params | List<Object?> | Yes | The method parameters |
signatureApprovalMemo | String | No | Optional memo for signing operations |
idempotencyKey | String? | No | Key that stops Portal from broadcasting the same transaction twice. Honored for eth_sendTransaction, sol_signAndSendTransaction and sol_signAndConfirmTransaction. Throws IDEMPOTENCY_KEY_UNSUPPORTED for eth_sendRawTransaction and sol_sendTransaction. For other methods, the key is validated and then ignored with a warning. Generate one with generateIdempotencyKey. See Idempotency keys. |
Returns
PortalProviderResponse - An object containing:
| Property | Type | Description |
|---|---|---|
result | dynamic | The result of the RPC call |
error | dynamic | Error information if the call failed |
Example
import 'package:portal_flutter/portal_flutter.dart';
final portal = Portal();
// Get account balance
final response = await portal.request(
chainId: 'eip155:1',
method: 'eth_getBalance',
params: ['0xYourAddress', 'latest'],
);
print('Balance: ${response.result}');
Common RPC Methods
// Get block number
final blockResponse = await portal.request(
chainId: 'eip155:1',
method: 'eth_blockNumber',
params: [],
);
// Estimate gas
final gasResponse = await portal.request(
chainId: 'eip155:1',
method: 'eth_estimateGas',
params: [{
'from': '0xFromAddress',
'to': '0xToAddress',
'value': '0x10',
}],
);
// Call contract (read-only)
final callResponse = await portal.request(
chainId: 'eip155:1',
method: 'eth_call',
params: [{
'to': '0xContractAddress',
'data': '0x70a08231...', // balanceOf encoded
}, 'latest'],
);
Signing Methods
// personal_sign
final signResponse = await portal.request(
chainId: 'eip155:1',
method: 'personal_sign',
params: ['0x48656c6c6f', '0xYourAddress'],
signatureApprovalMemo: 'Sign hello message',
);
// eth_sendTransaction
final txResponse = await portal.request(
chainId: 'eip155:1',
method: 'eth_sendTransaction',
params: [{
'from': '0xYourAddress',
'to': '0xRecipientAddress',
'value': '0x10',
}],
);
// eth_sendTransaction with an idempotency key, safe to retry with the same key
final idempotencyKey = generateIdempotencyKey(); // Store it with this transfer
final idempotentTxResponse = await portal.request(
chainId: 'eip155:1',
method: 'eth_sendTransaction',
params: [{
'from': '0xYourAddress',
'to': '0xRecipientAddress',
'value': '0x10',
}],
idempotencyKey: idempotencyKey,
);
Errors
| Code | Description |
|---|---|
NOT_INITIALIZED | Portal was not initialized |
RPC_ERROR | The RPC call returned an error |
NETWORK_ERROR | Network connectivity issue |
INVALID_IDEMPOTENCY_KEY | idempotencyKey is empty, longer than 255 characters, or contains a character outside A-Z a-z 0-9 . _ ~ - |
IDEMPOTENCY_KEY_UNSUPPORTED | idempotencyKey was passed with eth_sendRawTransaction or sol_sendTransaction |
IDEMPOTENT_*, IDEMPOTENCY_KEY_REUSED | Portal rejected the request because of its idempotencyKey. See Handle a rejection |
request returns most failures in PortalProviderResponse.error, but it throws a PortalException for the idempotency codes above.