From c18b4b434179cca19dcd7ecc026b1b6d6369fa64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Bruus=20Zeppelin?= Date: Fri, 6 Sep 2024 12:16:15 +0200 Subject: [PATCH] Add fallback for available balance to be compatible with node version 6 --- packages/sdk/src/grpc/translation.ts | 45 ++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/packages/sdk/src/grpc/translation.ts b/packages/sdk/src/grpc/translation.ts index 80581f69..50114d8b 100644 --- a/packages/sdk/src/grpc/translation.ts +++ b/packages/sdk/src/grpc/translation.ts @@ -339,7 +339,7 @@ export function accountInfo(acc: v2.AccountInfo): v1.AccountInfo { const aggAmount = acc.encryptedBalance?.aggregatedAmount?.value; const numAggregated = acc.encryptedBalance?.numAggregated; - const encryptedAmount: v1.AccountEncryptedAmount = { + const accountEncryptedAmount: v1.AccountEncryptedAmount = { selfAmount: unwrapValToHex(acc.encryptedBalance?.selfAmount), startIndex: unwrap(acc.encryptedBalance?.startIndex), incomingAmounts: unwrap(acc.encryptedBalance?.incomingAmounts).map(unwrapValToHex), @@ -347,25 +347,52 @@ export function accountInfo(acc: v2.AccountInfo): v1.AccountInfo { ...(numAggregated && { numAggregated: numAggregated }), ...(aggAmount && { aggregatedAmount: unwrapToHex(aggAmount) }), }; - const releaseSchedule = { + const accountReleaseSchedule = { total: CcdAmount.fromProto(unwrap(acc.schedule?.total)), schedule: unwrap(acc.schedule?.schedules).map(trRelease), }; - const cooldowns = acc.cooldowns.map(transCooldown); - const availableBalance = CcdAmount.fromProto(unwrap(acc.availableBalance)); + const accountCooldowns = acc.cooldowns.map(transCooldown); + const accountAmount = CcdAmount.fromProto(unwrap(acc.amount)); + + let accountAvailableBalance: CcdAmount.Type; + if (acc.availableBalance) { + accountAvailableBalance = CcdAmount.fromProto(unwrap(acc.availableBalance)); + } else { + // NOTE: implementation borrowed from concordium-browser-wallet. + let staked = 0n; + switch (acc.stake?.stakingInfo.oneofKind) { + case 'baker': { + staked = unwrap(acc.stake.stakingInfo.baker.stakedAmount?.value); + break; + } + case 'delegator': { + staked = unwrap(acc.stake.stakingInfo.delegator.stakedAmount?.value); + break; + } + } + + const scheduled = accountReleaseSchedule ? BigInt(accountReleaseSchedule.total.microCcdAmount) : 0n; + + const max = (first: bigint, second: bigint) => { + return first > second ? first : second; + }; + + const atDisposal = accountAmount.microCcdAmount - max(scheduled, staked); + accountAvailableBalance = CcdAmount.fromMicroCcd(atDisposal); + } const accInfoCommon: v1.AccountInfoSimple = { type: v1.AccountInfoType.Simple, accountAddress: AccountAddress.fromProto(unwrap(acc.address)), accountNonce: SequenceNumber.fromProto(unwrap(acc.sequenceNumber)), - accountAmount: CcdAmount.fromProto(unwrap(acc.amount)), + accountAmount, accountIndex: unwrap(acc.index?.value), accountThreshold: unwrap(acc.threshold?.value), accountEncryptionKey: unwrapValToHex(acc.encryptionKey), - accountEncryptedAmount: encryptedAmount, - accountReleaseSchedule: releaseSchedule, + accountEncryptedAmount, + accountReleaseSchedule, accountCredentials: mapRecord(acc.creds, trCred), - accountCooldowns: cooldowns, - accountAvailableBalance: availableBalance, + accountCooldowns, + accountAvailableBalance, }; if (acc.stake?.stakingInfo.oneofKind === 'delegator') {