import { Int } from "@keplr-wallet/unit" ; import { AxiosInstance } from "axios" ; export interface Account { getType ( ) : string ; getAddress ( ) : string ; getAccountNumber ( ) : Int ; getSequence ( ) : Int ; } export class BaseAccount implements Account { public static async fetchFromRest ( instance , address , defaultBech32Address = false ) { const result = await instance . get ( `auth/accounts/${ address }` ) ; return BaseAccount . fromAminoJSON ( result . data , defaultBech32Address ? address : "" ) ; } public static fromAminoJSON ( obj , defaultBech32Address = "" ) { if ( "height" in obj ) { obj = obj . result ; } const type = obj . type || "" ; let value = "value" in obj ? obj . value : obj ; const baseAccount = value . BaseAccount || value . baseAccount || value . base_account ; if ( baseAccount ) { value = baseAccount ; } const baseVestingAccount = value . BaseVestingAccount || value . baseVestingAccount || value . base_vesting_account ; if ( baseVestingAccount ) { value = baseVestingAccount . BaseAccount || baseVestingAccount . baseAccount || baseVestingAccount . base_account ; } let address = value . address ; if ( ! address ) { if ( ! defaultBech32Address ) { throw new Error ( `Account's address is unknown: ${ JSON . stringify ( obj ) }` ) ; } address = defaultBech32Address ; } const accountNumber = value . account_number ; const sequence = value . sequence ; return new BaseAccount ( type , address , new Int ( accountNumber ?? "0" ) , new Int ( sequence ?? "0" ) ) ; } constructor ( protected readonly type , protected readonly address , protected readonly accountNumber , protected readonly sequence ) { } getType ( ) { return this . type ; } getAddress ( ) { return this . address ; } getAccountNumber ( ) { return this . accountNumber ; } getSequence ( ) { return this . sequence ; } }