Examples
Basic Transaction Submission
Submit a simple transaction to transfer ETH:
import { Tachyon, ChainId } from '@rathfi/tachyon';
const tachyon = new Tachyon({
apiKey: process.env.TACHYON_API_KEY!
});
async function sendEth() {
try {
const txId = await tachyon.relay({
chainId: ChainId.BASE,
to: '0x742d35cc6634C0532925a3b8D1C9b53e6aC3',
value: '1000000000000000000', // 1 ETH in wei
callData: '0x',
label: 'Send 1 ETH'
});
console.log('Transaction submitted:', txId);
return txId;
} catch (error) {
console.error('Failed to submit transaction:', error);
}
}
Smart Contract Interaction
Interact with a smart contract by encoding function calls:
import { ethers } from 'ethers';
async function callContract() {
// Encode function call data
const iface = new ethers.Interface([
'function transfer(address to, uint256 amount)'
]);
const callData = iface.encodeFunctionData('transfer', [
'0x742d35cc6634C0532925a3b8D1C9b53e6aC3',
ethers.parseUnits('100', 18) // 100 tokens
]);
const txId = await tachyon.relay({
chainId: ChainId.ETHEREUM,
to: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC contract
value: '0',
callData,
label: 'Transfer USDC',
gasLimit: '100000'
});
return txId;
}
Transaction Status Monitoring
Monitor transaction status with real-time updates:
async function monitorTransaction(txId: string) {
try {
// Get initial status
let status = await tachyon.getRelayStatus(txId);
console.log('Initial status:', status.status);
// Wait for execution with custom timeout
if (status.status !== 'EXECUTED') {
console.log('Waiting for execution...');
const executedTx = await tachyon.waitForExecutionHash(txId);
console.log('Transaction executed!');
console.log('Execution hash:', executedTx.executionTxHash);
console.log('Gas cost:', executedTx.costUSD, 'USD');
}
} catch (error) {
console.error('Transaction failed:', error);
}
}
Multi-Chain Operations
Work with multiple blockchain networks:
async function multiChainExample() {
// Submit transactions to multiple chains
const transactions = [
{
chainId: ChainId.ETHEREUM,
to: '0x742d35cc6634C0532925a3b8D1C9b53e6aC3',
value: '0',
callData: '0x',
label: 'Ethereum transaction'
},
{
chainId: ChainId.BASE,
to: '0x742d35cc6634C0532925a3b8D1C9b53e6aC3',
value: '0',
callData: '0x',
label: 'Base transaction'
}
];
const txIds = await Promise.all(
transactions.map(tx => tachyon.relay(tx))
);
console.log('Submitted transactions:', txIds);
}
Account Management
Check account information and balances:
async function checkAccount(walletAddress: string) {
try {
const accountInfo = await tachyon.checkAccountInfo(walletAddress);
console.log('Account Info:');
console.log('- User ID:', accountInfo.userId);
console.log('- Address:', accountInfo.address);
console.log('- Available Balance:', accountInfo.availableBalance);
console.log('- Pending Balance:', accountInfo.pendingBalance);
console.log('Default Tachyon Account:');
console.log('- Address:', accountInfo.defaultTachyonAccount.address);
console.log('- Chain ID:', accountInfo.defaultTachyonAccount.chainId);
console.log('- Token:', accountInfo.defaultTachyonAccount.token.symbol);
return accountInfo;
} catch (error) {
console.error('Failed to fetch account info:', error);
}
}
Last updated on