Transaction Examples
Common transaction patterns with the TypeScript SDK.
Basic Send
import { RP1Client, Wallet } from '@rp1/sdk';
const client = new RP1Client('https://rpc.rp.one');
const wallet = Wallet.fromMnemonic('your mnemonic...');
// Simple send
const tx = await client.send({
from: wallet,
to: 'rp1recipient...',
amount: '1000000',
denom: 'urp1',
});
console.log(`TX Hash: ${tx.hash}`);
Multi-Send
const tx = await client.multiSend({
from: wallet,
outputs: [
{ address: 'rp1recipient1...', coins: [{ denom: 'urp1', amount: '1000000' }] },
{ address: 'rp1recipient2...', coins: [{ denom: 'urp1', amount: '2000000' }] },
{ address: 'rp1recipient3...', coins: [{ denom: 'urp1', amount: '3000000' }] },
],
});
Custom Gas
const tx = await client.send({
from: wallet,
to: 'rp1recipient...',
amount: '1000000',
denom: 'urp1',
// Custom gas settings
gas: '100000',
gasPrice: '0.025urp1',
});
With Memo
const tx = await client.send({
from: wallet,
to: 'rp1recipient...',
amount: '1000000',
denom: 'urp1',
memo: 'Payment for services',
});
DEX Swap
import { DEXModule } from '@rp1/sdk';
const dex = new DEXModule(client);
// Get quote first
const quote = await dex.getQuote({
poolId: '1',
tokenIn: 'urp1',
amountIn: '1000000',
});
console.log(`Expected output: ${quote.amountOut}`);
console.log(`Price impact: ${quote.priceImpact}%`);
// Execute swap with 1% slippage tolerance
const minOutput = BigInt(quote.amountOut) * 99n / 100n;
const tx = await dex.swap(wallet, {
poolId: '1',
tokenIn: { denom: 'urp1', amount: '1000000' },
minTokenOut: minOutput.toString(),
});
Add Liquidity
const tx = await dex.addLiquidity(wallet, {
poolId: '1',
tokenAAmount: '1000000',
tokenBAmount: '500000',
minShares: '450000', // Minimum LP tokens to receive
});
Lending Operations
import { LendingModule } from '@rp1/sdk';
const lending = new LendingModule(client);
// Supply
await lending.supply(wallet, '1000000', 'uusdc');
// Enable as collateral
await lending.enableCollateral(wallet, 'uusdc');
// Borrow
await lending.borrow(wallet, '500000', 'uusdc');
// Repay
await lending.repay(wallet, '500000', 'uusdc');
Staking
import { StakingModule } from '@rp1/sdk';
const staking = new StakingModule(client);
// Delegate to validator
await staking.delegate(wallet, 'rp1valoper...', '10000000');
// Claim rewards
await staking.claimRewards(wallet, 'rp1valoper...');
// Undelegate (21 day unbonding)
await staking.undelegate(wallet, 'rp1valoper...', '5000000');
Governance
import { GovModule } from '@rp1/sdk';
const gov = new GovModule(client);
// Vote on proposal
await gov.vote(wallet, proposalId, 'VOTE_OPTION_YES');
// Submit proposal
await gov.submitProposal(wallet, {
title: 'Parameter Change',
description: 'Adjust fee burn rate',
type: 'ParameterChange',
changes: [
{ subspace: 'feeburn', key: 'BurnPercentage', value: '0.60' },
],
deposit: '10000000urp1',
});
Batch Transactions
// Build multiple messages
const messages = [
client.buildSendMsg(wallet.address, 'rp1a...', '100000', 'urp1'),
client.buildSendMsg(wallet.address, 'rp1b...', '200000', 'urp1'),
client.buildSendMsg(wallet.address, 'rp1c...', '300000', 'urp1'),
];
// Sign and broadcast as single transaction
const tx = await client.signAndBroadcast(wallet, messages);
Transaction Monitoring
// Wait for transaction
const tx = await client.send({ ... });
// Poll for confirmation
const confirmed = await client.waitForTx(tx.hash, {
timeout: 60000,
pollInterval: 1000,
});
console.log(`Confirmed at height: ${confirmed.height}`);
console.log(`Gas used: ${confirmed.gasUsed}`);
Error Handling
import { TxFailedError, InsufficientFundsError } from '@rp1/sdk';
try {
await client.send({ ... });
} catch (error) {
if (error instanceof InsufficientFundsError) {
console.log('Need more tokens!');
console.log(`Required: ${error.required}`);
console.log(`Available: ${error.available}`);
} else if (error instanceof TxFailedError) {
console.log(`Transaction failed!`);
console.log(`Code: ${error.code}`);
console.log(`Message: ${error.message}`);
// Common error codes
switch (error.code) {
case 5: console.log('Insufficient funds'); break;
case 11: console.log('Out of gas'); break;
case 13: console.log('Insufficient fee'); break;
default: console.log('Unknown error');
}
}
}
Simulate Before Broadcast
// Build unsigned transaction
const unsignedTx = client.buildTx({
messages: [sendMsg],
memo: 'Test',
});
// Simulate to estimate gas
const simulation = await client.simulate(unsignedTx, wallet.address);
console.log(`Estimated gas: ${simulation.gasUsed}`);
// Broadcast with buffer
const tx = await client.signAndBroadcast(wallet, unsignedTx.messages, {
gas: Math.ceil(simulation.gasUsed * 1.3).toString(),
});