package Ġio . nova foundation . nova . feature _ wallet _ impl . data . network . blockchain . assets . balances . orm l Ċ Ċ import Ġio . nova foundation . nova . common . data . network . runtime . binding . Block Hash Ċ import Ġio . nova foundation . nova . common . utils . tokens Ċ import Ġio . nova foundation . nova . core . updater . Subscription Builder Ċ import Ġio . nova foundation . nova . feature _ account _ api . domain . model . Meta Account Ċ import Ġio . nova foundation . nova . feature _ wallet _ api . data . cache . Asset Cache Ċ import Ġio . nova foundation . nova . feature _ wallet _ api . data . network . block h ain . assets . balances . Asset Balance Ċ import Ġio . nova foundation . nova . runtime . ext . orm l Currency Id Ċ import Ġio . nova foundation . nova . runtime . ext . require Or ml Ċ import Ġio . nova foundation . nova . runtime . multi Network . Chain Registry Ċ import Ġio . nova foundation . nova . runtime . multi Network . chain . model . Chain Ċ import Ġio . nova foundation . nova . runtime . multi Network . get Runtime Ċ import Ġio . nova foundation . nova . runtime . storage . source . Storage DataSource Ċ import Ġjp . co . sor am its u . fe ar less _ utils . runtime . AccountId Ċ import Ġjp . co . sor am its u . fe ar less _ utils . runtime . Runtime Snapshot Ċ import Ġjp . co . sor am its u . fe ar less _ utils . runtime . metadata . storage Ċ import Ġjp . co . sor am its u . fe ar less _ utils . runtime . metadata . storage Key Ċ import Ġkotlinx . coroutines . flow . Flow Ċ import Ġkotlinx . coroutines . flow . map Ċ import Ġjava . math . BigInteger Ċ Ċ class ĠOr ml Asset Balance ( ĊĠĠĠ Ġprivate Ġval Ġasset Cache : ĠAsset Cache , ĊĠĠĠ Ġprivate Ġval Ġstorage Source : ĠStorage DataSource , ĊĠĠĠ Ġprivate Ġval Ġchain Registry : ĠChain Registry , Ċ ) Ġ: ĠAsset Balance Ġ{ ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġexist ential Deposit ( chain : ĠChain , Ġchain Asset : ĠChain . Asset ): ĠBigInteger Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġchain Asset . require Or ml (). exist ential Deposit ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġquery Total Balance ( chain : ĠChain , Ġchain Asset : ĠChain . Asset , ĠaccountId : ĠAccount Id ): ĠBigInteger Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġorm l Account Data Ġ= Ġstorage Source . query ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġchain Id Ġ= Ġchain . id , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġkey Builder Ġ= Ġ{ Ġit . orm l Balance Key ( accountId , Ġchain Asset ) Ġ}, ĊĠĠĠĠĠĠĠĠĠĠĠ Ġbinding Ġ= Ġ{ Ġscale , Ġruntime Ġ-> Ġbind Or ml Account Data OrEmpty ( scale , Ġruntime ) Ġ} ĊĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġorm l Account Data . free Ġ+ Ġorm l Account Data . reserved ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġsuspend Ġfun Ġstart Sync ing Balance ( ĊĠĠĠĠĠĠĠ Ġchain : ĠChain , ĊĠĠĠĠĠĠĠ Ġchain Asset : ĠChain . Asset , ĊĠĠĠĠĠĠĠ Ġmeta Account : ĠMeta Account , ĊĠĠĠĠĠĠĠ ĠaccountId : ĠAccount Id , ĊĠĠĠĠĠĠĠ Ġsubscription Builder : ĠSubscription Builder ĊĠĠĠ Ġ): ĠFlow < Block Hash ?> Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġruntime Ġ= Ġchain Registry . get Runtime ( chain . id ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġsubscription Builder . subscribe ( runtime . orm l Balance Key ( accountId , Ġchain Asset )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. map Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġorm l Account Data Ġ= Ġbind Or ml Account Data OrEmpty ( it . value , Ġruntime ) ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġasset Changed Ġ= Ġupdate Asset Balance ( meta Account . id , Ġchain Asset , Ġorm l Account Data ) ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġit . block . take If Ġ{ Ġasset Changed Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġsuspend Ġfun Ġupdate Asset Balance ( ĊĠĠĠĠĠĠĠ Ġmeta Id : ĠLong , ĊĠĠĠĠĠĠĠ Ġchain Asset : ĠChain . Asset , ĊĠĠĠĠĠĠĠ Ġorm l Account Data : ĠOr ml Account Data ĊĠĠĠ Ġ) Ġ= Ġasset Cache . update Asset ( meta Id , Ġchain Asset ) Ġ{ ĊĠĠĠĠĠĠĠ Ġit . copy ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfrozen In Pl anks Ġ= Ġorm l Account Data . frozen , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfree In Pl anks Ġ= Ġorm l Account Data . free , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġreserved In Pl anks Ġ= Ġorm l Account Data . reserved ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠRuntime Snapshot . orm l Balance Key ( accountId : ĠAccount Id , Ġchain Asset : ĠChain . Asset ): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġmetadata . tokens (). storage (" Accounts "). storage Key ( this , ĠaccountId , Ġchain Asset . orm l Currency Id ( this )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġbind Or ml Account Data OrEmpty ( scale : ĠString ?, Ġruntime : ĠRuntime Snapshot ): ĠOr ml Account Data Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn Ġscale ?. let Ġ{ Ġbind Or ml Account Data ( it , Ġruntime ) Ġ} Ġ?: ĠOr ml Account Data . empty () ĊĠĠĠ Ġ} Ċ } Ċ