Skip to content

Commit

Permalink
feat: parse more account types (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronCQL authored Oct 12, 2023
1 parent 3d391cd commit 3539799
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions src/client/utils/toBaseAccount.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
import { Any } from "@bufbuild/protobuf";
import {
CosmosAuthV1beta1BaseAccount as BaseAccount,
CosmosVestingV1beta1BaseVestingAccount as BaseVestingAccount,
CosmosVestingV1beta1ContinuousVestingAccount as ContinuousVestingAccount,
CosmosVestingV1beta1DelayedVestingAccount as DelayedVesting,
CosmosVestingV1beta1PeriodicVestingAccount as PeriodicVestingAccount,
} from "cosmes/protobufs";

const ERR_UNKNOWN_ACCOUNT_TYPE = "Unknown account type";
const ERR_UNABLE_TO_RESOLVE_BASE_ACCOUNT = "Unable to resolve base account";

// TODO: add more account types
/**
* Parses an `Any` protobuf message and returns the `BaseAccount`. Throws if unable
* to parse correctly.
*/
export function toBaseAccount({ typeUrl, value }: Any): BaseAccount {
switch (typeUrl.slice(1)) {
case BaseAccount.typeName:
case BaseAccount.typeName: {
return BaseAccount.fromBinary(value);
case PeriodicVestingAccount.typeName:
}
case BaseVestingAccount.typeName: {
const { baseAccount } = BaseVestingAccount.fromBinary(value);
if (!baseAccount) {
throw new Error(ERR_UNABLE_TO_RESOLVE_BASE_ACCOUNT);
}
return baseAccount;
}
case ContinuousVestingAccount.typeName: {
const { baseVestingAccount } = ContinuousVestingAccount.fromBinary(value);
if (!baseVestingAccount?.baseAccount) {
throw new Error(ERR_UNABLE_TO_RESOLVE_BASE_ACCOUNT);
}
return baseVestingAccount.baseAccount;
}
case DelayedVesting.typeName: {
const { baseVestingAccount } = DelayedVesting.fromBinary(value);
if (!baseVestingAccount?.baseAccount) {
throw new Error(ERR_UNABLE_TO_RESOLVE_BASE_ACCOUNT);
}
return baseVestingAccount.baseAccount;
}
case PeriodicVestingAccount.typeName: {
const { baseVestingAccount } = PeriodicVestingAccount.fromBinary(value);
if (!baseVestingAccount?.baseAccount) {
throw new Error("Unable to resolve base account");
throw new Error(ERR_UNABLE_TO_RESOLVE_BASE_ACCOUNT);
}
return baseVestingAccount.baseAccount;
default:
throw new Error("Unknown account type");
}
default: {
throw new Error(`${ERR_UNKNOWN_ACCOUNT_TYPE}: ${typeUrl.slice(1)}`);
}
}
}

0 comments on commit 3539799

Please sign in to comment.