import { NativeModules } from "react-native" ; import { Pool , PoolTags } from "./PoolInterface" ; import { Record , WalletConfig , WalletCredentials } from "../wallet/WalletInterface" ; import { RecordType } from "../utils/Helpers" ; import WalletStorageService from "../wallet/WalletStorageService" ; const { ArnimaSdk } = NativeModules ; class PoolService { param { WalletConfig } configJson param { WalletCredentials } credentialsJson param { string } poolName param { string } poolConfig param { boolean } defaultPool return { * } memberof async createPool ( configJson , credentialsJson , poolName , poolConfig , defaultPool = false ) { try { const response = await ArnimaSdk . createPoolLedgerConfig ( poolName , poolConfig ) ; if ( response === null || response === 'NULL' ) { const poolRecord = { poolName : poolName , poolConfig : poolConfig } ; const poolTags = { poolName : poolName , isSelected : JSON . stringify ( defaultPool ) } ; await WalletStorageService . addWalletRecord ( configJson , credentialsJson , RecordType . Pool , poolName , JSON . stringify ( poolRecord ) , JSON . stringify ( poolTags ) ) ; } return response ; } catch ( error ) { console . log ( "Pool - Create pool error = " , error ) ; throw error ; } } ; param { WalletConfig } configJson param { WalletCredentials } credentialsJson return { * } memberof async getAllPool ( configJson , credentialsJson ) { try { return await WalletStorageService . getWalletRecordsFromQuery ( configJson , credentialsJson , RecordType . Pool , '{}' ) ; } catch ( error ) { console . log ( "Pool - Get all pools error = " , error ) ; throw error ; } } param { WalletConfig } configJson param { WalletCredentials } credentialsJson param { string } poolName return { * } memberof async selectDefaultPool ( configJson , credentialsJson , poolName ) { try { const poolRecords = await WalletStorageService . getWalletRecordsFromQuery ( configJson , credentialsJson , RecordType . Pool , '{}' ) ; for await ( let record of poolRecords ) { const pool = JSON . parse ( record . value ) ; const poolRecord = { poolName : pool . poolName , poolConfig : pool . poolConfig } ; const poolTags = { poolName : pool . poolName , isSelected : JSON . stringify ( false ) } ; if ( pool . poolName === poolName ) { poolTags . isSelected = JSON . stringify ( true ) ; } await WalletStorageService . updateWalletRecord ( configJson , credentialsJson , RecordType . Pool , pool . poolName , JSON . stringify ( poolRecord ) , JSON . stringify ( poolTags ) ) ; } return true ; } catch ( error ) { console . log ( "Pool - Select default pool error = " , error ) ; throw error ; } } param { WalletConfig } configJson param { WalletCredentials } credentialsJson return { * } memberof async deletePoolRecords ( configJson , credentialsJson ) { try { const poolRecords = await WalletStorageService . getWalletRecordsFromQuery ( configJson , credentialsJson , RecordType . Pool , '{}' ) ; for await ( let record of poolRecords ) { const pool = JSON . parse ( record . value ) ; await WalletStorageService . deleteWalletRecord ( configJson , credentialsJson , RecordType . Pool , pool . poolName ) ; } return true ; } catch ( error ) { console . log ( "Pool - Select default pool error = " , error ) ; throw error ; } } } export default new PoolService ( ) ;