UNPKG

@zebec-network/exchange-card-sdk

Version:
145 lines (144 loc) 5.28 kB
import { Client, isValidAddress, xrpToDrops, } from "xrpl"; import { XRPL_RPC_URL } from "../constants"; import { ZebecCardAPIService } from "../helpers/apiHelpers"; export class XRPLService { wallet; apiService; client; constructor(wallet, options) { this.wallet = wallet; const sandbox = options?.sandbox ? options.sandbox : false; this.apiService = new ZebecCardAPIService(sandbox); const xrplNetwork = sandbox ? XRPL_RPC_URL.Sandbox : XRPL_RPC_URL.Production; this.client = new Client(xrplNetwork); } /** * 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 (!isValidAddress(walletAddress)) { throw new Error("Invalid wallet address"); } const { address: destination, tag } = await this.fetchVault(); console.debug("destination:", destination); console.debug("tag:", tag); if (!isValidAddress(destination)) { throw new Error("Invalid destination address"); } const destinationTag = tag && tag !== "" ? parseInt(tag) : undefined; const amountInDrops = xrpToDrops(params.amount); const transaction = { TransactionType: "Payment", Account: walletAddress, Destination: destination, Amount: amountInDrops, DestinationTag: destinationTag, }; 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 (!isValidAddress(walletAddress)) { throw new Error("Invalid wallet address"); } const { address: destination, tag } = await this.fetchVault(); console.debug("destination:", destination); console.debug("tag:", tag); if (!isValidAddress(destination)) { throw new Error("Invalid destination address"); } const destinationTag = tag && tag !== "" ? parseInt(tag) : undefined; const transaction = { TransactionType: "Payment", Account: walletAddress, Destination: destination, Amount: { currency: params.token.currency, value: params.amount, issuer: params.token.issuer, }, DestinationTag: destinationTag, }; 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 (!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 (!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(); } } }