@blockscout/app-sdk
Version:
A React SDK for Blockscout integration
186 lines (135 loc) • 4.13 kB
Markdown
A React SDK for integrating Blockscout transaction notifications and transaction history into your dApp.
- Transaction Toast Notifications
- Transaction History Popup
- Transaction interpretation and summaries
- Support for multiple chains
- Mobile-responsive design
```bash
npm install @blockscout/app-sdk
yarn add @blockscout/app-sdk
```
The transaction toast feature provides real-time notifications for transaction status updates. It shows pending, success, and error states with detailed transaction information.
Wrap your application with the `NotificationProvider`:
```tsx
import { NotificationProvider } from "@blockscout/app-sdk";
function App() {
return (
<NotificationProvider>
<YourApp />
</NotificationProvider>
);
}
```
```tsx
import { useNotification } from "@blockscout/app-sdk";
function YourComponent() {
const { openTxToast } = useNotification();
const handleTransaction = async (txHash) => {
try {
// Your transaction logic here
await sendTransaction();
// Show transaction toast
openTxToast("1", txHash); // '1' is the chain ID for Ethereum mainnet
} catch (error) {
console.error("Transaction failed:", error);
}
};
return (
<button onClick={() => handleTransaction("0x123...")}>
Send Transaction
</button>
);
}
```
The transaction history popup allows users to view recent transactions for a specific address or all transactions on a chain.
Wrap your application with the `TransactionPopupProvider`:
```tsx
import { TransactionPopupProvider } from "@blockscout/app-sdk";
function App() {
return (
<TransactionPopupProvider>
<YourApp />
</TransactionPopupProvider>
);
}
```
```tsx
import { useTransactionPopup } from "@blockscout/app-sdk";
function YourComponent() {
const { openPopup } = useTransactionPopup();
// Show transactions for a specific address
const showAddressTransactions = () => {
openPopup({
chainId: "1", // Ethereum mainnet
address: "0x123...", // Optional: specific address
});
};
// Show all transactions for a chain
const showAllTransactions = () => {
openPopup({
chainId: "1", // Ethereum mainnet
});
};
return (
<div>
<button onClick={showAddressTransactions}>
View Address Transactions
</button>
<button onClick={showAllTransactions}>View All Transactions</button>
</div>
);
}
```
- Real-time status updates (pending, success, error)
- Transaction interpretation and summaries
- Links to block explorer
- Automatic status polling
- Error handling with revert reasons
### Transaction Popup
- View recent transactions
- Filter by address
- Transaction status indicators
- Transaction interpretation
- Links to block explorer
- Mobile-responsive design
- Loading states and error handling
## Chain Compatibility
The SDK is compatible with any blockchain that has a Blockscout explorer instance with API v2 support. These chains are listed in the [Chainscout](https://chains.blockscout.com/). To verify if your target chain is supported, visit our [compatibility checker](https://sdk-compatibility.blockscout.com/).
Here are some common supported chain IDs:
- `1`: Ethereum Mainnet
- `137`: Polygon Mainnet
- `42161`: Arbitrum One
- `10`: Optimism
## API Reference
### useNotification Hook
```typescript
const { openTxToast } = useNotification();
// Open a transaction toast
openTxToast(chainId: string, hash: string): Promise<void>
```
```typescript
const { openPopup } = useTransactionPopup();
// Open transaction popup
openPopup(options: {
chainId: string;
address?: string;
}): void
```
This project is currently closed for external contributions. We appreciate your interest, but we are not accepting pull requests at this time.
MIT