@zebec-network/exchange-card-sdk 1.1.3 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { StellarWalletsKit } from "@creit.tech/stellar-wallets-kit";
2
+ import { Asset } from "@stellar/stellar-sdk";
2
3
  import { APIConfig } from "../helpers/apiHelpers";
3
4
  import { Quote } from "../types";
4
5
  export declare class StellarService {
@@ -42,6 +43,14 @@ export declare class StellarService {
42
43
  * @throws {Error} If the quote is invalid or expired, if there is not enough balance, or if the transaction fails.
43
44
  */
44
45
  transferXLM(amount: string): Promise<string>;
46
+ /**
47
+ * Transfers USDC tokens.
48
+ *
49
+ * @param {string} amount - The amount of USDC to transfer.
50
+ * @returns {Promise<string>} A promise that resolves to the transaction hash.
51
+ * @throws {Error} If there is not enough USDC balance or if the transaction fails.
52
+ */
53
+ transferUSDC(amount: string): Promise<string>;
45
54
  /**
46
55
  * Retrieves the balance of the specified wallet.
47
56
  *
@@ -49,4 +58,12 @@ export declare class StellarService {
49
58
  * @returns {Promise<string>} - A promise that resolves to the balance of the wallet.
50
59
  */
51
60
  getWalletBalance(wallet: string): Promise<string>;
61
+ /**
62
+ * Retrieves the balance of a specific token for the specified wallet.
63
+ *
64
+ * @param {string} wallet - The public key of the wallet to get the token balance for.
65
+ * @param {Asset} asset - The asset object representing the token.
66
+ * @returns {Promise<string>} - A promise that resolves to the balance of the token.
67
+ */
68
+ getTokenBalance(wallet: string, asset: Asset): Promise<string>;
52
69
  }
@@ -4,6 +4,11 @@ exports.StellarService = void 0;
4
4
  const stellar_sdk_1 = require("@stellar/stellar-sdk");
5
5
  const constants_1 = require("../constants");
6
6
  const apiHelpers_1 = require("../helpers/apiHelpers");
7
+ // Add USDC asset constants
8
+ const USDC_ISSUER = {
9
+ TESTNET: "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
10
+ MAINNET: "GA5ZSEJYB37JRC5AVCIA5MOP4RHTM335X2KGX3IHOJAPP5RE34K4KZVN",
11
+ };
7
12
  class StellarService {
8
13
  kit;
9
14
  apiService;
@@ -103,6 +108,68 @@ class StellarService {
103
108
  }
104
109
  throw new Error("Max retries reached");
105
110
  }
111
+ /**
112
+ * Transfers USDC tokens.
113
+ *
114
+ * @param {string} amount - The amount of USDC to transfer.
115
+ * @returns {Promise<string>} A promise that resolves to the transaction hash.
116
+ * @throws {Error} If there is not enough USDC balance or if the transaction fails.
117
+ */
118
+ async transferUSDC(amount) {
119
+ // Fetch deposit address
120
+ const vault = await this.fetchVault();
121
+ const accountAddress = await this.kit.getAddress();
122
+ // Prepare transaction
123
+ const account = await this.server.loadAccount(accountAddress.address);
124
+ const fee = await this.server.fetchBaseFee();
125
+ const memo = stellar_sdk_1.Memo.id(vault.tag?.toString() || "");
126
+ // Create USDC asset object
127
+ const usdcAsset = new stellar_sdk_1.Asset("USDC", this.sandbox ? USDC_ISSUER.TESTNET : USDC_ISSUER.MAINNET);
128
+ // Check Wallet balance
129
+ const balance = await this.getTokenBalance(accountAddress.address, usdcAsset);
130
+ if (Number(balance) < Number(amount)) {
131
+ throw new Error("Insufficient USDC balance");
132
+ }
133
+ // Build and submit transaction
134
+ const transaction = new stellar_sdk_1.TransactionBuilder(account, {
135
+ fee: fee.toString(),
136
+ networkPassphrase: this.sandbox ? stellar_sdk_1.Networks.TESTNET : stellar_sdk_1.Networks.PUBLIC,
137
+ })
138
+ .addOperation(stellar_sdk_1.Operation.payment({
139
+ destination: vault.address,
140
+ asset: usdcAsset,
141
+ amount,
142
+ }))
143
+ .addMemo(memo)
144
+ .setTimeout(stellar_sdk_1.TimeoutInfinite)
145
+ .build();
146
+ // Sign the transaction
147
+ const { signedTxXdr } = await this.kit.signTransaction(transaction.toXDR());
148
+ const tx = stellar_sdk_1.TransactionBuilder.fromXDR(signedTxXdr, this.sandbox ? stellar_sdk_1.Networks.TESTNET : stellar_sdk_1.Networks.PUBLIC);
149
+ let retries = 0;
150
+ const maxRetries = 5;
151
+ let delay = 1000;
152
+ while (retries < maxRetries) {
153
+ try {
154
+ const transactionResult = await this.server.submitTransaction(tx, {
155
+ skipMemoRequiredCheck: false,
156
+ });
157
+ const txHash = transactionResult.hash;
158
+ return txHash;
159
+ }
160
+ catch (error) {
161
+ console.debug("error: ", error);
162
+ if (retries >= maxRetries) {
163
+ throw error;
164
+ }
165
+ retries += 1;
166
+ console.debug(`Retrying in ${delay / 1000} seconds...`);
167
+ await new Promise((resolve) => setTimeout(resolve, delay));
168
+ delay *= 2; // Exponential backoff
169
+ }
170
+ }
171
+ throw new Error("Max retries reached");
172
+ }
106
173
  /**
107
174
  * Retrieves the balance of the specified wallet.
108
175
  *
@@ -114,5 +181,25 @@ class StellarService {
114
181
  const nativeBalance = account.balances.find((balance) => balance.asset_type === "native");
115
182
  return nativeBalance ? nativeBalance.balance : "0";
116
183
  }
184
+ /**
185
+ * Retrieves the balance of a specific token for the specified wallet.
186
+ *
187
+ * @param {string} wallet - The public key of the wallet to get the token balance for.
188
+ * @param {Asset} asset - The asset object representing the token.
189
+ * @returns {Promise<string>} - A promise that resolves to the balance of the token.
190
+ */
191
+ async getTokenBalance(wallet, asset) {
192
+ const account = await this.server.loadAccount(wallet);
193
+ try {
194
+ const balance = account.balances.find((b) => b.asset_type !== "native" &&
195
+ b.asset_type !== "liquidity_pool_shares" &&
196
+ b.asset_code === asset.getCode())?.balance || "0";
197
+ return balance;
198
+ }
199
+ catch (error) {
200
+ console.debug(`Error fetching ${asset.getCode()} balance:`, error);
201
+ return "0";
202
+ }
203
+ }
117
204
  }
118
205
  exports.StellarService = StellarService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zebec-network/exchange-card-sdk",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "description": "An sdk for purchasing silver card in zebec",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",