Skip to content

Commit

Permalink
Merge pull request #3330 from aura-nw/baseline/main_20240419
Browse files Browse the repository at this point in the history
Baseline/main 20240419
  • Loading branch information
nttnguyen136 authored Apr 19, 2024
2 parents 8f7aa9b + d29d8f0 commit b1deece
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
74 changes: 61 additions & 13 deletions src/app/core/services/wallet.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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',
Expand Down Expand Up @@ -52,7 +55,7 @@ export class WalletService implements OnDestroy {
return this._chain;
}

constructor() {
constructor(private env: EnvironmentService) {
this._walletAccountSubject$ = new BehaviorSubject<WalletAccount>(null);
this.walletAccount$ = this._walletAccountSubject$.asObservable();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -223,20 +244,31 @@ 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,
fee: StdFee | 'auto' | number = 'auto',
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) {
Expand All @@ -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(
Expand All @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions src/app/core/utils/cosmoskit/wallets/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -71,4 +71,6 @@ function checkDesktopWallets(walletName: WalletName) {
}
}

export { desktopWallets, mobileWallets, wcWallets, checkDesktopWallets };
type ExtendsWalletClient = KeplrClient | Coin98Client;

export { desktopWallets, mobileWallets, wcWallets, checkDesktopWallets, ExtendsWalletClient };
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/app/pages/validators/validators.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit b1deece

Please sign in to comment.