Skip to content

Commit c220df3

Browse files
author
konrad-w3
committed
feat: add comments
1 parent 96ccadd commit c220df3

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/evm/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { base } from 'viem/chains';
44
import * as dotenv from 'dotenv';
55
dotenv.config();
66

7+
// The code demonstrates how to use the Stargate API to fetch quotes and execute transactions.
78
// Setup: initialize wallet and client
89
const API = 'https://stargate.finance/api/v2';
910
const API_KEY = process.env.STARGATE_API_KEY!;
@@ -90,7 +91,9 @@ async function getJson<T>(path: string): Promise<T> {
9091
return res.json() as Promise<T>;
9192
}
9293

93-
// Core API calls
94+
// Core API operations for Stargate cross-chain transfers.
95+
// Here, we fetch quotes for sending native tokens (ETH) from Base to Optimism.
96+
// The options specify to use the exact source amount and include a fee tolerance of 2%.
9497
async function fetchQuotes(): Promise<GetQuotesResult> {
9598
const payload: GetQuotesInput = {
9699
srcTokenAddress: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
@@ -102,21 +105,30 @@ async function fetchQuotes(): Promise<GetQuotesResult> {
102105
dstWalletAddress: account.address,
103106
options: {
104107
amountType: 'EXACT_SRC_AMOUNT',
105-
feeTolerance: { type: 'PERCENT', amount: 50 },
108+
feeTolerance: { type: 'PERCENT', amount: 2 },
106109
dstNativeDropAmount: 0,
107110
},
108111
};
109112
return postJson<GetQuotesResult>('/quotes', payload);
110113
}
111114

115+
// Builds user-interactive steps required to complete the transaction.
116+
// Useful for both signature requests (like EIP-712) and direct EVM transactions.
117+
// Can be integrated into a UI to guide users through signing messages or submitting transactions.
112118
async function buildUserSteps(quoteId: string) {
113119
return postJson<BuildUserStepsResult>('/build-user-steps', { quoteId });
114120
}
115121

122+
// Submits signatures for a given quote.
123+
// Required for EIP-712 messages that need to be signed by the user.
124+
// Can be integrated into a UI to submit signatures after users have signed messages.
116125
async function submitSignature(quoteId: string, signatures: string[]) {
117126
await postJson<Record<string, never>>('/submit-signature', { quoteId, signatures });
118127
}
119128

129+
// Checks the status of a transaction.
130+
// Useful for monitoring the progress of a transaction.
131+
// Can be integrated into a UI to display the status of a transaction.
120132
async function getStatus(quoteId: string, txHash?: Hex) {
121133
const query = txHash ? `?txHash=${txHash}` : '';
122134
return getJson<GetStatusResult>(`/status/${encodeURIComponent(quoteId)}${query}`);

src/solana/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import bs58 from 'bs58';
33
import * as dotenv from 'dotenv';
44
dotenv.config();
55

6+
// Setup: initialize wallet and client
67
const API = 'https://stargate.finance/api/v2';
78
const API_KEY = process.env.STARGATE_API_KEY!;
89
const PRIVATE_KEY = process.env.SOLANA_PRIVATE_KEY!;
9-
1010
const connection = new web3.Connection(web3.clusterApiUrl('mainnet-beta'), 'confirmed');
1111

1212
type FeeTolerance = { type: 'PERCENT'; amount?: number };
@@ -53,6 +53,7 @@ function parseSolanaSecretKey(raw: string): Uint8Array {
5353

5454
const keypair = web3.Keypair.fromSecretKey(parseSolanaSecretKey(PRIVATE_KEY));
5555

56+
// Helper functions for API requests
5657
async 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%.
7882
async 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.
96103
async 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.
100110
async 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
105116
async function pollStatus(quoteId: string, txSig?: string) {
106117
const deadline = Date.now() + 5 * 60_000;
107118
for (;;) {

0 commit comments

Comments
 (0)