Skip to content

Instantly share code, notes, and snippets.

@srph
Created November 25, 2022 17:27
Show Gist options
  • Save srph/9806ec4fdf79b6f409ece16528bb19d0 to your computer and use it in GitHub Desktop.
Save srph/9806ec4fdf79b6f409ece16528bb19d0 to your computer and use it in GitHub Desktop.
Stride: stride.vesting.StridePeriodicVestingAccount

Problem

At the moment, completing any task turns your accouint into a different account type.

By default, this is not supported by the cosmjs sdk.

Usage

import { accountFromAny } from './parser'

const client = await SigningStargateClient.connectWithSigner(chainInfo.rpc, offlineSigner, {
  // @ts-ignore
  registry,
  aminoTypes,
  accountParser: accountFromAny
})
import { Pubkey } from '@cosmjs/amino'
import { Uint64 } from '@cosmjs/math'
import { decodePubkey } from '@cosmjs/proto-signing'
import { assert } from '@cosmjs/utils'
import { stride } from '@stride/proto'
import { BaseAccount, ModuleAccount } from 'cosmjs-types/cosmos/auth/v1beta1/auth'
import {
BaseVestingAccount,
ContinuousVestingAccount,
DelayedVestingAccount,
PeriodicVestingAccount
} from 'cosmjs-types/cosmos/vesting/v1beta1/vesting'
import { Any } from 'cosmjs-types/google/protobuf/any'
import Long from 'long'
export interface Account {
/** Bech32 account address */
readonly address: string
readonly pubkey: Pubkey | null
readonly accountNumber: number
readonly sequence: number
}
function uint64FromProto(input: number | Long): Uint64 {
return Uint64.fromString(input.toString())
}
function accountFromBaseAccount(input: BaseAccount): Account {
const { address, pubKey, accountNumber, sequence } = input
const pubkey = decodePubkey(pubKey)
return {
address: address,
pubkey: pubkey,
accountNumber: uint64FromProto(accountNumber).toNumber(),
sequence: uint64FromProto(sequence).toNumber()
}
}
/**
* Represents a generic function that takes an `Any` encoded account from the chain
* and extracts some common `Account` information from it.
*/
export type AccountParser = (any: Any) => Account
/**
* Basic implementation of AccountParser. This is supposed to support the most relevant
* common Cosmos SDK account types. If you need support for exotic account types,
* you'll need to write your own account decoder.
*/
export function accountFromAny(input: Any): Account {
const { typeUrl, value } = input
switch (typeUrl) {
// auth
case '/cosmos.auth.v1beta1.BaseAccount':
return accountFromBaseAccount(BaseAccount.decode(value))
case '/stride.vesting.StridePeriodicVestingAccount': {
const baseAccount = stride.vesting.StridePeriodicVestingAccount.decode(value).baseVestingAccount.baseAccount
console.log('We are here', baseAccount)
assert(baseAccount)
return accountFromBaseAccount(baseAccount)
}
case '/cosmos.auth.v1beta1.ModuleAccount': {
const baseAccount = ModuleAccount.decode(value).baseAccount
assert(baseAccount)
return accountFromBaseAccount(baseAccount)
}
// vesting
case '/cosmos.vesting.v1beta1.BaseVestingAccount': {
const baseAccount = BaseVestingAccount.decode(value)?.baseAccount
assert(baseAccount)
return accountFromBaseAccount(baseAccount)
}
case '/cosmos.vesting.v1beta1.ContinuousVestingAccount': {
const baseAccount = ContinuousVestingAccount.decode(value)?.baseVestingAccount?.baseAccount
assert(baseAccount)
return accountFromBaseAccount(baseAccount)
}
case '/cosmos.vesting.v1beta1.DelayedVestingAccount': {
const baseAccount = DelayedVestingAccount.decode(value)?.baseVestingAccount?.baseAccount
assert(baseAccount)
return accountFromBaseAccount(baseAccount)
}
case '/cosmos.vesting.v1beta1.PeriodicVestingAccount': {
const baseAccount = PeriodicVestingAccount.decode(value)?.baseVestingAccount?.baseAccount
assert(baseAccount)
return accountFromBaseAccount(baseAccount)
}
default:
throw new Error(`Unsupported type: '${typeUrl}'`)
}
}
@omniwired
Copy link

thanks you so much for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment