Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ZcashPiratechain) Add error listeners #825

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
## 4.21.0 (2024-08-29)

- added: POL token on Ethereum.
- added: (Zcash/Piratechain) Log errors thrown by synchronizer
- added: (Zcash/Piratechain) Restart engine after receiving critical error
- changed: Rename MATIC to POL on the Polygon chain.
- fixed: Display name COMBO (Ethereum) to Furucombo
- fixed: Calculate Arbitrum fees more correctly.
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@
"path-browserify": "^1.0.1",
"prettier": "^2.2.0",
"process": "^0.11.10",
"react-native-piratechain": "0.4.14",
"react-native-zcash": "0.7.0",
"react-native-piratechain": "0.5.0",
"react-native-zcash": "0.8.0",
"rimraf": "^3.0.2",
"shell-quote": "^1.8.1",
"stream-browserify": "^2.0.2",
Expand All @@ -174,7 +174,7 @@
"webpack-dev-server": "^4.11.1"
},
"peerDependencies": {
"react-native-piratechain": "^0.4.14",
"react-native-zcash": "^0.7.0"
"react-native-piratechain": "v0.5.0",
"react-native-zcash": "^0.8.0"
}
}
29 changes: 19 additions & 10 deletions src/piratechain/PiratechainEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {
InsufficientFundsError,
NoAmountSpecifiedError
} from 'edge-core-js/types'
import type {
ConfirmedTransaction,
InitializerConfig,
SpendInfo,
StatusEvent
} from 'react-native-piratechain'
import { base16, base64 } from 'rfc4648'

import { CurrencyEngine } from '../common/CurrencyEngine'
Expand All @@ -22,12 +28,8 @@ import {
asPiratechainPrivateKeys,
asPiratechainWalletOtherData,
asSafePiratechainWalletInfo,
PiratechainInitializerConfig,
PiratechainNetworkInfo,
PiratechainSpendInfo,
PiratechainSynchronizer,
PiratechainSynchronizerStatus,
PiratechainTransaction,
PiratechainWalletOtherData,
SafePiratechainWalletInfo
} from './piratechainTypes'
Expand All @@ -41,14 +43,14 @@ export class PiratechainEngine extends CurrencyEngine<
pluginId: string
networkInfo: PiratechainNetworkInfo
otherData!: PiratechainWalletOtherData
synchronizerStatus!: PiratechainSynchronizerStatus
synchronizerStatus!: StatusEvent['name']
availableZatoshi!: string
initialNumBlocksToDownload!: number
initializer!: PiratechainInitializerConfig
initializer!: InitializerConfig
progressRatio!: number
queryMutex: boolean
makeSynchronizer: (
config: PiratechainInitializerConfig
config: InitializerConfig
) => Promise<PiratechainSynchronizer>

// Synchronizer management
Expand Down Expand Up @@ -112,6 +114,14 @@ export class PiratechainEngine extends CurrencyEngine<
this.synchronizerStatus = payload.name
await this.queryAll()
})
this.synchronizer.on('error', async payload => {
this.log.warn(`Synchronizer error: ${payload.message}`)
if (payload.level === 'critical') {
await this.killEngine()
this.lastUpdateFromSynchronizer = undefined
await this.startEngine()
}
})
}

async queryAll(): Promise<void> {
Expand Down Expand Up @@ -268,7 +278,7 @@ export class PiratechainEngine extends CurrencyEngine<
}
}

processTransaction(tx: PiratechainTransaction): void {
processTransaction(tx: ConfirmedTransaction): void {
let netNativeAmount = tx.value
const ourReceiveAddresses = []
if (tx.toAddress != null) {
Expand Down Expand Up @@ -453,14 +463,13 @@ export class PiratechainEngine extends CurrencyEngine<

const memo = memos[0]?.type === 'text' ? memos[0].value : ''
const spendTarget = edgeTransaction.spendTargets[0]
const txParams: PiratechainSpendInfo = {
const txParams: SpendInfo = {
zatoshi: sub(
abs(edgeTransaction.nativeAmount),
edgeTransaction.networkFee
),
toAddress: spendTarget.publicAddress,
memo,
fromAccountIndex: 0,
mnemonicSeed: piratechainPrivateKeys.mnemonic
}

Expand Down
99 changes: 21 additions & 78 deletions src/piratechain/piratechainTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import {
asValue,
Cleaner
} from 'cleaners'
import type {
Addresses,
BlockRange,
ConfirmedTransaction,
ErrorEvent as PiratechainErrorEvent,
InitializerConfig,
SpendInfo,
SpendSuccess,
StatusEvent,
UpdateEvent,
WalletBalance
} from 'react-native-piratechain'
samholmes marked this conversation as resolved.
Show resolved Hide resolved
import type { Subscriber } from 'yaob'

import { asWalletInfo } from '../common/types'
Expand All @@ -24,77 +36,11 @@ export interface PiratechainNetworkInfo {
transactionQueryLimit: number
}

export interface PiratechainSpendInfo {
zatoshi: string
toAddress: string
memo: string
fromAccountIndex: number
mnemonicSeed: string
}

export interface PiratechainTransaction {
rawTransactionId: string
blockTimeInSeconds: number
minedHeight: number
value: string
toAddress?: string
memos: string[]
}

export interface PiratechainPendingTransaction {
txId: string
raw: string
}

export interface PiratechainWalletBalance {
availableZatoshi: string
totalZatoshi: string
}

export interface PiratechainInitializerConfig {
networkName: PiratechainNetworkName
defaultHost: string
defaultPort: number
mnemonicSeed: string
alias: string
birthdayHeight: number
}

export interface PiratechainAddresses {
// unifiedAddress: string
saplingAddress: string
// transparentAddress: string
}

export type PiratechainSynchronizerStatus =
| 'STOPPED'
| 'DISCONNECTED'
| 'SYNCING'
| 'SYNCED'

export interface PiratechainStatusEvent {
alias: string
name: PiratechainSynchronizerStatus
}

export interface PiratechainUpdateEvent {
alias: string
isDownloading: boolean
isScanning: boolean
lastDownloadedHeight: number
lastScannedHeight: number
scanProgress: number // 0 - 100
networkBlockHeight: number
}

// Block range is inclusive
export const asPiratechainBlockRange = asObject({
const asPiratechainBlockRange = asObject<BlockRange>({
first: asNumber,
last: asNumber
})

export type PiratechainBlockRange = ReturnType<typeof asPiratechainBlockRange>

export const asPiratechainWalletOtherData = asObject({
alias: asMaybe(asString),
blockRange: asMaybe(asPiratechainBlockRange, () => ({
Expand All @@ -110,24 +56,21 @@ export type PiratechainWalletOtherData = ReturnType<

export interface PiratechainSynchronizer {
on: Subscriber<{
statusChanged: PiratechainStatusEvent
update: PiratechainUpdateEvent
statusChanged: StatusEvent
update: UpdateEvent
error: PiratechainErrorEvent
}>
start: () => Promise<void>
stop: () => Promise<void>
deriveUnifiedAddress: () => Promise<PiratechainAddresses>
getTransactions: (
arg: PiratechainBlockRange
) => Promise<PiratechainTransaction[]>
deriveUnifiedAddress: () => Promise<Addresses>
getTransactions: (arg: BlockRange) => Promise<ConfirmedTransaction[]>
rescan: () => Promise<string>
sendToAddress: (
arg: PiratechainSpendInfo
) => Promise<PiratechainPendingTransaction>
getBalance: () => Promise<PiratechainWalletBalance>
sendToAddress: (arg: SpendInfo) => Promise<SpendSuccess>
getBalance: () => Promise<WalletBalance>
}

export type PiratechainMakeSynchronizer = () => (
config: PiratechainInitializerConfig
config: InitializerConfig
) => Promise<PiratechainSynchronizer>

export const asArrrPublicKey = asObject({
Expand Down
15 changes: 10 additions & 5 deletions src/react-native.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { EdgeOtherMethods } from 'edge-core-js/types'
import { NativeModules } from 'react-native'
import {
InitializerConfig as PiratechainInitializerConfig,
makeSynchronizer as PiratechainMakeSynchronizer,
Synchronizer as PiratechainSynchronizer,
Tools as PiratechainNativeTools
} from 'react-native-piratechain'
import {
InitializerConfig,
InitializerConfig as ZcashInitializerConfig,
makeSynchronizer as ZcashMakeSynchronizer,
Synchronizer as ZcashSynchronizer,
Tools as ZcashNativeTools
} from 'react-native-zcash'
import { bridgifyObject, emit, onMethod } from 'yaob'

import { PiratechainInitializerConfig } from './piratechain/piratechainTypes'

const { EdgeCurrencyAccountbasedModule } = NativeModules
const { sourceUri } = EdgeCurrencyAccountbasedModule.getConstants()

Expand All @@ -32,6 +31,9 @@ const makePiratechainSynchronizer = async (
},
onUpdate(event): void {
emit(out, 'update', event)
},
onError(event): void {
emit(out, 'error', event)
}
})

Expand Down Expand Up @@ -61,7 +63,7 @@ const makePiratechainSynchronizer = async (
}

const makeZcashSynchronizer = async (
config: InitializerConfig
config: ZcashInitializerConfig
): Promise<ZcashSynchronizer> => {
const realSynchronizer = await ZcashMakeSynchronizer(config)

Expand All @@ -77,6 +79,9 @@ const makeZcashSynchronizer = async (
},
onUpdate(event): void {
emit(out, 'update', event)
},
onError(event): void {
emit(out, 'error', event)
}
})

Expand Down Expand Up @@ -130,7 +135,7 @@ export function makePluginIo(): EdgeOtherMethods {
}),
zcash: bridgifyObject({
Tools: ZcashNativeTools,
async makeSynchronizer(config: InitializerConfig) {
async makeSynchronizer(config: ZcashInitializerConfig) {
return await makeZcashSynchronizer(config)
}
})
Expand Down
7 changes: 7 additions & 0 deletions src/zcash/ZcashEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ export class ZcashEngine extends CurrencyEngine<
})
this.onUpdateTransactions()
})
this.synchronizer.on('error', async payload => {
samholmes marked this conversation as resolved.
Show resolved Hide resolved
this.log.warn(`Synchronizer error: ${payload.message}`)
if (payload.level === 'critical') {
await this.killEngine()
await this.startEngine()
}
})
}

onUpdateBlockHeight(networkBlockHeight: number): void {
Expand Down
10 changes: 4 additions & 6 deletions src/zcash/ZcashTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
EdgeWalletInfo,
JsonObject
} from 'edge-core-js/types'
import type { UnifiedViewingKey } from 'react-native-zcash'
import { Tools as ToolsType } from 'react-native-zcash'

import { PluginEnvironment } from '../common/innerPlugin'
Expand Down Expand Up @@ -140,11 +139,10 @@ export class ZcashTools implements EdgeCurrencyTools {
if (typeof mnemonic !== 'string') {
throw new Error('InvalidMnemonic')
}
const unifiedViewingKey: UnifiedViewingKey =
await this.nativeTools.deriveViewingKey(
mnemonic,
this.networkInfo.rpcNode.networkName
)
const unifiedViewingKey = await this.nativeTools.deriveViewingKey(
mnemonic,
this.networkInfo.rpcNode.networkName
)
return {
birthdayHeight: zcashPrivateKeys.birthdayHeight,
publicKey: unifiedViewingKey
Expand Down
2 changes: 2 additions & 0 deletions src/zcash/zcashTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import type {
Addresses,
BalanceEvent,
ErrorEvent as ZcashErrorEvent,
samholmes marked this conversation as resolved.
Show resolved Hide resolved
InitializerConfig,
ProposalSuccess,
ShieldFundsInfo,
Expand Down Expand Up @@ -53,6 +54,7 @@ export interface ZcashSynchronizer {
statusChanged: StatusEvent
transactionsChanged: TransactionEvent
update: UpdateEvent
error: ZcashErrorEvent
}>
start: () => Promise<void>
stop: () => Promise<void>
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7389,17 +7389,17 @@ react-is@^16.12.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

react-native-piratechain@0.4.14:
version "0.4.14"
resolved "https://registry.yarnpkg.com/react-native-piratechain/-/react-native-piratechain-0.4.14.tgz#006e9e36242c132a9ff4c9b01ca536ddf15f9e30"
integrity sha512-HxyCopyh5iAk3Ub50YIExp96Ck/iXG9PCLmxFAwhl58jWO2IgEcziUv1JD1ReHfflEi62c+hSrSyu8KgfWI3Rw==
react-native-piratechain@0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/react-native-piratechain/-/react-native-piratechain-0.5.0.tgz#74519d6983d6281eda9b0b788081eb2221a08204"
integrity sha512-cCYNGll6Zye+2oIABBLMSg6DEuIEUJomZkODXKnEDvG8SSIByz2w00Crt6Ol3UjIaDlA69Y10Ty4SiKvLTy2EQ==
dependencies:
rfc4648 "^1.3.0"

react-native-zcash@0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/react-native-zcash/-/react-native-zcash-0.7.0.tgz#9709a0cd6ae7952357a77b2f82520b6011540b05"
integrity sha512-ERzkdpBjVqlwA7GqbJ3e3rJdejsYGCGuXfPS5fgV+xwEyqP9WC/HeoAUIMLdBbcKGHI8mEIFxQtlGQbtV/dkDw==
react-native-zcash@0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/react-native-zcash/-/react-native-zcash-0.8.0.tgz#ca3dc76c9e4ae9e7b3e4840526f38acb4b139d7b"
integrity sha512-UtgdWTo8ZNAHvYaKiGGlm9jJZ+jGQxUVZWuoEUUjPxgr87+Tb8FpXTpkvzm5ThRxDrgEKS8Dj98mdJnKO06rAA==
dependencies:
biggystring "^4.1.3"
rfc4648 "^1.3.0"
Expand Down
Loading