import { ChainType } from '@acala-network/sdk' ; import { Token , MaybeAccount , MaybeCurrency , ObOrPromiseResult , forceToTokenSymbolCurrencyId , forceToCurrencyName , unzipDexShareName , getStableAssetPoolIdFromName , FixedPointNumber as FN , isDexShareName , isStableAssetName , createLiquidCroadloanName } from '@acala-network/sdk-core' ; import { getChainType } from '@acala-network/sdk/utils/get-chain-type' ; import { CurrencyId } from '@acala-network/types/interfaces' ; import { ApiRx , ApiPromise } from '@polkadot/api' ; import { SubmittableExtrinsic } from '@polkadot/api/types' ; import { ISubmittableResult } from '@polkadot/types/types' ; import { BalanceData , PriceData , PriceDataWithTimestamp , TransferConfig } from './types' ; import { getExistentialDepositConfig } from './utils/get-existential-deposit-config' ; export abstract class WalletBase < T extends ApiRx | ApiPromise > { protected api : T ; protected decimalsMap : Map < string , number > ; protected currencyIdMap : Map < string , CurrencyId > ; protected tokenMap : Map < string , Token > ; protected nativeToken ! : string ; protected runtimeChain ! : string ; protected constructor ( api ) { this . api = api ; this . decimalsMap = new Map < string , number > ( [ ] ) ; this . currencyIdMap = new Map < string , CurrencyId > ( [ ] ) ; this . tokenMap = new Map < string , Token > ( [ ] ) ; this . init ( ) ; } private init ( ) { const tokenDecimals = this . api . registry . chainDecimals ; const tokenSymbol = this . api . registry . chainTokens ; const defaultTokenDecimal = Number ( tokenDecimals ?. [ 0 ] ) || 12 ; this . runtimeChain = this . api . runtimeChain . toString ( ) ; this . nativeToken = tokenSymbol [ 0 ] . toString ( ) ; tokenSymbol . forEach ( ( item , index ) => { try { const key = item . toString ( ) ; const currencyId = forceToTokenSymbolCurrencyId ( this . api , key ) ; const decimals = Number ( tokenDecimals ?. [ index ] ) || defaultTokenDecimal ; this . decimalsMap . set ( key , Number ( tokenDecimals ?. [ index ] ) || defaultTokenDecimal ) ; this . currencyIdMap . set ( key , currencyId ) ; this . tokenMap . set ( key , Token . fromCurrencyId ( currencyId , { decimals } ) ) ; } catch ( e ) { } } ) ; if ( getChainType ( this . runtimeChain ) === ChainType . ACALA ) { const name = createLiquidCroadloanName ( 13 ) ; this . tokenMap . set ( name , new Token ( name , { decimals : this . tokenMap . get ( 'DOT' ) ?. decimals || 12 } ) ) ; } } public isNativeToken ( currency ) { return forceToCurrencyName ( currency ) === forceToCurrencyName ( this . nativeToken ) ; } name description public getAllTokens ( ) { return Array . from ( this . tokenMap . values ( ) ) . map ( ( item ) => item . clone ( ) ) ; } public getNativeToken ( ) { const nativeCurrencyId = this . api . consts . currencies . getNativeCurrencyId ; return this . getToken ( nativeCurrencyId ) . clone ( ) ; } name description public getToken ( currency ) { const currencyName = forceToCurrencyName ( currency ) ; if ( isDexShareName ( currencyName ) ) { const [ token1 , token2 ] = unzipDexShareName ( currencyName ) ; const _token1 = this . getToken ( token1 ) ; const _token2 = this . getToken ( token2 ) ; return Token . fromTokens ( _token1 , _token2 ) ; } else if ( isStableAssetName ( currencyName ) ) { const poolId = getStableAssetPoolIdFromName ( currencyName ) ; return Token . fromStableAssetPool ( this . api . runtimeChain . toString ( ) , poolId ) ; } return this . tokenMap . get ( currencyName ) ?. clone ( ) || new Token ( 'EMPTY' ) ; } public getTransferConfig ( currency ) { const name = forceToCurrencyName ( currency ) ; const token = this . getToken ( name ) ; if ( token . isForeignAsset ) { return { existentialDeposit : token . ed } ; } if ( isDexShareName ( name ) ) { const [ token1 ] = Token . sortTokenNames ( ... unzipDexShareName ( name ) ) ; return { existentialDeposit : getExistentialDepositConfig ( this . runtimeChain , token1 ) } ; } const existentialDeposit = getExistentialDepositConfig ( this . runtimeChain , forceToCurrencyName ( currency ) ) ; return { existentialDeposit } ; } name description public abstract checkTransfer ( account , currency , amount , direction ? ) ; name description public abstract queryBalance ( account , currency , at ? ) ; name description public abstract queryPrices ( tokens , at ? ) ; name description public abstract queryPrice ( currency , at ? ) ; name description public abstract queryOraclePrice ( ) ; name description public abstract queryPriceFromDex ( currency , at ? ) ; name description public abstract queryDexSharePriceFormDex ( currency , at ? ) ; name public abstract subscribeOracleFeed ( provider ) ; public abstract getMaxInputBalance ( call , currency , account , isAllowDeath , feeFactor ? ) ; }