Skip to content

Commit

Permalink
Correctly parse cat balances and skip login if already logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbrucker committed May 24, 2023
1 parent a5f9076 commit 6b524d0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/chia-amount.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ class ChiaAmount {
return 12;
}

static fromRaw(amount) {
return new ChiaAmount(new BigNumber(amount).shiftedBy(-ChiaAmount.decimalPlaces));
static get decimalPlacesCat() {
return 3;
}

static fromRaw(amount, decimalPlaces = ChiaAmount.decimalPlaces) {
return new ChiaAmount(new BigNumber(amount).shiftedBy(-decimalPlaces));
}

constructor(amount) {
Expand Down
29 changes: 25 additions & 4 deletions lib/service/stats-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Capacity = require('../capacity');
const ChiaAmount = require('../chia-amount');
const { updateStartedAtOfJob, getProgressOfJob, getEffectivePlotSizeInBytes } = require('../util');
const {getUpdateInterval} = require('../update-mode')
const WalletType = require('../wallet-type')

const fullNodeService = 'fullNode';
const walletService = 'wallet';
Expand Down Expand Up @@ -434,6 +435,12 @@ class StatsCollection {
if (this.walletIsLoggedIn) {
return;
}
const loggedInFingerPrint = await this.walletApiClient.getLoggedInFingerprint()
if (loggedInFingerPrint !== null) {
this.walletIsLoggedIn = true

return
}
await this.walletApiClient.logInAndSkip({ fingerprint: await this.walletApiClient.getPublicKey() });
this.walletIsLoggedIn = true;
}
Expand All @@ -444,18 +451,32 @@ class StatsCollection {
}
const walletStats = this.stats.has(walletService) ? this.stats.get(walletService) : {};
const wallets = await this.walletApiClient.getWallets();
const relevantWallets = wallets.filter(wallet => {
switch (wallet.type) {
case WalletType.standard:
case WalletType.atomicSwap:
case WalletType.authorizedPayee:
case WalletType.multiSig:
case WalletType.custody:
case WalletType.cat:
case WalletType.recoverable:
return true
default: return false
}
})
let walletPartialStats = undefined
const newWallets = await Promise.all(wallets.map(async wallet => {
const balance = await this.walletApiClient.getBalance({ walletId: wallet.id });
const newWallets = await Promise.all(relevantWallets.map(async wallet => {
const balance = await this.walletApiClient.getBalance({ walletId: wallet.id })
const decimalPlaces = wallet.type === WalletType.cat ? ChiaAmount.decimalPlacesCat : ChiaAmount.decimalPlaces

return {
id: wallet.id,
name: wallet.name,
type: wallet.type,
balance: {
unconfirmed: ChiaAmount.fromRaw(balance.unconfirmed_wallet_balance).toString(),
unconfirmed: ChiaAmount.fromRaw(balance.unconfirmed_wallet_balance, decimalPlaces).toString(),
},
};
}
}))
if (walletStats.wallets === undefined || this.doWalletsDiffer(walletStats.wallets, newWallets)) {
walletPartialStats = { wallets: newWallets }
Expand Down
16 changes: 16 additions & 0 deletions lib/wallet-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const WalletType = {
standard: 0,
atomicSwap: 2,
authorizedPayee: 3,
multiSig: 4,
custody: 5,
cat: 6,
recoverable: 7,
did: 8,
plotNft: 9,
nft: 10,
dataLayer: 11,
dataLayerOffer: 12,
}

module.exports = WalletType

0 comments on commit 6b524d0

Please sign in to comment.