import { StargateClient } from "@cosmjs/stargate"; import { OmniWallet, SignedAuth } from "../OmniWallet"; import { chainsMap, WalletType } from "../omni/config"; import { ReviewFee } from "../omni/bridge"; import CosmosConnector from "./connector"; interface ProtocolWallet { disconnect?: () => Promise; sendTransaction?: (signDoc: any) => Promise; address: string; publicKeyHex: string; } export default class CosmosWallet extends OmniWallet { readonly type = WalletType.COSMOS; constructor(readonly connector: CosmosConnector, readonly wallet: ProtocolWallet) { super(connector); } get address() { return this.wallet.address; } get publicKey() { return this.wallet.publicKeyHex; } get omniAddress() { return ""; } async disconnect() { super.disconnect(); this.wallet.disconnect?.(); } sendTransaction(signDoc: any): Promise { if (!this.wallet.sendTransaction) throw "Not impl"; return this.wallet.sendTransaction(signDoc); } transferFee(): Promise { throw new Error("Method not implemented."); } transfer(): Promise { throw "Not impl"; } signIntentsWithAuth(): Promise { throw new Error("Method not implemented."); } signIntents(): Promise> { throw new Error("Method not implemented."); } async fetchBalances(chain: number, whitelist: string[]): Promise> { const balances = await Promise.all( whitelist.map(async (token) => { const balance = await this.fetchBalance(chain, token); return [token, balance]; }) ); return Object.fromEntries(balances); } async fetchBalance(chain: number, token: string): Promise { const config = this.connector.getConfig(chainsMap[chain].id); if (!config) throw new Error("Config not found"); const client = await StargateClient.connect(config.rpc); const balance = await client.getBalance(this.address, token); return BigInt(balance.amount || 0); } }