@@ -47,137 +47,139 @@ class RNZcashModule(
4747 defaultPort : Int = 9067,
4848 newWallet : Boolean ,
4949 promise : Promise ,
50- ) = moduleScope.launch {
51- promise.wrap {
52- val network = networks.getOrDefault(networkName, ZcashNetwork .Mainnet )
53- val endpoint = LightWalletEndpoint (defaultHost, defaultPort, true )
54- val seedPhrase = SeedPhrase .new(seed)
55- val initMode = if (newWallet) WalletInitMode .NewWallet else WalletInitMode .ExistingWallet
56- if (! synchronizerMap.containsKey(alias)) {
57- synchronizerMap[alias] =
58- Synchronizer .new(
59- reactApplicationContext,
60- network,
61- alias,
62- endpoint,
63- seedPhrase.toByteArray(),
64- BlockHeight .new(birthdayHeight.toLong()),
65- initMode,
66- ) as SdkSynchronizer
67- }
68- val wallet = getWallet(alias)
69- val scope = wallet.coroutineScope
70- combine(wallet.progress, wallet.networkHeight) { progress, networkHeight ->
71- return @combine mapOf (" progress" to progress, " networkHeight" to networkHeight)
72- }.collectWith(scope) { map ->
73- val progress = map[" progress" ] as PercentDecimal
74- var networkBlockHeight = map[" networkHeight" ] as BlockHeight ?
75- if (networkBlockHeight == null ) networkBlockHeight = BlockHeight .new(birthdayHeight.toLong())
76-
77- sendEvent(" UpdateEvent" ) { args ->
78- args.putString(" alias" , alias)
79- args.putInt(
80- " scanProgress" ,
81- progress.toPercentage(),
82- )
83- args.putInt(" networkBlockHeight" , networkBlockHeight.value.toInt())
84- }
85- }
86- wallet.status.collectWith(scope) { status ->
87- sendEvent(" StatusEvent" ) { args ->
88- args.putString(" alias" , alias)
89- args.putString(" name" , status.toString())
50+ ) {
51+ moduleScope.launch {
52+ promise.wrap {
53+ val network = networks.getOrDefault(networkName, ZcashNetwork .Mainnet )
54+ val endpoint = LightWalletEndpoint (defaultHost, defaultPort, true )
55+ val seedPhrase = SeedPhrase .new(seed)
56+ val initMode = if (newWallet) WalletInitMode .NewWallet else WalletInitMode .ExistingWallet
57+ if (! synchronizerMap.containsKey(alias)) {
58+ synchronizerMap[alias] =
59+ Synchronizer .new(
60+ reactApplicationContext,
61+ network,
62+ alias,
63+ endpoint,
64+ seedPhrase.toByteArray(),
65+ BlockHeight .new(birthdayHeight.toLong()),
66+ initMode,
67+ ) as SdkSynchronizer
9068 }
91- }
92- wallet.transactions.collectWith(scope) { txList ->
93- scope.launch {
94- val nativeArray = Arguments .createArray()
95- txList
96- .filter { tx -> tx.transactionState != TransactionState .Expired }
97- .map { tx ->
98- launch {
99- val parsedTx = parseTx(wallet, tx)
100- nativeArray.pushMap(parsedTx)
101- }
102- }.forEach { it.join() }
103-
104- sendEvent(" TransactionEvent" ) { args ->
69+ val wallet = getWallet(alias)
70+ val scope = wallet.coroutineScope
71+ combine(wallet.progress, wallet.networkHeight) { progress, networkHeight ->
72+ return @combine mapOf (" progress" to progress, " networkHeight" to networkHeight)
73+ }.collectWith(scope) { map ->
74+ val progress = map[" progress" ] as PercentDecimal
75+ var networkBlockHeight = map[" networkHeight" ] as BlockHeight ?
76+ if (networkBlockHeight == null ) networkBlockHeight = BlockHeight .new(birthdayHeight.toLong())
77+
78+ sendEvent(" UpdateEvent" ) { args ->
10579 args.putString(" alias" , alias)
106- args.putArray (
107- " transactions " ,
108- nativeArray ,
80+ args.putInt (
81+ " scanProgress " ,
82+ progress.toPercentage() ,
10983 )
84+ args.putInt(" networkBlockHeight" , networkBlockHeight.value.toInt())
11085 }
11186 }
112- }
113- combine(
114- wallet.transparentBalance,
115- wallet.saplingBalances,
116- wallet.orchardBalances,
117- ) { transparentBalance: Zatoshi ? , saplingBalances: WalletBalance ? , orchardBalances: WalletBalance ? ->
118- return @combine Balances (
119- transparentBalance = transparentBalance,
120- saplingBalances = saplingBalances,
121- orchardBalances = orchardBalances,
122- )
123- }.collectWith(scope) { map ->
124- val transparentBalance = map.transparentBalance
125- val saplingBalances = map.saplingBalances
126- val orchardBalances = map.orchardBalances
127-
128- val transparentAvailableZatoshi = transparentBalance ? : Zatoshi (0L )
129- val transparentTotalZatoshi = transparentBalance ? : Zatoshi (0L )
130-
131- val saplingAvailableZatoshi = saplingBalances?.available ? : Zatoshi (0L )
132- val saplingTotalZatoshi = saplingBalances?.total ? : Zatoshi (0L )
133-
134- val orchardAvailableZatoshi = orchardBalances?.available ? : Zatoshi (0L )
135- val orchardTotalZatoshi = orchardBalances?.total ? : Zatoshi (0L )
136-
137- sendEvent(" BalanceEvent" ) { args ->
138- args.putString(" alias" , alias)
139- args.putString(" transparentAvailableZatoshi" , transparentAvailableZatoshi.value.toString())
140- args.putString(" transparentTotalZatoshi" , transparentTotalZatoshi.value.toString())
141- args.putString(" saplingAvailableZatoshi" , saplingAvailableZatoshi.value.toString())
142- args.putString(" saplingTotalZatoshi" , saplingTotalZatoshi.value.toString())
143- args.putString(" orchardAvailableZatoshi" , orchardAvailableZatoshi.value.toString())
144- args.putString(" orchardTotalZatoshi" , orchardTotalZatoshi.value.toString())
87+ wallet.status.collectWith(scope) { status ->
88+ sendEvent(" StatusEvent" ) { args ->
89+ args.putString(" alias" , alias)
90+ args.putString(" name" , status.toString())
91+ }
14592 }
146- }
93+ wallet.transactions.collectWith(scope) { txList ->
94+ scope.launch {
95+ val nativeArray = Arguments .createArray()
96+ txList
97+ .filter { tx -> tx.transactionState != TransactionState .Expired }
98+ .map { tx ->
99+ launch {
100+ val parsedTx = parseTx(wallet, tx)
101+ nativeArray.pushMap(parsedTx)
102+ }
103+ }.forEach { it.join() }
104+
105+ sendEvent(" TransactionEvent" ) { args ->
106+ args.putString(" alias" , alias)
107+ args.putArray(
108+ " transactions" ,
109+ nativeArray,
110+ )
111+ }
112+ }
113+ }
114+ combine(
115+ wallet.transparentBalance,
116+ wallet.saplingBalances,
117+ wallet.orchardBalances,
118+ ) { transparentBalance: Zatoshi ? , saplingBalances: WalletBalance ? , orchardBalances: WalletBalance ? ->
119+ return @combine Balances (
120+ transparentBalance = transparentBalance,
121+ saplingBalances = saplingBalances,
122+ orchardBalances = orchardBalances,
123+ )
124+ }.collectWith(scope) { map ->
125+ val transparentBalance = map.transparentBalance
126+ val saplingBalances = map.saplingBalances
127+ val orchardBalances = map.orchardBalances
128+
129+ val transparentAvailableZatoshi = transparentBalance ? : Zatoshi (0L )
130+ val transparentTotalZatoshi = transparentBalance ? : Zatoshi (0L )
147131
148- fun handleError (
149- level : String ,
150- error : Throwable ? ,
151- ) {
152- sendEvent(" ErrorEvent" ) { args ->
153- args.putString(" alias" , alias)
154- args.putString(" level" , level)
155- args.putString(" message" , error?.message ? : " Unknown error" )
132+ val saplingAvailableZatoshi = saplingBalances?.available ? : Zatoshi (0L )
133+ val saplingTotalZatoshi = saplingBalances?.total ? : Zatoshi (0L )
134+
135+ val orchardAvailableZatoshi = orchardBalances?.available ? : Zatoshi (0L )
136+ val orchardTotalZatoshi = orchardBalances?.total ? : Zatoshi (0L )
137+
138+ sendEvent(" BalanceEvent" ) { args ->
139+ args.putString(" alias" , alias)
140+ args.putString(" transparentAvailableZatoshi" , transparentAvailableZatoshi.value.toString())
141+ args.putString(" transparentTotalZatoshi" , transparentTotalZatoshi.value.toString())
142+ args.putString(" saplingAvailableZatoshi" , saplingAvailableZatoshi.value.toString())
143+ args.putString(" saplingTotalZatoshi" , saplingTotalZatoshi.value.toString())
144+ args.putString(" orchardAvailableZatoshi" , orchardAvailableZatoshi.value.toString())
145+ args.putString(" orchardTotalZatoshi" , orchardTotalZatoshi.value.toString())
146+ }
156147 }
157- }
158148
159- // Error listeners
160- wallet.onCriticalErrorHandler = { error ->
161- handleError(" critical" , error)
162- false
163- }
164- wallet.onProcessorErrorHandler = { error ->
165- handleError(" error" , error)
166- true
167- }
168- wallet.onSetupErrorHandler = { error ->
169- handleError(" error" , error)
170- false
171- }
172- wallet.onSubmissionErrorHandler = { error ->
173- handleError(" error" , error)
174- false
175- }
176- wallet.onChainErrorHandler = { errorHeight, rewindHeight ->
177- val message = " Chain error detected at height: $errorHeight . Rewinding to: $rewindHeight "
178- handleError(" error" , Throwable (message))
149+ fun handleError (
150+ level : String ,
151+ error : Throwable ? ,
152+ ) {
153+ sendEvent(" ErrorEvent" ) { args ->
154+ args.putString(" alias" , alias)
155+ args.putString(" level" , level)
156+ args.putString(" message" , error?.message ? : " Unknown error" )
157+ }
158+ }
159+
160+ // Error listeners
161+ wallet.onCriticalErrorHandler = { error ->
162+ handleError(" critical" , error)
163+ false
164+ }
165+ wallet.onProcessorErrorHandler = { error ->
166+ handleError(" error" , error)
167+ true
168+ }
169+ wallet.onSetupErrorHandler = { error ->
170+ handleError(" error" , error)
171+ false
172+ }
173+ wallet.onSubmissionErrorHandler = { error ->
174+ handleError(" error" , error)
175+ false
176+ }
177+ wallet.onChainErrorHandler = { errorHeight, rewindHeight ->
178+ val message = " Chain error detected at height: $errorHeight . Rewinding to: $rewindHeight "
179+ handleError(" error" , Throwable (message))
180+ }
181+ return @wrap null
179182 }
180- return @wrap null
181183 }
182184 }
183185
0 commit comments