UNPKG

@ledgerhq/live-common

Version:
140 lines 4.85 kB
import network from "@ledgerhq/live-network/network"; class ChainwatchAccountManager { chainwatchBaseUrl; userId; network; suffixes; constructor(chainwatchBaseUrl, userId, network) { this.chainwatchBaseUrl = chainwatchBaseUrl; this.userId = userId; this.network = network; this.suffixes = []; } async getChainwatchAccount() { try { const { data } = await network({ method: "GET", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/`, }); return data; } catch { return; } } async removeChainwatchAccount() { try { await network({ method: "DELETE", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/`, }); } catch { return; } } async registerNewChainwatchAccount() { try { const { data } = await network({ method: "PUT", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/`, }); return data; } catch { return; } } getAccountAddress(account) { return account.freshAddress; } accountAlreadySubscribed(account) { const address = this.getAccountAddress(account); return (address && this.suffixes.some(suffix => address?.toLowerCase()?.endsWith(suffix.toLowerCase()))); } async registerNewAccountsAddresses(accountsToRegister) { try { const addresses = accountsToRegister .filter(account => this.getAccountAddress(account) && !this.accountAlreadySubscribed(account)) .map(account => this.getAccountAddress(account)); if (addresses.length > 0) { await network({ method: "PUT", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/addresses/`, data: addresses, }); } } catch { return; } } async removeAccountsAddresses(accountsToRemove) { try { const addresses = accountsToRemove .filter(account => this.getAccountAddress(account) && this.accountAlreadySubscribed(account)) .map(account => this.getAccountAddress(account)); if (addresses.length > 0) { await network({ method: "DELETE", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/addresses/`, data: addresses, }); } } catch { return; } } async registerNewMonitor(monitor) { try { await network({ method: "PUT", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/monitor/`, data: { confirmations: this.network.nbConfirmations, type: monitor, }, }); } catch { return; } } async registerNewTarget(target) { try { await network({ method: "PUT", url: `${this.chainwatchBaseUrl}/${this.network.chainwatchId}/account/${this.userId}/target/`, data: { equipment: this.userId, type: target, }, }); } catch { return; } } async setupChainwatchAccount() { // Get or set Chainwatch Account const chainwatchAccount = (await this.getChainwatchAccount()) || (await this.registerNewChainwatchAccount()); if (chainwatchAccount) { this.suffixes = chainwatchAccount?.suffixes || []; // Set Chainwatch account's monitors (receive and send) if they don't exist yet if (!chainwatchAccount?.monitors?.find(monitor => monitor.type === "send")) { await this.registerNewMonitor("send"); } if (!chainwatchAccount?.monitors?.find(monitor => monitor.type === "receive")) { await this.registerNewMonitor("receive"); } // Set Chainwatch account's target (braze) if it doesn't exist yet if (!chainwatchAccount?.targets?.find(target => target.type === "braze")) { await this.registerNewTarget("braze"); } } } } export default ChainwatchAccountManager; //# sourceMappingURL=ChainwatchAccountManager.js.map