Skip to content

Commit

Permalink
Merge pull request #389 from Concordium/node-6-compat
Browse files Browse the repository at this point in the history
Add fallback for available balance to be compatible with node version 6
  • Loading branch information
soerenbf authored Sep 6, 2024
2 parents a76f5b9 + c4c54fe commit d2e5a42
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions packages/sdk/src/grpc/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,33 +339,62 @@ 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),
// Set the following values if they are not undefined
...(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;

// This is undefined for node version <7, so we add this check to be backwards compatible.
if (acc.availableBalance !== undefined) {
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') {
Expand Down

0 comments on commit d2e5a42

Please sign in to comment.