diff --git a/src/app/core/services/wallet.service.ts b/src/app/core/services/wallet.service.ts index 70b52cb3e..42af83bb5 100644 --- a/src/app/core/services/wallet.service.ts +++ b/src/app/core/services/wallet.service.ts @@ -1,6 +1,6 @@ import { Injectable, OnDestroy } from '@angular/core'; import { Chain } from '@chain-registry/types'; -import { JsonObject } from '@cosmjs/cosmwasm-stargate'; +import { JsonObject, SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate'; import { EncodeObject } from '@cosmjs/proto-signing'; import { Coin, StdFee } from '@cosmjs/stargate'; import { @@ -19,7 +19,10 @@ import { WalletName, } from '@cosmos-kit/core'; import { BehaviorSubject, Observable } from 'rxjs'; +import { EnvironmentService } from '../data-services/environment.service'; import { allAssets, STORAGE_KEY } from '../utils/cosmoskit'; +import { getGasPriceByChain } from '../utils/cosmoskit/helpers/gas'; +import { ExtendsWalletClient } from '../utils/cosmoskit/wallets'; @Injectable({ providedIn: 'root', @@ -52,7 +55,7 @@ export class WalletService implements OnDestroy { return this._chain; } - constructor() { + constructor(private env: EnvironmentService) { this._walletAccountSubject$ = new BehaviorSubject(null); this.walletAccount$ = this._walletAccountSubject$.asObservable(); } @@ -203,6 +206,24 @@ export class WalletService implements OnDestroy { }); } + private async _getSigningCosmWasmClientAuto() { + let _walletName = localStorage.getItem(STORAGE_KEY.CURRENT_WALLET); + const chainWallet = this._walletManager.getMainWallet(_walletName); + + try { + const client = chainWallet?.clientMutable?.data as ExtendsWalletClient; + const signer = await client?.client?.getOfflineSignerAuto(this._chain.chain_id); + + return SigningCosmWasmClient.connectWithSigner(this.env.chainInfo.rpc, signer, { + gasPrice: getGasPriceByChain(this._chain), + }); + } catch (error) { + console.log(`Error: ${error}`); + + return undefined; + } + } + getChainWallet(walletName?: WalletName): ChainWalletBase { let _walletName = walletName ?? localStorage.getItem(STORAGE_KEY.CURRENT_WALLET); if (!_walletName) { @@ -223,10 +244,17 @@ export class WalletService implements OnDestroy { memo?: string, timeoutHeight?: bigint, ) { - return (await this._getSigningCosmWasmClient()).signAndBroadcast(signerAddress, messages, fee, memo, timeoutHeight); + let client; + try { + client = await this._getSigningCosmWasmClientAuto(); + } catch (error) { + client = await this._getSigningCosmWasmClient(); + } + + return client.signAndBroadcast(signerAddress, messages, fee, memo); } - executeContract( + async executeContract( senderAddress: string, contractAddress: string, msg: JsonObject, @@ -234,9 +262,13 @@ export class WalletService implements OnDestroy { memo?: string, funds?: readonly Coin[], ) { - return this._getSigningCosmWasmClient().then( - (client) => client?.execute(senderAddress, contractAddress, msg, fee, memo, funds), - ); + let client; + try { + client = await this._getSigningCosmWasmClientAuto(); + } catch (error) { + client = await this._getSigningCosmWasmClient(); + } + return client?.execute(senderAddress, contractAddress, msg, fee, memo, funds); } signArbitrary(signer: string, data: string | Uint8Array) { @@ -263,9 +295,17 @@ export class WalletService implements OnDestroy { fee: StdFee | 'auto' | number = 'auto', memo?: string, ) { - return this._getSigningCosmWasmClient().then((client) => - client.delegateTokens(delegatorAddress, validatorAddress, amount, fee, memo), - ); + let client; + try { + client = await this._getSigningCosmWasmClientAuto(); + } catch (error) { + client = await this._getSigningCosmWasmClient(); + } + + return client.delegateTokens(delegatorAddress, validatorAddress, amount, fee, memo); + // return this._getSigningCosmWasmClient().then((client) => + // client.delegateTokens(delegatorAddress, validatorAddress, amount, fee, memo), + // ); } async undelegateTokens( @@ -275,9 +315,17 @@ export class WalletService implements OnDestroy { fee: StdFee | 'auto' | number = 'auto', memo?: string, ) { - return this._getSigningCosmWasmClient().then((client) => - client.undelegateTokens(delegatorAddress, validatorAddress, amount, fee, memo), - ); + let client; + try { + client = await this._getSigningCosmWasmClientAuto(); + } catch (error) { + client = await this._getSigningCosmWasmClient(); + } + + return client.undelegateTokens(delegatorAddress, validatorAddress, amount, fee, memo); + // return this._getSigningCosmWasmClient().then((client) => + // client.undelegateTokens(delegatorAddress, validatorAddress, amount, fee, memo), + // ); } estimateFee(messages: EncodeObject[], type?: CosmosClientType, memo?: string, multiplier?: number) { diff --git a/src/app/core/utils/cosmoskit/wallets/index.ts b/src/app/core/utils/cosmoskit/wallets/index.ts index 5a2991e38..f3c10ba9e 100644 --- a/src/app/core/utils/cosmoskit/wallets/index.ts +++ b/src/app/core/utils/cosmoskit/wallets/index.ts @@ -1,6 +1,6 @@ import { MainWalletBase } from '@cosmos-kit/core'; import { WalletName } from '@cosmos-kit/core/cjs/types'; -import { wallets as keplrWallets } from '@cosmos-kit/keplr-extension'; +import { wallets as keplrWallets, KeplrClient } from '@cosmos-kit/keplr-extension'; import { wallets as keplrMobileWallets } from '@cosmos-kit/keplr-mobile'; import { wallets as leapWallets } from '@cosmos-kit/leap-extension'; import { wallets as leapMobileWallets } from '@cosmos-kit/leap-mobile'; @@ -16,7 +16,7 @@ import { isLeapExtention, isMetamaskExtention, } from '../helpers/browser'; -import { wallets as coin98Wallets } from './coin98-extension'; +import { Coin98Client, wallets as coin98Wallets } from './coin98-extension'; import { wallets as coin98MobileWallets } from './coin98-mobile'; import { wallets as leapSnapMetaMaskWallets } from './leap-metamask-cosmos-snap'; import { wallets as wcWallets } from './wallet-connect/wc'; @@ -71,4 +71,6 @@ function checkDesktopWallets(walletName: WalletName) { } } -export { desktopWallets, mobileWallets, wcWallets, checkDesktopWallets }; +type ExtendsWalletClient = KeplrClient | Coin98Client; + +export { desktopWallets, mobileWallets, wcWallets, checkDesktopWallets, ExtendsWalletClient }; diff --git a/src/app/pages/fee-grant/popup-add-grant/popup-add-grant.component.ts b/src/app/pages/fee-grant/popup-add-grant/popup-add-grant.component.ts index 384e55267..0e2e73760 100644 --- a/src/app/pages/fee-grant/popup-add-grant/popup-add-grant.component.ts +++ b/src/app/pages/fee-grant/popup-add-grant/popup-add-grant.component.ts @@ -199,10 +199,10 @@ export class PopupAddGrantComponent implements OnInit { async executeGrant(granter: string, msg) { const multiplier = 1.7; // Grant multiplier - const fee = await this.walletService.estimateFee([msg], 'stargate', '', multiplier).catch(() => undefined); + const fee = await this.walletService.estimateFee([msg], 'cosmwasm', '', multiplier).catch(() => undefined); this.walletService - .signAndBroadcastStargate(granter, [msg], fee, '') + .signAndBroadcast(granter, [msg], fee, '') .then((result) => { this.closeDialog(result?.transactionHash); }) diff --git a/src/app/pages/fee-grant/popup-revoke/popup-revoke.component.ts b/src/app/pages/fee-grant/popup-revoke/popup-revoke.component.ts index 1b35c40af..447cfc63e 100644 --- a/src/app/pages/fee-grant/popup-revoke/popup-revoke.component.ts +++ b/src/app/pages/fee-grant/popup-revoke/popup-revoke.component.ts @@ -45,7 +45,7 @@ export class PopupRevokeComponent implements OnInit { }; const revokeMultiplier = 1.7; // revoke multiplier - NOT FOR ALL - const fee = await this.walletService.estimateFee([msg], 'stargate', '', revokeMultiplier).catch(() => undefined); + const fee = await this.walletService.estimateFee([msg], 'cosmwasm', '', revokeMultiplier).catch(() => undefined); this.walletService .signAndBroadcastStargate(account.address, [msg], fee, '') diff --git a/src/app/pages/proposal/proposal-vote/proposal-vote.component.ts b/src/app/pages/proposal/proposal-vote/proposal-vote.component.ts index 2e9691efa..81b444a13 100644 --- a/src/app/pages/proposal/proposal-vote/proposal-vote.component.ts +++ b/src/app/pages/proposal/proposal-vote/proposal-vote.component.ts @@ -61,7 +61,7 @@ export class ProposalVoteComponent { this.isLoading = true; const multiplier = 1.7; // multiplier - const fee = await this.walletService.estimateFee([msg], 'stargate', '', multiplier).catch(() => undefined); + const fee = await this.walletService.estimateFee([msg], 'cosmwasm', '', multiplier).catch(() => undefined); this.walletService .signAndBroadcast(account.address, [msg], fee) diff --git a/src/app/pages/validators/validators.component.ts b/src/app/pages/validators/validators.component.ts index 370785e52..35e6b4583 100644 --- a/src/app/pages/validators/validators.component.ts +++ b/src/app/pages/validators/validators.component.ts @@ -507,7 +507,6 @@ export class ValidatorsComponent implements OnInit, OnDestroy { this.walletService .delegateTokens(msg.delegatorAddress, msg.validatorAddress, msg.amount, 'auto') .then((broadcastResult) => { - console.log('🐛 broadcastResult: ', broadcastResult); let error = undefined; if (broadcastResult?.code != 0) { error = broadcastResult; @@ -537,7 +536,7 @@ export class ValidatorsComponent implements OnInit, OnDestroy { })); const revokeMultiplier = 1.7; // revoke multiplier - NOT FOR ALL - const fee = await this.walletService.estimateFee(msg, 'stargate', '', revokeMultiplier); + const fee = await this.walletService.estimateFee(msg, 'cosmwasm', '', revokeMultiplier); this.walletService .signAndBroadcast(this.userAddress, msg, fee || 'auto')