Simple Payment Application
A complete payment application with instant confirmations and balance tracking.
payment-app.ts
import { createWalletClient, createPublicClient, http, parseEther, formatEther } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { riseTestnet } from 'viem/chains'
import { sendRawTransactionSync } from 'shreds/viem'
// Setup
const account = privateKeyToAccount('0x...')
const walletClient = createWalletClient({
account,
chain: riseTestnet,
transport: http()
})
const publicClient = createPublicClient({
chain: riseTestnet,
transport: http()
})
// Payment function with instant confirmation
async function sendPayment(to: string, amount: string) {
console.log(`Sending ${amount} ETH to ${to}...`)
try {
// Get current balance
const balanceBefore = await publicClient.getBalance({
address: account.address
})
// Prepare and sign transaction
const request = await walletClient.prepareTransactionRequest({
to: to as `0x${string}`,
value: parseEther(amount)
})
const serializedTransaction = await walletClient.signTransaction(request)
// Send with instant confirmation
const receipt = await sendRawTransactionSync(publicClient, {
serializedTransaction
})
console.log('[SUCCESS] Payment confirmed instantly!')
console.log('Receipt:', receipt)
// Check new balance
const balanceAfter = await publicClient.getBalance({
address: account.address
})
console.log('Balance change:',
formatEther(balanceBefore - balanceAfter), 'ETH'
)
return receipt
} catch (error) {
console.error('[ERROR] Payment failed:', error.message)
throw error
}
}
// Example usage
await sendPayment('0x742d35Cc6634C0532925a3b844Bc9e7595f6E98d', '0.1')
Key Features
- Instant Confirmation: Transactions are confirmed in 3-5ms
- Balance Tracking: Shows balance changes immediately after transaction
- Error Handling: Proper error handling for failed transactions
- Type Safety: Full TypeScript support with viem
Usage
-
Install dependencies:
npm install viem shreds
-
Set up your private key and run the payment function
-
The transaction will be confirmed instantly, showing the receipt and balance change
Next Steps
- Add transaction history tracking
- Implement multi-signature support
- Create a user interface
- Add support for ERC-20 tokens