@@ -3,10 +3,10 @@ import bs58 from 'bs58';
33import * as dotenv from 'dotenv' ;
44dotenv . config ( ) ;
55
6+ // Setup: initialize wallet and client
67const API = 'https://stargate.finance/api/v2' ;
78const API_KEY = process . env . STARGATE_API_KEY ! ;
89const PRIVATE_KEY = process . env . SOLANA_PRIVATE_KEY ! ;
9-
1010const connection = new web3 . Connection ( web3 . clusterApiUrl ( 'mainnet-beta' ) , 'confirmed' ) ;
1111
1212type FeeTolerance = { type : 'PERCENT' ; amount ?: number } ;
@@ -53,6 +53,7 @@ function parseSolanaSecretKey(raw: string): Uint8Array {
5353
5454const keypair = web3 . Keypair . fromSecretKey ( parseSolanaSecretKey ( PRIVATE_KEY ) ) ;
5555
56+ // Helper functions for API requests
5657async function postJson < T > ( path : string , body : unknown ) : Promise < T > {
5758 const res = await fetch ( `${ API } ${ path } ` , {
5859 method : 'POST' ,
@@ -75,6 +76,9 @@ async function getJson<T>(path: string): Promise<T> {
7576 return res . json ( ) as Promise < T > ;
7677}
7778
79+ // Core API operations for Stargate cross-chain transfers.
80+ // Here, we fetch quotes for sending OFT tokens (CAW) from Solana to Arbitrum.
81+ // The options specify to use the exact source amount and include a fee tolerance of 2%.
7882async function fetchQuotes ( ) : Promise < GetQuotesResult > {
7983 const payload : GetQuotesInput = {
8084 srcChainKey : 'solana' ,
@@ -86,22 +90,29 @@ async function fetchQuotes(): Promise<GetQuotesResult> {
8690 amount : '1000000000000' ,
8791 options : {
8892 amountType : 'EXACT_SRC_AMOUNT' ,
89- feeTolerance : { type : 'PERCENT' , amount : 20 } ,
93+ feeTolerance : { type : 'PERCENT' , amount : 2 } ,
9094 dstNativeDropAmount : 0 ,
9195 } ,
9296 } ;
9397 return postJson < GetQuotesResult > ( '/quotes' , payload ) ;
9498}
9599
100+ // Builds user-interactive steps required to complete the transaction.
101+ // Useful for both signature requests (like EIP-712) and direct Solana transactions.
102+ // Can be integrated into a UI to guide users through signing messages or submitting transactions.
96103async function buildUserSteps ( quoteId : string ) {
97104 return postJson < BuildUserStepsResult > ( '/build-user-steps' , { quoteId } ) ;
98105}
99106
107+ // Checks the status of a transaction.
108+ // Useful for monitoring the progress of a transaction.
109+ // Can be integrated into a UI to display the status of a transaction.
100110async function getStatus ( quoteId : string , txSig ?: string ) {
101111 const query = txSig ? `?txHash=${ encodeURIComponent ( txSig ) } ` : '' ;
102112 return getJson < GetStatusResult > ( `/status/${ encodeURIComponent ( quoteId ) } ${ query } ` ) ;
103113}
104114
115+ // Execution logic
105116async function pollStatus ( quoteId : string , txSig ?: string ) {
106117 const deadline = Date . now ( ) + 5 * 60_000 ;
107118 for ( ; ; ) {
0 commit comments