Skip to Content

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); } }

Webhooks Registration

Register webhooks to receive tx updates

const payload = [ { url: "http://abcd.com/webhook", authType: "bearer", secret: "b12f7d89c33c4a6e9a8f02a5bdff1c7e", enabled: true, }, { url: "http://efgh.com/webhook2", authType: "api-key", apiKeyPlacement: "header", apiKeyVar: "secret-api-key", secret: "b12f7d89c33c4a6e9a8f02a5bdff1c7e", enabled: true, }, ]; const res = await tachyon.registerWebhooks(payload);

Websocket Registration

Register websocket to receive tx updates

const res = await tachyon.registerWebsocket();

Websocket connection

Connect to tachyon using a websocket connection for tx updates

const socket = tachyon.connectWebSocket( wsRes.apiKey, // input your secret api key (msg: WebsocketMessage) => { console.log("Received message:", msg); }, (err) => { console.error("WebSocket error:", err); }, () => { console.log("WebSocket closed"); } );
Last updated on