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

Pelagus Address Subscriptions #396

Merged
merged 10 commits into from
Oct 25, 2024
71 changes: 7 additions & 64 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
AddressOnNetwork,
NameOnNetwork,
QiCoinbaseAddress,
QiWalletBalance,
} from "./accounts"
import rootReducer from "./redux-slices"
import {
Expand Down Expand Up @@ -168,7 +167,6 @@ import { LocalNodeNetworkStatusEventTypes } from "./services/provider-factory/ev
import NotificationsManager from "./services/notifications"
import BlockService from "./services/block"
import TransactionService from "./services/transactions"
import { QI } from "./constants"

// This sanitizer runs on store and action data before serializing for remote
// redux devtools. The goal is to end up with an object that is directly
Expand Down Expand Up @@ -286,8 +284,6 @@ export default class Main extends BaseService<never> {

balanceChecker: NodeJS.Timeout

qiMiningAddressBalanceChecker: NodeJS.Timeout

static create: ServiceCreatorFunction<never, Main, []> = async () => {
const preferenceService = PreferenceService.create()
const providerFactoryService = ProviderFactory.create(preferenceService)
Expand Down Expand Up @@ -402,7 +398,7 @@ export default class Main extends BaseService<never> {
* A promise to the indexing service, keeping track of token balances and
* prices. The promise will be resolved when the service is initialized.
*/
private indexingService: IndexingService,
public indexingService: IndexingService,
/**
* A promise to the keyring service, which stores key material, derives
* accounts, and signs messagees and transactions. The promise will be
Expand Down Expand Up @@ -494,54 +490,6 @@ export default class Main extends BaseService<never> {
await this.store.dispatch(setNewNetworkConnectError(chainIdWithError))
}

async startQiMiningAddressBalanceChecker(): Promise<void> {
const interval = setInterval(async () => {
const qiMiningAddresses =
await this.indexingService.getQiCoinbaseAddresses()
const allOutpoints = (
await Promise.all(
qiMiningAddresses.map(async (qiAddress) => {
const outpoints = await this.chainService.getOutpointsForQiAddress(
qiAddress.address
)
return outpoints.map((outpoint) => ({
outpoint,
address: qiAddress.address,
account: qiAddress.account,
zone: Zone.Cyprus1,
}))
})
)
).flat()

if (allOutpoints.length === 0) return

const qiWallet = await this.keyringService.getQiHDWallet()
qiWallet.importOutpoints(allOutpoints)
const serializedQiHDWallet = qiWallet.serialize()
await this.keyringService.vaultManager.add(
{ qiHDWallet: serializedQiHDWallet },
{}
)

const qiWalletBalance: QiWalletBalance = {
paymentCode: qiWallet.getPaymentCode(0),
network: this.chainService.selectedNetwork,
assetAmount: {
asset: QI,
amount: qiWallet.getBalanceForZone(Zone.Cyprus1),
},
dataSource: "local",
retrievedAt: Date.now(),
}

this.store.dispatch(
updateUtxoAccountsBalances({ balances: [qiWalletBalance] })
)
}, 30000)
this.qiMiningAddressBalanceChecker = interval
}

async startBalanceChecker(): Promise<void> {
const interval = setInterval(async () => {
if (!walletOpen) return
Expand Down Expand Up @@ -577,21 +525,12 @@ export default class Main extends BaseService<never> {
asset.contractAddress
)
).amount
} else if (isBuiltInNetworkBaseAsset(asset, selectedAccount.network)) {
newBalance = (
await this.chainService.getLatestBaseAccountBalance(selectedAccount)
).assetAmount.amount
} else {
logger.error(
`Unknown asset type for balance checker, asset: ${asset.symbol}`
)
continue
}
// isSmartContractFungibleAsset(asset)
// ? logger.info(
// `Balance checker: ${asset.symbol} ${newBalance} ${asset.contractAddress}`
// )
// : logger.info(`Balance checker: ${asset.symbol} ${newBalance}`)

if (newBalance > amount && !this.keyringService.isLocked()) {
const parsedAmount = bigIntToDecimal(newBalance - amount)
Expand Down Expand Up @@ -708,7 +647,6 @@ export default class Main extends BaseService<never> {
this.signingService.startService(),
this.analyticsService.startService(),
this.startBalanceChecker(),
this.startQiMiningAddressBalanceChecker(),
]
await Promise.all(independentServices)

Expand All @@ -734,7 +672,6 @@ export default class Main extends BaseService<never> {
this.transactionService.stopService(),
this.blockService.stopService(),
clearInterval(this.balanceChecker),
clearInterval(this.qiMiningAddressBalanceChecker),
]

await Promise.all(servicesToBeStopped)
Expand Down Expand Up @@ -1945,6 +1882,12 @@ export default class Main extends BaseService<never> {
index,
zone,
})
await this.chainService.onNewQiAccountCreated({
address,
account,
index,
zone,
})

return { address, account, index, zone }
}
Expand Down
1 change: 0 additions & 1 deletion background/redux-slices/convertAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const setConvertRateHandle = createBackgroundAsyncThunk(
return
}

// console.log("10 Quai", parseQuai("10"))
rate = await jsonRpcProvider.getLatestQuaiRate(
Zone.Cyprus1,
parseQuai("10")
Expand Down
Loading