@zebec-network/exchange-card-sdk 1.1.2 → 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.
@@ -41,7 +41,7 @@ export declare class StellarService {
41
41
  * @throws {InvalidEmailError} If the recipient's email address is invalid.
42
42
  * @throws {Error} If the quote is invalid or expired, if there is not enough balance, or if the transaction fails.
43
43
  */
44
- transferXDB(amount: number): Promise<string>;
44
+ transferXDB(amount: string): Promise<string>;
45
45
  /**
46
46
  * Retrieves the balance of the specified wallet.
47
47
  *
@@ -53,18 +53,14 @@ class StellarService {
53
53
  async transferXDB(amount) {
54
54
  // Fetch deposit address
55
55
  const vault = await this.fetchVault();
56
- console.log("depositAddress", vault.address);
57
- console.log("tag", vault.tag);
58
56
  const accountAddress = await this.kit.getAddress();
59
57
  // Prepare transaction
60
58
  const account = await this.server.loadAccount(accountAddress.address);
61
59
  const fee = await this.server.fetchBaseFee();
62
- const platform_fee = (amount * constants_1.PLATFORM_FEE) / 10000;
63
- const totalAmount = (amount + platform_fee).toString();
64
60
  const memo = stellar_sdk_1.Memo.id(vault.tag?.toString() || "");
65
61
  // Check Wallet balance
66
62
  const balance = await this.getWalletBalance(accountAddress.address);
67
- if (Number(balance) < Number(totalAmount)) {
63
+ if (Number(balance) < Number(amount)) {
68
64
  throw new Error("Insufficient balance");
69
65
  }
70
66
  // Build and submit transaction
@@ -75,14 +71,14 @@ class StellarService {
75
71
  .addOperation(stellar_sdk_1.Operation.payment({
76
72
  destination: vault.address,
77
73
  asset: stellar_sdk_1.Asset.native(),
78
- amount: totalAmount,
74
+ amount,
79
75
  }))
80
76
  .addMemo(memo)
81
77
  .setTimeout(stellar_sdk_1.TimeoutInfinite)
82
78
  .build();
83
79
  // Sign the transaction
84
80
  const { signedTxXdr } = await this.kit.signTransaction(transaction.toXDR());
85
- const tx = stellar_sdk_1.TransactionBuilder.fromXDR(signedTxXdr, this.sandbox ? stellar_sdk_1.Networks.TESTNET : stellar_sdk_1.Networks.PUBLIC);
81
+ const tx = stellar_sdk_1.TransactionBuilder.fromXDR(signedTxXdr, this.sandbox ? constants_1.XDB_PASSPHRASE.Sandbox : constants_1.XDB_PASSPHRASE.Production);
86
82
  let retries = 0;
87
83
  const maxRetries = 5;
88
84
  let delay = 1000;
@@ -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 {
@@ -41,7 +42,15 @@ export declare class StellarService {
41
42
  * @throws {InvalidEmailError} If the recipient's email address is invalid.
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
- transferXLM(amount: number): Promise<string>;
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;
@@ -53,18 +58,14 @@ class StellarService {
53
58
  async transferXLM(amount) {
54
59
  // Fetch deposit address
55
60
  const vault = await this.fetchVault();
56
- console.log("depositAddress", vault.address);
57
- console.log("tag", vault.tag);
58
61
  const accountAddress = await this.kit.getAddress();
59
62
  // Prepare transaction
60
63
  const account = await this.server.loadAccount(accountAddress.address);
61
64
  const fee = await this.server.fetchBaseFee();
62
- const platform_fee = (amount * constants_1.PLATFORM_FEE) / 10000;
63
- const totalAmount = (amount + platform_fee).toString();
64
65
  const memo = stellar_sdk_1.Memo.id(vault.tag?.toString() || "");
65
66
  // Check Wallet balance
66
67
  const balance = await this.getWalletBalance(accountAddress.address);
67
- if (Number(balance) < Number(totalAmount)) {
68
+ if (Number(balance) < Number(amount)) {
68
69
  throw new Error("Insufficient balance");
69
70
  }
70
71
  // Build and submit transaction
@@ -75,7 +76,69 @@ class StellarService {
75
76
  .addOperation(stellar_sdk_1.Operation.payment({
76
77
  destination: vault.address,
77
78
  asset: stellar_sdk_1.Asset.native(),
78
- amount: totalAmount,
79
+ amount,
80
+ }))
81
+ .addMemo(memo)
82
+ .setTimeout(stellar_sdk_1.TimeoutInfinite)
83
+ .build();
84
+ // Sign the transaction
85
+ const { signedTxXdr } = await this.kit.signTransaction(transaction.toXDR());
86
+ const tx = stellar_sdk_1.TransactionBuilder.fromXDR(signedTxXdr, this.sandbox ? stellar_sdk_1.Networks.TESTNET : stellar_sdk_1.Networks.PUBLIC);
87
+ let retries = 0;
88
+ const maxRetries = 5;
89
+ let delay = 1000;
90
+ while (retries < maxRetries) {
91
+ try {
92
+ const transactionResult = await this.server.submitTransaction(tx, {
93
+ skipMemoRequiredCheck: false,
94
+ });
95
+ const txHash = transactionResult.hash;
96
+ return txHash;
97
+ }
98
+ catch (error) {
99
+ console.debug("error: ", error);
100
+ if (retries >= maxRetries) {
101
+ throw error;
102
+ }
103
+ retries += 1;
104
+ console.debug(`Retrying in ${delay / 1000} seconds...`);
105
+ await new Promise((resolve) => setTimeout(resolve, delay));
106
+ delay *= 2; // Exponential backoff
107
+ }
108
+ }
109
+ throw new Error("Max retries reached");
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,
79
142
  }))
80
143
  .addMemo(memo)
81
144
  .setTimeout(stellar_sdk_1.TimeoutInfinite)
@@ -118,5 +181,25 @@ class StellarService {
118
181
  const nativeBalance = account.balances.find((balance) => balance.asset_type === "native");
119
182
  return nativeBalance ? nativeBalance.balance : "0";
120
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
+ }
121
204
  }
122
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.2",
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",