UNPKG

@zebec-network/exchange-card-sdk

Version:
154 lines (153 loc) 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XRPLService = void 0; const xrpl_1 = require("xrpl"); const constants_1 = require("../constants"); const apiHelpers_1 = require("../helpers/apiHelpers"); class XRPLService { wallet; apiService; client; constructor(wallet, apiConfig, options) { this.wallet = wallet; const sandbox = options?.sandbox ? options.sandbox : false; this.apiService = new apiHelpers_1.ZebecCardAPIService(apiConfig, sandbox); const xrplNetwork = sandbox ? constants_1.XRPL_RPC_URL.Sandbox : constants_1.XRPL_RPC_URL.Production; this.client = new xrpl_1.Client(xrplNetwork); } /** * Fetches a quote for Bitcoin transfer. * * @returns {Promise<Quote>} A promise that resolves to a Quote object. */ async fetchQuote(symbol = "XRP") { const res = await this.apiService.fetchQuote(symbol); return res; } /** * Fetches the Bitcoin vault address. * * @returns {Promise<{ address: string }>} A promise that resolves to the vault address. */ async fetchVault(symbol = "XRP") { const data = await this.apiService.fetchVault(symbol); return data; } async transferXRP(params) { console.debug("walletAddress:", params.walletAddress); const walletAddress = params.walletAddress ? params.walletAddress : this.wallet.address; if (!(0, xrpl_1.isValidAddress)(walletAddress)) { throw new Error("Invalid wallet address"); } const fetchVault = await this.fetchVault(); const destination = fetchVault.address; console.debug("destination:", destination); if (!(0, xrpl_1.isValidAddress)(destination)) { throw new Error("Invalid destination address"); } const amountInDrops = (0, xrpl_1.xrpToDrops)(params.amount); const transaction = { TransactionType: "Payment", Account: walletAddress, Destination: destination, Amount: amountInDrops, }; await this.client.connect(); try { const preparedTx = await this.client.autofill(transaction); const signedTx = await this.wallet.signTransaction(preparedTx); const response = await this.client.submitAndWait(signedTx); return response; } catch (error) { throw error; } finally { await this.client.disconnect(); } } async transferTokens(params) { const walletAddress = params.walletAddress ? params.walletAddress : this.wallet.address; console.log("walletAddress:", params.walletAddress); if (!(0, xrpl_1.isValidAddress)(walletAddress)) { throw new Error("Invalid wallet address"); } const fetchVault = await this.fetchVault("RLUSD"); const destination = fetchVault.address; console.log("destination:", destination); if (!(0, xrpl_1.isValidAddress)(destination)) { throw new Error("Invalid destination address"); } const transaction = { TransactionType: "Payment", Account: walletAddress, Destination: destination, Amount: { currency: params.token.currency, value: params.amount, issuer: params.token.issuer, }, }; await this.client.connect(); try { const preparedTx = await this.client.autofill(transaction); const signedTx = await this.wallet.signTransaction(preparedTx); const response = await this.client.submitAndWait(signedTx); return response; } catch (error) { throw error; } finally { await this.client.disconnect(); } } async createTrustLine(params) { const walletAddress = params.walletAddress ? params.walletAddress : this.wallet.address; if (!(0, xrpl_1.isValidAddress)(walletAddress)) { throw new Error("Invalid wallet address"); } const transaction = { TransactionType: "TrustSet", Account: walletAddress, LimitAmount: { currency: params.token.currency, issuer: params.token.issuer, value: params.amount, }, }; await this.client.connect(); try { const preparedTx = await this.client.autofill(transaction); const signedTx = await this.wallet.signTransaction(preparedTx); const response = await this.client.submitAndWait(signedTx); return response; } catch (error) { throw error; } finally { await this.client.disconnect(); } } async getTokenBalances(walletAddress) { const address = walletAddress ? walletAddress : this.wallet.address; if (!(0, xrpl_1.isValidAddress)(address)) { throw new Error("Invalid wallet address"); } await this.client.connect(); try { const balances = await this.client.getBalances(address, { ledger_index: "validated", }); return balances; } catch (error) { throw error; } finally { await this.client.disconnect(); } } } exports.XRPLService = XRPLService;